C++ Program to find Inoder Predecessor in Threaded Binary Tree
Description :
Threaded binary tree is a binary tree in which null pointers are replaced with either inorder predecessor or inorder successors. We have already seen insertion, displaying and deletion in Threaded binary tree.
Now lets see now to find inorder predecessor of a Node in Threaded Binary Tree
Inorder Predecessor :Inorder Predecessor of an input node can be defined as the node with the largest key smaller than the key of the input node.
Node of Threaded binary tree is defined as :
structNode
{structNode *left, *right;int data;
bool lthread; //false if left pointers is a thread;bool rthread; //false if right pointer is a thread;
};
C++ Function to find inorder predecessor of a node
//returns inorder predecessor using lthreadstruct Node *inorderpedecessor ( struct Node *ptr)
{
//if lthread is true, we can quickly find itif( ptr -> lthread == true )
return ptr -> left;
//else return rightmost child of left subtreeelse
{
ptr = ptr -> left;
while(ptr -> rthread == false)
ptr = ptr -> right;
return ptr;
}
}
Comments