tree properties and traversals cs16: introduction to data structures & algorithms thursday...
TRANSCRIPT
1
TREE PROPERTIES AND TRAVERSALS
CS16: Introduction to Data Structures & Algorithms
Thursday February 12, 2015
2
Outline
1) Tree ADT
2) Binary Tree ADT
3) Breadth-first traversal
4) Depth-first traversal
5) Recursive DFT: pre-order, post-order, in-order
6) Euler Tour traversal
7) Practice problems
Thursday February 12, 2015
3
What is a Tree?
Thursday February 12, 2015
• In computer science, a tree is an abstract model of a hierarchical structure
• A tree consists of nodes with a parent-child relation
• Applications:• Organization
charts• File systems
Computers ‘R’ Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
4
Tree Terminology
Thursday February 12, 2015
• Root: node without a 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)
• Parent node: node immediately above a given node (parent of C is A)
• Child node: node(s) immediately below a given node (children of C are G and H)
• Ancestors of a node: parent, grandparent, grand-grandparent, etc. (ancestors of G are C, A)
• Depth of a node: number of ancestors (I has depth 3)
• Height of a tree: maximum depth of any node (tree with just a root has height 0, this tree has height 3)
• Descendant of a node: child, grandchild, grand-grandchild, etc.
• Subtree: tree consisting of a node and its descendants
A
B DC
G HE F
I J K
subtree
5
Tree ADT (Abstract Data Structure)• Tree methods:
• int size(): returns the number of nodes• boolean isEmpty(): returns true if the tree is empty• Node root(): returns the root of the tree
• Node methods:• Node parent(): returns the parent of the node• Node[ ] children(): returns the children of the node• boolean isInternal(): returns true if the node has children• boolean isExternal(): returns true if the node is a leaf• boolean isRoot(): returns true if the node is the root
Thursday February 12, 2015
6
Binary Tree• In CS16, we’ll be looking mainly at
binary trees• A binary tree is a tree with the
following property:• Each internal node has at most 2
children: a left child and a right child. Even if there is only one child, the child is still specified as left or right.
• Recursive definition:• A tree consisting of a single node, or• A tree whose root has at most 2
children, each of which is a binary tree
Thursday February 12, 2015
A
B C
F GD E
H I
7
Binary Tree ADT• In addition to the methods specified in the Tree ADT, binary
tree Nodes have a few additional methods:• Node left(): returns the left child of the node if there is
one, otherwise null• Node right(): returns the right child of the node if there is
one, otherwise null• boolean hasLeft(): returns true if node has a left child• boolean hasRight(): returns true if node has a right child
Thursday February 12, 2015
8
Perfection
• A binary tree is a perfect binary tree if every level is completely full
Thursday February 12, 2015
Not perfect Perfect!
9
Completeness
• A binary tree is a left-complete binary tree if:• every level is completely full, possibly excluding the
lowest level• all nodes are as far left as possible
Thursday February 12, 2015
Left-complete! Not left-complete
10
Tree Traversals
• Unlike an array, it’s not always obvious how to visit every element stored in a tree
• A tree traversal is an algorithm for visiting every node in a tree
• There are many possible tree traversals that visit the nodes in different orders
Thursday February 12, 2015
11
Breadth-first traversal
• Starting from the root node, visit both of its children first, then all of its grandchildren, then great-grandchildren etc…
• Also known as level-order traversal
Thursday February 12, 2015
Breadth-first Traversal: A,B,C,D,E,F,G,H,I
A
B C
F GD E
H I
12
Breadth-first traversal (2)• General strategy: use a queue to keep track of the order
of nodes• Pseudocode:
• The queue guarantees that all the nodes of a given level are visited before any of their children are visited• Can enqueue node’s left and right children in any order
Thursday February 12, 2015
function bft(root): //Input: root node of tree //Output: None Q = new Queue() enqueue root while Q is not empty: node = Q.dequeue() visit(node) enqueue node’s left & right children
13
Depth-first traversal
• Starting from the root node, explore each branch as far as possible before backtracking
• This can still produce many different orders, depending on which branch you visit first
Thursday February 12, 2015
Depth-first Traversal: A,C,G,F,B,E,I,H,D
orA,B,D,E,H,I,C,F,G
A
B C
F GD E
H I
14
Depth-first traversal (2)• General strategy: use a stack to keep track of the order of nodes
• Pseudocode:
• The stack guarantees that you explore a branch all the way to a leaf before exploring another• Can add children to stack in any order
Thursday February 12, 2015
function dft(root): //Input: root node of tree //Output: none S = new Stack() push root onto S while S is not empty: node = S.pop() visit(node) push node’s left and right children
15
Visualizations
BFT
Thursday February 12, 2015
DFT
BFT gif: http://en.wikipedia.org/wiki/Breadth-first_search#mediaviewer/File:Animated_BFS.gif
DFT gif: https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif
16
Recursive Depth-First Traversals
• We can also implement DFT recursively!• Using recursion, we can end up with 3 different orders of nodes, depending on our implementation• Pre-order – visits each node before visiting its left and right children
• Post-order – visits each node after visiting its left and right children
• In-order – visits the node’s left child, itself, and then its right child
Thursday February 12, 2015
17
Pre-order traversal
Thursday February 12, 2015
function preorder(node): //Input: root node of tree //Output: None visit(node) if node has left child preorder(node.left) if node has right child preorder(node.right)
Pre-order Traversal: A,B,D,E,H,I,C,F,G
A
B C
F GD E
H I
Note: this is similar to iterative DFT
18
Post-order traversal
Thursday February 12, 2015
function postorder(node): //Input: root node of tree //Output: None if node has left child postorder(node.left) if node has right child postorder(node.right) visit(node)
A
B C
F GD E
H I
Post-order Traversal:D,H,I,E,B,F,G,C,A
19
In-order traversal
Thursday February 12, 2015
function inorder(node): //Input: root node of tree //Output: None if node has left child inorder(node.left) visit(node) if node has right child inorder(node.right) In-order Traversal:
D,B,H,E,I,A,F,C,G
A
B C
F GD E
H I
20
Visualizations
Pre-Order
Thursday February 12, 2015
In-order
Post-order
Preorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/PreOrderTrav.gifInorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/InorderTrav.gifPostorder gif: http://ceadserv1.nku.edu/longa//classes/mat385_resources/docs/traversal_files/PostorderTrav.gif
Think of theprefix as when
the root is visited!
21
Euler Tour Traversal • Generic traversal of a binary tree
• Includes as special cases the pre-order, post-order, and in-order traversals
• Walk around the tree and visit each node three times:• On the left (pre-order)• From below (in-order)• On the right (post-order)
• Creates subtrees for all nodes
Thursday February 12, 2015
B
L R
22
10
5
Euler Traversal Pseudocode
Thursday February 12, 2015
function eulerTour(node): // Input: root node of tree // Output: None
visitLeft(node) // Pre-order
if node has left child: eulerTour(node.left)
visitBelow(node) // In-order
if node has right child: eulerTour(node.right)
visitRight(node) // Post-order
1
2
3
4
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27A
B C
F GD E
H I
23
Aside: Decoration
• Often you’ll be asked to decorate each node in a tree with some value
• “Decorating” a node just means associating a value to it. There are two conventional ways to do this:
• Add a new attribute to each node• e.g. node.numDescendants = 5
• Maintain a dictionary that maps each node to its decoration – do this if you don’t want to or can’t modify the original tree• e.g. descendantDict[node] = 5
Thursday February 12, 2015
24
When do I use what traversal?
• With so many tree traversals in your arsenal, how do you know which one to use?
• Sometimes it doesn’t matter, but often there will be one traversal that makes solving a problem much easier
Thursday February 12, 2015
25
Practice Problem 1
• Decorate each node with the number of its descendants
• Best traversal: post-order• It’s easy to calculate the number descendants of a node if you already know how many descendants each of its children has
• Since post-order traversal visits both children before the parent, this is the traversal we want
• Try writing some pseudocode for this!
Thursday February 12, 2015
26
Practice Problem 2
• Given the root of a tree, determine if the tree is perfect
• Best traversal: breadth-first• This problem is easiest solved by traversing the tree
level-by-level• We can keep track of the current level, and at each
level count the number of nodes we see• Each level should have exactly twice as many nodes
as the last• There are other ways to solve this problem too - can
you think of any?
Thursday February 12, 2015
27
Practice Problem 3
• Given an arithmetic expression tree, evaluate the expression
• Best traversal: post-order• In order to evaluate an arithmetic operation, you first
need to evaluate the sub-expression on each side• What should you do when you get to a leaf?
Thursday February 12, 2015
+
- /
9 37 +
4 3
(7 – (4 + 3)) + (9 / 3)
28
Practice Problem 4
• Given an arithmetic expression tree, print out the expression, without parentheses
• Best traversal: in-order• in-order traversals gives you the nodes from left to right, which is exactly the order we want!
Thursday February 12, 2015
7 – 4 + 3 + 9 / 3+
- /
9 37 +
4 3
29
Practice Problem 5• Given an arithmetic expression tree, print out the expression, with parentheses
• Best traversal: Euler tour• If the nodes is an internal node (i.e. an operator):
• For the pre-order visit, print out “(“• For the in-order visit, print out the operator• For the post-order visit, print out “)”
• If the node is a leaf (i.e. a number):• Don’t do anything for pre-order and post-order visits• For the in-order visit, print out the number
Thursday February 12, 2015
((7 – (4 + 3)) + (9 / 3))+
- /
9 37 +
4 3
30
Analyzing Binary Trees• As you’ve seen, many things can be modeled as binary trees
• e.g. recursive calls made by a fibonacci function
• It’s often helpful to have some stats on binary trees to help analyze our data• e.g. how many recursive calls are made if the tree has height h?
• Perfect binary trees are easy to analyze, so we often use what we know about perfect binary trees to estimate what happens in the general case
Thursday February 12, 2015
fib(3)
fib(2)
fib(2)
fib(1)
fib(1)
fib(0)
fib(1)
fib(0)
fib(4)
31
Analyzing Binary Trees (2)
• The number of nodes in a perfect binary tree of height h is 2h+1 – 1
• The height of a perfect binary tree with n nodes is log2(n+1) – 1
• The number of leaves in a perfect binary tree of height h is 2h
• The number of nodes in a perfect binary tree with L leaves is 2L – 1
Thursday February 12, 2015
32
Perfect Binary Tree Proof
• Another definition of a perfect binary tree is a binary tree T which• has only one node (the root), or• has a root with left and right subtrees which are both perfect binary trees of the same height, h, in which case the height of T is h+1
• Because we have this recursive definition to work with, proofs on binary trees lend themselves well to induction
Thursday February 12, 2015
33
Perfect Binary Tree Proof (2)• Prove P(n): The number of nodes in a perfect binary tree of height n
is 2n+1 – 1• Base case, P(0):
• 20+1 – 1 = 2 – 1 = 1• The number of nodes in a perfect binary tree of height 0 is 1, because the tree
only has a root by definition
• Assume P(k), for some k ≥ 0: The number of nodes in a perfect binary tree of height k is 2k+1 – 1
• Prove P(k+1):• Let T be any perfect binary tree of height k+1• By definition, T consists of a root with two subtrees, L and R, which are both
perfect binary trees of height k• By our inductive hypothesis, L and R both have 2k+1 – 1 nodes• The number of nodes in T is therefore:1 + 2 * (2k+1 – 1) = 1 + 2k+2 – 2 = 2(k+1)+1 – 1
• Since we’ve proved P(0) and P(k) P(k+1) for any nonnegative k, by induction P(n) is true for all n ≥ 0.
Thursday February 12, 2015