Download presentation
Presentation is loading. Please wait.
1
8.2 Tree Traversals Chapter 8 - Trees
2
Tip #26: "Path of Least Resistance"
Recursion "The great learner expects resistance and overcomes it…. Some learning has been easy for you. But more often your enemy has been discouragement. You may try to avoid that by choosing to learn only what is easy for you, looking for the path of least resistance. But the great learner expects difficulty as part of learning and is determined to work through it." Henry B. Eyring, BYU Speeches, A Child of God;
3
Fullness, Predecessor, Successor
Trees Every node in a binary tree has at most 2 children. Each node contains Data Pointer to left child (TL). Pointer to right child (TR). 7 10 1 12 9 3 5 2 11 6 4 13 Root Successor A full binary tree is a binary tree where all internal nodes have exactly 2 children. A node predecessor is the left right-most node. Root Predecessor A node successor is the right left-most node.
4
Completeness A complete binary tree of height h
Trees Note: not a full binary tree as node 9 only has 1 child. 7 10 3 11 9 5 1 6 4 8 2 Height: 4 Level: 1 Height: 3 Level: 2 is filled (has a value) down to level (h – 1) and, Height: 2 Level: 3 Height: 1 Level: 4 at level h, any un-filled nodes are to the right. A complete binary tree of height h Also, with a complete binary tree, All nodes down to level (h – 2) have two children. If a node at level (h – 1) has one child, it is a left child and all nodes to the left have two children.
5
Tree Terminology Summary
Trees Ancestor – A parent of a node or its ancestors. Ancestor-Descendant relationship – Tree with parent-child relationships. Branch (edge) – Link between nodes. Child – An immediate successor node. Complete Binary Tree – All unfilled nodes are on max tree depth and to the right. Depth – The number of branches between the root of the tree and the node. Descendent – A child node or a descendant of a child node. External Node (leaf node) – A node without children. Full Binary Tree – All internal nodes have exactly 2 children. Height – The number of nodes in the longest path from the node to a leaf node. Internal Node – A non-external node (non-leaf node.) Level – depth of a node plus 1. (The root is the only node at level 1, depth 0.) Node (vertex) – A basic unit of storage. Parent – Immediate ancestor of a node. Path – A sequence of nodes and branches connecting the node with descendants. Predecessor – The left, right-most descendant node. Root – The node at top of tree (without parent.) Sibling – A node with the same parent as another node. Subtree – A node and all of its children. Successor – The right, left-most descendant node. Traversal – Moving thru a path of tree nodes. Visit – Processing a node’s value.
6
Binary Trees
7
Recursively Searching a Binary Tree
Trees Binary search tree 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 Bear
8
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).
9
8.2, pgs. 454-455 8.2 Tree Traversals Visualizing Tree Traversals
Traversals of Binary Search Trees and Expression Trees 8.2, pgs
10
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
11
Recursive Tree Traversals
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
12
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.
13
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
14
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
15
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
16
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
17
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
18
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
19
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. 8 4 12 2 6 10 14 1 3 5 7 The sequence in this example is: 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14
20
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 Note: in-order traversal of an expression tree requires sub-tree parentheses: (x + y) * ((a + b) / c)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.