Balancing a binary search tree recursively
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 + "nn");
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);
}
java binary-search-tree tree-balancing
add a comment |
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 + "nn");
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);
}
java binary-search-tree tree-balancing
add a comment |
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 + "nn");
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);
}
java binary-search-tree tree-balancing
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 + "nn");
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);
}
java binary-search-tree tree-balancing
java binary-search-tree tree-balancing
edited Jan 3 at 9:54
lidor718
asked Jan 3 at 8:59
lidor718lidor718
315
315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at therootpart
– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printingList<BankAccount> listafter callingcreateListto verify you get a sorted data.
– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019057%2fbalancing-a-binary-search-tree-recursively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at therootpart
– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printingList<BankAccount> listafter callingcreateListto verify you get a sorted data.
– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
add a comment |
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.
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at therootpart
– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printingList<BankAccount> listafter callingcreateListto verify you get a sorted data.
– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
add a comment |
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.
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.
edited Jan 3 at 9:22
answered Jan 3 at 9:08
user7user7
9,69432546
9,69432546
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at therootpart
– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printingList<BankAccount> listafter callingcreateListto verify you get a sorted data.
– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
add a comment |
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at therootpart
– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printingList<BankAccount> listafter callingcreateListto verify you get a sorted data.
– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
if i'm passing to it this.root i'm getting a null pointer exception
– lidor718
Jan 3 at 9:37
Where does it throw a NPE? It is difficult to provide a complete answer without looking at the
root part– user7
Jan 3 at 9:48
Where does it throw a NPE? It is difficult to provide a complete answer without looking at the
root part– user7
Jan 3 at 9: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)
– lidor718
Jan 3 at 9:55
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)
– lidor718
Jan 3 at 9:55
@lidor718 It is not that helpful for me. First try printing
List<BankAccount> list after calling createList to verify you get a sorted data.– user7
Jan 3 at 12:12
@lidor718 It is not that helpful for me. First try printing
List<BankAccount> list after calling createList to verify you get a sorted data.– user7
Jan 3 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
– lidor718
Jan 3 at 12:16
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
– lidor718
Jan 3 at 12:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019057%2fbalancing-a-binary-search-tree-recursively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown