3

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);

    }
Ed_
  • 347
  • 1
  • 8

1 Answers1

0
BankAccountsBinarySearchTree tree = new BankAccountsBinarySearchTree(comparator);
createList(tree.root, (DynamicArray<BankAccount>) list);

You are creating a new BankAccountsBinarySearchTree object and then passing that object's root (which will be null) to the createList method.

You need to pass the current object's root (which is not shown in your code) to createList method.

user7
  • 14,505
  • 3
  • 35
  • 62
  • if i'm passing to it this.root i'm getting a null pointer exception – Ed_ Jan 03 '19 at 09:37
  • Where does it throw a NPE? It is difficult to provide a complete answer without looking at the `root` part – user7 Jan 03 '19 at 09:48
  • Exception in thread "main" java.lang.NullPointerException at BankAccountsBinarySearchTree.buildBalancedTree(BankAccountsBinarySearchTree.java:42) at BankAccountsBinarySearchTree.buildBalancedTree(BankAccountsBinarySearchTree.java:49) at BankAccountsBinarySearchTree.balance(BankAccountsBinarySearchTree.java:29) at TestBalance.main(TestBalance.java:23) – Ed_ Jan 03 '19 at 09:55
  • @lidor718 It is not that helpful for me. First try printing `List list` after calling `createList` to verify you get a sorted data. – user7 Jan 03 '19 at 12:12
  • I did that , and the data was sorted. And just to make sure I commented the buildBalancedTree out and exception was gone so I know that is it I just can't figure out what exactly . I could attach the other classes ( BinaryNode, BinarySearchNode, BinaryTree, BankAccount) if that helps – Ed_ Jan 03 '19 at 12:16