Inorder Predecessor in Binary Search Tree

Find inorder predecessor in BST

Here given code implementation process.

//C Program 
//Inorder Predecessor in Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
//structure of Binary Search Tree node
struct Node
{
  int data;
  struct Node *left,*right; 
};

//Adding a new node in binary search tree
void add( struct Node **root, int data)
{
  //Create a dynamic node of binary search tree 
  struct Node *new_node = (struct Node *)malloc(sizeof(struct Node ));

  if(new_node!=NULL)
  {
    //Set data and pointer values
    new_node->data = data;
    new_node->left = NULL; //Initially node left-pointer is NULL
    new_node->right = NULL;//Initially node right-pointer is NULL

    if(*root == NULL)
    {
      //When adds a first node in binary tree
      *root = new_node;
    }
    else
    {
      struct Node *find = *root;
      //iterate binary tree and add new node to proper position
      while(find != NULL)
      {
        if(find -> data > data)
        { 
          if(find->left==NULL)
          {
            find->left = new_node;
            break;
          }
          else
          { //visit left sub-tree
            find = find->left;
          }
        }
        else
        {
          if(find->right == NULL)
          {
            find->right = new_node;
            break;
          }
          else
          {
            //visit right sub-tree
            find = find->right;
          }
        }
      }
    }
  }else
  {
    printf("Memory Overflow\n");
    exit(0); //Terminate program execution
  }

}
void find_element(struct Node*root,struct Node**prev,int element)
{
  
  if(root!=NULL)
  {
  
    find_element(root->left,prev,element);
    if(root->data==element)
    {
      if(*prev!=NULL)
      {
        printf("%d\n",(*prev)->data);
      }
      else
      {
        printf("NULL\n");
      }
      return;
    }
    *prev=root;
    find_element(root->right,prev,element);
   
  }
}
void inorder_predecessor(struct Node*root,int element)
{
  if(root != NULL)
  {
    
   struct Node*prev=NULL; 
   printf(" Node %d inorder predecessor => ",element);
   find_element(root,&prev,element);
   printf("\n");
  }
 
 
}

int main(){
    
  struct Node*root = NULL;

  //Add nodes in binary search tree
  /*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    8   11
       / \   /   /      \
     -3  2  4   7       12


  */                


    add(&root,6); 
    add(&root,3); 
    add(&root,9); 
    add(&root,1); 
    add(&root,5); 
    add(&root,8); 
    add(&root,11); 
    add(&root,-3); 
    add(&root,2); 
    add(&root,7); 
    add(&root,12);
    add(&root,4);  
  
    inorder_predecessor(root,3);
    inorder_predecessor(root,6);
    inorder_predecessor(root,12);
    inorder_predecessor(root,7);
    
  return 0;
}

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
/*
 C++ Program
 Inorder Predecessor in Binary Search Tree
*/

#include<iostream>

using namespace std;
class Node {
  public:
  int data;
  Node *left;
  Node *right;
  Node(int value) {
    this->data = value;
    this->left = NULL;
    this->right = NULL;
  }
};
class BinarySearchTree {
public:
  Node *root;
  Node *result;
  BinarySearchTree() {
    this->root = NULL;
    this->result = NULL;
  }
  void add(int value) {
    Node *new_node = new Node(value);
    if (new_node != NULL) {
      if (this->root == NULL) {
        this->root = new_node;
      } else {
        Node *find = this->root;
        while (find != NULL) {
          if (find->data >= value) {
            if (find->left == NULL) {
              find->left = new_node;
              break;
            } else {
              find = find->left;
            }
          } else {
            if (find->right == NULL) {
              find->right = new_node;
              break;
            } else {
              find = find->right;
            }
          }
        }
      }
    } else {
      cout << "\nMemory Overflow\n";
    }
  }
  void find_element(Node *head, int element) {
    if (head != NULL) {
      this->find_element(head->left, element);
      if (head->data == element) {
        if (this->result != NULL) {
          cout << this->result->data << " \n";
        } else {
          cout << "NULL\n";
        }
        return;
      }
      this->result = head;
      this->find_element(head->right, element);
    }
  }
  void inorder_predecessor(int element) {
    if (this->root != NULL) {
      this->result = NULL;
      cout << "\n Node " << element << " inorder predecessor => ";
      this->find_element(this->root, element);
    }
  }
};

int main() {
  BinarySearchTree obj ;
  /*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    8   11
       / \   /   /      \
     -3  2  4   7       12


  */       
  obj.add(6);
  obj.add(3);
  obj.add(9);
  obj.add(1);
  obj.add(5);
  obj.add(7);
  obj.add(8);
  obj.add(11);
  obj.add(-3);
  obj.add(2);
  obj.add(12);
  obj.add(4);
  obj.inorder_predecessor(3);
  obj.inorder_predecessor(6);
  obj.inorder_predecessor(12);
  obj.inorder_predecessor(7);
  return 0;
}

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
//Java program
//Inorder predecessor in Binary Search Tree

class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int value) {
    data = value;
    left = null;
    right = null;
  }
}
public class BinarySearchTree {


  public Node root;
  public Node result;
  BinarySearchTree() {
    root = null;
    result = null;

  }
  //insert a node in BST
  public void add(int value) {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node(value);

    if (new_node != null) {
      if (root == null) {
        //When adds a first node in binary tree
        root = new_node;
      } else {
        Node find = root;

        //add new node to proper position
        while (find != null) {
          if (find.data >= value) {
            if (find.left == null) {
              find.left = new_node;
              break;
            } else {
              //visit left sub-tree
              find = find.left;
            }
          } else {
            if (find.right == null) {
              find.right = new_node;
              break;
            } else {
              //visit right sub-tree
              find = find.right;
            }
          }
        }
      }
    } else {
      System.out.print("\nMemory Overflow\n");
    }
  }
public void  find_element(Node head,int element)
{
  
  if(head!=null)
  {
  
    find_element(head.left,element);

    if(head.data==element)
    {
      if(this.result!=null)
      {
        System.out.print(this.result.data+" \n");
      }
      else
      {
        System.out.print("null\n");
      }
      return;
    }
    this.result = head;
    find_element(head.right,element);
  }
}
public void  inorder_predecessor(int element)
{
  if(this.root != null)
  {
    
   this.result=null; 

    System.out.print("\n Node "+element+" inorder predecessor => ");
   find_element(this.root,element);
   
  }
 
 
}
  public static void main(String[] args) {

    BinarySearchTree obj = new BinarySearchTree();
    //Add nodes in binary search tree
  /*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    7   11
       / \   /     \    \
     -3  2  4       8   12


  */                


  obj.add(6); 
  obj.add(3); 
  obj.add(9); 
  obj.add(1); 
  obj.add(5); 
  obj.add(7); 
  obj.add(8); 
  obj.add(11); 
  obj.add(-3); 
  obj.add(2); 
  
  obj.add(12);
  obj.add(4);  

  obj.inorder_predecessor(3);
  obj.inorder_predecessor(6);
  obj.inorder_predecessor(12);
  obj.inorder_predecessor(7);

  }
}

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
//C# program
//Inorder predecessor in Binary Search Tree
using System;
public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int value) {
		data = value;
		left = null;
		right = null;
	}
}
public class BinarySearchTree {


