Find maximum element between two nodes of BST

find max element between two nodes in BST

Here given code implementation process.

//C Program 
//Maximum element between two nodes of 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
  }

}



struct Node* find_node(struct Node *root,
  int data)
{

    if(root!=NULL)
    {
     
      if(root->data > data)
      {
        return find_node(root->left,data);
      }
      else if(root->data < data )
      {
        return find_node(root->right,data);
      }
      else
      {
        return root;
      }
    }
    return NULL;
}

struct Node* lca(struct Node *root,
  int first,int second)
{

    if(root!=NULL)
    {

      if(root->data > first && root->data > second)
      {
        return lca(root->left,first,second);
      }
      else if(root->data < first && root->data < second)
      {
        return lca(root->right,first,second);
      }
      else
      {
        return root;
      }

    }
    return NULL;
}
void find_elements(struct Node *root,int first,int second)
{

  if(root!=NULL)
  {
    //First we are check that given elements are exist or not in BST
    //If given element is exist then get its address
    struct Node*node1=find_node(root,first);
    struct Node*node2=find_node(root,second);

    if(node1!=NULL && node2!=NULL)
    {

      struct Node*head=lca(root,first,second);
      
      int find=first,result=0;

      if(first < second)
      {
        find=second;
      }
      result=find;

      while(head!=NULL)
      {
        if(head->data > result)
        {
          result=head->data;
        }
        if(head->data > find)
        {
          head=head->left;
        }
        else if(head->data < find)
        {
          head=head->right;
        }
        else
        {
          break;
        }
      }
      printf("Max Node Between [%d,%d] is %d \n",first,second,result);
    }
    else
    {
      printf("Node are missing\n");
    }
  }
  else
  {
    printf("\nEmpty BST");
  }
}
int main(){
    
  struct Node*root = NULL;

  //Add nodes in binary search tree

  /*
         10
       /   \
      3     19
     / \   /  \
    1   4  14  50
   / \     
 -3   2          

  */



  add(&root,10); 
  add(&root,3); 
  add(&root,19); 
  add(&root,1); 
  add(&root,4);
  add(&root,-3); 
  add(&root,2); 
  add(&root,14); 
  add(&root,50); 

  //Test case
  find_elements(root,2,14);
  find_elements(root,-3,4);
  find_elements(root,10,2);
  find_elements(root,3,2);

  return 0;
}

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
/*
 C++ Program
 Maximum element between two nodes of 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";
    }
  }
  Node *find_node(Node *head, int value) {
    if (head != NULL) {
      if (head->data > value) {
        return this->find_node(head->left, value);
      } else
      if (head->data < value) {
        return this->find_node(head->right, value);
      } else {
        return head;
      }
    }
    return NULL;
  }
  Node *lca(Node *head, int first, int second) {
    if (head != NULL) {
      if (head->data > first && head->data > second) {
        return this->lca(head->left, first, second);
      } else
      if (head->data < first && head->data < second) {
        return this->lca(head->right, first, second);
      } else {
        return head;
      }
    }
    return NULL;
  }
  void find_elements(int first, int second) {
    if (this->root != NULL) {
      Node *node1 = this->find_node(this->root, first);
      Node *node2 = this->find_node(this->root, second);
      if (node1 != NULL && node2 != NULL) {
        Node *head = this->lca(this->root, first, second);
        int find = first, result = 0;
        if (first < second) {
          find = second;
        }
        result = find;
        while (head != NULL) {
          if (head->data > result) {
            result = head->data;
          }
          if (head->data > find) {
            head = head->left;
          } else
          if (head->data < find) {
            head = head->right;
          } else {
            break;
          }
        }
        cout << "Max Node Between [" << first << "," << second << "] is " << result << " \n";
      } else {
        cout << "Node [" << first << "," << second << "] are missing";
      }
    } else {
      cout << "\nEmpty BST";
    }
  }
};

int main() {
  BinarySearchTree obj;
  
  /*
         10
       /   \
      3     19
     / \   /  \
    1   4  14  50
   / \     
 -3   2          

  */
  obj.add(10);
  obj.add(3);
  obj.add(19);
  obj.add(1);
  obj.add(4);
  obj.add(-3);
  obj.add(2);
  obj.add(14);
  obj.add(50);
  obj.find_elements(2, 14);
  obj.find_elements(-3, 4);
  obj.find_elements(10, 2);
  obj.find_elements(3, 2);
  return 0;
}

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
//Java program
//Maximum element between two nodes of 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");
    }
  }
  Node find_node(Node head,int value) {

    if (head != null) {

      if (head.data > value) {
        return find_node(head.left, value);
      } else if (head.data < value) {
        return find_node(head.right, value);
      } else {
        return head;
      }
    }
    return null;
  }

  Node lca(Node head,
    int first, int second) {

    if (head != null) {

      if (head.data > first && head.data > second) {
        return lca(head.left, first, second);
      } else if (head.data < first && head.data < second) {
        return lca(head.right, first, second);
      } else {
        return head;
      }

    }
    return null;
  }
  public void find_elements(int first, int second) {

    if (root != null) {
      //First we are check that given elements are exist or not in BST
      //If given element is exist then get its address
      Node node1 = find_node(root, first);
      Node node2 = find_node(root, second);

      if (node1 != null && node2 != null) {

        Node head = lca(root, first, second);

        int find = first, result = 0;

        if (first < second) {
          find = second;
        }
        result = find;

        while (head != null) {
          if (head.data > result) {
            result = head.data;
          }
          if (head.data > find) {
            head = head.left;
          } else if (head.data < find) {
            head = head.right;
          } else {
            break;
          }
        }
        System.out.print("Max Node Between [" + first + "," + second + "] is "+result + " \n");
      } else {
        System.out.print("Node [" + first + "," + second + "] are missing");
      }
    } else {
      System.out.print("\nEmpty BST");
    }
  }
  public static void main(String[] args) {

    BinarySearchTree obj = new BinarySearchTree();
    /*
           10
         /   \
        3     19
       / \   /  \
      1   4  14  50
     / \     
   -3   2          

    */



    obj.add(10);
    obj.add(3);
    obj.add(19);
    obj.add(1);
    obj.add(4);
    obj.add(-3);
    obj.add(2);
    obj.add(14);
    obj.add(50);

    //Test case
    obj.find_elements(2, 14);
    obj.find_elements(-3, 4);
    obj.find_elements(10, 2);
    obj.find_elements(3, 2);

  }
}

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
//C# program
//Maximum element between two nodes of 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");
		}
	}
	Node find_node(Node head,int value) {

		if (head != null) {

			if (head.data > value) {
				return find_node(head.left, value);
			} else if (head.data < value) {
				return find_node(head.right, value);
			} else {
				return head;
			}
		}
		return null;
	}

	Node lca(Node head,
		int first, int second) {

		if (head != null) {

			if (head.data > first && head.data > second) {
				return lca(head.left, first, second);
			} else if (head.data < first && head.data < second) {
				return lca(head.right, first, second);
			} else {
				return head;
			}

		}
		return null;
	}
	public void find_elements(int first, int second) {

		if (root != null) {
			//First we are check that given elements are exist or not in BST
			//If given element is exist then get its address
			Node node1 = find_node(root, first);
			Node node2 = find_node(root, second);

			if (node1 != null && node2 != null) {

				Node head = lca(root, first, second);

				int find = first, result = 0;

				if (first < second) {
					find = second;
				}
				result = find;

				while (head != null) {
					if (head.data > result) {
						result = head.data;
					}
					if (head.data > find) {
						head = head.left;
					} else if (head.data < find) {
						head = head.right;
					} else {
						break;
					}
				}
				Console.Write("Max Node Between [" + first + "," + second + "] is "+result + " \n");
			} else {
				Console.Write("Node [" + first + "," + second + "] are missing");
			}
		} else {
			Console.Write("\nEmpty BST");
		}
	}
	public static void Main(String[] args) {

		BinarySearchTree obj = new BinarySearchTree();
		/*
           10
         /   \
        3     19
       / \   /  \
      1   4  14  50
     / \     
   -3   2          

    */



		obj.add(10);
		obj.add(3);
		obj.add(19);
		obj.add(1);
		obj.add(4);
		obj.add(-3);
		obj.add(2);
		obj.add(14);
		obj.add(50);

		//Test case
		obj.find_elements(2, 14);
		obj.find_elements(-3, 4);
		obj.find_elements(10, 2);
		obj.find_elements(3, 2);

	}
}

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
# Python 3 Program
# Maximum element between two nodes of 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_node(self, head, value) :
    if (head != None) :
      if (head.data > value) :
        return self.find_node(head.left, value)
      elif (head.data < value) :
        return self.find_node(head.right, value)
      else :
        return head
      
    
    return None
  
  def lca(self, head, first, second) :
    if (head != None) :
      if (head.data > first and head.data > second) :
        return self.lca(head.left, first, second)
      elif (head.data < first and head.data < second) :
        return self.lca(head.right, first, second)
      else :
        return head
      
    
    return None
  
  def find_elements(self, first, second) :
    if (self.root != None) :
      node1 = self.find_node(self.root, first)
      node2 = self.find_node(self.root, second)
      if (node1 != None and node2 != None) :
        head = self.lca(self.root, first, second)
        find = first
        result = 0
        if (first < second) :
          find = second
        
        result = find
        while (head != None) :
          if (head.data > result) :
            result = head.data
          
          if (head.data > find) :
            head = head.left
          elif (head.data < find) :
            head = head.right
          else :
            break
          
        
        print("Max Node Between [", first ,",", second ,"] is ", result )
      else :
        print("Node [", first ,",", second ,"] are missing")
      
    else :
      print("\nEmpty BST")
    
  
