aspose file tools
The moose likes Beginning Java and the fly likes traversing an ordered binary tree Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "traversing an ordered binary tree" Watch "traversing an ordered binary tree" New topic
Author

traversing an ordered binary tree

Johnny Steele
Greenhorn

Joined: Nov 12, 2010
Posts: 12
Hi,

Im trying to build a method that adds a node to this tree. It takes arguments, a number representing the order at which this node needs to be placed in the tree, and the element that the node is to contain.

so for example,

..............25
............./...\
.........16......50
......./...\.........\
...14....20........75

so if I were to add the number 30 it would be at 50.left

if there is a matching duplicate number, I have to return the element of the node being replaced.

this is what I have so far...
its not wright but its a start.
I'm getting nullPointerExceptions on my tests so far.

any advice?

thanks


el Duderino
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3056
    
    1

I suggest you use a recursive method.

Hint: inserting a node in a tree, is the same as inserting the node in either the left or the right subtree, depending on how the node compares to the parent's key.
Johnny Steele
Greenhorn

Joined: Nov 12, 2010
Posts: 12
got it!!! I figured it out. thanks!!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: traversing an ordered binary tree
 
Similar Threads
Help with expression Tree
how to set a node on expression tree
oldskool linked list
Java Linked List
Path of TreeSelectionEvent