I have a BinarySearchTree
with objects in it of Instance bankaccount which is a class I created, so basically it's just a binarysearchtree and I wrote a method that will take the tree and balance it, for some reason it prints out exactly the tree before balance:
Now, first I have the createList
method which takes in a list and a tree(one node)
and creates an arrayList(DynamicArray)
of the tree data by going over it inorder so it's a sorted array.
Then the other method is used to create the tree in a balanced way by making the middle element of the array root, then the left middle the root of the left subtree and the right middle the root of the right subtree
import java.util.Comparator;
import java.util.Iterator;
public class BankAccountsBinarySearchTree extends BinarySearchTree<BankAccount>{
public BankAccountsBinarySearchTree(Comparator<BankAccount> myComparator) {
super(myComparator);
}
//Complete the following method
public void balance(){
// create a sorted list and a binary tree
List<BankAccount> list = new DynamicArray<BankAccount>();
BankAccountsBinarySearchTree tree = new BankAccountsBinarySearchTree(comparator);
createList(tree.root, (DynamicArray<BankAccount>) list);
// build balanced tree recursively
buildBalancedTree(tree, list, 0, list.size()-1);
}
//Complete the following method
private void buildBalancedTree(BankAccountsBinarySearchTree tree, List<BankAccount> list, int low, int high){
// base case
if (low > high)
return ;
// Get the middle element and make it root
int mid = (low + high) / 2;
tree.root.data = list.get(mid);
// create left and right subtrees and go on to balance each
BankAccountsBinarySearchTree leftTree = new BankAccountsBinarySearchTree(comparator);
BankAccountsBinarySearchTree rightTree = new BankAccountsBinarySearchTree(comparator);
buildBalancedTree(leftTree, list , low, mid - 1);
buildBalancedTree(rightTree, list, mid + 1, high);
root.left = leftTree.root;
root.right = rightTree.root;
}
// method to create a list with all objects of BankAccountBinarySearchTree in a sorted array because it's in Order.
private void createList(BinaryNode<BankAccount> root, DynamicArray<BankAccount> list)
{
// Base case
if (root == null)
return;
// Store nodes in Inorder (which is sorted
// order for BST)
createList(root.left, list);
list.add(root.data);
createList((BinarySearchNode) root.right, list);
}
public Iterator<BankAccount> iterator(){
return new FilteredBankAccountsIterator(this);
}
}
For some reason if I do this:
Comparator<BankAccount> c = new AccountComparatorByNumber();
BankAccountsBinarySearchTree t3 = new BankAccountsBinarySearchTree(c);
t3.insert(new BankAccount("a", 2, 0));
t3.insert(new BankAccount("a", 1, 0));
t3.insert(new BankAccount("a", 3, 0));
t3.insert(new BankAccount("a", 4, 0));
t3.insert(new BankAccount("a", 5, 0));
t3.insert(new BankAccount("a", 6, 0));
t3.insert(new BankAccount("a", 7, 0));
t3.insert(new BankAccount("a", 8, 0));
System.out.println("----------unbalanced t3:----------\n" + t3);
t3.balance();
System.out.println("\n----------balanced t3:----------\n" + t3 + "\n\n");
well it'll first use a comparator to sort the array by number so the array should be {1,2,3,4,5,6,7,8} ( this is how this comparator works) and then I'd expected the tree to be balanced however it remains the same. Any idea of what's wrong with the code?
edit: this is what i've changed so far and buildBalancedTree is giving me a NullpointerException
public void balance(){
// create a sorted list and a binary tree
List<BankAccount> list = new DynamicArray<BankAccount>();
BankAccountsBinarySearchTree tree = new BankAccountsBinarySearchTree(comparator);
tree.root = this.root;
createList(tree.root, (DynamicArray<BankAccount>) list);
// build balanced tree recursively
buildBalancedTree(tree, list, 0, list.size()-1);
}