Advertisement
binibiningtinamoran

BinarySearchTreeWithDuplicates

Jun 6th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.02 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BinarySearchTreeWithDups<T extends Comparable<? super T>> extends BinarySearchTree<T>
  4.         implements SearchTreeInterface<T>, java.io.Serializable {
  5.  
  6.     public BinarySearchTreeWithDups() {
  7.         super();
  8.     }
  9.  
  10.     public BinarySearchTreeWithDups(T rootEntry) {
  11.         super(rootEntry);
  12.         setRootNode(new BinaryNode <>(rootEntry));
  13.     }
  14.  
  15.     @Override
  16.     public T add(T newEntry) {
  17.         T result = newEntry;
  18.         if (isEmpty()) {
  19.             setRootNode(new BinaryNode <T>(newEntry));
  20.         } else {
  21.             addEntryHelperNonRecursive(newEntry);
  22.         }
  23.         return result;
  24.     }
  25.  
  26.     // YOUR CODE HERE! THIS METHOD CANNOT BE RECURSIVE
  27.     private void addEntryHelperNonRecursive(T newEntry) {
  28.         BinaryNode<T> currentNode = getRootNode();
  29.         BinaryNode<T> nodeToAdd = new BinaryNode<T>(newEntry);
  30.         boolean found = false;
  31.         while(currentNode != null && !found) {
  32.             int compareVal = currentNode.getData().compareTo(newEntry);
  33.             if (compareVal < 0) {
  34.                 if (currentNode.hasRightChild()) {
  35.                     currentNode = currentNode.getRightChild();
  36.                 } else {
  37.                     currentNode.setRightChild(nodeToAdd);
  38.                     found = true;
  39.                 }
  40.             } else {
  41.                 if (currentNode.hasLeftChild()) {
  42.                     currentNode = currentNode.getLeftChild();
  43.                 } else {
  44.                     currentNode.setLeftChild(nodeToAdd);
  45.                     found = true;
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     /*private void addEntryHelperNonRecursive(T newEntry) {
  52.         BinaryNode <T> currentNode = getRootNode();
  53.         boolean found = false;
  54.         // int comparison = newEntry.compareTo(currentNode.getData()); // getRootData()
  55.  
  56.         while (!found) {
  57.             //int comparison = newEntry.compareTo(currentNode.getData()); // getRootData()
  58.             if (newEntry.compareTo(currentNode.getData()) == 0) { // newEntry == rootData
  59.                 if (currentNode.hasLeftChild()) {
  60.                     currentNode = currentNode.getLeftChild();
  61.                 } else {
  62.                     currentNode.setLeftChild(new BinaryNode <>(newEntry));
  63.                     found = true;
  64.                 }
  65.             } else if (newEntry.compareTo(currentNode.getData()) < 0) {
  66.                 if (currentNode.hasLeftChild()) {
  67.                     currentNode = currentNode.getLeftChild();
  68.                 } else {
  69.                     currentNode.setLeftChild(new BinaryNode <>(newEntry));
  70.                     found = true;
  71.                 }
  72.  
  73.             } else { // if comparison > 0
  74.                 if (currentNode.hasRightChild()) {
  75.                     currentNode = currentNode.getRightChild();
  76.                 } else {
  77.                     currentNode.setRightChild(new BinaryNode <>(newEntry));
  78.                     found = true;
  79.                 }
  80.             } // end last else
  81.         } // end while()
  82.     } // end addEntryHelperNonRecursive()
  83.     */
  84.  
  85.     // YOUR CODE HERE! THIS METHOD CANNOT BE RECURSIVE.
  86.     // MAKE SURE TO TAKE ADVANTAGE OF THE SORTED NATURE OF THE BST!
  87.     public int countEntriesNonRecursive(T target) {
  88.         int count = 0;
  89.         BinaryNode <T> currentNode = getRootNode();
  90.  
  91.         while (currentNode != null) {
  92.             int compareVal = currentNode.getData().compareTo(target);
  93.             if (compareVal == 0) {
  94.                 count++;
  95.                 currentNode = currentNode.getLeftChild();
  96.             } else if (compareVal < 0) {
  97.                 currentNode = currentNode.getRightChild();
  98.             } else {
  99.                 currentNode = currentNode.getLeftChild();
  100.             }
  101.         }
  102.         return count;
  103.     }
  104.  
  105.     // YOUR CODE HERE! MUST BE RECURSIVE! YOU ARE ALLOWED TO CREATE A PRIVATE HELPER.
  106.     // MAKE SURE TO TAKE ADVANTAGE OF THE SORTED NATURE OF THE BST!
  107.     public int countGreaterRecursive(T target) {
  108.         return countGreaterRecursiveHelper(getRootNode(), target);
  109.     }
  110.  
  111.     private int countGreaterRecursiveHelper(BinaryNode<T> node, T target) {
  112.         if (node != null) {
  113.             T data = node.getData();
  114.             int comparison = data.compareTo(target);
  115.             BinaryNode<T> leftChild = node.getRightChild();
  116.             if (comparison > 0) {
  117.                 BinaryNode<T> rightChild = node.getLeftChild();
  118.                 return 1 + countGreaterRecursiveHelper(rightChild, target) + countGreaterRecursiveHelper(leftChild, target);
  119.             } else {
  120.                 return countGreaterRecursiveHelper(leftChild, target);
  121.             }
  122.         } else {
  123.             return 0;
  124.         }
  125.     }
  126.  
  127.     /*
  128.     private int countGreaterRecursiveHelper(BinaryNode <T> node, T target) {
  129.         int count = 0;
  130.         if (node != null) {
  131.             if (target.compareTo(node.getData()) < 0) { // target < nodeData
  132.                 count++;
  133.             }
  134.             if (node.hasRightChild()) {
  135.                 count = count + countGreaterRecursiveHelper(node.getRightChild(), target);
  136.             }
  137.             if (node.hasLeftChild()) {
  138.                 count = count + countGreaterRecursiveHelper(node.getLeftChild(), target);
  139.             }
  140.         } else {
  141.             return 0;
  142.         }
  143.         return count;
  144.     }
  145.     */
  146.  
  147.     // The method counts the number of elements in the tree greater than the parameter.
  148.     // YOUR CODE HERE! MUST USE A STACK!! MUST NOT BE RECURSIVE!
  149.     // MAKE SURE TO TAKE ADVANTAGE OF THE SORTED NATURE OF THE BST!
  150.     public int countGreaterWithStack(T target) {
  151.         int count = 0;
  152.         Stack <BinaryNode<T>> nodeStack = new Stack <>();
  153.         nodeStack.push(getRootNode());
  154.  
  155.         while (!nodeStack.isEmpty()) {
  156.             BinaryNode <T> topNodeFromStack = nodeStack.pop();
  157.             if (topNodeFromStack != null) {
  158.                 int compareVal = topNodeFromStack.getData().compareTo(target);
  159.                 if (compareVal > 0) {
  160.                     count++;
  161.                     nodeStack.push(topNodeFromStack.getLeftChild());
  162.                     nodeStack.push(topNodeFromStack.getRightChild());
  163.                 } else {
  164.                     nodeStack.push(topNodeFromStack.getRightChild());
  165.                 }
  166.             }
  167.         }
  168.         return count;
  169.     }
  170.  
  171.     /*
  172.     public int countGreaterWithStack(T target) {
  173.         int count = 0;
  174.         BinaryNode <T> rootNode = getRootNode();
  175.         Stack <BinaryNode <T>> nodeStack = new Stack <>();
  176.         nodeStack.push(rootNode);
  177.         int compareVal = target.compareTo(topStack.getData());
  178.         while (!nodeStack.isEmpty() && rootNode != null) {
  179.             BinaryNode <T> topStack = nodeStack.pop();
  180.             if (topStack.hasLeftChild()) {
  181.                 nodeStack.push(topStack.getLeftChild());
  182.             }
  183.             if (topStack.hasRightChild()) {
  184.                 nodeStack.push(topStack.getRightChild());
  185.             }
  186.             if (compareVal < 0) {
  187.                 count++;
  188.             }
  189.         }
  190.         return count;
  191.     }
  192.      */
  193.  
  194.  
  195.     // YOUR EXTRA CREDIT CODE HERE! THIS METHOD MUST BE O(n).
  196.     // YOU ARE ALLOWED TO USE A HELPER METHOD. THE METHOD CAN BE ITERATIVE OR RECURSIVE.
  197.     public int countUniqueValues() {
  198.         //TreeSet<T> someTree = new TreeSet <>();
  199.         Set<T> set = new HashSet<T>();
  200.         return countUniqueValuesHelper(getRootNode(), set);
  201.     }
  202.  
  203.     private int countUniqueValuesHelper(BinaryNode<T> root, Set<T> set) {
  204.         int count = 0;
  205.         if (root != null) {
  206.             T val = root.getData();
  207.             if (!set.contains(val)) {
  208.                 count++;
  209.                 set.add(val);
  210.             }
  211.             return count + countUniqueValuesHelper(root.getLeftChild(),set) +
  212.                     countUniqueValuesHelper(root.getRightChild(),set);
  213.         } else {
  214.             return count;
  215.         }
  216.     }
  217.  
  218.     /* // Using iterator
  219.     public int countUniqueValues() {
  220.         Iterator <T> iterator = this.getInorderIterator();
  221.         int count = 0;
  222.         T last = null;
  223.         while (iterator.hasNext()) {
  224.             T nodeData = iterator.next();
  225.             if (!nodeData.equals(last)) {
  226.                 count++;
  227.                 last = nodeData;
  228.             }
  229.         }
  230.         return count;
  231.     }*/
  232.  
  233.     /*
  234.     private int countUniqueValuesHelper(BinaryNode<T> root, Set<T> set) {
  235.         if (root != null) {
  236.             T val = root.getData();
  237.             set.add(val);
  238.             countUniqueValuesHelper(root.getLeftChild(),set);
  239.             countUniqueValuesHelper(root.getRightChild(),set);
  240.         }
  241.         return set.size();
  242.     }
  243.  
  244.      */
  245.  
  246.  
  247.     public int getLeftHeight() {
  248.         BinaryNode<T> rootNode = getRootNode();
  249.         if(rootNode==null) {
  250.             return 0;
  251.         } else if(!rootNode.hasLeftChild()) {
  252.             return 0;
  253.         } else {
  254.             return rootNode.getLeftChild().getHeight();
  255.         }
  256.     }
  257.  
  258.     public int getRightHeight() {
  259.         BinaryNode<T> rootNode = getRootNode();
  260.         if(rootNode==null) {
  261.             return 0;
  262.         } else if(!rootNode.hasRightChild()) {
  263.             return 0;
  264.         } else {
  265.             return rootNode.getRightChild().getHeight();
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement