Results 1 to 4 of 4
  1. #1
    bassam200 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Unhappy root always still null on my binary tree

    XML Code:
    import java.io.*;
    
    class treeNode {
    
        int data;
        treeNode left = null;
        treeNode right = null;
        treeNode parent = null;
    
        treeNode (int value)
        {
            data = value;
        }
    }
    
    
    
    class treeNodes {
    
        treeNode root = null;         
    
        void insert (treeNode node)
        {
            if (root == null) // the value of root is always null
            {           
                root = node;            
                return;
            }
    
            treeNode cur = null;
            treeNode father = null;
    
            while (cur != null)
            {
                father = cur;
                if (cur.data > node.data)
                    cur = cur.left;
                else
                    cur = cur.right;
            }
    
            if (father.data > node.data)
                father.left = node;
            else
                father.right = node;
    
            node.parent = father;
        }
    
        void walkInorder (treeNode n)
        {
            if (n == null)
                return;
    
            walkInorder (n.left);
            System.out.print (n.data + " ");
            walkInorder (n.right);
        }
    }
    
    class binarySearchTree {
    
        public static void main (String [] args) {   
    
            treeNodes obj1 = new treeNodes ( );     
    
            System.out.println ("============ Array Elements ============"); 
    
            int A[] = {3, 5, 7, 9, 8, 6};
    
            for (int i = 0; i < 6; i++)
            {
                treeNode node = new treeNode (A[i]);
                treeNodes obj = new treeNodes ( );
                obj.insert(node);                     
            }      
    
            System.out.println ("\n============ Inorder ============");
            System.out.println (obj1.root.data);
            obj1.walkInorder (obj1.root);        
    
        }
    }
    i'm tried to make binary tree and when i send the first value to insert function it should change the value of root

    In my case the root never change can someone tell me why and how can i solve it

    sorry for my bad english

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default Re: root always still null on my binary tree

    You create a treeNodes (which should be called TreeNodes, id you want to follow proper naming standards, which you should) each time around the loop and add your new treeNode (which should be TreeNode) to it. You add nothing to your original TreeNodes object ('obj1'). That still has a null root.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    bassam200 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: root always still null on my binary tree

    Quote Originally Posted by Tolls View Post
    You create a treeNodes (which should be called TreeNodes, id you want to follow proper naming standards, which you should) each time around the loop and add your new treeNode (which should be TreeNode) to it. You add nothing to your original TreeNodes object ('obj1'). That still has a null root.
    Thanke you so much

    now what can I do to solve this problrm?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default Re: root always still null on my binary tree

    Um, I'd have thought that was clear.
    You are not populating the TreeNode object you later test.
    You are populating 6 other ones, which you do nothing else with.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. binary tree q
    By stuckonjava in forum New To Java
    Replies: 6
    Last Post: 05-21-2012, 05:58 PM
  2. Traversing Binary Tree from Root to each Branch
    By vluong in forum Advanced Java
    Replies: 3
    Last Post: 04-16-2012, 05:28 PM
  3. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 02:08 AM
  4. binary tree
    By ryamz in forum New To Java
    Replies: 2
    Last Post: 08-12-2010, 02:45 AM
  5. Replies: 0
    Last Post: 04-04-2010, 07:40 AM

Tags for this Thread

Posting Permissions

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