ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,

Slides:



Advertisements
Similar presentations
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Advertisements

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Fixed-Point Iteration Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Proof by Induction.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Binary Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Compiled by: Dr. Mohammad Omar Alhawarat
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Decimal Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
Double-Precision Floating-Point Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 The tree data structure Outline In this topic, we will cover: –Definition of a tree data structure and its components –Concepts of: Root, internal, and.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 N -ary trees This topic quickly looks at a generalization of a binary tree, where each node has up to N children –Definition –Perfect N -ary trees –Complete.
1 Binary trees Outline In this talk, we will look at the binary tree data structure: –Definition –Properties –A few applications Ropes (strings) Expression.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Complete binary trees Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
The mechanics of recursion
Outline In this topic we will look at:
Open Addressing: Quadratic Probing
AVL Trees.
Outline Introducing perfect binary trees Definitions and examples
Multiway Search Trees.
Presentation transcript:

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Tree traversals

2 Outline This topic will cover tree traversals: –A means of visiting all the objects in a tree data structure –We will look at Breadth-first traversals Depth-first traversals –Pre-order and Post-order depth-first traversals –Applications –General guidelines

3 Tree traversals Background All the objects stored in an array or linked list can be accessed sequentially When discussing deques, we introduced iterators in C++: –These allow the user to step through all the objects in a container Question: how can we iterate through all the objects in a tree in a predictable and efficient manner –Requirements:  (n) run time and o(n) memory 4.3

4 Tree traversals Types of Traversals We have already seen one traversal: –The breadth-first traversal visits all nodes at depth k before proceeding onto depth k + 1 –Easy to implement using a queue Another approach is to visit always go as deep as possible before visiting other siblings: depth-first traversals 4.3.1

5 Tree traversals Breadth-First Traversal Breadth-first traversals visit all nodes at a given depth –Can be implemented using a queue –Run time is  (n) –Memory is potentially expensive: maximum nodes at a given depth –Order: A B H C D G I E F J K 4.3.1

6 Tree traversals Breadth-First Traversal The implementation was already discussed: –Create a queue and push the root node onto the queue –While the queue is not empty: Push all of its children of the front node onto the queue Pop the front node 4.3.1

7 Tree traversals Backtracking To discuss depth-first traversals, we will define a backtracking algorithm for stepping through a tree: –At any node, we proceed to the first child that has not yet been visited –Or, if we have visited all the children (of which a leaf node is a special case), we backtrack to the parent and repeat this decision making process We end once all the children of the root are visited 4.3.2

8 Tree traversals Depth-first Traversal We define such a path as a depth-first traversal We note that each node could be visited twice in such a scheme –The first time the node is approached (before any children) –The last time it is approached (after all children) 4.3.2

9 Tree traversals Pre-order Depth-first Traversal Visiting each node first results in the sequence A, B, C, D, E, F, G, H, I, J, K, L, M

10 Tree traversals Post-order Depth-first Traversal Visiting the nodes with their last visit: D, C, F, G, E, B, J, K, L, I, M, H, A

11 Tree traversals Implementing Depth-First Traversals Depth-first traversals can be implemented with recursion: template void Simple_tree ::depth_first_traversal() const { // Perform pre-visit operations on the element std::cout << element << ' '; // Perform a depth-first traversal on each of the children for ( ece250::Single_node *ptr = children.head(); ptr != 0; ptr = ptr->next() ) { ptr->retrieve()->depth_first_traversal(); } // Perform post-visit operations on the element std::cout << element << ' '; } 4.3.3

12 Tree traversals Implementing Depth-First Traversals Alternatively, we can use a stack: –Create a stack and push the root node onto the stack –While the stack is not empty: Pop the top node Push all of the children of that node to the top of the stack in reverse order –Run time is  (n) –The objects on the stack are all unvisited siblings from the root to the current node If each node has a maximum of two children, the memory required is  (h) : the height of the tree With the recursive implementation, the memory is  (h) : recursion just hides the memory 4.3.3

13 Tree traversals Guidelines Depth-first traversals are used whenever: –The parent needs information about all its children or descendants, or –The children require information about all its parent or ancestors In designing a depth-first traversal, it is necessary to consider: 1.Before the children are traversed, what initializations, operations and calculations must be performed? 2.In recursively traversing the children: a)What information must be passed to the children during the recursive call? b)What information must the children pass back, and how must this information be collated? 3.Once all children have been traversed, what operations and calculations depend on information collated during the recursive traversals? 4.What information must be passed back to the parent? 4.3.4

