Download presentation
Presentation is loading. Please wait.
Published byAngel Snow Modified over 6 years ago
1
Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb CLRS, Section 10.4
2
What is a Tree? In computer science, a tree is a implementation-dependent data structure representing a hierarchical structure. A tree consists of nodes with a parent-child relation. Applications: Organization charts File systems Programming environments Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada CS Data Structures
3
Tree Terminology Root: node without parent (A)
Internal node: node with at least one child (A, B, C, F) External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Descendant of a node: child, grandchild, etc. A B D C G H E F I J K sub-tree Sub-tree: tree consisting of a node and its descendants, grandchildren, etc. CS Data Structures
4
Tree Traversal A tree traversal visits the nodes of a tree in a systematic manner. Four types of traversal: Pre-order Post-order In-order Level-order pre-order post-order in-order level-order CS Data Structures
5
Pre-order Traversal In a pre-order traversal, a node is visited before its descendants. Application: Print a structured document. Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) Make Money Fast! 1. Motivations References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 CS Data Structures
6
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(Make Money Fast!) visit(Make Money Fast!) // 1 preOrder(1. Motivations) // left child CS Data Structures
7
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(1. Motivations) visit(1. Motivations) // 2 preOrder(1.1 Greed) // left child CS Data Structures
8
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(1.1 Greed) visit(1.1 Greed) // 3 return // no children CS Data Structures
9
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(1. Motivation) preorder(1.2 Avidity) // right child CS Data Structures
10
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(1.2 Avidity) visit(1.2 Avidity) // 4 return // no children CS Data Structures
11
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(1. Motivations) return // all children visited CS Data Structures
12
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(Make Money Fast!) preOrder(2. Methods) // middle child CS Data Structures
13
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2. Methods) visit(2. Methods) // 5 preOrder(2.1 Stock Fraud) // left child CS Data Structures
14
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2.1 Stock Fraud) visit(2.1 Stock Fraud) // 6 return // no children CS Data Structures
15
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2. Methods) preOrder(2.2 Ponzi Scheme) // middle child CS Data Structures
16
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2.2 Ponzi Scheme) visit(2.2 Ponzi Scheme) // 7 return // no children CS Data Structures
17
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2. Methods) preOrder(2.3 Bank Robbery) // right child CS Data Structures
18
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2.3 Bank Robbery) visit(2.3 Bank Robbery) // 8 return // no children CS Data Structures
19
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(2. Methods) return // all children visited CS Data Structures
20
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(Make Money Fast!) preOrder(3. References) // right child CS Data Structures
21
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(3. References) return // no children CS Data Structures
22
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(Make Money Fast!) return // all children visited CS Data Structures
23
Pre-order Traversal 1 2 3 5 4 6 7 8 9 Make Money Fast! 1. Motivations
References 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed 1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 6 7 8 9 Algorithm preOrder(v) visit(v) for each child w of v preOrder (w) preOrder(Make Money Fast!) Done. CS Data Structures
24
Post-order Traversal In a post-order traversal, a node is visited after its descendants. Application: Compute space used by files in a directory and its sub-directories. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 9 cs16/ 8 3 7 todo.txt 1K homeworks/ programs/ 1 2 4 5 6 h1c.doc 3K h1nc.doc 2K DDR.java 10K Stocks.java 25K Robot.java 20K CS Data Structures
25
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(cs16/) postOrder(homeworks/) // left child CS Data Structures
26
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(homeworks/) postOrder(h1c.doc/) // left child CS Data Structures
27
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(h1c.doc/) // no children visit(h1c.doc) // 1 CS Data Structures
28
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(homeworks/) postOrder(h1nc.doc/) // right child CS Data Structures
29
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(h1nc.doc/) // no children visit(h1nc.doc) // 2 CS Data Structures
30
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(homeworks/) // all children visited visit(homeworks/) // 3 CS Data Structures
31
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(cs16/) postOrder(programs/) // middle child CS Data Structures
32
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(programs/) postOrder(DDR.java/) // left child CS Data Structures
33
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(DDR.java) // no children visit(DDR.java) // 4 CS Data Structures
34
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(programs/) postOrder(Stocks.java/) // middle child CS Data Structures
35
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(Stocks.java) // no children visit(Stock.java) // 5 CS Data Structures
36
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(programs/) postOrder(Robot.java/) // right child CS Data Structures
37
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(Robot.java) // no children visit(Robot.java) // 6 CS Data Structures
38
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(programs/) // all children visited visit(programs/) // 7 CS Data Structures
39
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(cs16/) postOrder(todo.txt) // right child CS Data Structures
40
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(todo.txt) // no children visit(todo.txt) // 8 CS Data Structures
41
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) postOrder(cs16/) // all children visited visit(cs16/) // 9 CS Data Structures
42
Post-order Traversal 9 3 1 7 2 4 5 6 8 cs16/ homeworks/ todo.txt 1K
programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) Done. CS Data Structures
43
In-order Traversal In an in-order traversal a node is visited after its left sub-tree and before its right sub-tree. Applies to Binary Trees only. Application: Draw a binary tree. Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) 3 1 2 5 6 7 9 8 4 A B C D E F G H I CS Data Structures
44
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(A) inOrder(B) CS Data Structures
45
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(B) inOrder(D) CS Data Structures
46
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(D) // no left children visit(D) // 1 // no right children return CS Data Structures
47
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(B) // left child visited visit(B) // 2 inOrder(E) CS Data Structures
48
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(E) inOrder(H) CS Data Structures
49
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(H) // no left children visit(H) // 3 // no right children return CS Data Structures
50
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(E) // left child visited visit(E) // 4 inOrder(I) CS Data Structures
51
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(I) // no left children visit(I) // 5 // no right children return CS Data Structures
52
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(E) // right children visited return CS Data Structures
53
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(B) // right children visited return CS Data Structures
54
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(A) // left child visited visit(A) // 6 inOrder(C) CS Data Structures
55
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(C) inOrder(F) CS Data Structures
56
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(F) // no left children visit(F) // 7 // no right children return CS Data Structures
57
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(C) // left child visited visit(C) // 8 inOrder(G) CS Data Structures
58
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(G) // no left children visit(G) // 9 // no right children return CS Data Structures
59
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(C) // right child visited return CS Data Structures
60
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) inOrder(A) // right child visited return CS Data Structures
61
In-order Traversal A B C D E F G H I 3 1 2 5 6 7 9 8 4
Algorithm inOrder(v) if left (v) ≠ null inOrder (left (v)) visit(v) if right(v) ≠ null inOrder (right (v)) Done. CS Data Structures
62
Level-order Traversal
In a level-order traversal, all nodes at the same depth are visited from left to right, then all the nodes at the next depth are visited, etc. Application: Breadth-first search. 1 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) A B C F G D E 2 3 4 5 6 7 CS Data Structures
63
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q ← A Q not empty w ← A // Q empty Q.enqueue(B) // Q = B Q.enqueue(C) // Q = B C visit(A) // 1 CS Data Structures
64
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← B // Q = C Q.enqueue(D) // Q = C D Q.enqueue(E) // Q = C D E visit(B) // 2 CS Data Structures
65
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← C // Q = D E Q.enqueue(F) // Q = D E F Q.enqueue(G) // Q = D E F G visit(C) // 3 CS Data Structures
66
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← D // Q = E F G Q.enqueue(null) // Q = E F G Q.enqueue(null) // Q = E F G visit(D) // 4 CS Data Structures
67
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← E // Q = F G Q.enqueue(null) // Q = F G Q.enqueue(null) // Q = F G visit(E) // 5 CS Data Structures
68
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← F // Q = G Q.enqueue(null) // Q = G Q.enqueue(null) // Q = G visit(F) // 6 CS Data Structures
69
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q not empty w ← G // Q = empty Q.enqueue(null) // Q = empty Q.enqueue(null) // Q = empty visit(G) // 7 CS Data Structures
70
Level-order Traversal
B C F G D E 3 1 2 5 6 7 4 Algorithm levelOrder(v) Q ← v while !Q.empty w ← Q.dequeue Q.enqueue(left (w)) Q.enqueue(right (w)) visit(w) levelOrder(A) Q is empty return CS Data Structures
71
Traversal Implementation
Pre-order, in-order, and post-order traversals. Big-O time and space? Level-order traversal using a queue. Level-order traversal without a queue. CS Data Structures
72
Binary Trees A binary tree is a tree with the following properties:
Each internal node has at most two children (exactly two for proper binary trees). The children of a node are an ordered pair. We call the children of an internal node left child and right child. A B C F G D E H I Applications: arithmetic expressions decision processes searching CS Data Structures
73
Types of Binary Trees full binary tree: A binary tree is which each node was exactly 2 or 0 children. complete binary tree: A binary tree in which every level, except possibly the deepest, is completely filled. At depth n, the height of the tree, all nodes are as far left as possible. perfect binary tree: A binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. A perfect binary tree has the maximum number of nodes for a given height. G E D B A C F E D B A C F C A F G E D B
74
Question 1 What is the maximum height of a full binary tree with 11 nodes? 1 3 5 7 Not possible to construct a full binary tree with 11 nodes. CS Data Structures
75
Question 1 What is the maximum height of a full binary tree with 11 nodes? 1 3 5 7 Not possible to construct a full binary tree with 11 nodes. CS Data Structures
76
Question 2 What is the height of a complete binary tree that contains n nodes? 1 log 2 𝑛 𝑛 𝑛 log 2 𝑛 CS Data Structures
77
Question 2 What is the height of a complete binary tree that contains n nodes? 1 log 2 𝑛 𝑛 𝑛 log 2 𝑛 CS Data Structures
78
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression. internal nodes: operators external nodes: operands Example: Expression tree for this arithmetic expression: (2 (a - 1) + (3 b)) + - 2 a 1 3 b CS Data Structures
79
Evaluate Arithmetic Expressions
Specialization of a post-order traversal. Recursive method returning the value of a sub-tree. When visiting an internal node, combine the values of the sub-trees. Algorithm evalExpr(v) if isExternal (v) return v.element () else x evalExpr(left(v)) y evalExpr(right(v)) operator stored at v return x y + - 2 5 1 3 ((2 (5 - 1)) + (3 2)) = 14 CS Data Structures
80
Print Arithmetic Expressions
Specialization of an in-order traversal. Print operand or operator when visiting node. Print “(“ before traversing left subtree. Print “)“ after traversing right subtree. Algorithm printExpression(v) if left (v) ≠ null print(“(’’) inOrder (left(v)) print(v.element ()) if right(v) ≠ null inOrder (right(v)) print (“)’’) + - 2 a 1 3 b ((2 (a - 1)) + (3 b)) CS Data Structures
81
Decision Tree Binary tree associated with a decision process.
internal nodes: Questions with yes/no answer. external nodes: Decisions Example: Whether to check . CS Data Structures
82
Example: Decision Tree
What is the maximum of these numbers: x1, x2, x3? x1 > x2 x2 > x3 x1 > x3 x3 x1 x2 No Yes No Yes No Yes CS Data Structures
83
Properties of Binary Trees
Notation: n - number of nodes e - number of external nodes (leaves) i - number of internal nodes h - height Properties: e = i + 1 n = 2e - 1 h i h (n - 1)/2 e 2h h log2 e h log2(n + 1) - 1 CS Data Structures
84
Tree Searches Algorithms for searching tree data structures.
Breadth-First Search: Starting at the root, explores all the nodes on a given level before exploring nodes on the next level. A form of Level-Order Traversal. Usually implemented with a Queue. Depth-First Search: Starting at the root, explores a node one level down. Continues searching, moving down one level down, until reach a leaf. Then backtrack to last node with a non-empty child node and repeat the search. A form of Pre-Order Traversal. Usually implemented with a Stack. CS Data Structures
85
Breadth-First Search Algorithm BFS(r) // root node of tree
Q.enqueue(r) mark r as visited while(!Q.empty) v ← Q.dequeue for all children w of v if w is not visited Q.enqueue(w) mark w as visited CS Data Structures
86
Example: Breadth-First Search
8 3 10 1 6 13 4 7 14 CS Data Structures
87
Depth-First Search Algorithm DFS(r) // root node of tree S.push(r)
mark s as visited while(!S.empty) v ← S.pop for all children w of v if w is not visited S.push(w) mark w as visited CS Data Structures
88
Example: Depth-First Search
8 3 10 1 6 13 4 7 14 CS Data Structures
89
Linked Structure for Trees
A node is represented by an object storing: Element Parent node Sequence of children nodes B A D F C E B D A C E F CS Data Structures
90
Linked Structure for Binary Trees
A node is represented by an object storing: Element Parent node Left child node Right child node B A D C E B D A C E CS Data Structures
91
Array-Based Representation of Binary Trees
3 1 2 5 6 4 9 10 A H G F E D C B J Nodes are stored in an array A A B D G H … 1 2 9 10 Node v is stored at A[rank(v)] rank(root) = 0 If node is the left child of parent(node), rank(node) = 2*rank(parent(node)) + 1 If node is the right child of parent(node), rank(node) = 2*rank(parent(node)) + 2 CS Data Structures
92
CS Data Structures
93
Tree ADT We use positions to abstract nodes Generic methods:
integer size() boolean isEmpty() Iterator iterator() Iterable positions() Accessor methods: position root() position parent(p) Iterable children(p) Integer numChildren(p) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Additional update methods may be defined by data structures implementing the Tree ADT CS Data Structures
94
Java Interface Methods for a Tree interface: CS Data Structures
95
BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position left(p) position right(p) position sibling(p) The above methods return null when there is no left, right, or sibling of p, respectively Update methods may be defined by data structures implementing the BinaryTree ADT CS Data Structures
96
Euler Tour Traversal + 2 - 3 2 5 1
Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) + L R B 2 - 3 2 5 1 CS Data Structures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.