170 likes | 378 Views
Binary Tree . Chapter 6 (cont’). 6.4 Tree Traversal . Tree traversal is the process of visiting each node in the tree exactly one time. This definition does not specify the order in which the nodes are visited.
E N D
Binary Tree Chapter 6 (cont’)
6.4 Tree Traversal • Tree traversal is the process of visiting each node in the tree exactly one time. • This definition does not specify the order in which the nodes are visited. • There are three simple ways to traverse a tree. They’re called preorder, inorder, and postorder. The order most commonly used for binary search trees is inorder.
6.4.1 Breadth-first Traversal • BFT is visiting each node starting from the highest(or lowest) level and moving down (or up) level by level, visiting nodes on each level from left to right(or from right to left). • So, we will get 4 possibilities. • This traverse an be done using queue: after a node is visited, its children, if any, are placed at the end of the queue, and the node at the beginning of the queue is visited. • All nodes on level n must be visited before visiting any nodes on level n+1 is accomplished.
A top-down, left-to-right breadth first traversal of this tree is: 13, 10, 25, 2, 12, 20, 31, 29
6.4.2 Depth- First Traversal • DFT proceeds as far as possible to the left(or right), then backs up until the first crossroad, goes one step to the right(or left), and again as far as possible to the left(or right). • Repeat this process until all nodes are visited . • There are some variations of the depth-first traversal. • There are three tasks here: • V- visiting a node. • L- traversing the left subtree. • R- traversing the right subtree.
The number of different orders can be reduce to three traversals where the move is always from left to right and focus on the first column. • VLR – preorder tree traversal • LVR – inorder tree traversal • LRV – postorder tree traversal • The real job is done by the system on the run-time stack (using double recursion) • Remember that visiting a node means doing something to it.
Given a binary tree having a root, left subtree(LST), right sub tree(RST): 1 2 3 2 3 1 3 1 2 L R L R L R Preorder traversal NLR Inorder traversal LNR Postorder traversal LRN M. ALSabhan & M. ALMusallam Standard Traversals
Inorder traversal • An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. • The simplest way to carry out a traversal is the use of recursion. • A recursive function to traverse the tree is called with a node as an argument. Initially, this node is the root. The function must perform only three tasks. 1. Call itself to traverse the node’s left subtree. 2. Visit the node. 3. Call itself to traverse the node’s right subtree.
Inorder traversal So , the output of visiting this tree is 1 4 15 16 20 25
Inorder (another example) CBDAEF A E B C D F C B D A E F
C++ code • Like any recursive function, there must be a base case: the condition that causes the routine to return immediately, without calling itself. void inOrder(Node* pLocalRoot) { if(pLocalRoot != 0) { inOrder(pLocalRoot->pLeftChild); //left child cout << pLocalRoot->iData << “ “; //display node inOrder(pLocalRoot->pRightChild); //right child } }
Preorder and Postorder traversal • These traversals are useful with programs that analyze algebraic expressions. • A binary tree (not a binary search tree) can be used to represent an algebraic expression that involves the binary arithmetic operators +, -, /, and *. The root node holds an operator, and the other nodes represent either a variable name (like A, B, or C), or another operator. Each subtree is an algebraic expression. • For example, the algebraic expression A*(B+C) • Besides writing different kinds of algebraic expressions, you might find other uses for the different kinds of traversals like using postorder traversal to delete all the nodes when the tree is destroyed.
Preorder Traversal 1- Visit the node. 2- Call itself to traverse the node's left subtree. 3- Call itself to traverse the node's right subtree. • Postorder traversal 1. Call itself to traverse the node’s left subtree. 2. Call itself to traverse the node’s right subtree. 3. Visit the node.
Traversing the tree using preorder would generate the expression *A+BC. This is called prefix notation. • Note : parentheses are not required. • Traversing the tree using postorder would generate the expression ABC+* . This is called postfix notation..
Preorder (another example) ABCDEF A E B C D F A B C D E F
Postorder (another example) CDBFEA A E B C D F C D B F E A