Results 1 to 6 of 6
  1. #1
    Igorock is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Full binary tree

    Please, help me to implement full binary tree, insert nodes into it.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    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

  3. #3
    Igorock is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Excuse me please. Give me an advice. I wrote the code:

    PHP 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;
                }
            }
           
        }
    I receive:

    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?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Igorock is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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!

Similar Threads

  1. binary tree
    By ryamz in forum New To Java
    Replies: 2
    Last Post: 08-12-2010, 02:45 AM
  2. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  3. Binary Tree
    By MuslimCoder in forum New To Java
    Replies: 8
    Last Post: 11-19-2009, 05:57 PM
  4. Binary Search Tree
    By anmadie in forum New To Java
    Replies: 5
    Last Post: 11-17-2009, 02:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •