Advertisement
Giftednarwhals

Hw # 18, last one!!!!

Dec 18th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.35 KB | None | 0 0
  1. // Name                 :       Kyle Blanchard
  2. // Due                  :       12/19/2014
  3. // Class                :       CSCI-401
  4. // Assignment           :       Add new methods in BST
  5. // Contact              :       Kwblanchard@student.stcc.edu
  6.  
  7. //package chapter25;
  8.  
  9. public class BST<E extends Comparable<E>>
  10.     extends AbstractTree<E> {
  11.   protected TreeNode<E> root;
  12.   protected int size = 0;
  13.  
  14.   /** Create a default binary tree */
  15.   public BST() {
  16.   }
  17.  
  18.   /** Create a binary tree from an array of objects */
  19.   public BST(E[] objects) {
  20.     for (int i = 0; i < objects.length; i++)
  21.       insert(objects[i]);
  22.   }
  23.  
  24.   @Override /** Returns true if the element is in the tree */
  25.   public boolean search(E e) {
  26.     TreeNode<E> current = root; // Start from the root
  27.  
  28.     while (current != null) {
  29.       if (e.compareTo(current.element) < 0) {
  30.         current = current.left;
  31.       }
  32.       else if (e.compareTo(current.element) > 0) {
  33.         current = current.right;
  34.       }
  35.       else // element matches current.element
  36.         return true; // Element is found
  37.     }
  38.  
  39.     return false;
  40.   }
  41.  
  42.   @Override /** Insert element o into the binary tree
  43.    * Return true if the element is inserted successfully */
  44.   public boolean insert(E e) {
  45.     if (root == null)
  46.       root = createNewNode(e); // Create a new root
  47.     else {
  48.       // Locate the parent node
  49.       TreeNode<E> parent = null;
  50.       TreeNode<E> current = root;
  51.       while (current != null)
  52.         if (e.compareTo(current.element) < 0) {
  53.           parent = current;
  54.           current = current.left;
  55.         }
  56.         else if (e.compareTo(current.element) > 0) {
  57.           parent = current;
  58.           current = current.right;
  59.         }
  60.         else
  61.           return false; // Duplicate node not inserted
  62.  
  63.       // Create the new node and attach it to the parent node
  64.       if (e.compareTo(parent.element) < 0)
  65.         parent.left = createNewNode(e);
  66.       else
  67.         parent.right = createNewNode(e);
  68.     }
  69.  
  70.     size++;
  71.     return true; // Element inserted successfully
  72.   }
  73.  
  74.   protected TreeNode<E> createNewNode(E e) {
  75.     return new TreeNode<>(e);
  76.   }
  77.  
  78.   @Override /** Inorder traversal from the root */
  79.   public void inorder() {
  80.     inorder(root);
  81.   }
  82.  
  83.   /** Inorder traversal from a subtree */
  84.   protected void inorder(TreeNode<E> root) {
  85.     if (root == null) return;
  86.     inorder(root.left);
  87.     System.out.print(root.element + " ");
  88.     inorder(root.right);
  89.   }
  90.  
  91.   @Override /** Postorder traversal from the root */
  92.   public void postorder() {
  93.     postorder(root);
  94.   }
  95.  
  96.   /** Postorder traversal from a subtree */
  97.   protected void postorder(TreeNode<E> root) {
  98.     if (root == null) return;
  99.     postorder(root.left);
  100.     postorder(root.right);
  101.     System.out.print(root.element + " ");
  102.   }
  103.  
  104.   @Override /** Preorder traversal from the root */
  105.   public void preorder() {
  106.     preorder(root);
  107.   }
  108.  
  109.   /** Preorder traversal from a subtree */
  110.   protected void preorder(TreeNode<E> root) {
  111.     if (root == null) return;
  112.     System.out.print(root.element + " ");
  113.     preorder(root.left);
  114.     preorder(root.right);
  115.   }
  116.  
  117.   /** This inner class is static, because it does not access
  118.       any instance members defined in its outer class */
  119.   public static class TreeNode<E extends Comparable<E>> {
  120.     public E element;
  121.     public TreeNode<E> left;
  122.     public TreeNode<E> right;
  123.  
  124.     public TreeNode(E e) {
  125.       element = e;
  126.     }
  127.   }
  128.  
  129.   @Override /** Get the number of nodes in the tree */
  130.   public int getSize() {
  131.     return size;
  132.   }
  133.  
  134.   /** Returns the root of the tree */
  135.   public TreeNode<E> getRoot() {
  136.     return root;
  137.   }
  138.  
  139.   /** Returns a path from the root leading to the specified element */
  140.   public java.util.ArrayList<TreeNode<E>> path(E e) {
  141.     java.util.ArrayList<TreeNode<E>> list =
  142.       new java.util.ArrayList<>();
  143.     TreeNode<E> current = root; // Start from the root
  144.  
  145.     while (current != null) {
  146.       list.add(current); // Add the node to the list
  147.       if (e.compareTo(current.element) < 0) {
  148.         current = current.left;
  149.       }
  150.       else if (e.compareTo(current.element) > 0) {
  151.         current = current.right;
  152.       }
  153.       else
  154.         break;
  155.     }
  156.  
  157.     return list; // Return an array list of nodes
  158.   }
  159.  
  160.   @Override /** Delete an element from the binary tree.
  161.    * Return true if the element is deleted successfully
  162.    * Return false if the element is not in the tree */
  163.   public boolean delete(E e) {
  164.     // Locate the node to be deleted and also locate its parent node
  165.     TreeNode<E> parent = null;
  166.     TreeNode<E> current = root;
  167.     while (current != null) {
  168.       if (e.compareTo(current.element) < 0) {
  169.         parent = current;
  170.         current = current.left;
  171.       }
  172.       else if (e.compareTo(current.element) > 0) {
  173.         parent = current;
  174.         current = current.right;
  175.       }
  176.       else
  177.         break; // Element is in the tree pointed at by current
  178.     }
  179.  
  180.     if (current == null)
  181.       return false; // Element is not in the tree
  182.  
  183.     // Case 1: current has no left child
  184.     if (current.left == null) {
  185.       // Connect the parent with the right child of the current node
  186.       if (parent == null) {
  187.         root = current.right;
  188.       }
  189.       else {
  190.         if (e.compareTo(parent.element) < 0)
  191.           parent.left = current.right;
  192.         else
  193.           parent.right = current.right;
  194.       }
  195.     }
  196.     else {
  197.       // Case 2: The current node has a left child
  198.       // Locate the rightmost node in the left subtree of
  199.       // the current node and also its parent
  200.       TreeNode<E> parentOfRightMost = current;
  201.       TreeNode<E> rightMost = current.left;
  202.  
  203.       while (rightMost.right != null) {
  204.         parentOfRightMost = rightMost;
  205.         rightMost = rightMost.right; // Keep going to the right
  206.       }
  207.  
  208.       // Replace the element in current by the element in rightMost
  209.       current.element = rightMost.element;
  210.  
  211.       // Eliminate rightmost node
  212.       if (parentOfRightMost.right == rightMost)
  213.         parentOfRightMost.right = rightMost.left;
  214.       else
  215.         // Special case: parentOfRightMost == current
  216.         parentOfRightMost.left = rightMost.left;    
  217.     }
  218.  
  219.     size--;
  220.     return true; // Element deleted successfully
  221.   }
  222.  
  223.   @Override /** Obtain an iterator. Use inorder. */
  224.   public java.util.Iterator<E> iterator() {
  225.     return new InorderIterator();
  226.   }
  227.  
  228.   // Inner class InorderIterator
  229.   private class InorderIterator implements java.util.Iterator<E> {
  230.     // Store the elements in a list
  231.     private java.util.ArrayList<E> list =
  232.       new java.util.ArrayList<>();
  233.     private int current = 0; // Point to the current element in list
  234.  
  235.     public InorderIterator() {
  236.       inorder(); // Traverse binary tree and store elements in list
  237.     }
  238.  
  239.     /** Inorder traversal from the root*/
  240.     private void inorder() {
  241.       inorder(root);
  242.     }
  243.  
  244.     /** Inorder traversal from a subtree */
  245.     private void inorder(TreeNode<E> root) {
  246.       if (root == null)return;
  247.       inorder(root.left);
  248.       list.add(root.element);
  249.       inorder(root.right);
  250.     }
  251.  
  252.     @Override /** More elements for traversing? */
  253.     public boolean hasNext() {
  254.       if (current < list.size())
  255.         return true;
  256.  
  257.       return false;
  258.     }
  259.  
  260.     @Override /** Get the current element and move to the next */
  261.     public E next() {
  262.       return list.get(current++);
  263.     }
  264.  
  265.     @Override /** Remove the current element */
  266.     public void remove() {
  267.       delete(list.get(current)); // Delete the current element
  268.       list.clear(); // Clear the list
  269.       inorder(); // Rebuild the list
  270.     }
  271.   }
  272.  
  273.   /** Remove all elements from the tree */
  274.   public void clear() {
  275.     root = null;
  276.     size = 0;
  277.   }
  278.   // gets the height of the tree
  279.     public int Height() {
  280.         return height(root);
  281.     }
  282.     private int height(TreeNode<E> x) {
  283.         if (x == null)
  284.             return 0;
  285.         else
  286.             return 1 + Math.max(height(x.left), height(x.right));
  287.     }
  288.  // Displays the breadthFirstTraversal of the tree
  289.     public void breadthFirstTraversal() {
  290.         System.out.println(root.element + " " + getNode(root));
  291.     }
  292.  // Gets the node(s) of the tree recursively
  293.     private String getNode(TreeNode<E> x) {
  294.         if (x.left == null && x.right == null) {
  295.             return "";
  296.         } else if (x.left == null) {
  297.             return x.right.element + " " + getNode(x.right);
  298.         } else if (x.right == null) {
  299.             return x.left.element + " " + getNode(x.left);
  300.         } else {
  301.             return x.left.element + " " + x.right.element + " "
  302.                     + getNode(x.left) + getNode(x.right);
  303.         }
  304.     }
  305.  // counts the leaves
  306.     public int treeLeavesCount() {
  307.         return leavesCount(root);
  308.     }
  309.     private int leavesCount(TreeNode<E> x) {
  310.         if (x == null)
  311.             return 0;
  312.         else if (x.left == null && x.right == null)
  313.             return 1;
  314.         else
  315.             return leavesCount(x.left) + leavesCount(x.right);
  316.     }
  317.  // counts the nodes
  318.     public int treeNodeCount() {
  319.         return nodeCount(root);
  320.     }
  321.     private int nodeCount(TreeNode<E> x) {
  322.         if (x == null)
  323.             return 0;
  324.         else
  325.             return 1 + nodeCount(x.left) + nodeCount(x.right);
  326.     }
  327.  // uses the number of leaves and nodes to check if the tree is a full binary search tree
  328.     public boolean isFullBST() {
  329.  
  330.         int leaves = this.treeLeavesCount();
  331.         int nodes = this.treeNodeCount();
  332.  
  333.         if (nodes == 1)
  334.             return true;
  335.         else {
  336.             if (((nodes / 2) + 1) == leaves)
  337.                 return true;
  338.             else
  339.                 return false;
  340.         }
  341.     }
  342.  
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement