treeInsert(BinaryTree t, int newItem) is a method that requires two parameters
[I]treeInsert(arrNum); is a method call that has only one parameter.
Error!
Plus: this doesn't do anything
if ( t.root == 0 ) {
// The tree is empty. Set root to point to a new node containing
// the new item. This becomes the only node in the tree.
t = new BinaryTree(newItem);
// NOTE: The left and right subtrees of root
// are automatically set to NULL by the constructor.
// This is important!
return;
You create a new BinaryTree, make t ( a parameter - hence local) a reference to it, then exit. t goes out of scope, the new BinaryTree will be garbage collected. You may have made this mistake because earlier you commentedNote that root is passed by reference
Java does not support parameter passing by reference. All parameters are passed by value.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
System.out.println is your new best friend.
At each stage of your code, put in temporary System.out.println statements that print the values that you have just set/created. That way you can see:
1. Whether the code is being executed at all
2. Whether all the (local) values are being set as you expected.
When you see where it's going wrong you can add more temporary System.out.println's to narrow down the problem until it becomes obvious.
It's no good running the whole program then just looking at the results at the end. If it goes wrong you have no idea where or why.
ps Did you fix the error with new BinaryTree that I described before?
pps:
public boolean isEmpty(){
boolean ai = false;
if(root == 0){
return true;
}return ai;
}
is a very long way to say
public boolean isEmpty(){
return root == 0;
}
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
You still have the same problem with this code:
public void BTreeInsert(BinaryTree myTree, int newItem) {
if ( myTree == null ) {
myTree = new BinaryTree(newItem);
} else ...
myTree is a parameter, a local variable, you cannot change the value of the myTree declared in main(...) by doing anything to the local myTree in BTreeInsert.
Because the only reference to your new BinaryTree is held in the local parameter, that reference will be lost when the method ends, and your new BinaryTree will be garbage collected.
I'm sure this isn'ty what you intended.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
.. but i don't know how
how to do what exactly? I confess that I've been dealing with specific Java syntax problems and not the overall design of your code because I can't really follow it. It looks like your BinaryTree class is really a Node class, and the the root variable is really the node data, and you don't really have a root node for the whole tree, but maybe that's just because I'm confused.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
It';s a long time since I did this stuff at the theoretical level, so I may get this wrong (maybe someone else can confirm? Please?).
A Tree is a collection of Nodes. Each Node contains some data, and links to child Nodes. There is one top-most ("root") Node, and each child has one parent (except the root node). If each Node has two children, it's a binary tree.
To represent it you'd expect a Node class - each instance is 1 node with its data and references to its child nodes. To represent the tree all you need is a reference to the topmost Node - everything else is found by traversing the tree starting from there. It makes sense to have a Tree class (1) to hold the ref to the topmost Node and (2) as a good place to put all the methods that work on the tree (add node, count nodes etc etc). A sub-tree is a tree that's part of another bigger tree, and needs no special classes or code; all you need is a reference to the topmost node of that sub-tree.
I suspect the reason that you're having difficulties with your program structure is that you have 1 class when you really need two.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073