Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.87 KB | None | 0 0
  1. public class BST {
  2.     /* Root of BST */
  3.     private Node root;
  4.     /* Number of nodes in BST */
  5.     private int size;
  6.  
  7.     public BST() {
  8.         this.root = null;
  9.         this.size = 0;
  10.     }
  11.  
  12.     /**
  13.      * Is the BST empty?
  14.      */
  15.     public boolean isEmpty() {
  16.         return size() == 0;
  17.     }
  18.  
  19.     /**
  20.      * Return root of BST
  21.      */
  22.     public Node getRoot() {
  23.         return root;
  24.     }
  25.  
  26.     /**
  27.      * Return number of key-value pairs in BST
  28.      */
  29.     public int size() {
  30.         return size;
  31.     }
  32.  
  33.     /**
  34.      * Does there exist a key-value pair with given key?
  35.      */
  36.     public boolean contains(int key) {
  37.         return find(key) != null;
  38.     }
  39.  
  40.     /**
  41.      * Return value associated with the given key, or null if no such key exists
  42.      */
  43.     public String find(int key) {
  44.         return find(root, key);
  45.     }
  46.  
  47.     /**
  48.      * Return value associated with the given key, or null if no such key exists
  49.      * in subtree rooted at x
  50.      */
  51.     private String find(Node x, int key) {
  52.         if (x == null) {
  53.             return null;
  54.         }
  55.         if (key < x.key) {
  56.             return find(x.left, key);
  57.         } else if (key > x.key) {
  58.             return find(x.right, key);
  59.         } else {
  60.             return x.val;
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Insert key-value pair into BST. If key already exists, update with new
  66.      * value
  67.      */
  68.     public void insert(int key, String val) {
  69.         if (val == null) {
  70.             remove(key);
  71.             return;
  72.         }
  73.         root = insert(root, key, val);
  74.     }
  75.  
  76.     /**
  77.      * Insert key-value pair into subtree rooted at x. If key already exists,
  78.      * update with new value.
  79.      */
  80.     private Node insert(Node x, int key, String val) {
  81.         if (x == null) {
  82.             size++;
  83.             return new Node(key, val);
  84.         }
  85.         if (key < x.key) {
  86.             x.left = insert(x.left, key, val);
  87.         } else if (key > x.key) {
  88.             x.right = insert(x.right, key, val);
  89.         } else {
  90.             x.val = val;
  91.         }
  92.  
  93.         return x;
  94.     }
  95.  
  96.     /**
  97.      * Remove node with given key from BST
  98.      */
  99.     public void remove(int key) {
  100.         if(root == null){
  101.             return;
  102.         }
  103.         if(root.left != null && key < root.key){
  104.             remove(root.left, root, key);
  105.         }else if(root.right != null && key > root.key){
  106.             remove(root.right, root, key);
  107.         }else{
  108.             if(root.key != key){
  109.                 System.out.println("There are no key with value : " + key);
  110.                 return;
  111.             }
  112.             if(root.left == null && root.right == null){
  113.                 root = null;
  114.                 size--;
  115.             }else if(root.left != null && root.right == null){
  116.                 root = root.left;
  117.                 size--;
  118.             }else if(root.left == null && root.right != null){
  119.                 root = root.right;
  120.                 size--;
  121.             }else{
  122.                 removeTwoChildren(root);
  123.             }
  124.             //Code for removing root
  125.         }
  126.     } // dummy code
  127.     /**
  128.      * Removes node with given Key from BST
  129.      * recursive help function for remove(int key)
  130.      */
  131.     private void remove(Node current, Node parent, int key) {
  132.         if(current.left != null && key < current.key){
  133.             remove(current.left, current, key);
  134.         }else if(current.right != null && key > current.key){
  135.             remove(current.right, current, key);
  136.         }else{
  137.             if(current.key != key){
  138.                 System.out.println("There are no key with value : " + key);
  139.                 return;
  140.             }
  141.             if(current.left == null && current.right == null){
  142.                 removeNoChild(parent, current);
  143.             }else if(current.left != null && current.right == null){
  144.                 removeLeftChild(parent, current);
  145.             }else if(current.left == null && current.right != null){
  146.                 removeRightChild(parent, current);
  147.             }else{
  148.                 removeTwoChildren(current);
  149.             }
  150.         }
  151.         return;
  152.     }
  153.     /**
  154.      * Removes node with two children
  155.      */
  156.     private void removeTwoChildren(Node removedNode) {
  157.         Node replacement = findreplacement(removedNode.left);   //Returns the largest node in the left subtree
  158.         int newKey = replacement.key;
  159.         String newVal = replacement.val;
  160.         remove(replacement.key);
  161.         removedNode.key = newKey;
  162.         removedNode.val = newVal;
  163.     }
  164.  
  165.     /**
  166.      * Returnes replacement node for a removed node with two children
  167.      */
  168.     private Node findreplacement(Node n) {
  169.         if(n.right == null){
  170.             return n;
  171.         }
  172.         return findreplacement(n.right);
  173.     }
  174.     /**
  175.      * Removes node with right child
  176.      */
  177.     private void removeRightChild(Node parent, Node current) {
  178.         if(parent.right == current){
  179.             parent.right = current.right;
  180.         }else if(parent.left == current){
  181.             parent.left = current.right;
  182.         }
  183.         size--;
  184.     }
  185.     /**
  186.      * Removes node with left child
  187.      */
  188.     private void removeLeftChild(Node parent, Node current) {
  189.         if(parent.right == current){
  190.             parent.right = current.left;
  191.         }else if(parent.left == current){
  192.             parent.left = current.left;
  193.         }
  194.         size--;
  195.     }
  196.     /**
  197.      * Removes node with no child
  198.      */
  199.     private void removeNoChild(Node parent, Node current) {
  200.         if(parent.left == current){
  201.             parent.left = null;
  202.         }else if(parent.right == current){
  203.             parent.right = null;
  204.         }
  205.         size--;
  206.     }
  207.  
  208.     public PreorderIterator preorder() {
  209.         return new PreorderIterator(root);
  210.     }
  211.  
  212.     public LevelorderIterator levelorder() {
  213.         return new LevelorderIterator(root);
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement