Advertisement
Guest User

Untitled

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