Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1.  
  2.     public void balance() {
  3.         Iterator<StudentCard> it = iterator();
  4.         List<StudentCard> list = new DynamicArray<StudentCard>();
  5.         while(it.hasNext())
  6.             list.add(it.next());
  7.  
  8.         buildBalancedTree(new StudentCardBinarySearchTree(this.comparator),list,0,list.size()-1);
  9.     }
  10.  
  11.     private void buildBalancedTree(StudentCardBinarySearchTree tree, List<StudentCard> list, int low, int high) {
  12.         int mid = (low + high) / 2;
  13.         tree.root = new BinarySearchNode<StudentCard>(list.get(mid),comparator);
  14.  
  15.         if (!(low < high)){
  16.             return;
  17.         }
  18.         else{
  19.             StudentCardBinarySearchTree leftTree = new StudentCardBinarySearchTree(this.comparator);
  20.             buildBalancedTree(leftTree,list,low,mid-1);
  21.             tree.root.left = leftTree.root;
  22.  
  23.             StudentCardBinarySearchTree rightTree = new StudentCardBinarySearchTree(this.comparator);
  24.             buildBalancedTree(rightTree,list,mid+1,high);
  25.             tree.root.right = rightTree.root;
  26.         }
  27.     }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement