Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Node
- {
- Node parent,left, right;
- String key;
- Node(String data)
- {
- parent=null;
- left=null;
- right=null;
- key=data;
- }
- void setLeft(Node a)
- {
- left=a;
- }
- void setRight(Node a)
- {
- right=a;
- }
- void setParent(Node a)
- {
- parent=a;
- }
- }
- class BinarySearchTree
- {
- Node root;
- BinarySearchTree()
- {
- root=null;
- }
- Node search(String a)
- {
- Node x=root;
- while(x!=null)
- {
- if(x.key.equals(a))
- {
- break;
- }
- if(a.compareToIgnoreCase(x.key)<0)
- {
- x=x.left;
- }
- else
- {
- x=x.right;
- }
- }
- return x;
- }
- Node Minimum(Node x)
- {
- while(x.left!=null)
- {
- x=x.left;
- }
- return x;
- }
- Node Maximum(Node x)
- {
- while(x.right!=null)
- {
- x=x.right;
- }
- return x;
- }
- Node successor(Node a)
- {
- Node x=null;
- Node y=null;
- if(a.right!=null)
- {
- x=a.right;
- return Minimum(x);
- }
- else
- {
- y=a.parent;
- x=a;
- while(y!=null)
- {
- x=y;
- y=y.parent;
- if(y.left==x)
- {
- break;
- }
- }
- }
- return y;
- }
- // Node predecessor(Node a)
- // {
- // Node x=null;
- // Node y=null;
- // if(a.left!=null)
- // {
- // x=a.left;
- // return Maximum(a);
- // }
- // else
- // {
- // x=a;
- // y=x.parent;
- // while(y!=null&&y.left.key.equals(x.key))
- // {
- // x=y;
- // y=y.parent;
- // }
- // }
- // return y;
- // }
- void insert(Node a)
- {
- Node x=root;
- Node y=root;
- while(x!=null)
- {
- y=x;
- if(a.key.compareToIgnoreCase(x.key)<0)
- {
- x=x.left;
- }
- else
- {
- x=x.right;
- }
- }
- if(y==null)
- {
- root=a;
- }
- else if(a.key.compareToIgnoreCase(y.key)<0)
- {
- a.setParent(y);
- y.setLeft(a);
- }
- else
- {
- a.setParent(y);
- y.setRight(a);
- }
- }
- void Replace(Node a, Node b)
- {
- if(a.parent==null)
- {
- root=b;
- }
- else if(a.parent.left!=null)
- {
- if(a.parent.left.key.equals(a.key))
- {
- a.parent.left=b;
- }
- }
- else if(a.parent.right!=null)
- {
- if(a.parent.right.key.equals(a.key))
- {
- a.parent.right=b;
- }
- }
- if(a.parent!=null)
- b.parent=a.parent;
- }
- void delete(Node a)
- {
- Node x=null;
- x=a.parent;
- if(a.left==null&&a.right==null)
- {
- Replace(a,null);
- }
- else if(a.left!=null&&a.right==null)
- {
- Replace(a,a.left);
- }
- else if(a.left==null&&a.right!=null)
- {
- Replace(a,a.right);
- }
- else
- {
- Node y=successor(a);
- if(y.key.equals(a.right.key))
- {
- y.left=a.left;
- Replace(a,y);
- }
- else
- {
- delete(y);
- y.left=a.left;
- if(a.left!=null)
- y.left.parent=y;
- y.right=a.right;
- if(a.right!=null)
- y.right.parent=y;
- Replace(a,y);
- }
- }
- }
- void inorder(Node a)
- {
- if(a==null)
- return;
- inorder(a.left);
- System.out.println(a.key);
- inorder(a.right);
- }
- Node findSmallest(String a)
- {
- Node x=root;
- Node min=null;
- while(x!=null)
- {
- if(a.compareToIgnoreCase(x.key)<0)
- {
- min=x;
- x=x.left;
- }
- else
- {
- x=x.right;
- }
- }
- return min;
- }
- }
- class Ass3_1
- {
- public static void main(String args[])throws IOException
- {
- InputStreamReader isr=new InputStreamReader(System.in);
- BufferedReader br=new BufferedReader(isr);
- BinarySearchTree T= new BinarySearchTree();
- int b=Integer.parseInt(br.readLine());
- String word="";
- int i,j;
- int len=0;
- Node x;
- String a="";
- char ch=' ';
- for(i=0;i<b;i++)
- {
- a=br.readLine();
- len=a.length();
- for(j=1;j<len;j++)
- {
- word+=a.charAt(j);
- }
- ch=a.charAt(0);
- if(ch=='+')
- T.insert(new Node(word));
- else if(ch=='-')
- {
- x=T.search(word);
- if(x!=null)
- T.delete(x);
- }
- else if(ch=='>')
- {
- x=T.findSmallest(word);
- if(x!=null)
- System.out.println(x.key);
- else
- System.out.println("ERROR");
- }
- else if(ch=='?')
- {
- x=T.search(word);
- if(x!=null)
- {
- System.out.println(word);
- }
- else
- {
- System.out.println("ERROR");
- }
- }
- else if(a.equals("."))
- {
- break;
- }
- word="";
- }
- T.inorder(T.root);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement