LoganBlackisle

DictionaryBST

Dec 6th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. package binarysearchtreestuderende;
  2.  
  3. public class DictionaryBST<K extends Comparable<K>, V> implements Dictionary<K, V> {
  4.     private Node root;
  5.  
  6.     /**
  7.      * Constructs an empty tree.
  8.      */
  9.     public DictionaryBST() {
  10.         root = null;
  11.     }
  12.  
  13.     @Override
  14.     public V get(K key) {
  15.         return find(key).value;
  16.     }
  17.  
  18.     /**
  19.      * Tries to find an object in the tree.
  20.      *
  21.      * @param obj the object to find
  22.      * @return true if the object is contained in the tree
  23.      */
  24.     public Node find(K key) {
  25.         Node current = root;
  26.         boolean found = false;
  27.         while (!found && current != null) {
  28.             int d = current.key.compareTo(key);
  29.             if (d == 0) {
  30.                 found = true;
  31.             } else if (d > 0) {
  32.                 current = current.left;
  33.             } else {
  34.                 current = current.right;
  35.             }
  36.         }
  37.         if (found) {
  38.             return current;
  39.         } else {
  40.             return null;
  41.         }
  42.     }
  43.  
  44.     /**
  45.      * Tries to find the smallest object in the tree.
  46.      *
  47.      * @param obj the object to find
  48.      * @return true if the object is contained in the tree
  49.      */
  50.     public K findMin(K key) {
  51.         Node parent = null;
  52.         Node child = root;
  53.         K toReturn = null;
  54.         if (root.left == null) {
  55.             if (root != null) {
  56.                 root = root.right;
  57.             } else {
  58.                 while (child.left != null) {
  59.                     parent = child;
  60.                     child = parent.left;
  61.                 }
  62.             }
  63.             toReturn = child.data;
  64.             parent.left = parent.left.right;
  65.         }
  66.         return toReturn;
  67.     }
  68.  
  69.     /**
  70.      * Tries to find the largest object in the tree.
  71.      *
  72.      * @param obj the object to find
  73.      * @return true if the object is contained in the tree
  74.      */
  75.     public K findMax(K key) {
  76.         Node max = root;
  77.         if (root != null) {
  78.             while (max.right != null) {
  79.                 max = max.right;
  80.             }
  81.         } else {
  82.             return null;
  83.         }
  84.         return max.data;
  85.     }
  86.  
  87.     /**
  88.      * Inserts a new node into the tree.
  89.      *
  90.      * @param obj the object to insert
  91.      */
  92.     public void add(Comparable obj) {
  93.         Node newNode = new Node(null, null);
  94.         newNode.left = null;
  95.         newNode.right = null;
  96.         if (root == null) {
  97.             root = newNode;
  98.         } else {
  99.             root.addNode(newNode);
  100.         }
  101.     }
  102.  
  103.     @Override
  104.     public boolean isEmpty() {
  105.         boolean empty = true;
  106.         if (root == null) {
  107.             return empty;
  108.         } else {
  109.             return empty = false;
  110.         }
  111.     }
  112.  
  113.     @Override
  114.     public V put(K key, V value) {
  115.         Node newNode = new Node(key, value);
  116.         if (root == null) {
  117.             root = newNode;
  118.         }
  119.         return find(key).value;
  120.     }
  121.  
  122.     /**
  123.      * Tries to remove an object from the tree. Does nothing if the object is not
  124.      * contained in the tree.
  125.      *
  126.      * @param obj the object to remove
  127.      */
  128.     @Override
  129.     public V remove(K key) {
  130.         Node toBeRemoved = root;
  131.         Node parent = null;
  132.         V toReturn = null;
  133.         boolean found = false;
  134.         while (!found && toBeRemoved != null) {
  135.             int d = toBeRemoved.key.compareTo(key);
  136.             if (d == 0) {
  137.                 found = true;
  138.                 toReturn = toBeRemoved.value;
  139.             } else {
  140.                 parent = toBeRemoved;
  141.                 if (d > 0) {
  142.                     toBeRemoved = toBeRemoved.left;
  143.                 } else {
  144.                     toBeRemoved = toBeRemoved.right;
  145.                 }
  146.             }
  147.         }
  148.         if (found) {
  149.  
  150.             // toBeRemoved contains obj
  151.  
  152.             // If one of the children is empty, use the other
  153.  
  154.             if (toBeRemoved.left == null || toBeRemoved.right == null) {
  155.                 Node newChild;
  156.                 if (toBeRemoved.left == null) {
  157.                     newChild = toBeRemoved.right;
  158.                 } else {
  159.                     newChild = toBeRemoved.left;
  160.                 }
  161.                 if (parent == null) // Found in root
  162.                 {
  163.                     root = newChild;
  164.                 } else if (parent.left == toBeRemoved) {
  165.                     parent.left = newChild;
  166.                 } else {
  167.                     parent.right = newChild;
  168.                 }
  169.             } else {
  170.  
  171.                 // Neither subtree is empty
  172.  
  173.                 // Find smallest element of the right subtree
  174.  
  175.                 Node smallestParent = toBeRemoved;
  176.                 Node smallest = toBeRemoved.right;
  177.                 while (smallest.left != null) {
  178.                     smallestParent = smallest;
  179.                     smallest = smallest.left;
  180.                 }
  181.  
  182.                 // smallest contains smallest child in right subtree
  183.  
  184.                 // Move contents, unlink child
  185.  
  186.                 toBeRemoved.key = smallest.key;
  187.                 toBeRemoved.value = smallest.value;
  188.                 if (smallestParent == toBeRemoved) {
  189.                     smallestParent.right = smallest.right;
  190.                 } else {
  191.                     smallestParent.left = smallest.right;
  192.                 }
  193.             }
  194.         }
  195.         return toReturn;
  196.     }
  197.  
  198.     /**
  199.      * Tries to remove the smallest object from the tree. Does nothing if the object
  200.      * is not contained in the tree.
  201.      *
  202.      * @param obj the object to remove
  203.      */
  204.     private Node removeMin(K key) {
  205.         findMin(key);
  206.     }
  207.  
  208.     /**
  209.      * Tries to remove the largest object from the tree. Does nothing if the object
  210.      * is not contained in the tree.
  211.      *
  212.      * @param obj the object to remove
  213.      */
  214.     private Node removeMax(K key) {
  215.         findMax(key);
  216.     }
  217.  
  218.     /**
  219.      * Prints the contents of the tree in sorted order.
  220.      */
  221.     public void print() {
  222.         print(root);
  223.         System.out.println();
  224.     }
  225.  
  226.     /**
  227.      * Prints a node and all of its descendants in sorted order.
  228.      *
  229.      * @param parent the root of the subtree to print
  230.      */
  231.     private void print(Node parent) {
  232.         if (parent != null) {
  233.             print(parent.left);
  234.             System.out.print(parent.data + " ");
  235.             print(parent.right);
  236.         }
  237.     }
  238.  
  239.     @Override
  240.     public int size() {
  241.         return size(root);
  242.     }
  243.  
  244.     private int size(Node n) {
  245.         if (n == null) {
  246.             return 0;
  247.         } else {
  248.             return size(n.left) + size(n.right) + 1;
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * A node of a tree stores a data item and references to the left and right
  254.      * child nodes.
  255.      */
  256.     private class Node {
  257.         K key;
  258.         V value;
  259.         K data;
  260.         Node left;
  261.         Node right;
  262.  
  263.         public Node(K key, V value) {
  264.             this.key = key;
  265.             this.value = value;
  266.             this.data = null;
  267.             this.left = null;
  268.             this.right = null;
  269.         }
  270.  
  271.         /**
  272.          * Inserts a new node as a descendant of this node.
  273.          *
  274.          * @param newNode the node to insert
  275.          */
  276.         private V addNode(Node newNode) {
  277.             int comp = newNode.data.compareTo(data);
  278.             V toReturn = null;
  279.             if (comp < 0) {
  280.                 if (left == null) {
  281.                     left = newNode;
  282.                 } else {
  283.                     left.addNode(newNode);
  284.                 }
  285.             } else if (comp > 0) {
  286.                 if (right == null) {
  287.                     right = newNode;
  288.                 } else {
  289.                     toReturn = value;
  290.                     value = newNode.value;
  291.                 }
  292.             }
  293.             return toReturn;
  294.         }
  295.     }
  296. }
Add Comment
Please, Sign In to add comment