# Inorder predecessor in binary search tree

What is an inorder predecessor in binary tree? Inorder predecessor is the node which traversed before given node in inorder traversal of binary tree. In binary search tree, it’s the previous big value before a node. For example, inorder predecessor of node(6) in below tree will 5 and for node(10) it’s 6.

If node is leftmost node in BST or least node, then there is no inorder predecessor for that node.

## Inorder predecessor : Thoughts

To find inorder predecessor , first thing to find is the node itself. As we know in inorder traversal, root node is visited after left subtree. A node can be predecessor for given node which is on right side of it.

Let’s come up with examples and see what algorithm works. First case, if given node is left most leaf node of tree, there is no inorder predecessor, in that case return NULL. For example, predecessor for node 1 is NULL.

What if node has left subtree? In that case, maximum value in left subtree will be predecessor of given node. We can find maximum value in tree by going deep down right subtree, till right subtree is NULL, and then return the last node. For example, predecessor node(10) is 6.

What are the other cases? Another case is that node does not have left subtree but it is also not the leftmost leaf node? Then parent of given node will be inorder predecessor. While moving down the tree on right side, keep track of parent node as it may be solution. predecessor of node(12) will be 10 as that’s where we moved to right subtree last time. Note that we change predecessor candidate only while moving down right subtree.

### Algorithm to find inorder predecessor

- Start with root, current = root, successor = NULL.
- If
`node.value`

>`current.value`

, then predecessor = current, current = current.right. - If
`node.value`

<`current.value`

, current = current.left. - If
`node.value`

==`current.value`

and node.left!= null, predecessor = maximum(current.left). - Return predecessor

### Inorder predevessor: Implementation

#include<stdio.h> #include<stdlib.h> struct node{ int value; struct node *left; struct node *right; }; typedef struct node Node; /* This function return the maximum node in tree rooted at node root */ Node *findMaximum(Node *root){ if(!root) return NULL; while(root->right) root = root->right; return root; } /* This function implements the logic described in algorithm to find inorder predecessor of a given node */ Node *inorderPredecessor(Node *root, int K){ Node *predecessor = NULL; Node *current = root; if(!root) return NULL; while(current && current->value != K){ /* Else take left turn and no need to update predecessor pointer */ if(current->value >K){ current= current->left; } /* If node value is less than the node which are looking for, then go to right sub tree Also when we move right, update the predecessor pointer to keep track of last right turn */ else{ predecessor = current; current = current->right; } } /*Once we reached at the node for which inorder predecessor is to be found, check if it has left sub tree, if yes then find the maximum in that right sub tree and return that node Else last right turn taken node is already stored in predecessor pointer and will be returned*/ if(current && current->left){ predecessor = findMaximum(current->left); } return predecessor; } Node * createNode(int value){ Node *newNode = (Node *)malloc(sizeof(Node)); newNode->value = value; newNode->right= NULL; newNode->left = NULL; return newNode; } Node * addNode(Node *node, int value){ if(node == NULL){ return createNode(value); } else{ if (node->value > value){ node->left = addNode(node->left, value); } else{ node->right = addNode(node->right, value); } } return node; } /* Driver program for the function written above */ int main(){ Node *root = NULL; int n = 0; //Creating a binary tree root = addNode(root,30); root = addNode(root,20); root = addNode(root,15); root = addNode(root,25); root = addNode(root,40); root = addNode(root,37); root = addNode(root,45); Node *predecessor = inorderPredecessor(root, 40); printf("\n Inorder successor node is : %d ",predecessor ? predecessor->value: 0); return 0; }

Complexity of algorithm to find *inorder predecessor * will be ** O(logN)** in almost balanced binary tree. If tree is skewed, then we have worst case complexity of O(N).

Please share if there is something wrong or missing. If you want to contribute to website and share your knowledge with thousands of learners across the world, please reach out to us at communications@algorithmsandme.com