about binary tree

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

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

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

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

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

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 ]

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 ]

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);

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);

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'm so happy! And I wish to make this tiny ad happy too:

Rocket Oven Kickstarter - from the trailboss

https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
This thread has been viewed 879 times.

All times above are in ranch (not your local) time.

The current ranch time is

Jul 21, 2018 18:48:47.

The current ranch time is

Jul 21, 2018 18:48:47.