jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes about binary tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about binary tree" Watch "about binary tree" New topic
Author

about binary tree

Manal Ahmad
Greenhorn

Joined: Feb 22, 2005
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

Joined: Oct 23, 2002
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.

Linked List: 4-5-2-10
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.

Does this help you? if not - tell me where your stuck, and I'll reply back.

/Svend Rost
Manal Ahmad
Greenhorn

Joined: Feb 22, 2005
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

Joined: Oct 23, 2002
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
work"), then I cant help you.

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 ]
Manal Ahmad
Greenhorn

Joined: Feb 22, 2005
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

Joined: Oct 23, 2002
Posts: 904
Originally posted by Manal Ahmad:
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!
}


Happy coding im looking forward for your answer

/Svend Rost
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about binary tree