Remove BST keys outside the given range

Remove BST keys outside of given range

Here given code implementation process.

//C Program 
//Remove BST keys outside the given range
#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_element(struct Node*root,

  int first,int second)
{
  
  if(root!=NULL)
  {
  
    root->left=find_element(root->left,first,second);

    root->right=find_element(root->right,first,second);

    
    if(root->data < first || root->data > second)
    {
      struct Node*temp;
      //Condition which are delete node

      if(root->left==NULL && root->right==NULL)
      {
        //delete leaf node
        free(root);
        root=NULL;
      }
      else if(root->left==NULL)
      {
        temp=root->right;
        free(root);
        root=temp;
      }
      else if(root->right==NULL)
      {
        temp=root->left;
        free(root);
        root=temp;
      }
    }
  }
 
  return root;
}
struct Node* remove_element(struct Node*root,int first,int second)
{
  if(root != NULL)
  {
   
    if(first>second)
    {
      printf("\nRemove nodes outside the range of [%d - %d]\n", second,first);
    
      return find_element(root,second,first);
    }
    else
    {
      printf("\nRemove nodes outside the range of [%d - %d]\n", first,second);
    
      return find_element(root,first,second);
    }
    
  }
  else
  {
    printf("Empty Tree\n");
    return NULL;
  }
}
void preorder(struct Node*root)
{
  if(root!=NULL)
  {
    printf("%3d ",root->data );
    preorder(root->left);
    preorder(root->right);
  }
}
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);  
    preorder(root);
   
    root=remove_element(root,5,8);
    preorder(root);
    printf("\n");
   
  return 0;
}

Output

  6   3   1  -3   2   5   4   9   7   8  11  12 
Remove nodes outside the range of [5 - 8]
  6   5   7   8 
//C++ Program 
//Remove BST keys outside the given range
#include <iostream>
using namespace std;

//ure of Binary Search Tree node
struct Node
{
  int data;
  Node *left,*right; 
};

