Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void balance() {
- Iterator<StudentCard> it = iterator();
- List<StudentCard> list = new DynamicArray<StudentCard>();
- while(it.hasNext())
- list.add(it.next());
- buildBalancedTree(new StudentCardBinarySearchTree(this.comparator),list,0,list.size()-1);
- }
- private void buildBalancedTree(StudentCardBinarySearchTree tree, List<StudentCard> list, int low, int high) {
- int mid = (low + high) / 2;
- tree.root = new BinarySearchNode<StudentCard>(list.get(mid),comparator);
- if (!(low < high)){
- return;
- }
- else{
- StudentCardBinarySearchTree leftTree = new StudentCardBinarySearchTree(this.comparator);
- buildBalancedTree(leftTree,list,low,mid-1);
- tree.root.left = leftTree.root;
- StudentCardBinarySearchTree rightTree = new StudentCardBinarySearchTree(this.comparator);
- buildBalancedTree(rightTree,list,mid+1,high);
- tree.root.right = rightTree.root;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement