Inorder predecessor and successor for a given key in BST

Find Inorder predecessor and successor in BST

Here given code implementation process.

//C Program 
//Inorder predecessor and successor for a given key in BST
#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*left_p,
  struct Node*right_p,
  int element)
{
  
  if(root!=NULL)
  {
  
    find_element(root->left,left_p,root,element);

    find_element(root->right,root,right_p,element);

    //Predecessor Condition
    if(root->right == NULL && right_p != NULL && (right_p->data == element))
    {
      printf("Predecessor : %d ",root->data );
    }

    else if(root->left == NULL && left_p!=NULL && root->data == element)
    {
      printf("Predecessor : %d ",left_p->data );
    }
    else if(root->left == NULL && root->data == element && left_p == NULL)
    {
      printf("Predecessor : NULL ");
    }

    //Successor Condition
    if(root->left == NULL && left_p != NULL && left_p->data == element)
    {
      printf("Successor : %d ",root->data );
    }
    else if(root->right == NULL && right_p != NULL && root->data == element)
    {
      printf("Successor : %d ",right_p->data );
    }
    else if(root->right == NULL && right_p == NULL && root->data == element)
    {
      printf("Successor : NULL " );
    }
  }
 
 
}
void successor_predecessor(struct Node*root,int element)
{
  if(root != NULL)
  {
    
   struct Node*prev=NULL; 
   printf("\nNode : %d => ", element);
   find_element(root,NULL,NULL,element);
   
  }
}

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

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


  */                


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

  successor_predecessor(root,3);
  successor_predecessor(root,6);
  successor_predecessor(root,12);
  successor_predecessor(root,7);
  successor_predecessor(root,-3);
  successor_predecessor(root,1);
    
  return 0;
}

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
/*
 C++ Program
Inorder predecessor and successor for a given key in BST
*/

#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;
  BinarySearchTree() {
    this->root = 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, Node *left_p, Node *right_p, int element) {
    if (head != NULL) {
      this->find_element(head->left, left_p, head, element);
      this->find_element(head->right, head, right_p, element);
      if (head->right == NULL && right_p != NULL && (right_p->data == element)) {
        cout << " Predecessor : " << head->data;
      } else
      if (head->left == NULL && left_p != NULL && head->data == element) {
        cout << " Predecessor :  " << left_p->data;
      } else
      if (head->left == NULL && head->data == element && left_p == NULL) {
        cout << " Predecessor : NULL ";
      }
      if (head->left == NULL && left_p != NULL && left_p->data == element) {
        cout << " Successor : " << head->data;
      } else
      if (head->right == NULL && right_p != NULL && head->data == element) {
        cout << " Successor :  " << right_p->data;
      } else
      if (head->right == NULL && right_p == NULL && head->data == element) {
        cout << " Successor : NULL ";
      }
    }
  }
  void successor_predecessor(int element) {
    if (this->root != NULL) {
      cout << "\nNode : " << element << " => ";
      this->find_element(this->root, NULL, NULL, element);
    }
  }
};