class BST
{

public:
  Node*root;
  BST();
  //public methods
  void add(int);
  void preorder( Node*root);
  void remove_element( int first,int second);
  Node* find_element( Node*root,int first,int second);
};
BST::BST()
{
  root=NULL;
}
//Adding a new node in binary search tree
void BST :: add(int data)
{
  //Create a dynamic node of binary search tree 
   Node *new_node = new 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
    {
       Node *find = root;

      //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
  {
    cout<<("Memory Overflow\n");
  }
}
Node* BST:: find_element( Node*root,int first,int second)
{
    
    if (root!=NULL)
    {
    
      root->left=find_element(root->left,first,second);
      root->right=find_element(root->right,first,second);
      
      if (root->data < first || root->data > second)
      {
         Node*temp;
      //Condition which are delete node
        if (root->left==NULL && root->right==NULL)
        {
        //delete leaf node
          delete (root);
          root=NULL;
        }
        else if (root->left==NULL)
        {
          temp=root->right;
          delete (root);
          root=temp;
        }
        else if (root->right==NULL)
        {
          temp=root->left;
          delete (root);
          root=temp;
        }
      }
    }
  
    return root;
}
void BST:: remove_element( int first,int second)
{
    if (root != NULL)
    {
     
      if (first>second)
      {
        cout<<"\nRemove nodes outside the range of ["<< second <<" - "<<first<<"]\n";
      
        root = find_element(root,second,first);
      }
      else
      {
        cout<<"\nRemove nodes outside the range of ["<<first <<" - "<< second<<"]\n";
      
        root = find_element(root,first,second);
      }
      
    }
    else
    {
      cout<<("Empty Tree\n");
     
    }
}
void BST:: preorder( Node*root)
{
    if (root!=NULL)
    {
      cout<<"  "<<root->data;
      preorder(root->left);
      preorder(root->right);
    }
}
int main()
{
  //Create object
  BST obj;

  //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.preorder(obj.root);
 
  obj.remove_element(5,8);
  obj.preorder(obj.root);
  cout<<("\n");

  return 0;
}

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
//Java program
//Remove BST keys outside the given range
public class BST
{
  static class Node
  {
    int data;
    Node left,right;
  }
  public Node root;
  //Class constructors
  BST()
  {
    root=null;

  } 

  //insert  element
  public void add(int data)
  {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node();

    if(new_node != null)
    {
      //Set data and pointer values
      new_node.data = data;
      //Initially node left-pointer is null
      new_node.left = null; 
      //Initially node right-pointer is null
      new_node.right = 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 >= 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
    {
      System.out.println("Memory Overflow");
    }
  }
  public Node find_element( Node root,int first,int second)
  {
      
      if (root!=null)
      {
      
        root.left=find_element(root.left,first,second);
        root.right=find_element(root.right,first,second);
        
        if (root.data < first || root.data > second)
        {
          Node temp=null;
          //Condition which are delete node
          if (root.left==null && root.right==null)
          {
            //delete leaf node
           
            root=null;
          }
          else if (root.left==null)
          {
            temp=root.right;
            
            root=temp;
          }
          else if (root.right==null)
          {
            temp=root.left;
        
            root=temp;
          }
          temp=null;
        }
      }
    
      return root;
  }
  public void   remove_element( int first,int second)
  {
      if (root != null)
      {
       
        if (first>second)
        {
          System.out.print("\nRemove nodes outside the range of ["+ second +" - "+first+"]\n");
        
          root = find_element(root,second,first);
        }
        else
        {
          System.out.print("\nRemove nodes outside the range of ["+first +" - "+ second+"]\n");
        
          root = find_element(root,first,second);
        }
        
      }
      else
      {
        System.out.print("Empty Tree\n");
       
      }
  }
  public void   preorder( Node root)
  {
      if (root!=null)
      {
        System.out.print("  "+root.data);
        preorder(root.left);
        preorder(root.right);
      }
  }

  public static void main(String[] args) 
  {

    BST obj = new BST();

  //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.preorder(obj.root);
 
  obj.remove_element(5,8);
  obj.preorder(obj.root);
  System.out.println();

  }
}

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
#Python Program 
#Remove BST keys outside the given range
class Node:
  def __init__(self,data):
    self.data=data
    self.left=None
    self.right=None

   
class BST:

  def __init__(self):

    #Assign default value
    self.root=None
   
  #insert  element
  def add(self,data):

    #Create a dynamic node of binary search tree 
    new_node = Node(data)

    if(self.root == None):
      #When adds a first node in binary tree
      self.root = new_node
    
    else:
      find = self.root

      #add new node to proper position
      while(find != None):

        if(find.data >= data): 

          if(find.left==None):

            find.left = new_node
            break
          
          else: 

            #visit left sub-tree
            find = find.left
          
        
        else:

          if(find.right == None):

            find.right = new_node
            break
          
          else:

            #visit right sub-tree
            find = find.right
  
 

  def  find_element(self, root, first, second) :

    if (root!=None) :

      root.left=self.find_element(root.left,first,second)
      root.right=self.find_element(root.right,first,second)
      if (root.data < first  or  root.data > second) :

        temp=None
        #Condition which are delete node
        if (root.left==None  and  root.right==None) :

          root=None
        elif (root.left==None) :

          temp=root.right
          root=temp
        elif (root.right==None) :

          temp=root.left
          root=temp
        temp=None
    return root

  def remove_element(self, first, second) :

    if(self.root != None) :

      if(first>second) :

        print("\nRemove nodes outside the range of [", second ," - ",first,"]")

        self.root = self.find_element(self.root,second,first)
      else :

        print("\nRemove nodes outside the range of [",first ," - ", second,"]")

        self.root = self.find_element(self.root,first,second)
    else :

      print("Empty Tree\n")

  def preorder(self,root) :

    if (root!=None) :

      print(root.data,end=" ")

      self.preorder(root.left)
      self.preorder(root.right)

def main():

  #Make a object of BST class
  obj=  BST()



#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.preorder(obj.root)
  obj.remove_element(5,8)
  obj.preorder(obj.root)
  print()


if __name__ =="__main__":
  main()

Output

6 3 1 -3 2 5 4 9 7 8 11 12 
Remove nodes outside the range of [ 5  -  8 ]
6 5 7 8 
//C# program
//Remove BST keys outside the given range
using System;
public class Node
{
  public int data;
  public Node left,right;
}
public class BST
{

  public Node root;
  //Class constructors
  BST()
  {
    root=null;

  } 

  //insert  element
  public void add(int data)
  {
    //Create a dynamic node of binary search tree 
    Node new_node = new Node();

    if(new_node != null)
    {
      //Set data and pointer values
      new_node.data = data;
      //Initially node left-pointer is null
      new_node.left = null; 
      //Initially node right-pointer is null
      new_node.right = 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 >= 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
    {
      Console.WriteLine("Memory Overflow");
    }
  }
  public Node find_element( Node root,int first,int second)
  {

    if (root!=null)
    {

      root.left=find_element(root.left,first,second);
      root.right=find_element(root.right,first,second);

      if (root.data < first || root.data > second)
      {
        Node temp=null;
        //Condition which are delete node
        if (root.left==null && root.right==null)
        {
          //delete leaf node

          root=null;
        }
        else if (root.left==null)
        {
          temp=root.right;

          root=temp;
        }
        else if (root.right==null)
        {
          temp=root.left;

          root=temp;
        }
        temp=null;
      }
    }

    return root;
  }
  public void   remove_element( int first,int second)
  {
    if (root != null)
    {

      if (first>second)
      {
        Console.Write("\nRemove nodes outside the range of ["+ second +" - "+first+"]\n");

        root = find_element(root,second,first);
      }
      else
      {
        Console.Write("\nRemove nodes outside the range of ["+first +" - "+ second+"]\n");

        root = find_element(root,first,second);
      }

    }
    else
    {
      Console.Write("Empty Tree\n");

    }
  }
  public void   preorder( Node root)
  {
    if (root!=null)
    {
      Console.Write("  "+root.data);
      preorder(root.left);
      preorder(root.right);
    }
  }

  public static void Main(String[] args) 
  {

    BST obj = new BST();

    //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.preorder(obj.root);

    obj.remove_element(5,8);
    obj.preorder(obj.root);
    Console.WriteLine();

  }
}

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
<?php
//Php program 
//Remove BST keys outside the given range
class Node
{
  public $data;
  public $left;
  public $right;
  function __construct($data)
  {
    $this->data = $data;
    $this->length = NULL;
    $this->right = NULL;
  }
}
class BST
{

  public $root;
  function __construct()
  {
    $root=NULL;
  }
  //Adding a new node in binary search tree
  public function add($data)
  {
    //Create a dynamic node of binary search tree 
    $new_node = new Node($data);

    if($this->root == NULL)
    {
      //When adds a first node in binary tree
      $this->root = $new_node;
    }
    else
    {
      $find = $this->root;

      //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;
          }
        }
      }
    }

  }

  public function find_element( $root, $first, $second)
  {

    if ($root!=NULL)
    {

      $root->left=$this->find_element($root->left,$first,$second);
      $root->right=$this->find_element($root->right,$first,$second);
      
      if ($root->data < $first || $root->data > $second)
      {
        $temp=NULL;
        //Condition which are delete node
        if ($root->left==NULL && $root->right==NULL)
        {

          $root=NULL;
        }
        else if ($root->left==NULL)
        {
          $temp=$root->right;

          $root=$temp;
        }
        else if ($root->right==NULL)
        {
          $temp=$root->left;
          $root=$temp;
        }
        $temp=NULL;
      }
    }

    return $root;
  }
  public function  remove_element( $first, $second)
  {
    if($this->root != NULL)
    {
      if($first>$second)
      {
        echo "\nRemove nodes outside the range of [". $second ." - ".$first."]\n";

        $this->root = $this->find_element($this->root,$second,$first);
      }
      else
      {
        echo "\nRemove nodes outside the range of [".$first ." - ". $second."]\n";

        $this->root = $this->find_element($this->root,$first,$second);
      }
      
    }
    else
    {
      echo "Empty Tree\n";

    }
  }
  public function   preorder($root)
  {
    if ($root!=NULL)
    {
      echo "  ".$root->data;
      $this->preorder($root->left);
      $this->preorder($root->right);
    }
  }
}
function main()
{
  //Make a object of BST class
  $obj= new BST();

  //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->preorder($obj->root);
 
  $obj->remove_element(5,8);
  $obj->preorder($obj->root);
  echo ("\n");



}
main();
?>

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
# Ruby Program
# Remove BST keys outside the given range

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, first, second) 
    if (head != nil) 
      head.left = self.find_element(head.left, first, second)
      head.right = self.find_element(head.right, first, second)
      if (head.data < first or head.data > second) 
        temp = nil
        if (head.left == nil and head.right == nil) 
          head = nil
        elsif (head.left == nil) 
          temp = head.right
          head = temp
        elsif (head.right == nil) 
          temp = head.left
          head = temp
        end
        temp = nil
      end
    end
    return head
  end
  def remove_element(first, second) 
    if (@root != nil) 
      if (first > second) 
        print("\nRemove nodes outside the range of [", second ," - ", first ,"]\n")
        @root = self.find_element(@root, second, first)
      else 
        print("\nRemove nodes outside the range of [", first ," - ", second ,"]\n")
        @root = self.find_element(@root, first, second)
      end
    else 
      print("Empty Tree\n")
    end
  end
  def preorder(head) 
    if (head != nil) 
      print("  ", head.data)
      self.preorder(head.left)
      self.preorder(head.right)
    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.preorder(obj.root)
  obj.remove_element(5, 8)
  obj.preorder(obj.root)
  print("\n")
