K
Karlo
Hello!
I implemented a tree in Java.
The problem I have is when I use the method insertNode():
//in the main method...
Tree t = new Tree();
//I want to insert number 5 at the root..
t.insertNode( t.getRoot(), 5 );
t.findNode( 5 );//doesn't work because t's root is still null
The problem is that t.getRoot() returns a reference to tree's
root and this reference gets passed by value to method insertNode().
So when I "link" this reference in insertNode() to a
different object ( eg. n = new TreeNode(); ) it's only the n variable
that points to this new object. But I want t's root to point to this
new object too. How can I do this in Java?
Any help would be appreciated. Thanks.
Karlo.
It's not finished yet but the code is below...
class Tree
{
public Tree() { root = null; }
public synchronized void insertNode( TreeNode n, int d )
{
if ( n == null )
{ n = new TreeNode( d );
}
else
n.addChild( d );
}
public synchronized TreeNode getRoot() { return root; }
public synchronized TreeNode findNode( int d )
{
TreeNode pnode = root;
TreeNode leftmost_child = root;
while ( pnode != null )
{ if ( pnode.data == d )
return pnode;
pnode = pnode.right_sibling;
if ( pnode == null )
{ pnode = leftmost_child.left_child;
leftmost_child = pnode;
}
}
return null;
}
public static synchronized int height( TreeNode n )
{
if ( n == null )
return 0;
else
return Math.max( height( n.left_child )+1, height( n.right_sibling ) );
}
private TreeNode root;
class TreeNode
{
public TreeNode( int d )
{
data = d;
parent = left_child = right_sibling = null;
}
public synchronized void addChild( int d )
{
if (left_child == null)
{ left_child = new TreeNode( d );
left_child.parent = this;
}
else
{ TreeNode pnode = left_child;
while (pnode.right_sibling != null)
pnode = pnode.right_sibling;
pnode.right_sibling = new TreeNode( d );
pnode.right_sibling.parent = this;
}
}
public synchronized int getData() { return data; }
protected int data;
protected TreeNode parent;
protected TreeNode left_child;
protected TreeNode right_sibling;
}
}
I implemented a tree in Java.
The problem I have is when I use the method insertNode():
//in the main method...
Tree t = new Tree();
//I want to insert number 5 at the root..
t.insertNode( t.getRoot(), 5 );
t.findNode( 5 );//doesn't work because t's root is still null
The problem is that t.getRoot() returns a reference to tree's
root and this reference gets passed by value to method insertNode().
So when I "link" this reference in insertNode() to a
different object ( eg. n = new TreeNode(); ) it's only the n variable
that points to this new object. But I want t's root to point to this
new object too. How can I do this in Java?
Any help would be appreciated. Thanks.
Karlo.
It's not finished yet but the code is below...
class Tree
{
public Tree() { root = null; }
public synchronized void insertNode( TreeNode n, int d )
{
if ( n == null )
{ n = new TreeNode( d );
}
else
n.addChild( d );
}
public synchronized TreeNode getRoot() { return root; }
public synchronized TreeNode findNode( int d )
{
TreeNode pnode = root;
TreeNode leftmost_child = root;
while ( pnode != null )
{ if ( pnode.data == d )
return pnode;
pnode = pnode.right_sibling;
if ( pnode == null )
{ pnode = leftmost_child.left_child;
leftmost_child = pnode;
}
}
return null;
}
public static synchronized int height( TreeNode n )
{
if ( n == null )
return 0;
else
return Math.max( height( n.left_child )+1, height( n.right_sibling ) );
}
private TreeNode root;
class TreeNode
{
public TreeNode( int d )
{
data = d;
parent = left_child = right_sibling = null;
}
public synchronized void addChild( int d )
{
if (left_child == null)
{ left_child = new TreeNode( d );
left_child.parent = this;
}
else
{ TreeNode pnode = left_child;
while (pnode.right_sibling != null)
pnode = pnode.right_sibling;
pnode.right_sibling = new TreeNode( d );
pnode.right_sibling.parent = this;
}
}
public synchronized int getData() { return data; }
protected int data;
protected TreeNode parent;
protected TreeNode left_child;
protected TreeNode right_sibling;
}
}