Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

Greenhorn
Posts: 21
hi there i have one question and i hope i find the solution from this site

how can i convert the linkedlist to thr binart tree i tried to write the code but i couldnt find the true one
so please if any one have an idea about it reply me soooooooon as possibile u can

thanks alot

Svend Rost
Ranch Hand
Posts: 904
Hi,

Depends on the type of binary tree you wish to make.

Let's make a tree, in which a parent nodes left child is equal or less
the parents value, and the right child has a higher value.

Will become a Binary Tree through the following transfermation (FIFO):
We choose 4 as the root node. The next node is 5, it's larger than 4, and
will be 4's right child. The next node is 2, which is smaller than 4 and
will be 4's left child. 10 is larger than 4, and then we evaluate it to 5
and since 10>5, it'll become 5's right child.

/Svend Rost

Greenhorn
Posts: 21
i understand it but the problem is how can i write the code how can i begin ??please help me i m late in submit it

Svend Rost
Ranch Hand
Posts: 904
Sorry - I wont write your code.

If your not willing to "play along", i.e. showing what code you've
written, and where your stuck ("I can't get the insertion method to

Getting started: Think recursive.. as I explained in my example.
each node has two children.. if node is null, we create a node.

/Svend Rost
[ November 07, 2005: Message edited by: Svend Rost ]

Greenhorn
Posts: 21
i tried to write the code but there still an erro so can u pkease fixed it to me
public void insertValue(int value) {

public void insertValue(int value) {

if (root == null)
root = new TreeNode(value);
else
insertValue(root, value);

}

public void insertValue (TreeNode nodeCheck, int value){

int r = value.compareTo(nodeCheck.getValue());

if (r == 0)
return;

else if (r < 0) {
if (nodeCheck.getLeftNode() == null)
nodeCheck.setLeftNode( new TreeNode(value));
else
insertvalue(nodeCheck.getLeftNode(), value);
}
else {
if (nodeCheck.getRightNode() == null)
nodeCheck.setRightNode( new TreeNode(value));
else
inserValue(nodeCheck.getRightNode(), value);

Svend Rost
Ranch Hand
Posts: 904
i tried to write the code but there still an erro...

Let's look at it:

My advice is to add the recursive step. Let's use two classes.

---
Class BinaryTree
public void insertItem(Object key, int element)
private void placeObject(Object key, int element, BinaryTreeNode node)
private int size()

Class BinaryTreeNode
public BinaryTreeNode(Object key, int element, BinaryTreeNode parent)
public int getKey()
----

BinaryTree.insertItem(Object key, int element)
{
if size is 0 make (key,element) the new root
else call placeObject with (key,element,root)
}
BinaryTree.placeObject(Object key, int element, BinaryTreeNode node)
{
if key <= node.getKey() insert (key,element,node) in Node's left child
unless it exists.. else we'll just recurse the left subtree.
else
.. well.. you do the math!
}