Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
ECE250: Algorithms and Data Structures
Trees
Materials from CLRS: Chapter 10.4 and Appendix B.5 Weiss: Chapter 4
Ladan Tahvildari, PEng, SMIEEE Professor
Software Technologies Applied Research (STAR) Group
Dept. of Elect. & Comp. Eng.
University of Waterloo
Acknowledgements
v The following resources have been used to prepare materials for this course: Ø MIT OpenCourseWare Ø Introduction To Algorithms (CLRS Book) Ø Data Structures and Algorithm Analysis in C++ (M. Wiess) Ø Data Structures and Algorithms in C++ (M. Goodrich)
v Thanks to many people for pointing out mistakes, providing suggestions, or helping to improve the quality of this course over the last ten years: Ø http://www.stargroup.uwaterloo.ca/~ece250/acknowledgment/
Lecture 10 ECE250 2
Lecture 10 ECE250 3
New Data Structure
v Linear access time of linked lists is prohibitive Ø Does there exist any simple data structure for
which the running time of most operations (search, insert, delete) is O(log N)?
Trees
v A tree is a collection of nodes Ø The collection can be empty Ø (recursive definition) If not empty, a tree consists of a
distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r
Lecture 10 ECE250 4
Some Terminologies v Parent and Child
Ø Every node except the root has one parent Ø A node can have an arbitrary number of children
v Leaves Ø Nodes with no children
v Sibling Ø Nodes with same parent
Lecture 10 ECE250 5
Some Terminologies v Path
Ø A sequence of nodes such that is the parent of for
v Length Ø Number of edges on the path
v Depth of a node Ø Length of the unique path from the root to that node Ø The depth of a tree is equal to the depth of the deepest leaf
v Height of a node Ø Length of the longest path from that node to a leaf Ø All leaves are at height 0 Ø The height of a tree is equal to the height of the root
Lecture 10 ECE250 6
knnn ,...,, 21 in1+in ki <≤1
Example: Unix Directory
Lecture 10 ECE250 7
Binary Trees
v A tree in which no node can have more than two children.
v The depth of an “average” binary tree is considerably smaller
than N, even though in the worst case, the depth can be as large as N-1.
Lecture 10 ECE250 8
An Example: Expression Trees
v Leaves are operands (constants or variables) v The other nodes (internal nodes) contain operators v Will not be a binary tree if some operators are not
binary Lecture 10 ECE250 9
Tree Traversal v Traversal is the process of visiting every node once
Ø Visiting a node entails doing some processing at that node, but when describing a traversal strategy, we need not concern ourselves with what that processing is
v Three recursive techniques for tree traversal Ø the left subtree is traversed recursively Ø the right subtree is traversed recursively Ø the root is visited
v What distinguishes the techniques from one another is the order of those 3 tasks
Lecture 10 ECE250 10
Pre-order Traversal
v Node, Left, Right v Prefix Expression
Ø ++a*bc*+*defg
Lecture 10 ECE250 11
The Pre-order Directory Listing
Lecture 10 ECE250 12
Post-order Traversal
v Left, Right, Node v Postfix Expression
Ø abc*+de*f+g*+
Lecture 10 ECE250 13
In-order Traversal
v Left, Node, Right v Infix Expression
Ø a+b*c+d*e+f*g
Lecture 10 ECE250 14
Algorithms
Lecture 10 ECE250 15
Lecture 10 ECE250 16
Binary Tree ADT v BinTree ADT:
Ø Accessor functions: § key():int § parent(): BinTree § left(): BinTree § right(): BinTree
Ø Modification procedures: § setKey(k:int) § setParent(T:BinTree) § setLeft(T:BinTree) § setRight(T:BinTree)
∅
∅
∅ ∅
∅ ∅
∅
∅∅∅∅
Root