int main() {
  BinarySearchTree obj ;
    /*
              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.successor_predecessor(3);
  obj.successor_predecessor(6);
  obj.successor_predecessor(12);
  obj.successor_predecessor(7);
  obj.successor_predecessor(-3);
  obj.successor_predecessor(1);
  return 0;
}

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
//Java program
//Inorder predecessor and successor for a given key in BST

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;

  BinarySearchTree() {
    root = 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,
    Node left_p,
    Node right_p,
    int element)
  {
    
    if(head!=null)
    {
    
      find_element(head.left,left_p,head,element);

      find_element(head.right,head,right_p,element);

      //Predecessor Condition
      if(head.right == null && right_p != null && (right_p.data == element))
      {
        System.out.print(" Predecessor : "+head.data );
      }

      else if(head.left == null && left_p!=null && head.data == element)
      {
        System.out.print(" Predecessor :  "+left_p.data );
      }
      else if(head.left == null && head.data == element && left_p == null)
      {
        System.out.print(" Predecessor : NULL ");
      }

      //Successor Condition
      if(head.left == null && left_p != null && left_p.data == element)
      {
        System.out.print(" Successor : "+head.data );
      }
      else if(head.right == null && right_p != null && head.data == element)
      {
        System.out.print(" Successor :  "+right_p.data );
      }
      else if(head.right == null && right_p == null && head.data == element)
      {
        System.out.print(" Successor : NULL " );
      }
    }
   
   
  }
  public void  successor_predecessor(int element)
  {
    if(root != null)
    {
      
     System.out.print("\nNode : "+element+" => ");
     find_element(root,null,null,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.successor_predecessor(3);
  obj.successor_predecessor(6);
  obj.successor_predecessor(12);
  obj.successor_predecessor(7);
  obj.successor_predecessor(-3);
  obj.successor_predecessor(1);
    

  }
}

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
//C# program
//Inorder predecessor and successor for a given key in BST
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;

	BinarySearchTree() {
		root = 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,
		Node left_p,
		Node right_p,
		int element)
	{

		if(head!=null)
		{

			find_element(head.left,left_p,head,element);

			find_element(head.right,head,right_p,element);

			//Predecessor Condition
			if(head.right == null && right_p != null && (right_p.data == element))
			{
				Console.Write(" Predecessor : "+head.data );
			}

			else if(head.left == null && left_p!=null && head.data == element)
			{
				Console.Write(" Predecessor :  "+left_p.data );
			}
			else if(head.left == null && head.data == element && left_p == null)
			{
				Console.Write(" Predecessor : NULL ");
			}

			//Successor Condition
			if(head.left == null && left_p != null && left_p.data == element)
			{
				Console.Write(" Successor : "+head.data );
			}
			else if(head.right == null && right_p != null && head.data == element)
			{
				Console.Write(" Successor :  "+right_p.data );
			}
			else if(head.right == null && right_p == null && head.data == element)
			{
				Console.Write(" Successor : NULL " );
			}
		}


	}
	public void  successor_predecessor(int element)
	{
		if(root != null)
		{

			Console.Write("\nNode : "+element+" => ");
			find_element(root,null,null,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.successor_predecessor(3);
		obj.successor_predecessor(6);
		obj.successor_predecessor(12);
		obj.successor_predecessor(7);
		obj.successor_predecessor(-3);
		obj.successor_predecessor(1);


	}
}

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
# Python 3 Program
# Inorder predecessor and successor for a given key in BST

class Node :

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

class BinarySearchTree :
  
  def __init__(self) :
    self.root = 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, left_p, right_p, element) :
    if (head != None) :
      self.find_element(head.left, left_p, head, element)
      self.find_element(head.right, head, right_p, element)
      if (head.right == None and right_p != None and(right_p.data == element)) :
        print(" Predecessor : ", head.data,end=" ")
      elif (head.left == None and left_p != None and head.data == element) :
        print(" Predecessor :  ", left_p.data,end=" ")
      elif (head.left == None and head.data == element and left_p == None) :
        print(" Predecessor : NULL ",end=" ")
      
      if (head.left == None and left_p != None and left_p.data == element) :
        print(" Successor : ", head.data,end=" ")
      elif (head.right == None and right_p != None and head.data == element) :
        print(" Successor :  ", right_p.data,end=" ")
      elif (head.right == None and right_p == None and head.data == element) :
        print(" Successor : NULL ",end=" ")
      
    
  
  def successor_predecessor(self, element) :
    if (self.root != None) :
      print("\nNode : ", element ," => ",end=" ")
      self.find_element(self.root, None, None, element)
    
  
def main() :
  obj = BinarySearchTree()
  #
  #              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.successor_predecessor(3)
  obj.successor_predecessor(6)
  obj.successor_predecessor(12)
  obj.successor_predecessor(7)
  obj.successor_predecessor(-3)
  obj.successor_predecessor(1)
  

if __name__ == "__main__":
  main()

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
# Ruby Program
# Inorder predecessor and successor for a given key in BST

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
	attr_accessor :root
	def initialize() 
		@root = 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, left_p, right_p, element) 
		if (head != nil) 
			self.find_element(head.left, left_p, head, element)
			self.find_element(head.right, head, right_p, element)
			if (head.right == nil and right_p != nil and(right_p.data == element)) 
				print(" Predecessor  :", head.data)
			elsif (head.left == nil and left_p != nil and head.data == element) 
				print(" Predecessor  : ", left_p.data)
			elsif (head.left == nil and head.data == element and left_p == nil) 
				print(" Predecessor  :NULL ")
			end
			if (head.left == nil and left_p != nil and left_p.data == element) 
				print(" Successor  :", head.data)
			elsif (head.right == nil and right_p != nil and head.data == element) 
				print(" Successor  : ", right_p.data)
			elsif (head.right == nil and right_p == nil and head.data == element) 
				print(" Successor  :NULL ")
			end
		end
	end
	def successor_predecessor(element) 
		if (@root != nil) 
			print("\nNode  :", element ," => ")
			self.find_element(@root, nil, nil, element)
		end
	end
end
def main() 
	obj = BinarySearchTree.new()
	#
	#              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.successor_predecessor(3)
	obj.successor_predecessor(6)
	obj.successor_predecessor(12)
	obj.successor_predecessor(7)
	obj.successor_predecessor(-3)
	obj.successor_predecessor(1)
end


main()

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
<?php
/*
 Php Program
Inorder predecessor and successor for a given key in BST
*/

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

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

  function __construct() {
    $this->root = 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, $left_p, $right_p, $element) {
    if ($head != null) {
      $this->find_element($head->left, $left_p, $head, $element);
      $this->find_element($head->right, $head, $right_p, $element);
      if ($head->right == null && $right_p != null && ($right_p->data == $element)) {
        echo(" Predecessor : ". $head->data);
      } else
      if ($head->left == null && $left_p != null && $head->data == $element) {
        echo(" Predecessor :  ". $left_p->data);
      } else
      if ($head->left == null && $head->data == $element && $left_p == null) {
        echo(" Predecessor : NULL ");
      }
      if ($head->left == null && $left_p != null && $left_p->data == $element) {
        echo(" Successor : ". $head->data);
      } else
      if ($head->right == null && $right_p != null && $head->data == $element) {
        echo(" Successor :  ". $right_p->data);
      } else
      if ($head->right == null && $right_p == null && $head->data == $element) {
        echo(" Successor : NULL ");
      }
    }
  }
  public  function successor_predecessor($element) {
    if ($this->root != null) {
      echo("\nNode : ". $element ." => ");
      $this->find_element($this->root, null, null, $element);
    }
  }
}
function main() {
  $obj = new BinarySearchTree();
    /*
              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->successor_predecessor(3);
  $obj->successor_predecessor(6);
  $obj->successor_predecessor(12);
  $obj->successor_predecessor(7);
  $obj->successor_predecessor(-3);
  $obj->successor_predecessor(1);
}

main();

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
/*
 Node Js Program
Inorder predecessor and successor for a given key in BST
*/

class Node {

	constructor(value) {
		this.data = value;
		this.left = null;
		this.right = null;
	}
}
class BinarySearchTree {

	constructor() {
		this.root = 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, left_p, right_p, element) {
		if (head != null) {
			this.find_element(head.left, left_p, head, element);
			this.find_element(head.right, head, right_p, element);
			if (head.right == null && right_p != null && (right_p.data == element)) {
				process.stdout.write(" Predecessor : " + head.data);
			} else
			if (head.left == null && left_p != null && head.data == element) {
				process.stdout.write(" Predecessor :  " + left_p.data);
			} else
			if (head.left == null && head.data == element && left_p == null) {
				process.stdout.write(" Predecessor : NULL ");
			}
			if (head.left == null && left_p != null && left_p.data == element) {
				process.stdout.write(" Successor : " + head.data);
			} else
			if (head.right == null && right_p != null && head.data == element) {
				process.stdout.write(" Successor :  " + right_p.data);
			} else
			if (head.right == null && right_p == null && head.data == element) {
				process.stdout.write(" Successor : NULL ");
			}
		}
	}
	successor_predecessor(element) {
		if (this.root != null) {
			process.stdout.write("\nNode : " + element + " => ");
			this.find_element(this.root, null, null, element);
		}
	}
}

function main() {
	var obj = new BinarySearchTree();
	/*
              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.successor_predecessor(3);
	obj.successor_predecessor(6);
	obj.successor_predecessor(12);
	obj.successor_predecessor(7);
	obj.successor_predecessor(-3);
	obj.successor_predecessor(1);
}


main();

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 
/*
 Swift 4 Program
Inorder predecessor and successor for a given key in BST
*/

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? ;
  init() {
    self.root = 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? , _ left_p : Node? , _ right_p : Node? , _ element : Int) {
    if (head != nil) {
      self.find_element(head!.left, left_p, head, element);
      self.find_element(head!.right, head, right_p, element);
      if (head!.right == nil && right_p != nil && (right_p!.data == element)) {
        print(" Predecessor : ", head!.data,terminator:"");
      } else
      if (head!.left == nil && left_p != nil && head!.data == element) {
        print(" Predecessor :  ", left_p!.data,terminator:"");
      } else
      if (head!.left == nil && head!.data == element && left_p == nil) {
        print(" Predecessor : NULL ",terminator:"");
      }
      if (head!.left == nil && left_p != nil && left_p!.data == element) {
        print(" Successor : ", head!.data,terminator:"");
      } else
      if (head!.right == nil && right_p != nil && head!.data == element) {
        print(" Successor :  ", right_p!.data,terminator:"");
      } else
      if (head!.right == nil && right_p == nil && head!.data == element) {
        print(" Successor : NULL ",terminator:"");
      }
    }
  }
  func successor_predecessor(_ element: Int) {
    if (self.root != nil) {
      print("\nNode : ", element ," => ",terminator:"");
      self.find_element(self.root, nil, nil, element);
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
  /*
              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.successor_predecessor(3);
  obj.successor_predecessor(6);
  obj.successor_predecessor(12);
  obj.successor_predecessor(7);
  obj.successor_predecessor(-3);
  obj.successor_predecessor(1);
}
main();

Output

Node : 3 => Predecessor : 2 Successor : 4 
Node : 6 => Predecessor : 5 Successor : 7 
Node : 12 => Predecessor : 11 Successor : NULL 
Node : 7 => Successor : 8 Predecessor : 6 
Node : -3 => Predecessor : NULL Successor : 1 
Node : 1 => Predecessor : -3 Successor : 2 

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