Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. import java.io.*;
  2. class Node
  3. {
  4.     Node parent,left, right;
  5.     String key;
  6.     Node(String data)
  7.     {
  8.         parent=null;
  9.         left=null;
  10.         right=null;
  11.         key=data;
  12.     }
  13.     void setLeft(Node a)
  14.     {
  15.         left=a;
  16.     }
  17.     void setRight(Node a)
  18.     {
  19.         right=a;
  20.     }
  21.     void setParent(Node a)
  22.     {
  23.         parent=a;
  24.     }
  25. }
  26. class BinarySearchTree
  27. {
  28.     Node root;
  29.     BinarySearchTree()
  30.     {
  31.         root=null;
  32.     }
  33.     Node search(String a)
  34.     {
  35.         Node x=root;
  36.         while(x!=null)
  37.         {
  38.             if(x.key.equals(a))
  39.             {
  40.                 break;
  41.             }
  42.             if(a.compareToIgnoreCase(x.key)<0)
  43.             {
  44.                 x=x.left;
  45.             }
  46.             else
  47.             {
  48.                 x=x.right;
  49.             }
  50.         }
  51.         return x;
  52.     }
  53.     Node Minimum(Node x)
  54.     {
  55.        
  56.         while(x.left!=null)
  57.         {
  58.             x=x.left;
  59.         }
  60.         return x;
  61.     }
  62.     Node Maximum(Node x)
  63.     {
  64.        
  65.         while(x.right!=null)
  66.         {
  67.             x=x.right;
  68.         }
  69.         return x;
  70.     }
  71.    
  72.     Node successor(Node a)
  73.     {
  74.         Node x=null;
  75.         Node y=null;
  76.         if(a.right!=null)
  77.         {
  78.             x=a.right;
  79.             return Minimum(x);
  80.         }
  81.         else
  82.         {
  83.            
  84.             y=a.parent;
  85.             x=a;
  86.             while(y!=null)
  87.             {
  88.                 x=y;
  89.                 y=y.parent;
  90.                 if(y.left==x)
  91.                 {
  92.                     break;
  93.                 }
  94.             }
  95.         }
  96.         return y;
  97.     }
  98.     // Node predecessor(Node a)
  99.     // {
  100.     //  Node x=null;
  101.     //  Node y=null;
  102.     //  if(a.left!=null)
  103.     //  {
  104.     //      x=a.left;
  105.     //      return Maximum(a);
  106.     //  }
  107.     //  else
  108.     //  {
  109.     //      x=a;
  110.     //      y=x.parent;
  111.     //      while(y!=null&&y.left.key.equals(x.key))
  112.     //      {
  113.     //          x=y;
  114.     //          y=y.parent;
  115.     //      }
  116.     //  }
  117.     //  return y;
  118.     // }
  119.     void insert(Node a)
  120.     {
  121.         Node x=root;
  122.         Node y=root;
  123.         while(x!=null)
  124.         {
  125.             y=x;
  126.             if(a.key.compareToIgnoreCase(x.key)<0)
  127.             {  
  128.                 x=x.left;
  129.             }
  130.             else
  131.             {  
  132.                 x=x.right;
  133.             }
  134.         }
  135.         if(y==null)
  136.         {
  137.             root=a;
  138.         }          
  139.         else if(a.key.compareToIgnoreCase(y.key)<0)
  140.         {
  141.             a.setParent(y);
  142.             y.setLeft(a);
  143.         }
  144.         else       
  145.         {
  146.             a.setParent(y);
  147.             y.setRight(a);
  148.         }
  149.        
  150.     }
  151.     void Replace(Node a, Node b)
  152.     {
  153.         if(a.parent==null)
  154.         {
  155.             root=b;
  156.         }
  157.         else if(a.parent.left!=null)
  158.         {
  159.             if(a.parent.left.key.equals(a.key))
  160.             {
  161.                 a.parent.left=b;
  162.             }
  163.         }
  164.         else if(a.parent.right!=null)
  165.         {
  166.             if(a.parent.right.key.equals(a.key))
  167.             {
  168.                 a.parent.right=b;
  169.             }
  170.         }
  171.         if(a.parent!=null)
  172.             b.parent=a.parent;
  173.     }
  174.     void delete(Node a)
  175.     {
  176.         Node x=null;
  177.         x=a.parent;
  178.         if(a.left==null&&a.right==null)
  179.         {
  180.             Replace(a,null);
  181.         }
  182.         else if(a.left!=null&&a.right==null)
  183.         {
  184.             Replace(a,a.left);
  185.         }
  186.         else if(a.left==null&&a.right!=null)
  187.         {
  188.             Replace(a,a.right);
  189.         }  
  190.         else
  191.         {
  192.             Node y=successor(a);
  193.             if(y.key.equals(a.right.key))
  194.             {
  195.                 y.left=a.left;
  196.                 Replace(a,y);
  197.             }
  198.             else
  199.             {
  200.                 delete(y);
  201.                 y.left=a.left;
  202.                 if(a.left!=null)
  203.                     y.left.parent=y;
  204.                 y.right=a.right;
  205.                 if(a.right!=null)
  206.                     y.right.parent=y;
  207.                 Replace(a,y);
  208.             }
  209.         }
  210.     }
  211.     void inorder(Node a)
  212.     {
  213.         if(a==null)
  214.             return;
  215.         inorder(a.left);
  216.         System.out.println(a.key);
  217.         inorder(a.right);
  218.  
  219.     }
  220.     Node findSmallest(String a)
  221.     {
  222.         Node x=root;
  223.         Node min=null;
  224.         while(x!=null)
  225.         {
  226.             if(a.compareToIgnoreCase(x.key)<0)
  227.             {
  228.                 min=x;
  229.                 x=x.left;
  230.             }
  231.             else
  232.             {
  233.                 x=x.right;
  234.             }
  235.         }
  236.         return min;
  237.     }
  238. }
  239. class Ass3_1
  240. {
  241.     public static void main(String args[])throws IOException
  242.     {
  243.         InputStreamReader isr=new InputStreamReader(System.in);
  244.         BufferedReader br=new BufferedReader(isr);
  245.         BinarySearchTree T= new BinarySearchTree();
  246.         int b=Integer.parseInt(br.readLine());
  247.         String word="";
  248.         int i,j;
  249.         int len=0;
  250.         Node x;
  251.         String a="";
  252.         char ch=' ';
  253.         for(i=0;i<b;i++)
  254.         {
  255.             a=br.readLine();
  256.             len=a.length();
  257.             for(j=1;j<len;j++)
  258.             {
  259.                 word+=a.charAt(j);
  260.             }
  261.             ch=a.charAt(0);
  262.             if(ch=='+')
  263.                 T.insert(new Node(word));
  264.             else if(ch=='-')
  265.             {
  266.                 x=T.search(word);
  267.                 if(x!=null)
  268.                     T.delete(x);
  269.             }
  270.             else if(ch=='>')
  271.             {              
  272.                     x=T.findSmallest(word);
  273.                     if(x!=null)
  274.                         System.out.println(x.key);
  275.                     else
  276.                         System.out.println("ERROR");
  277.             }
  278.             else if(ch=='?')
  279.             {
  280.                 x=T.search(word);
  281.                 if(x!=null)
  282.                 {
  283.                     System.out.println(word);
  284.                 }
  285.                 else
  286.                 {
  287.                     System.out.println("ERROR");
  288.                 }
  289.             }
  290.             else if(a.equals("."))
  291.             {
  292.                 break;
  293.             }
  294.                 word="";
  295.         }
  296.         T.inorder(T.root);
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement