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,
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,
Laplace Transform Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2008 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,
Topics in Applied Mathematics Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
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,
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,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
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.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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,
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,
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,
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,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
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,
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary 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,
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,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
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.
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.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Outline In this topic we will look at:
AVL Trees.
Outline Introducing perfect binary trees Definitions and examples
Chapter 20: Binary Trees.
Multiway Search Trees.
Chapter 21: Binary Trees.
All-pairs shortest path
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. Left-child right-sibling binary tree

2 Background Our simple tree data structure is node-based where children are stored as a linked list –Is it possible to store a general tree as a binary tree?

3 Left-child right-sibling binary tree The Idea Consider the following: –The first child of each node is its left sub-tree –The next sibling of each node is in its right sub-tree This is called a left-child—right-sibling binary tree

4 Left-child right-sibling binary tree Consider this general tree Example

5 Left-child right-sibling binary tree Example B, the first child of A, is the left child of A For the three siblings C, D, E: –C is the right sub-tree of B –D is the right sub-tree of C –E is the right sub-tree of D

6 Left-child right-sibling binary tree Example B has no children, so it’s left sub-tree is empty F, the first child of C, is the left sub-tree of C For the next two siblings: –G is the right sub-tree of F –H is the right sub-tree of G

7 Left-child right-sibling binary tree Example I, the first child of D, is the left child of D Its sibling J is the right sub-tree of I

8 Left-child right-sibling binary tree Example Similarly, the four children of E start with K forming the left sub-tree of E and its three siblings form a chain along the left sub-trees

9 Left-child right-sibling binary tree Example The balance of the nodes in our general tree are shown here

10 Left-child right-sibling binary tree Example An empty left sub-tree indicates no children

11 Left-child right-sibling binary tree Example An empty right sub-tree indicates the node is the last of its siblings –The root node, has no siblings

12 Left-child right-sibling binary tree Transformation The transformation of a general tree into a left-child right-sibling binary tree has been called the Knuth transform

13 Left-child right-sibling binary tree Traversals A pre-order traversal of the original tree is identical to the pre-order traversal of the Knuth transform A B C F O P U G H D I Q V W J E K R L M S T X Y Z N Achieving a post-order traversal on the Knuth transform requires a different traversal: –The left sub-tree is traversed –The current node is visited –The right-sub-tree is traversed Called an in-order traversal B O U P F G H C V W Q I J D R K L S X Y Z T M N E A

14 Left-child right-sibling binary tree Forests A forest, described in Topic 4.5, can be stored in this representation as follows: –Choose one of the roots of the trees as the root of the binary tree –Let each subsequent root of a tree be a right child of the previous root –This is the binary-tree representation of this forest –Think of the roots as siblings of each other

15 Left-child right-sibling binary tree Implementation The class is similar to that of a binary tree template class LCRS_tree { private: Type element; LCRS_tree *first_child_tree; LCRS_tree *next_sibling_tree; public: LCRS_tree(); LCRS_tree *first_child(); LCRS_tree *next_sibling(); //... };

16 Left-child right-sibling binary tree Implementation The implementation of various functions now differs –An iterative approach is much more desirable template int LCRS_tree ::degree() const { int count = 0; for ( LCRS_tree *ptr = first_child(); ptr != nullptr; ptr = ptr->next_sibling() ) { ++count; } return count; }

17 Left-child right-sibling binary tree Implementation The implementation of various functions now differs template bool LCRS_tree ::is_leaf() const { return ( first_child() == nullptr ); }

18 Left-child right-sibling binary tree Implementation The implementation of various functions now differs template LCRS_tree *LCRS_tree ::child( int n ) const { if ( n = degree() ) { return nullptr; } LCRS_tree *ptr = first_child(); for ( int i = 0; i < n; ++i ) { ptr = ptr->next_sibling(); } return ptr; }

19 Left-child right-sibling binary tree Implementation The implementation of various functions now differs template void LCRS_tree ::append( Type const &obj ) { if ( first_child() == nullptr ) { first_child_tree = new LCRS_tree ( obj ); } else { LCRS_tree *ptr = first_child(); while ( ptr->next_sibling() != nullptr ) { ptr = ptr->next_sibling(); } ptr->next_sibling_tree = new LCRS_tree ( obj ); }

20 Left-child right-sibling binary tree Implementation The implementation of various functions now differs –The size doesn’t care that this is a general tree… template int LCRS_tree ::size() const { return 1 + ( first_child() == nullptr ? 0 : first_child()->size() ) + ( next_sibling() == nullptr ? 0 : next_sibling()->size() ); }

21 Left-child right-sibling binary tree Implementation The implementation of various functions now differs –The height member function is closer to the original implementation template int LCRS_tree ::height() const { int h = 0; for ( LCRS_tree *ptr = first_child(); ptr != nullptr; ptr = ptr->next_sibling() ) { h = std::max( h, 1 + ptr->height() ); } return h; }

22 Left-child right-sibling binary tree Summary This topic has covered a binary representation of general trees –The first child is the left sub-tree of a node –Subsequent siblings of that child form a chain down the right sub-trees –An empty left sub-tree indicates no children –An empty right sub-tree indicates no other siblings

23 Left-child right-sibling binary tree References [1]Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 2 nd Ed., MIT Press, 2001, §19.1, pp [2]Weiss, Data Structures and Algorithm Analysis in C++, 3 rd Ed., Addison Wesley, §6.8.1, p.240.

24 Left-child right-sibling binary tree 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