Hi guys I first of all want to apologize for posting this code in java not c++,I am very short on time but when I get a chance I will edit it and turn it into c++ code anyway the logic is the same for both languages just that in c++ you would have to delete the objects when done,I got the code from a c++ book jumping into c++ but converted the code into java as I was working with java,
I understand how it works but at the same time I don't get why it works,ok so lets say I first call the method insert with an empty(null) Node(tree),after the insert method method call newNode now points/refers to that newly created node lets say I repeat this process a few times and to end where I insert the node and the value 7 into the insert method now newNode points/refers to that newly created node,
now I call the search method on my node(tree) I enter my node into the search method and the value of the node I want to find in this case it is 9,so the search method begins
it checks if the node is null which it's not so then it checks if the value is less than the nodes key value which is 7??(because newNode is pointing to the node with the value 7??) this is false so it the returns the search method with the nodes right value which is pointing to null?? and the value the recursive method runs again before it can find a base case, it then again checks if node == null in this case it does because we passed node.right as a arguement and node.right equals null so it returns null,
but this brings me to the point where I'm confused,how does this code work when I search for the number 9 it returns the node with the value 9,and when I type any other value I called insert on it works fine,when I type in a number I didn't call insert on it gives me a null pointer error,so yeah how come this works because I thought newNode is pointing/referring to the last created object which was 7?
thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public class Main {
public static void main(String[] args) {
Node newNode = null;
Tree main = new Tree();
newNode = main.insert(newNode, 5); // newNode now points/refers to
//new Node with data value 5
newNode = main.insert(newNode, 8);
newNode = main.insert(newNode, 2);
newNode = main.insert(newNode, 9);
newNode = main.insert(newNode, 12);
newNode = main.insert(newNode, 35);
newNode = main.insert(newNode, 7); // newNode now points to new Node
//with data value = 7
// So how can the search method work if newNode's right value and left
//value are NULL won't the recursion call just return
// null but yet somehow the code works and finds the node with the
//value
System.out.println(main.search(newNode, 9).data);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
public class Main {
public Node insert(Node tree,int value){
if(tree == null){
Node newNode = new Node();
newNode.data = value;
newNode.left = null;
newNode.right = null;
return newNode;
}
if(value < tree.data){
tree.left = insert(tree.left,value);
}else{
tree.right = insert(tree.right,value);
}
return tree;
}
public Node search(Node tree,int value){
if(tree == null){
return null;
}else if( value == tree.data){
return tree;
}else if(value < tree.data){
return search(tree.left,value);
}else{
return search(tree.right,value);
}
}
}
class Node{
int data;
Node right;
Node left;
}
|