# Full binary tree

• 12-23-2010, 08:27 AM
Igorock
Full binary tree
Please, help me to implement full binary tree, insert nodes into it.
• 12-23-2010, 02:55 PM
KevinWorkman
No. That's not how this works. We aren't a code mill or a homework service. That would cost money. How much are you offering to pay?

Read this before you post again: How To Ask Questions The Smart Way
• 12-23-2010, 03:15 PM
Igorock

Code:

```public class Main {     public static void main(String[] args)     {         new Main().run();     }     static class Node   {         Node left;         Node right;         Node buf;         int index;         int value;         public Node(int value) {             this.value = value;             //System.out.println(" Node Value " + this.value);         }     }     public void run() {         Node rootnode = new Node(25);         System.out.println("Building tree with rootvalue " + rootnode.value);         System.out.println("=================================");         insert(rootnode, 11);         insert(rootnode, 15);         insert(rootnode, 16);         insert(rootnode, 23);         insert(rootnode, 45);         insert(rootnode, 36);     }     public void insert(Node node, int value) {         // System.out.println("  Value " + node.value);         if (node.index==1) {             if (node.right != null) {                 node.buf = node.right;                 insert(node.right, value);             } else {                 System.out.println("  Inserted " + value +                                 " to right of node " + node.value);                 node.right = new Node(value);                 node.index = 0;             }         } else {             if (node.left != null) {                 insert(node.left, value);             } else {                 System.out.println("  Inserted " + value + " to left of node " + node.value);                 node.left = new Node(value);                 node.index = 1;             }         }           }```

Inserted 11 to left of node 25
Inserted 15 to right of node 25
Inserted 16 to left of node 11
Inserted 23 to right of node 11
Inserted 45 to left of node 16
Inserted 36 to right of node 16

OR:

25
/ \
11 15
/ \
16 23
/ \
45 36

BUT I NEED:

25
/ \
11 15
/ \ / \
16 23 45 36

How can I do it?
• 12-23-2010, 03:21 PM
KevinWorkman
How would you do it "by hand" without worrying about the code? Write out the steps, as specifically as possible, so that you could give the instructions to a complete stranger and have them do the correct thing.

When you have that finished, it should be pretty easy to convert to code.

That'll be five dollars.
• 12-23-2010, 03:26 PM
JosAH
The nodes of a full binary search tree can be stored in an array where the root is stored at index #0 and the children of node #i are stored at indexes #(2*i+1) and #(2*i+2). Inserting a node is done by doing a binary search over the array and inserting the node at the wanted position.

kind regards,

Jos
• 12-23-2010, 03:43 PM
Igorock
Quote:

Originally Posted by JosAH
The nodes of a full binary search tree can be stored in an array where the root is stored at index #0 and the children of node #i are stored at indexes #(2*i+1) and #(2*i+2). Inserting a node is done by doing a binary search over the array and inserting the node at the wanted position.

kind regards,

Jos

Thank you, JosAH! You have helped me very much!