def main() :
  obj = BinarySearchTree()
  
  #
  #         10
  #       /   \
  #      3     19
  #     / \   /  \
  #    1   4  14  50
  #   / \
  # -3   2
  #  
  obj.add(10)
  obj.add(3)
  obj.add(19)
  obj.add(1)
  obj.add(4)
  obj.add(-3)
  obj.add(2)
  obj.add(14)
  obj.add(50)
  obj.find_elements(2, 14)
  obj.find_elements(-3, 4)
  obj.find_elements(10, 2)
  obj.find_elements(3, 2)
  

if __name__ == "__main__":
  main()

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
# Ruby Program
# Maximum element between two nodes of 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_node(head, value) 
		if (head != nil) 
			if (head.data > value) 
				return self.find_node(head.left, value)
			elsif (head.data < value) 
				return self.find_node(head.right, value)
			else 
				return head
			end
		end
		return nil
	end
	def lca(head, first, second) 
		if (head != nil) 
			if (head.data > first and head.data > second) 
				return self.lca(head.left, first, second)
			elsif (head.data < first and head.data < second) 
				return self.lca(head.right, first, second)
			else 
				return head
			end
		end
		return nil
	end
	def find_elements(first, second) 
		if (@root != nil) 
			node1 = self.find_node(@root, first)
			node2 = self.find_node(@root, second)
			if (node1 != nil and node2 != nil) 
				head = self.lca(@root, first, second)
				find = first
				result = 0
				if (first < second) 
					find = second
				end
				result = find
				while (head != nil) 
					if (head.data > result) 
						result = head.data
					end
					if (head.data > find) 
						head = head.left
					elsif (head.data < find) 
						head = head.right
					else 
						break
					end
				end
				print("Max Node Between [", first ,",", second ,"] is ", result ," \n")
			else 
				print("Node [", first ,",", second ,"] are missing")
			end
		else 
			print("\nEmpty BST")
		end
	end
end
def main() 
	obj = BinarySearchTree.new()
		
	#
	#         10
	#       /   \
	#      3     19
	#     / \   /  \
	#    1   4  14  50
	#   / \
	# -3   2
	#  
	obj.add(10)
	obj.add(3)
	obj.add(19)
	obj.add(1)
	obj.add(4)
	obj.add(-3)
	obj.add(2)
	obj.add(14)
	obj.add(50)
	obj.find_elements(2, 14)
	obj.find_elements(-3, 4)
	obj.find_elements(10, 2)
	obj.find_elements(3, 2)
end

main()

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
<?php
/*
 Php Program
 Maximum element between two nodes of 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");
    }
  }

  function find_node($head, $value) {
    if ($head != null) {
      if ($head->data > $value) {
        return $this->find_node($head->left, $value);
      } else
      if ($head->data < $value) {
        return $this->find_node($head->right, $value);
      } else {
        return $head;
      }
    }
    return null;
  }

  function lca($head, $first, $second) {
    if ($head != null) {
      if ($head->data > $first && $head->data > $second) {
        return $this->lca($head->left, $first, $second);
      } else
      if ($head->data < $first && $head->data < $second) {
        return $this->lca($head->right, $first, $second);
      } else {
        return $head;
      }
    }
    return null;
  }
  public  function find_elements($first, $second) {
    if ($this->root != null) {
      $node1 = $this->find_node($this->root, $first);
      $node2 = $this->find_node($this->root, $second);
      if ($node1 != null && $node2 != null) {
        $head = $this->lca($this->root, $first, $second);
        $find = $first;
        $result = 0;
        if ($first < $second) {
          $find = $second;
        }
        $result = $find;
        while ($head != null) {
          if ($head->data > $result) {
            $result = $head->data;
          }
          if ($head->data > $find) {
            $head = $head->left;
          } else
          if ($head->data < $find) {
            $head = $head->right;
          } else {
            break;
          }
        }
        echo("Max Node Between [". $first .",". $second ."] is ". $result ." \n");
      } else {
        echo("Node [". $first .",". $second ."] are missing");
      }
    } else {
      echo("\nEmpty BST");
    }
  }
}
function main() {
  $obj = new BinarySearchTree();
  
  /*
         10
       /   \
      3     19
     / \   /  \
    1   4  14  50
   / \     
 -3   2          

  */
  $obj->add(10);
  $obj->add(3);
  $obj->add(19);
  $obj->add(1);
  $obj->add(4);
  $obj->add(-3);
  $obj->add(2);
  $obj->add(14);
  $obj->add(50);
  $obj->find_elements(2, 14);
  $obj->find_elements(-3, 4);
  $obj->find_elements(10, 2);
  $obj->find_elements(3, 2);
}
main();

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
/*
 Node Js Program
 Maximum element between two nodes of 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_node(head, value) {
		if (head != null) {
			if (head.data > value) {
				return this.find_node(head.left, value);
			} else
			if (head.data < value) {
				return this.find_node(head.right, value);
			} else {
				return head;
			}
		}
		return null;
	}
	lca(head, first, second) {
		if (head != null) {
			if (head.data > first && head.data > second) {
				return this.lca(head.left, first, second);
			} else
			if (head.data < first && head.data < second) {
				return this.lca(head.right, first, second);
			} else {
				return head;
			}
		}
		return null;
	}
	find_elements(first, second) {
		if (this.root != null) {
			var node1 = this.find_node(this.root, first);
			var node2 = this.find_node(this.root, second);
			if (node1 != null && node2 != null) {
				var head = this.lca(this.root, first, second);
				var find = first;
				var result = 0;
				if (first < second) {
					find = second;
				}
				result = find;
				while (head != null) {
					if (head.data > result) {
						result = head.data;
					}
					if (head.data > find) {
						head = head.left;
					} else
					if (head.data < find) {
						head = head.right;
					} else {
						break;
					}
				}
				process.stdout.write("Max Node Between [" + first + "," + second + "] is " + result + " \n");
			} else {
				process.stdout.write("Node [" + first + "," + second + "] are missing");
			}
		} else {
			process.stdout.write("\nEmpty BST");
		}
	}
}

function main() {
	var obj = new BinarySearchTree();
	
	  /*
	         10
	       /   \
	      3     19
	     / \   /  \
	    1   4  14  50
	   / \     
	 -3   2          

	  */
	obj.add(10);
	obj.add(3);
	obj.add(19);
	obj.add(1);
	obj.add(4);
	obj.add(-3);
	obj.add(2);
	obj.add(14);
	obj.add(50);
	obj.find_elements(2, 14);
	obj.find_elements(-3, 4);
	obj.find_elements(10, 2);
	obj.find_elements(3, 2);
}


