WWW.VUTUBE.E DU.PK Assignment No.6 (Solution) Definitions: Inorder Successor: The node that will be visited immediately after a node is called its successor. Inorder Predecessor: The node that will be visited immediately before a node is called its predecessor. The logic behind using threaded binary trees is to used unused pointers in a binary tree in such a way that the traversal of tree becomes linear, this concept is explained in detail below, In ordinary recursive way of traversing, stack is used extensively because we call the same function again and again starting from root until we reach a leaf node so as tree size becomes larger the recursive way of traversing a tree becomes more inefficient. Tree traversal using Recursion stack Now we need some mechanism in which we don’t need to put these values in stack for this we observer use the interesting property of binary search trees, that is if we take inorder traversal of a binary tree then we always get sorted data as shown below, 30 15 10 8 45 20 12 18 40 28 38 50 42 47 52 Inorder Traversal of this tree is, 8,10,12,15,18,20,28,30,38,40,42,45,47,50,52 The data is in sorted form. We also know that in binary search trees, Inorder successor of a node is its right most leaf node in its left sub tree and inorder predecessor of a node is left most leaf node of right sub tree, this is done automatically when we insert values in binary search tree, For example consider the above tree again, 30 15 10 8 45 20 12 18 40 28 50 42 38 47 52 • The inorder predecessor of 30 is 28 and inorder successor of 30 is 38 Similarly, • The inorder predecessor of 15 is 12 and inorder successor of 15 is 18 • • The inorder predecessor of 10 is 8 and inorder successor of 10 is 12 The inorder predecessor of 20 is 18 and inorder successor of 20 is 28 and so on…. You can observe an important property of each node in Binary Search Tree that either it has both children or its pointers can work as pointers to its successors or predecessors in such a way that whole tree can be traversed without using recursion, this is explained below taking same tree again, 30 15 10 8 45 20 12 18 40 28 38 50 42 47 52 So we can have a routine that start from least node in the tree (left most leaf node) and then all its inorder successors to complete the whole traversal without the use of recursion. The basic concept for this action will be to find the inorder successor of a node by visiting the left most leaf node of right sub tree of the node. For this we do two things, 1. If a node right node pointer is a link to child then we simply move the that child and then its left most node (in this case we achieve the goal of going to left most leaf node of right sub tree) for example if we are on node 15 we will go to 20 as it is its right child and then go to 20’s left most leaf child (18 in this case) as shown below, 30 45 15 10 8 20 12 18 40 28 38 50 42 47 52 So after 15 node to be visited is 18 we will move to 18. 2. Second point is if node has right pointer as thread as will be for 18 we will go to its thread pointer (right thread of 18 will point to 20) as shown below, 30 15 10 8 45 20 12 18 40 28 38 50 42 47 52 Same applies to all nodes. One problem is still remaining that is how to start traversing because if we start from root our logic becomes incorrect as root has right child and the left most leaf node of its right child is 38 so our procedure will give output from 30 to onwards. i.e 30 38 40 42 45 and so on….. Now to correct the starting process we add a temporary node with right child as root. (We place any value in this node and make its left child point to itself as it has no use) Temporary node Dummy Left dummy node pointer Right dummy node pointer 30 15 40 20 10 8 45 12 18 28 38 50 42 47 52 Now we will stat from this temporary node and as its right child is node we will move to its right child it is 30 and then we will move to its extreme left child that is 8 we will visit it this function will return pointer to node 8 and this function will be called again with node pointer 8, as node 8 has right child as thread so we will go to it right thread it is 10 and visit it and this function will return node pointer to 10 and this function will be called again with value 10 as 10 has right child 12 as node so we go to 12 and move to its left Childs it has no left child we visiting 12 and return its value and calling now the function with pointer to 12 as 12 has now right pointer as thread so going to its thread pointer it is 15 and so on…………….. Temporary node Dummy Left dummy node pointer Right dummy node pointer 30 15 40 20 10 8 45 12 18 28 38 50 42 47 52 Now we see the code for insertion of any node keeping in view these details, Void Insert ( root * prt, int value) 1. If root pointer of the tree is NULL then, Create a new root node and assign it the int value, Create a dummy node and keep address of root in this temporary node right pointer and make this dummy node left pointer to point to itself 2. Else Do Move to current node pointer to right child from root node if value is greater than root value otherwise move to left While (child pointer is not thread) 3. Create a new node assign it the passed int value and make it the right child of the current node if its value is greater otherwise left child. 4. If this node has been inserted as right child then make current node right thread value as this node right thread value and this node left thread value equal to current node, similarly if this node has been inserted as left child then make current node left thread value as this node left thread value and current node as right thread of this new node.
© Copyright 2026 Paperzz