end


main()

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
/*
 Node Js Program
 Remove BST keys outside the given range
*/

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, first, second) {
    if (head != null) {
      head.left = this.find_element(head.left, first, second);
      head.right = this.find_element(head.right, first, second);
      if (head.data < first || head.data > second) {
        var temp = null;
        if (head.left == null && head.right == null) {
          head = null;
        } else
        if (head.left == null) {
          temp = head.right;
          head = temp;
        } else
        if (head.right == null) {
          temp = head.left;
          head = temp;
        }
        temp = null;
      }
    }
    return head;
  }
  remove_element(first, second) {
    if (this.root != null) {
      if (first > second) {
        process.stdout.write("\nRemove nodes outside the range of [" + second + " - " + first + "]\n");
        this.root = this.find_element(this.root, second, first);
      } else {
        process.stdout.write("\nRemove nodes outside the range of [" + first + " - " + second + "]\n");
        this.root = this.find_element(this.root, first, second);
      }
    } else {
      process.stdout.write("Empty Tree\n");
    }
  }
  preorder(head) {
    if (head != null) {
      process.stdout.write("  " + head.data);
      this.preorder(head.left);
      this.preorder(head.right);
    }
  }
}

function main() {
  var 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.preorder(obj.root);
  obj.remove_element(5, 8);
  obj.preorder(obj.root);
  process.stdout.write("\n");
}

