File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes inOrder traversal of binary search tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inOrder traversal of binary search tree" Watch "inOrder traversal of binary search tree" New topic
Author

inOrder traversal of binary search tree

Jim Standish
Greenhorn

Joined: Feb 24, 2006
Posts: 5
I'm having difficulty calling my inOrder method. The only thing that
shows is the root. I'm not absolutely sure if the data from my array
even got inserted, so I'll show you what I did to insert as well.

////////here's how I called *.inOrder(Node) from my main method://////////
Node node = new Node();
node = treeThreeData[0]
tree3.inOrder( node ); //The array is what was inserted
// previously to build the tree.

////////////here's the method I'm trying to call in my Tree class://////
public void inOrder ( Node aNode )
{
if (aNode != null)
{
inOrder (aNode.lChild);
aNode.display();
inOrder (aNode.rChild);
}
}

////////////here's the way I inserted:///////////////////
int[] treeThreeData = { 45,55,57,59,60,67,100,101,107 };
for( int i = 0 ; i <= treeThreeData.length - 1 ; i++ )
{
tree3.insert( treeThreeData[i] );
}

///////////////*.insert calls this method in my Tree class://///////////
public void insert (int val)
{
Node newNode = new Node (val);

if (root == null)//If no nodes in tree yet...
{
root = newNode;
}
else
{
Node current = root;
Node parent = root;
while (current != null)
{
parent = current;
if (val < current.iData)
current = current.lChild;
else
current = current.rChild;
}
if (val < parent.iData)
parent.lChild = newNode;
else
parent.rChild = newNode;
}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Your methods and initializing are okay.
When you call inOrder with a new Node that has only a root element then that is all you get back from inOrder. If you send in the root of the tree then you will get back a complete listing of the tree nodes. Note the difference between:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inOrder traversal of binary search tree
 
Similar Threads
Create an object?
recursive method to balance a binary tree
difficulty with binary searches
Tournament Tree help
Path of TreeSelectionEvent