yes you was right,. i have fix this.
now i want with this code to be able to delete nodes. i have try to do it but i cant set value null. how i can delete a node? for example i want to delete the min and max node. this is what i have done:
public class BinaryNodeWithSize {
BinaryNodeWithSize left;
BinaryNodeWithSize right;
int size;
int s;
int value;
public BinaryNodeWithSize(int value) {
this.value = value;
}
public BinaryNodeWithSize() {
this.value = 0;
}
public int findMin(BinaryNodeWithSize node) {
if(node.left!=null){
findMin(node.left);
}else{s= node.value;}
return s;
}
public int findMax(BinaryNodeWithSize node) {
if(node.right!=null){
findMax(node.right);
}else{s= node.value;}
return s;
}
public void deleteMin(BinaryNodeWithSize node){
if(node.left!=null){
deleteMin(node.left);
}else{node.value=0;}
}
public void deleteMax(BinaryNodeWithSize node){
if(node.right!=null){
deleteMax(node.right);
}else{node.value=0;}
}
public class BinaryTreeTest {
public static void main(String[] args) {
BinaryNodeWithSize root = new BinaryNodeWithSize(5);
System.out.println("Binary Tree Example");
System.out.println(new BinaryNodeWithSize(1).size);
root.size=1;
System.out.println("Building tree with root value " + root.value);
root.insert(root, 1);
root.insert(root, 8);
root.insert(root, 6);
root.insert(root, 3);
root.insert(root, 9);
System.out.println("min is " + root.findMin(root));
System.out.println("max is " + root.findMax(root));
System.out.println("Traversing tree in order");
root.print(root);
root.deleteMin(root);
root.deleteMax(root);
System.out.println("Traversing tree in order");
root.print(root);
System.out.println("min is " + root.findMin(root));
System.out.println("max is " + root.findMax(root));
and i get this
Binary Tree Example
0
Building tree with root value 5
Inserted 1 to left of 5
Inserted 8 to right of 5
Inserted 6 to left of 8
Inserted 3 to right of 1
Inserted 9 to right of 8
min is 1
max is 9
Traversing tree in order
5 size 6
(left child - 1) size 2
(right child - 8) size 3
1 size 2
(right child - 3) size 1
3 size 1
8 size 3
(left child - 6) size 1
(right child - 9) size 1
6 size 1
9 size 1
Traversing tree in order
5 size 6
(left child - 0) size 2
(right child - 8) size 3
0 size 2
(right child - 3) size 1
3 size 1
8 size 3
(left child - 6) size 1
(right child - 0) size 1
6 size 1
0 size 1
min is 0
max is 0
what am i doing wrong.. again?