main();

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8
/*
 Swift 4 Program
 Remove BST keys outside the given range
*/

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(_ node_root: Node? , _ first : Int, _ second: Int) ->Node? {
    var head : Node? = node_root;
    if (head != nil) {
      head!.left = self.find_element(head!.left, first, second);
      head!.right = self.find_element(head!.right, first, second);
      if (head!.data < first || head!.data > second) {
        var temp: Node? = nil;
        if (head!.left == nil && head!.right == nil) {
          head = nil;
        } else
        if (head!.left == nil) {
          temp = head!.right;
          head = temp;
        } else
        if (head!.right == nil) {
          temp = head!.left;
          head = temp;
        }
        temp = nil;
      }
    }
    return head;
  }
  func remove_element(_ first: Int, _ second: Int) {
    if (self.root != nil) {
      if (first > second) {
        print("\nRemove nodes outside the range of [", second ," - ", first ,"]\n");
        self.root = self.find_element(self.root, second, first);
      } else {
        print("\nRemove nodes outside the range of [", first ," - ", second ,"]\n");
        self.root = self.find_element(self.root, first, second);
      }
    } else {
      print("Empty Tree\n");
    }
  }
  func preorder(_ head: Node? ) {
    if (head != nil) {
      print("  ", head!.data,terminator:"");
      self.preorder(head!.left);
      self.preorder(head!.right);
    }
  }
}
func main() {
  let obj: BinarySearchTree = 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.preorder(obj.root);
  obj.remove_element(5, 8);
  obj.preorder(obj.root);
 
}
main();

Output

  6  3  1  -3  2  5  4  9  7  8  11  12
Remove nodes outside the range of [5 - 8]
  6  5  7  8


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