I am new to data structures and binary trees so I am a little bit lost . My problem is that how do we insert values in the tree itself ?
3 Answers
You just need to check for this case explicitly.
Update
Also you'll need to add another Add(int v) method for public use (you can also make you current Add(Node, int) private).
public void Add(int v) {
if (root == null) {
// creating root node if it doesn't exist
root = new Node();
root.setValue(v);
} else {
// adding new element to tree
Add(root, v);
}
}
2 Comments
user1351776
so in the Add method when i do t.setRight(v); or t.setLeft(v); i get an error saying that it can't be applied to given types because v is an int while setRight and setLeft only accept Node parammeters , so how can i fix this problem ?
Ivan Nevostruev
Got it. Your current
Add method should be private (because it expose implementation detail Node t to public). And you should add new Add(int v) method. See my updated answer.