• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Binary Tree's

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

thanks
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic