Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree properties and traversals

Similar presentations


Presentation on theme: "Tree properties and traversals"— Presentation transcript:

1 Tree properties and traversals
Thursday February 12, 2015 Tree properties and traversals CS16: Introduction to Data Structures & Algorithms

2 Outline Tree ADT Binary Tree ADT Breadth-first traversal
Thursday February 12, 2015 Outline Tree ADT Binary Tree ADT Breadth-first traversal Depth-first traversal Recursive DFT: pre-order, post-order, in-order Euler Tour traversal Practice problems

3 Thursday February 12, 2015 What is a Tree? 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&D Manufacturing Laptops Desktops US International Europe Asia Canada

4 Tree Terminology subtree A B D C G H E F I J K
Thursday February 12, 2015 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) 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 D C G H E F I J K subtree

5 Tree ADT (Abstract Data Structure)
Thursday February 12, 2015 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

6 Binary Tree In CS16, we’ll be looking mainly at binary trees
Thursday February 12, 2015 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 A B C F G D E H I

7 Thursday February 12, 2015 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

8 Thursday February 12, 2015 Perfection A binary tree is a perfect binary tree if every level is completely full Not perfect Perfect!

9 Completeness A binary tree is a left-complete binary tree if:
Thursday February 12, 2015 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 Left-complete! Not left-complete

10 Thursday February 12, 2015 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

11 Breadth-first traversal
Thursday February 12, 2015 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 A B C F G D E H I Breadth-first Traversal: A,B,C,D,E,F,G,H,I

12 Breadth-first traversal (2)
Thursday February 12, 2015 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 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
Thursday February 12, 2015 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 A B C F G D E H I Depth-first Traversal: A,C,G,F,B,E,I,H,D or A,B,D,E,H,I,C,F,G

14 Depth-first traversal (2)
Thursday February 12, 2015 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 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 DFT
Thursday February 12, 2015 Visualizations BFT DFT BFT gif: DFT gif: BFT gif: DFT gif:

16 Recursive Depth-First Traversals
Thursday February 12, 2015 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

17 Pre-order Traversal: A,B,D,E,H,I,C,F,G
Thursday February 12, 2015 Pre-order traversal A B C F G D E H I 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

18 Post-order Traversal:
Thursday February 12, 2015 Post-order traversal A B C F G D E H I 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

19 In-order traversal function inorder(node): //Input: root node of tree
Thursday February 12, 2015 In-order traversal A B C F G D E H I 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

20 Visualizations Pre-Order In-order Post-order Think of the
Thursday February 12, 2015 Visualizations Pre-Order Think of the prefix as when the root is visited! In-order Preorder gif: Inorder gif: Postorder gif: Post-order Preorder gif: Inorder gif: Postorder gif:

21 Euler Tour Traversal Generic traversal of a binary tree
Thursday February 12, 2015 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 L R B

22 Euler Traversal Pseudocode
Thursday February 12, 2015 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 A B C F G D E H I 27 17 2 16 18 26 22 6 3 5 25 7 15 19 21 23 4 20 24 11 8 10 12 14 9 13

23 Thursday February 12, 2015 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

24 When do I use what traversal?
Thursday February 12, 2015 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

25 Thursday February 12, 2015 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!

26 Thursday February 12, 2015 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?

27 Thursday February 12, 2015 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? + - / 9 3 7 4 (7 – (4 + 3)) + (9 / 3)

28 Thursday February 12, 2015 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! + - / 9 3 7 4 7 – / 3

29 Thursday February 12, 2015 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 + - / 9 3 7 4 ((7 – (4 + 3)) + (9 / 3))

30 Analyzing Binary Trees
Thursday February 12, 2015 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 fib(3) fib(2) fib(1) fib(0) fib(4)

31 Analyzing Binary Trees (2)
Thursday February 12, 2015 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

32 Perfect Binary Tree Proof
Thursday February 12, 2015 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

33 Perfect Binary Tree Proof (2)
Thursday February 12, 2015 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.


Download ppt "Tree properties and traversals"

Similar presentations


Ads by Google