Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package bst;
  2.  
  3. public class BinarySearchTree<E extends Comparable<? super E>> {
  4.     BinaryNode<E> root;
  5.     int size;
  6.    
  7.     /**
  8.      * Constructs an empty binary search tree.
  9.      */
  10.     public BinarySearchTree() {
  11.        
  12.     }
  13.  
  14.     /**
  15.      * Inserts the specified element in the tree if no duplicate exists.
  16.      * @param x element to be inserted
  17.      * @return true if the the element was inserted
  18.      */
  19.     public boolean add(E x) {
  20.         if(root == null) {
  21.             root = new BinaryNode<E>(x);
  22.             size++;
  23.             return true;
  24.         } else {
  25.             return add(root, x);
  26.         }
  27.     }
  28.    
  29.     private boolean add(BinaryNode<E> node, E item) {
  30.         int compResult = item.compareTo(node.element);
  31.         if(compResult == 0) {
  32.             return false;
  33.         }
  34.        
  35.         if(compResult < 0) {
  36.             if(node.left == null) {
  37.                 node.left = new BinaryNode<E>(item);
  38.                 size++;
  39.                 return true;
  40.             } else {
  41.                 return add(node.left, item);
  42.             }
  43.         } else {
  44.             if(node.right == null) {
  45.                 node.right = new BinaryNode<E>(item);
  46.                 size++;
  47.                 return true;
  48.             } else {
  49.                 return add(node.right, item);
  50.             }
  51.         }
  52.     }
  53.    
  54.     /**
  55.      * Computes the height of tree.
  56.      * @return the height of the tree
  57.      */
  58.     public int height() {
  59.         return height(root);
  60.     }
  61.    
  62.     private int height(BinaryNode<E> node) {
  63.         if(node == null) {
  64.             return 0;
  65.         } else {
  66.             int left = 1 + height(node.left) ;
  67.             int right = 1 + height(node.right);
  68.             return (left < right) ? right : left;
  69.         }
  70.     }
  71.    
  72.     /**
  73.      * Returns the number of elements in this tree.
  74.      * @return the number of elements in this tree
  75.      */
  76.     public int size() {
  77.         return size;
  78.     }
  79.    
  80.     /**
  81.      * Print tree contents in inorder.
  82.      */
  83.     public void printTree() {
  84.         printTree(root);
  85.     }
  86.    
  87.     private void printTree(BinaryNode<E> node) {
  88.         if(node == null) {
  89.             return;
  90.         } else {
  91.             printTree(node.left);
  92.             System.out.print(node.element);
  93.             printTree(node.right);
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Builds a complete tree from the elements in the tree.
  99.      */
  100.     public void rebuild() {
  101.         @SuppressWarnings("unchecked")
  102.         E[] a = (E[]) new Comparable[size];
  103.         root = buildTree(a, 0, toArray(root, a, 0)-1);
  104.     }
  105.    
  106.     /**
  107.      * Adds all elements from the tree rooted at n in inorder to the array a
  108.      * starting at a[index].
  109.      * Returns the index of the last inserted element + 1 (the first empty
  110.      * position in a).
  111.      */
  112.     private int toArray(BinaryNode<E> n, E[] a, int index) {
  113.         if(n != null) {
  114.             index = toArray(n.left, a, index);
  115.             a[index++] = n.element;
  116.             index = toArray(n.right, a, index);
  117.             return index;
  118.         }
  119.         return index;
  120.     }
  121.    
  122.     /**
  123.      * Builds a complete tree from the elements a[first]..a[last].
  124.      * Elements in the array a are assumed to be in ascending order.
  125.      * Returns the root of tree.
  126.      */
  127.     private BinaryNode<E> buildTree(E[] a, int first, int last) {
  128.         int mid = ((last + first) / 2);
  129.        
  130.         if(last == first) {
  131.             return new BinaryNode<E>(a[mid]);
  132.         } else if (first > last) {
  133.             return null;
  134.         }
  135.        
  136.         BinaryNode<E> temp = new BinaryNode<E>(a[mid]);
  137.         temp.left = buildTree(a, first, mid-1);
  138.        
  139.         mid = ((last + first) / 2);
  140.        
  141.         temp.right = buildTree(a, mid+1, last);
  142.        
  143.         return temp;
  144.     }
  145.    
  146.  
  147.  
  148.     static class BinaryNode<E> {
  149.         E element;
  150.         BinaryNode<E> left;
  151.         BinaryNode<E> right;
  152.  
  153.         private BinaryNode(E element) {
  154.             this.element = element;
  155.         }  
  156.     }
  157.    
  158.     public static void main(String[] args) {
  159.         BinarySearchTree<Integer> bintre = new BinarySearchTree<Integer>();
  160.         bintre.add(1);
  161.         bintre.add(3);
  162.         bintre.add(5);
  163.         bintre.add(7);
  164.         bintre.add(9);
  165.         bintre.add(11);
  166.         bintre.add(13);
  167.  
  168.         bintre.rebuild();
  169.         BSTVisualizer draw = new BSTVisualizer("Window", 600, 600);
  170.         draw.drawTree(bintre);
  171.        
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement