references and a binary tree  
Author Message
thx1137





PostPosted: 2004-11-21 22:49:00 Top

java-programmer, references and a binary tree Hello,

I am new to Java. I am filling a binary tree with values using a loop,
so when I need to descend one node deeper at the end of the loop, I
use node=node.right for example. Consider this pseudo code:

//...
while(1) {
node=new Node(new Leaf(),null);
node=node.right;
}

I thought that the node variable will contain reference after this
operation. But it does not, because when I look at the root of the
binary tree, it does not get filled any further after the
node=node.right operation.

So my question is, what actually happens in the node=node.right , I
tought that it should be only copying the references, not the values.
I coded the same in c++ and instead of references I used the pointers
and it worked right as I expected.
 
Neil Campbell





PostPosted: 2004-11-21 23:04:00 Top

java-programmer >> references and a binary tree Milan Stezka wrote:

> Hello,
>
> I am new to Java. I am filling a binary tree with values using a loop,
> so when I need to descend one node deeper at the end of the loop, I
> use node=node.right for example. Consider this pseudo code:
>
> //...
> while(1) {
> node=new Node(new Leaf(),null);
> node=node.right;
> }

It looks as though you are creating a node (with new Node(...)), with a
single child (the new Leaf). Then you assign a reference to this child to
node. Next time around the loop, you create another new Node, and assign a
reference to this to node, thus losing the reference you assigned with
node=node.right.

I'm not entirely certain what you're trying to do; but if you want to add a
new child node on to the end of the tree, you have to assign it to
node.right (or node.left):

node.right = new Node(...);

...but I'm not sure I've really understood your question.

Neil

--
Neil Campbell
batneil[AT]thebatcave[DOT]org[DOT]uk
http://www.thebatcave.org.uk
 
Stefan Schulz





PostPosted: 2004-11-21 23:18:00 Top

java-programmer >> references and a binary tree On 21 Nov 2004 06:48:37 -0800, Milan Stezka <email***@***.com> wrote:

> Hello,
>
> I am new to Java. I am filling a binary tree with values using a loop,
> so when I need to descend one node deeper at the end of the loop, I
> use node=node.right for example. Consider this pseudo code:
>
> //...
> while(1) {
> node=new Node(new Leaf(),null);
> node=node.right;
> }
>
> I thought that the node variable will contain reference after this
> operation. But it does not, because when I look at the root of the
> binary tree, it does not get filled any further after the
> node=node.right operation.

What happens? NullPointerException? StackOverflowErrror?
WannaGoForAWalkError? Endless loops terminating prematurely usually
have some exceptional reason to do so. ;)

> So my question is, what actually happens in the node=node.right , I
> tought that it should be only copying the references, not the values.
> I coded the same in c++ and instead of references I used the pointers
> and it worked right as I expected.

I have no idea. Post your code (at least the relevant portion). So far i
can
only guess what you mean... What does the node constructor do? If i guess
that you mean to create a new Leaf as a left child, and null as the
right child, the right way to do it would be

Node node = new Node(new Leaf(), null);

while (!exit) {
node.right = new Node(new Leaf(), null);
node = node.right;
}

node.right = new Leaf();



--

Whom the gods wish to destroy they first call promising.
 
 
Andrew Thompson





PostPosted: 2004-11-21 23:36:00 Top

java-programmer >> references and a binary tree On Sun, 21 Nov 2004 16:18:09 +0100, Stefan Schulz wrote:

> On 21 Nov 2004 06:48:37 -0800, Milan Stezka <email***@***.com> wrote:
...
>> I thought that the node variable will contain reference after this
>> operation. But it does not, because when I look at the root of the
>> binary tree, it does not get filled any further after the
>> node=node.right operation.
>
> What happens? NullPointerException? StackOverflowErrror?
> WannaGoForAWalkError? ..

My money is on WannaGoForAWalkError. To the OP, it would be
helpful to be specific[1] and precise[2].
[1] <http://www.physci.org/codes/javafaq.jsp#specific>
[2] <http://www.physci.org/codes/javafaq.jsp#exact>

>> So my question is, what actually happens in the node=node.right , I
>> tought that it should be only copying the references, not the values.
>> I coded the same in c++ and instead of references I used the pointers
>> and it worked right as I expected.
>
> I have no idea. Post your code (at least the relevant portion).

The trouble is determining what is relevant*. I generally
recommend a complete SHORT example from start to finish.
<http://www.physci.org/codes/sscce.jsp>

This has various advantages, including that
- trimming code to the bare minimum often reveals the problem
- complete code is much easier to assess, since we can see it
run (or fail) for ourselves.

* I have found that a person's ability to judge what is relevant
is not very good when they do not fully understand what is happening,
which is (almost without fail) the case when someone posts a question.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
 
Milan Stezka





PostPosted: 2004-11-22 3:55:00 Top

java-programmer >> references and a binary tree "Neil Campbell" <email***@***.com> p韘e v diskusn韒 pr韘pevku
news:41a0ae61$0$1077$email***@***.com...
> Milan Stezka wrote:
>
>> Hello,
>>
>> I am new to Java. I am filling a binary tree with values using a loop,
>> so when I need to descend one node deeper at the end of the loop, I
>> use node=node.right for example. Consider this pseudo code:
>>
>> //...
>> while(1) {
>> node=new Node(new Leaf(),null);
>> node=node.right;
>> }
>
> It looks as though you are creating a node (with new Node(...)), with a
> single child (the new Leaf). Then you assign a reference to this child to
> node. Next time around the loop, you create another new Node, and assign
> a
> reference to this to node, thus losing the reference you assigned with
> node=node.right.

Yes you are absollutely right, I have got it to work now. I was refering to
null. I feel embarassed to post such a bad question.

>
> I'm not entirely certain what you're trying to do; but if you want to add
> a
> new child node on to the end of the tree, you have to assign it to
> node.right (or node.left):
>
> node.right = new Node(...);
>
> ...but I'm not sure I've really understood your question.
>
> Neil
>
> --
> Neil Campbell
> batneil[AT]thebatcave[DOT]org[DOT]uk
> http://www.thebatcave.org.uk


 
 
John C. Bollinger





PostPosted: 2004-11-22 23:58:00 Top

java-programmer >> references and a binary tree Andrew Thompson wrote:

> On Sun, 21 Nov 2004 16:18:09 +0100, Stefan Schulz wrote:
>>What happens? NullPointerException? StackOverflowErrror?
>>WannaGoForAWalkError? ..
>
>
> My money is on WannaGoForAWalkError.

No, no, that's the cause of the HeSaysHesNotDeadError that's thrown.

:-)


John Bollinger
email***@***.com
 
 
Sudsy





PostPosted: 2004-11-23 6:30:00 Top

java-programmer >> references and a binary tree John C. Bollinger wrote:
> Andrew Thompson wrote:
<snip>
>> My money is on WannaGoForAWalkError.
>
>
> No, no, that's the cause of the HeSaysHesNotDeadError that's thrown.
>
> :-)

Is that a superclass of the PiningForTheFjordsException?

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
 
John C. Bollinger





PostPosted: 2004-11-23 22:38:00 Top

java-programmer >> references and a binary tree Sudsy wrote:

> John C. Bollinger wrote:
>
>> Andrew Thompson wrote:
>
> <snip>
>
>>> My money is on WannaGoForAWalkError.
>>
>>
>>
>> No, no, that's the cause of the HeSaysHesNotDeadError that's thrown.
>>
>> :-)
>
>
> Is that a superclass of the PiningForTheFjordsException?
>

No, but a lot of people get that confused: the superclass of
PiningForTheFjordsException is HeSaysItsNotDeadException.

Contrast DiedWhileCarvingItException.


John Bollinger
email***@***.com