330 likes | 637 Views
Tree properties and traversals. CS16: Introduction to Data Structures & Algorithms. Tree ADT Binary Tree ADT Breadth-first traversal Depth-first traversal Recursive DFT: pre-order, post-order, in-order Euler Tour traversal Practice problems. Outline.
E N D
Tree properties and traversals CS16: Introduction to Data Structures & Algorithms
Tree ADT Binary Tree ADT Breadth-first traversal Depth-first traversal Recursive DFT: pre-order, post-order, in-order Euler Tour traversal Practice problems Outline
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 Manufacturing R&D US International Laptops Desktops Europe Asia Canada What is a Tree?
A C D B E F G H I J K Tree Terminology • 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) • 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 subtree
Tree methods: • intsize(): returns the number of nodes • booleanisEmpty(): 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 • booleanisInternal(): returns true if the node has children • booleanisExternal(): returns true if the node is a leaf • booleanisRoot(): returns true if the node is the root Tree ADT
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 A C B F D E G I H Binary Tree
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 • booleanhasLeft(): returns true if node has a left child • booleanhasRight(): returns true if node has a right child Binary Tree ADT
A binary tree is a perfect binary tree if every level is completely full Perfection Not perfect Perfect!
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 Completeness Left-complete! Not left-complete
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 Tree Traversals
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 A B C D E F G H I Breadth-first traversal Breadth-first Traversal: A,B,C,D,E,F,G,H,I
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 Breadth-first traversal (2) 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
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 A B C D E F G H I Depth-first traversal Depth-first Traversal: A,C,G,F,B,E,I,H,D orA,B,D,E,H,I,C,F,G
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 Depth-first traversal (2) 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
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 Recursive Depth-First Traversals
A B C D E F G H I Pre-order traversal 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 Note: this is similar to iterative DFT
A B C D E F G H I Post-order traversal 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) Post-order Traversal: D,H,I,E,B,F,G,C,A
A B C D E F G H I In-order traversal 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
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) Euler Tour Traversal L R B
A B C D F G E H I Euler Traversal Pseudocode 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 27 17 2 18 16 26 22 6 25 3 5 21 19 15 23 7 4 20 24 11 12 8 10 14 9 13
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 Aside: Decoration
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 When do I use what traversal?
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! Practice Problem 1
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? Practice Problem 2
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? + - / 7 + 9 3 4 3 Practice Problem 3 (7 – (4 + 3)) + (9 / 3)
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! + - 7 + 9 3 4 Practice Problem 4 7 – 4 + 3 + 9 / 3 / 3
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 + - 7 + 9 3 4 Practice Problem 5 ((7 – (4 + 3)) + (9 / 3)) / 3
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 Analyzing Binary Trees fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
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 Analyzing Binary Trees (2)
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 Perfect Binary Tree Proof
Prove P(n): The number of nodes in a perfect binary tree of height nis 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 kis 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 = 2k+2 – 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. Perfect Binary Tree Proof (2)