14 Tree traversals Applications Tree application: displaying information about directory structures and the files contained within –Finding the height of a tree –Printing a hierarchical structure –Determining memory usage 4.3.4

15 Tree traversals Height The int height() const function is recursive in nature: 1.Before the children are traversed, we assume that the node has no children and we set the height to zero: h current = 0 2.In recursively traversing the children, each child returns its height h and we update the height if 1 + h > h current 3.Once all children have been traversed, we return h current When the root returns a value, that is the height of the tree

16 Tree traversals Printing a Hierarchy Consider the directory structure presented on the left—how do we display this in the format on the right? / usr/ bin/ local/ var/ adm/ cron/ log/ What do we do at each step?

17 Tree traversals Printing a Hierarchy For a directory, we initialize a tab level at the root to 0 We then do: 1.Before the children are traversed, we must: a)Indent an appropriate number of tabs, and b)Print the name of the directory followed by a '/' 2.In recursively traversing the children: a)A value of one plus the current tab level must be passed to the children, and b)No information must be passed back 3.Once all children have been traversed, we are finished

18 Tree traversals Printing a Hierarchy Assume the function void print_tabs( int n ) prints n tabs template void Simple_tree ::print( int depth ) const { print_tabs( depth ); std::cout name() << '/' << std::endl; for ( ece250::Single_node *ptr = children.head(); ptr != 0; ptr = ptr->next() ) { ptr->retrieve()->print( depth + 1 ); }

19 Tree traversals Determining Memory Usage Suppose we need to determine the memory usage of a directory and all its subdirectories: –We must determine and print the memory usage of all subdirectories before we can determine the memory usage of the current directory

20 Tree traversals Determining Memory Usage Suppose we are printing the directory usage of this tree: bin/ 12 local/ 15 usr/ 31 adm/ 6 cron/ 5 log/ 9 var/ 23 /

21 Tree traversals Determining Memory Usage For a directory, we initialize a tab level at the root to 0 We then do: 1.Before the children are traversed, we must: a)Initialize the memory usage to that in the current directory. 2.In recursively traversing the children: a)A value of one plus the current tab level must be passed to the children, and b)Each child will return the memory used within its directories and this must be added to the current memory usage. 3.Once all children have been traversed, we must: a)Print the appropriate number of tabs, b)Print the name of the directory followed by a "/ ", and c)Print the memory used by this directory and its descendants

22 Tree traversals Printing a Hierarchy template int Simple_tree ::du( int depth ) const { int usage = retrieve()->memory(); for ( ece250::Single_node *ptr = children.head(); ptr != 0; ptr = ptr->next() ) { usage += ptr->retrieve()->du( depth + 1 ); } print_tabs( depth ); std::cout name() << "/ " << usage << std::endl; return usage; }

23 Tree traversals Summary This topic covered two types of traversals: –Breadth-first traversals –Depth-first traversals –Applications –Determination of how to structure a depth-first traversal

24 Tree traversals References

25 Tree traversals Usage Notes These slides are made publicly available on the web for anyone to use If you choose to use them, or a part thereof, for a course at another institution, I ask only three things: –that you inform me that you are using the slides, –that you acknowledge my work, and –that you alert me of any mistakes which I made or changes which you make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides Sincerely, Douglas Wilhelm Harder, MMath