Tree Properties & Traversals
CS16: Introduction to Data Structures & Algorithms Spring 2020
Tree Properties & Traversals CS16: Introduction to Data - - PowerPoint PPT Presentation
Tree Properties & Traversals CS16: Introduction to Data Structures & Algorithms Spring 2020 How does OS calculate size of directories? Outline Tree & Binary Tree ADT Tree Traversals Breadth-First Traversal
Tree Properties & Traversals
CS16: Introduction to Data Structures & Algorithms Spring 2020
directories?
Outline
What is a Tree?
subtree
Tree “Anatomy”
5 A B D C G H E F I J Kroot internal leaves/ external nodes
height Does this remind you of something?
Tree Terminology
Tree ADT
Binary Trees
Binary Tree ADT
Perfection
Not perfect Perfect!
Completeness
Left- complete! Not left- complete
Aside: Decorations
Outline
Tree Traversals
Breadth- vs. Depth-First Traversals
15 A B C F G D E H I A B C F G D E H ITraversal Strategy
Traversal Strategy
17 function traversal(root): Store root in S while S is not empty get node from S do something with node store children in S A B C F G D E H JTraversal Strategy
Traversal Strategy — Grab Oldest Node
19 A B C F G D E H I function traversal(root): Store root in S while S is not empty get node from S do something with node store children in SS A B C D E F G H I A C D E F G H I B
Traversal Strategy — Grab Oldest Node
20 function traversal(root): Store root in S while S is not empty get node from S do something with node store children in SDoes S remind you of something?
A B C F G D E H IS A B C D E F G H I
Traversal Strategy — Grab Oldest Node
Breadth-First Traversal
A C D E F G H I B
Depth-First Traversal
backtracking
A H G B F K J I E C A B E F I J K C G H
Depth-First Traversal
its sibling’s descendants
Outline
Recursive Depth-First Traversal
children
Depth-First Visualizations
27Pre-order Traversal
28 function preorder(node): visit(node) if node has left child preorder(node.left) if node has right child preorder(node.right) A B C F G D E H IA D E H I C F G B
Note: like iterative DFT
Post-order Traversal
29 function postorder(node): if node has left child postorder(node.left) if node has right child postorder(node.right) visit(node) A B C F G D E H ID I E B F G C A H
In-order Traversal
30 function inorder(node): if node has left child inorder(node.left) visit(node) if node has right child inorder(node.right) A B C F G D E H ID H E I A F C G B
Outline
When to Use What Traversal?
Tree Traversal Problem
Which traversal should be used to decorate nodes with # of descendants?
33Activity #1
Tree Traversal Problem
34Activity #1
Which traversal should be used to decorate nodes with # of descendants?
Tree Traversal Problem
35Activity #1
Which traversal should be used to decorate nodes with # of descendants?
Tree Traversal Problem
descendants of both children
Tree Traversal Problem
Given root, which traversal should be used to test if tree is perfect?
37Activity #2
Tree Traversal Problem
38Activity #2
Given root, which traversal should be used to test if tree is perfect?
Tree Traversal Problem
39Activity #2
Given root, which traversal should be used to test if tree is perfect?
Tree Traversal Problem
level
40Tree Traversal Problem
before you can compute size of a folder
41Tree Traversals Problems
evaluate sub-expression on each side
(7 – (4 + 3)) + (9 / 3)
+=
Tree Traversals Problems
7 – 4 + 3 + 9 / 3
7 +parentheses
=
Euler Tour Traversal
are special cases
A
Euler Tour Traversal
L B R
Euler Tour Traversal
46 function eulerTour(node): # pre-order visitLeft(node) if node has left child: eulerTour(node.left) # in-order visitBelow(node) if node has right child: eulerTour(node.right) # post-order visitRight(node) 10 5 1 2 3 4 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27Tree Traversal Problems
parentheses
= (7 – (4 + 3)) + (9 / 3)
Tree Traversal Problem
Outline
Analyzing Binary Trees
F(n) = F(n − 1) + F(n − 2)
Analyzing Binary Trees
tree of height n?
Analyzing Perfect Binary Trees
Induction on Perfect Binary Trees
perfect binary trees of same height
Example Inductive Proof on PBTs
f(k)=2k+1–1
54Example Inductive Proof on PBTs
Tree ADT vs. Data Structure