main();

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 
/*
 Swift 4 Program
 Maximum element between two nodes of 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_node(_ head: Node? , _ value : Int) -> Node? {
    if (head != nil) {
      if (head!.data > value) {
        return self.find_node(head!.left, value);
      } else
      if (head!.data < value) {
        return self.find_node(head!.right, value);
      } else {
        return head;
      }
    }
    return nil;
  }
  func lca(_ head: Node? , _ first : Int, _ second: Int) -> Node? {
    if (head != nil) {
      if (head!.data > first && head!.data > second) {
        return self.lca(head!.left, first, second);
      } else
      if (head!.data < first && head!.data < second) {
        return self.lca(head!.right, first, second);
      } else {
        return head;
      }
    }
    return nil;
  }
  func find_elements(_ first: Int, _ second: Int) {
    if (self.root != nil) {
      let node1: Node? = self.find_node(self.root, first);
      let node2: Node? = self.find_node(self.root, second);
      if (node1 != nil && node2 != nil) {
        var head: Node? = self.lca(self.root, first, second);
        var find: Int = first;
        var result = 0;
        if (first < second) {
          find = second;
        }
        result = find;
        while (head != nil) {
          if (head!.data > result) {
            result = head!.data;
          }
          if (head!.data > find) {
            head = head!.left;
          } else
          if (head!.data < find) {
            head = head!.right;
          } else {
            break;
          }
        }
        print("Max Node Between [", first ,",", second ,"] is ", result );
      } else {
        print("Node [", first ,",", second ,"] are missing");
      }
    } else {
      print("\nEmpty BST");
    }
  }
}
func main() {
  let obj: BinarySearchTree = BinarySearchTree();
  
  /*
         10
       /   \
      3     19
     / \   /  \
    1   4  14  50
   / \     
 -3   2          

  */
  obj.add(10);
  obj.add(3);
  obj.add(19);
  obj.add(1);
  obj.add(4);
  obj.add(-3);
  obj.add(2);
  obj.add(14);
  obj.add(50);
  obj.find_elements(2, 14);
  obj.find_elements(-3, 4);
  obj.find_elements(10, 2);
  obj.find_elements(3, 2);
}
main();

Output

Max Node Between [2,14] is 19 
Max Node Between [-3,4] is 4 
Max Node Between [10,2] is 10 
Max Node Between [3,2] is 3 


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