Presentation is loading. Please wait.

Presentation is loading. Please wait.

8.2 Tree Traversals Chapter 8 - Trees.

Similar presentations


Presentation on theme: "8.2 Tree Traversals Chapter 8 - Trees."— Presentation transcript:

1 8.2 Tree Traversals Chapter 8 - Trees

2 Attendance Quiz #26 Stacks

3 Tip #28: Iterator end(); Trees How does STL guarantee that end will always point to one past the last element in container? The STL does not guarantee this behavior! What's one past the last element in a set or a map? The "end is one past the last element" thing is just a logical concept. Exactly how this behavior is achieved depends on the implementation. Don't take it too literally. end() might be implemented as you intuitively expect or not! What do we know about the STL iterators? begin() returns an iterator referring to the first element in the container. If the container is empty, then begin() == end(). end() returns an iterator which is the "past-the-end" value for the container and is NOT a member of the container. BST<int>::Iterator iter = bst.begin(); BST<int>::Iterator end_iter = bst.end(); if (iter == end_iter) cout << " Empty"; for (; iter != end_iter; iter++) cout << " " << *iter;

4 Binary Trees

5 Recursively Searching a Binary Tree
Trees Binary search trees All elements in the left subtree precede those in the right subtree A formal definition: A set of nodes T is a binary search tree if either of the following is true: T is empty If T is not empty, its root node has two subtrees, TL and TR, such that TL and TR are binary search trees and the value in the root node of T is greater than all values in TL and is less than all values in TR dog cat wolf canine

6 Recursively Searching a Binary Tree
Trees When searching a BST, a typical probe eliminates half the elements in the tree, so if the tree is relatively balanced, searching can be O(log n). In the worst case, searching is O(n). The elements in a binary search tree never need to be sorted because they always satisfy the required order relationships. When new elements are inserted (or existing elements are removed) properly, the binary search tree maintains its order. In contrast, a sorted array must be expanded whenever new elements are added, and compacted whenever elements are removed—expanding and contracting are both O(n).

7 Fullness and Completeness
Trees A full binary tree is a binary tree where all internal nodes have exactly 2 children. Note: the number of leaf nodes is one more than the number of internal nodes. 7 10 1 12 9 3 5 2 11 6 4 13

8 Fullness and Completeness
Trees 7 10 3 11 9 5 1 6 4 8 2 Note: not a full binary tree as node 9 only has 1 child. is filled (has a value) up to depth (h – 1) and, at depth h, any unfilled nodes are on the right. A complete binary tree of height h Also, with a complete binary tree, All nodes at depth (h – 2) and above have two children. When a node at depth (h – 1) has children, all nodes to the left of it have two children. If a node at depth (h – 1) has one child, it is a left child.

9 General Trees Trees We do not explore general trees in this chapter, but nodes of a general tree can have any number of subtrees.

10 General Trees A general tree can be represented using a binary tree.
The left branch of a node is the oldest child, and each right branch is connected to the next younger sibling (if any). Children Siblings Children Siblings Children Siblings Children Siblings Children Siblings Siblings

11 8.2, pgs. 454-455 8.2 Tree Traversals Visualizing Tree Traversals
Traversals of Binary Search Trees and Expression Trees 8.2, pgs

12 Tree Traversals Trees Often we want to determine the nodes of a tree and their relationship We can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three common kinds of tree traversal Pre-order In-order Post-order Level-order

13 Tree Traversals Preorder: visit root node, traverse TL , traverse TR
Trees Preorder: visit root node, traverse TL , traverse TR Inorder: traverse TL , visit root node, traverse TR Postorder: traverse TL , traverse TR , visit root node

14 Visualizing Tree Traversals
Trees You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree. If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour. The Euler tour is the path traced in blue in the figure on the right.

15 Visualizing Tree Traversals
Trees If the mouse follows an Euler tour route (blue path) and visits each node before traversing its subtrees (shown by the downward pointing arrows), then we get a pre- order traversal. The sequence in this example is: a b d g e h c f i j

16 Visualizing Tree Traversals
Trees If we record a node as the mouse returns after traversing its left subtree (shown by horizontal black arrows in the figure) we get an in-order traversal. The sequence in this example is: d g b h e a i f j c

17 Visualizing Tree Traversals
Trees If we record each node as the mouse last encounters it, we get a post-order traversal (shown by the upward pointing arrows). The sequence in this example is: g d h e b i j f c a

18 Visualizing Tree Traversals
Trees If we record each node by height within the tree, we get a level-order traversal. The sequence in this example is: a b c d e f g h i j

19 Quiz A A B C D E F G H I B D A G E C H F I A B D C E G F H I
Trees Level-order: In-order: Pre-order: Post-order: A B C D E F G H I B D A G E C H F I A B D C E G F H I D B G E H I F C A

20 Quiz B A B C D E F G H I J H D I B E A F C G J A B D H I E C F G J
Trees Level-order: In-order: Pre-order: Post-order: A B C D E F G H I J H D I B E A F C G J A B D H I E C F G J H I D E B F J G C A

21 Traversal of Binary Search Tree
Trees An in-order traversal of a binary search tree results in the nodes being visited in sequence by increasing data value. dog cat wolf canine The sequence in this example is: canine, cat, dog, wolf

22 Traversal of Expression Tree
Trees An in-order traversal of an expression tree results in the sequence x + y * a + b / c A post-order traversal of an expression tree results in the sequence x y + a b + c / * The postfix or reverse polish form. Operators follow operands. A pre-order traversal of an expression tree results in the sequence * + x y / + a b c This is the prefix form of the expression. Operators precede operands. * / + c y x b a

23


Download ppt "8.2 Tree Traversals Chapter 8 - Trees."

Similar presentations


Ads by Google