Changing references

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;

}
}
 
J

Joona I Palaste

Karlo said:
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...

You'll want to change your design to make the method return the new
root instead of trying to change it itself. I'll see if I can amend
your code to show the necessary changes.
class Tree
{
public Tree() { root = null; }

public synchronized void insertNode( TreeNode n, int d )

Change signature to:
public synchronized TreeNode insertNode(TreeNode n, int d)
{
if ( n == null )
{ n = new TreeNode( d );
}
else
n.addChild( d );

Insert:
return n;
}

public synchronized TreeNode getRoot() { return root; }

Insert method:
public synchronized void setRoot(TreeNode n) { root = n; }
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;

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

Change this to:
Tree t = new Tree();
TreeNode root = t.InsertNode(t.getRoot(), 5);
t.setRoot(root);
t.findNode(5);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top