Adding and generating a binary search tree
I'm having a bit of trouble figuring out how to add or insert Nodes to my
binary search tree. At the moment I have the following code:
public void add(int v) {
Node n = new Node(v);
if(root==null)
root = n;
else {
Node m = root;
while(...) { //not sure what to check
if(v < m.value)
m = m.left;
else
m = m.right;
}
if(...) //not sure what to check
m.left = n;
else
m.right = n;
}
}
Then I also would like to generate n number of Nodes within a certain
range. I know how to do this for arrays, but I'm not sure how to go about
it for nodes in a BST.
public void generate(int n, int range) {
}
No comments:
Post a Comment