	public Node root;
	public Node result;
	BinarySearchTree() {
		root = null;
		result = null;

	}
	//insert a node in BST
	public void add(int value) {
		//Create a dynamic node of binary search tree 
		Node new_node = new Node(value);

		if (new_node != null) {
			if (root == null) {
				//When adds a first node in binary tree
				root = new_node;
			} else {
				Node find = root;

				//add new node to proper position
				while (find != null) {
					if (find.data >= value) {
						if (find.left == null) {
							find.left = new_node;
							break;
						} else {
							//visit left sub-tree
							find = find.left;
						}
					} else {
						if (find.right == null) {
							find.right = new_node;
							break;
						} else {
							//visit right sub-tree
							find = find.right;
						}
					}
				}
			}
		} else {
			Console.Write("\nMemory Overflow\n");
		}
	}
	public void  find_element(Node head,int element)
	{

		if(head!=null)
		{

			find_element(head.left,element);

			if(head.data==element)
			{
				if(this.result!=null)
				{
					Console.Write(this.result.data+" \n");
				}
				else
				{
					Console.Write("null\n");
				}
				return;
			}
			this.result = head;
			find_element(head.right,element);
		}
	}
	public void  inorder_predecessor(int element)
	{
		if(this.root != null)
		{

			this.result=null; 

			Console.Write("\n Node "+element+" inorder predecessor => ");
			find_element(this.root,element);

		}


	}
	public static void Main(String[] args) {

		BinarySearchTree obj = new BinarySearchTree();
		//Add nodes in binary search tree
		/*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    7   11
       / \   /     \    \
     -3  2  4       8   12


  */                


		obj.add(6); 
		obj.add(3); 
		obj.add(9); 
		obj.add(1); 
		obj.add(5); 
		obj.add(7); 
		obj.add(8); 
		obj.add(11); 
		obj.add(-3); 
		obj.add(2); 

		obj.add(12);
		obj.add(4);  

		obj.inorder_predecessor(3);
		obj.inorder_predecessor(6);
		obj.inorder_predecessor(12);
		obj.inorder_predecessor(7);

	}
}

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
# Python 3 Program
# Inorder Predecessor in Binary Search Tree

class Node :

  def __init__(self, value) :
    self.data = value
    self.left = None
    self.right = None
  

class BinarySearchTree :

  def __init__(self) :
    self.root = None
    self.result = None
  
  def add(self, value) :
    new_node = Node(value)
    if (new_node != None) :
      if (self.root == None) :
        self.root = new_node
      else :
        find = self.root
        while (find != None) :
          if (find.data >= value) :
            if (find.left == None) :
              find.left = new_node
              break
            else :
              find = find.left
            
          else :
            if (find.right == None) :
              find.right = new_node
              break
            else :
              find = find.right
            
          
        
      
    else :
      print("\nMemory Overflow\n")
    
  
  def find_element(self, head, element) :
    if (head != None) :
      self.find_element(head.left, element)
      if (head.data == element) :
        if (self.result != None) :
          print(self.result.data )
        else :
          print("null")
        
        return
      
      self.result = head
      self.find_element(head.right, element)
    
  
  def inorder_predecessor(self, element) :
    if (self.root != None) :
      self.result = None
      print("\n Node ", element ," inorder predecessor => ",end="")
      self.find_element(self.root, element)
    
  
def main() :
  obj = BinarySearchTree()
  
  #
  #              6
  #            /    \
  #           /      \
  #          3        9
  #         /  \      / \
  #        1    5    8   11
  #       / \   /   /      \
  #     -3  2  4   7       12
  #  
  obj.add(6)
  obj.add(3)
  obj.add(9)
  obj.add(1)
  obj.add(5)
  obj.add(7)
  obj.add(8)
  obj.add(11)
  obj.add(-3)
  obj.add(2)
  obj.add(12)
  obj.add(4)
  obj.inorder_predecessor(3)
  obj.inorder_predecessor(6)
  obj.inorder_predecessor(12)
  obj.inorder_predecessor(7)


if __name__ == "__main__":
  main()

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
# Ruby Program
# Inorder Predecessor in Binary Search Tree

class Node 
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
	def initialize(value) 
		@data = value
		@left = nil
		@right = nil
	end
end

class BinarySearchTree 
	attr_reader :root, :result
	attr_accessor :root, :result
	def initialize() 
		@root = nil
		@result = nil
	end
	def add(value) 
		new_node = Node.new(value)
		if (new_node != nil) 
			if (@root == nil) 
				@root = new_node
			else 
				find = @root
				while (find != nil) 
					if (find.data >= value) 
						if (find.left == nil) 
							find.left = new_node
							break
						else 
							find = find.left
						end
					else 
						if (find.right == nil) 
							find.right = new_node
							break
						else 
							find = find.right
						end
					end
				end
			end
		else 
			print("\nMemory Overflow\n")
		end
	end
	def find_element(head, element) 
		if (head != nil) 
			self.find_element(head.left, element)
			if (head.data == element) 
				if (self.result != nil) 
					print(self.result.data ," \n")
				else 
					print("null\n")
				end
				return
			end
			self.result = head
			self.find_element(head.right, element)
		end
	end
	def inorder_predecessor(element) 
		if (self.root != nil) 
			self.result = nil
			print("\n Node ", element ," inorder predecessor => ")
			self.find_element(self.root, element)
		end
	end
end
def main() 
	obj = BinarySearchTree.new()
	
	#
	#              6
	#            /    \
	#           /      \
	#          3        9
	#         /  \      / \
	#        1    5    8   11
	#       / \   /   /      \
	#     -3  2  4   7       12
	#  
	obj.add(6)
	obj.add(3)
	obj.add(9)
	obj.add(1)
	obj.add(5)
	obj.add(7)
	obj.add(8)
	obj.add(11)
	obj.add(-3)
	obj.add(2)
	obj.add(12)
	obj.add(4)
	obj.inorder_predecessor(3)
	obj.inorder_predecessor(6)
	obj.inorder_predecessor(12)
	obj.inorder_predecessor(7)
end

main()

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
<?php
/*
 Php Program
 Inorder Predecessor in Binary Search Tree
*/

class Node {
  public $data;
  public $left;
  public $right;

  function __construct($value) {
    $this->data = $value;
    $this->left = null;
    $this->right = null;
  }
}
class BinarySearchTree {
  public $root;
  public $result;

  function __construct() {
    $this->root = null;
    $this->result = null;
  }
  public  function add($value) {
    $new_node = new Node($value);
    if ($new_node != null) {
      if ($this->root == null) {
        $this->root = $new_node;
      } else {
        $find = $this->root;
        while ($find != null) {
          if ($find->data >= $value) {
            if ($find->left == null) {
              $find->left = $new_node;
              break;
            } else {
              $find = $find->left;
            }
          } else {
            if ($find->right == null) {
              $find->right = $new_node;
              break;
            } else {
              $find = $find->right;
            }
          }
        }
      }
    } else {
      echo("\nMemory Overflow\n");
    }
  }
  public  function find_element($head, $element) {
    if ($head != null) {
      $this->find_element($head->left, $element);
      if ($head->data == $element) {
        if ($this->result != null) {
          echo($this->result->data ." \n");
        } else {
          echo("null\n");
        }
        return;
      }
      $this->result = $head;
      $this->find_element($head->right, $element);
    }
  }
  public  function inorder_predecessor($element) {
    if ($this->root != null) {
      $this->result = null;
      echo("\n Node ". $element ." inorder predecessor => ");
      $this->find_element($this->root, $element);
    }
  }
}
function main() {
  $obj = new BinarySearchTree();
  /*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    8   11
       / \   /   /      \
     -3  2  4   7       12


  */       
  $obj->add(6);
  $obj->add(3);
  $obj->add(9);
  $obj->add(1);
  $obj->add(5);
  $obj->add(7);
  $obj->add(8);
  $obj->add(11);
  $obj->add(-3);
  $obj->add(2);
  $obj->add(12);
  $obj->add(4);
  $obj->inorder_predecessor(3);
  $obj->inorder_predecessor(6);
  $obj->inorder_predecessor(12);
  $obj->inorder_predecessor(7);
}
main();

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
/*
 Node Js Program
 Inorder Predecessor in Binary Search Tree
*/

class Node {
	
