CS 261 – Recitation 9 & 10 Graphs & Final review

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 240: Data Structures Thursday, August 2nd Trees – Traversals, Representations, Recursion and Helpers.
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Tree Traversal.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Chapter 12 – Data Structures
Trees Chapter 15.
Trees Chapter 11 (continued)
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Trees ---- Soujanya.
Week 6 - Wednesday CS221.
CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures
Section 8.1 Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
AVL Trees CENG 213 Data Structures.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Binary Tree Traversal Methods
Lecture 12 CS203 1.
Binary Tree Traversal Methods
Stacks with Dynamic Memory
Trees.
Chapter 20: Binary Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

CS 261 – Recitation 9 & 10 Graphs & Final review Oregon State University School of Electrical Engineering and Computer Science CS 261 – Recitation 9 & 10 Graphs & Final review Fall 2013

Graph: Reachability Problem Given a single starting vertex, produce the set of vertices that can be reached starting from the initial location. A depth-first search follows each path as far (deep) as possible before backtracking. A breadth-first search looks at all possible paths at the same time. “DFS could be simulated as an ant walking along the edges of the graph, and returning to a previous node when it discovers it has reached a vertex it has already seen.” “A mental image for BFS would be to pour ink at the starting vertex, and see where it travels along all possible paths” Order in which nodes are reached: (left) DFS; and (right) BFS. Source: Wikipedia 2

Graph: Reachability Problem findReachable (graph g, vertex start) { create a set of reachable vertices, initially empty. call this r. create a container for vertices known to be reachable. call this c add start vertex to container c while the container c is not empty { remove first entry from the container c, assign to v if v is not already in the set of reachable vertices r { add v to the reachable set r add the neighbors of v to the container c } return r DFS: Container is a Stack BFS: Container is a Queue 3

Exercise Simulate the DFS and BFS on the following graph starting at node A. Notes: (1) Nodes must be added to the container (Stack or Queue) in COUNTER-CLOCKWISE order; (2) We do not add neighbors to the container if they have already been visited. DFS: r = {}; c = {A}; r={A}; c = {B}; r={A, B}; c={C, E, D}; // Note: D is the top of the stack r={A, B, D}; c={C, E, F}; r={A, B, D, F}; c={C, E}; r={A, B, D, F, E}; c={C}; r={A, B, D, F, E, C}; c={}; BFS r={A, B}; c={C, E, D}; // Note: C is the front of the queue r={A, B, C}; c={E, D}; r={A, B, C, E}; c={D, D}; r={A, B, C, E, D}; c={D, F}; r={A, B, C, E, D}; c={F}; r={A, B, C, E, D, F}; c={}; 4

Outline – Final exam review BST/AVL/Tree sort/Tree traversal/Tree iterator Heaps/heap sort Hash tables Materials in these slides were collected from different Internet sources. CS 261 – Data Structures

Question H D J B F I K A C E G CS 261 – Data Structures A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible A C E G CS 261 – Data Structures

Pre, In, Post-order traversal Pre-order: 10 – 5 – 1 – 8 – 7 – 6 – 34 – 56 – 40 - 60 In-order: 1 – 5 – 6 – 7 – 8 – 10 – 34 – 40 – 56 - 60 Post–order: 1 – 6 – 7 – 8 – 5 – 40 – 60 – 56 – 34 – 10 We can do other things with traversal other than just printing out values. CS 261 – Data Structures

Adding 13??? 13 CS 261 – Data Structures

Removing 10??? CS 261 – Data Structures

TreeSort struct AVLTree* newAVLTree(); void addAVLTree(struct AVLTree *tree, TYPE val); void treeSort (TYPE data[], int n) {…} void _treeSortHelper(AVLNode *cur, TYPE *data, int *count) {…} CS 261 – Data Structures

treeSort void treeSort(TYPE data[], int n){ int i; int count = 0; /* declare an AVL tree */ struct AVLTree *tree = newAVLtree(); assert(data != NULL && n > 0); /* add elements to the tree */ for (i = 0; i < n; i++) addAVLTree(tree, data[i]); /* call the helper function */ _treeSortHelper(tree->root, data, &count); } CS 261 – Data Structures

_treeSortHelper /* *count goes from 0 to n-1 */ void _treeSortHelper(AVLNode *cur, TYPE *data, int *count){ if (cur != NULL) { _treeSortHelper(cur->left, data, count); data[*count] = cur->val; (*count)++; _treeSortHelper(cur->right, data, count); } CS 261 – Data Structures

True or False CS 261 – Data Structures

Question Add 12, remove 3, remove 5 CS 261 – Data Structures

When to do a double rotation? Balance Factor = height(right subtree) - height(left subtree) At an unbalanced node N, a double rotation is needed when: N’s BF is positive and N’s right subtree’s BF is negative N’s BF is negative and N’s left subtree’s BF is positive. CS 261 – Data Structures 15 15

Draw the stack of in-order traversal using iterator if stack is empty perform slide left on root otherwise let n be top of stack pop n slide left on right child of n void _slideLeft(struct Stack *stk, struct Node *n) while (n != 0) { pushStack(stk, n); n = n->left; } CS 261 – Data Structures

Heaps and priority queues How to represent a binary heap? Using an array (dyArray) Suppose the root has index 0, what are the indices of the 2 children of a node at index i? What is the index of the parent of a node at index i? 2 * i + 1, 2 * i + 2 (i-1)/2 2 3 5 9 10 7 8 2 1 3 2 5 3 9 4 10 5 7 6 8 7 14 8 12 9 11 10 16 14 12 11 16 CS 261 – Data Structures

Simulate heap sort for the following heap 3 10 9 16 14 12 11 CS 261 – Data Structures