This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Binary Tree's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Binary Tree Watch "Binary Tree New topic

Binary Tree's

Bix Beiderbecke

Joined: Jun 28, 2004
Posts: 15
Ok this question may not make much since, but here it goes: Say i'm in a Binary Tree and I come to a leaf(child, null). Now i want to replace that leaf with a node, and have the data from the leaf become the right leaf of the new node. here is some code that might make it clear:

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
hmm thats pretty vauge . Personally when I do anything with binary trees. all the nodes and leafs ARE all nodes. Drawing a little picture often helps with these sorts of things.
The idea is that a node will use references to other nodes that correspond to their children: eg:
class Node{
Node leftChild;
Node rightChild;
if the node has no children when its two pointers can point to null;
In general to do what you are doing:
you need the parent node to point to a new node.
and the new node point to the old 'leaf' as a right child.

Hope that helps in general.
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
The left and right (child) references of the newly-created node will be null, or the (static object) called "null node". The newly-created node's parent reference is ans (I presume that is a reference to your parent node).

I would do this, thinking that this method "insert" is a BinaryTree method.

[ August 09, 2004: Message edited by: Elouise Kivineva ]
[ August 09, 2004: Message edited by: Elouise Kivineva ]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Intermediate forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Don't get me started about those stupid light bulbs.
subject: Binary Tree's
It's not a secret anymore!