	constructor(value) {
		this.data = value;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree {
	
	constructor() {
		this.root = null;
		this.result = null;
	}
	add(value) {
		var new_node = new Node(value);
		if (new_node != null) {
			if (this.root == null) {
				this.root = new_node;
			} else {
				var find = this.root;
				while (find != null) {
					if (find.data >= value) {
						if (find.left == null) {
							find.left = new_node;
							break;
						} else {
							find = find.left;
						}
					} else {
						if (find.right == null) {
							find.right = new_node;
							break;
						} else {
							find = find.right;
						}
					}
				}
			}
		} else {
			process.stdout.write("\nMemory Overflow\n");
		}
	}
	find_element(head, element) {
		if (head != null) {
			this.find_element(head.left, element);
			if (head.data == element) {
				if (this.result != null) {
					process.stdout.write(this.result.data + " \n");
				} else {
					process.stdout.write("null\n");
				}
				return;
			}
			this.result = head;
			this.find_element(head.right, element);
		}
	}
	inorder_predecessor(element) {
		if (this.root != null) {
			this.result = null;
			process.stdout.write("\n Node " + element + " inorder predecessor => ");
			this.find_element(this.root, element);
		}
	}
}

function main() {
	var obj = new BinarySearchTree();
	/*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    8   11
       / \   /   /      \
     -3  2  4   7       12


   */       
	obj.add(6);
	obj.add(3);
	obj.add(9);
	obj.add(1);
	obj.add(5);
	obj.add(7);
	obj.add(8);
	obj.add(11);
	obj.add(-3);
	obj.add(2);
	obj.add(12);
	obj.add(4);
	obj.inorder_predecessor(3);
	obj.inorder_predecessor(6);
	obj.inorder_predecessor(12);
	obj.inorder_predecessor(7);
}


main();

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6
/*
 Swift 4 Program
 Inorder Predecessor in Binary Search Tree
*/

class Node {
  var data: Int;
  var left: Node? ;
  var right: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.left = nil;
    self.right = nil;
  }
}
class BinarySearchTree {
  var root: Node? ;
  var result: Node? ;
  init() {
    self.root = nil;
    self.result = nil;
  }
  func add(_ value: Int) {
    let new_node: Node? = Node(value);
    if (new_node != nil) {
      if (self.root == nil) {
        self.root = new_node;
      } else {
        var find: Node? = self.root;
        while (find != nil) {
          if (find!.data >= value) {
            if (find!.left == nil) {
              find!.left = new_node;
              break;
            } else {
              find = find!.left;
            }
          } else {
            if (find!.right == nil) {
              find!.right = new_node;
              break;
            } else {
              find = find!.right;
            }
          }
        }
      }
    } else {
      print("\nMemory Overflow\n");
    }
  }
  func find_element(_ head: Node? , _ element : Int) {
    if (head != nil) {
      self.find_element(head!.left, element);
      if (head!.data == element) {
        if (self.result != nil) {
          print(self.result!.data );
        } else {
          print("null");
        }
        return;
      }
      self.result = head;
      self.find_element(head!.right, element);
    }
  }
  func inorder_predecessor(_ element: Int) {
    if (self.root != nil) {
      self.result = nil;
      print("\n Node ", element ," inorder predecessor => ", terminator:" ");
      self.find_element(self.root, element);
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
  /*
              6
            /    \
           /      \
          3        9
         /  \      / \
        1    5    8   11
       / \   /   /      \
     -3  2  4   7       12


  */       
  obj.add(6);
  obj.add(3);
  obj.add(9);
  obj.add(1);
  obj.add(5);
  obj.add(7);
  obj.add(8);
  obj.add(11);
  obj.add(-3);
  obj.add(2);
  obj.add(12);
  obj.add(4);
  obj.inorder_predecessor(3);
  obj.inorder_predecessor(6);
  obj.inorder_predecessor(12);
  obj.inorder_predecessor(7);
}
main();

Output

 Node 3 inorder predecessor => 2

 Node 6 inorder predecessor => 5

 Node 12 inorder predecessor => 11

 Node 7 inorder predecessor => 6


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved