Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: binary trees

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default binary trees

    Exception in thread "main" java.lang.StackOverflowError
    at D06_Binary_Tree$node.access$3(D06_Binary_Tree.java :22)
    at D06_Binary_Tree.inOrderTraversal(D06_Binary_Tree.j ava:116)


    public class D06_Binary_Tree  
    {  
        /*** CREATE NODE ***/  
        private class node  
        {  
            private String name;  
            private int age;  
            private node left_child;  
            private node right_child;  
     
            /*** Constructor ***/  
            public node()  
            {  
                name = "";  
                age = 07;  
                left_child = null;  
                right_child = null;  
            }  
            public node(String n, int a)  
            {  
                name = n;  
                age = a;  
                left_child = null;  
                right_child = null;  
            }/*** End of constructor ***/  
        }/*** End of Node class ***/  
     
     
     
     
     
        /*** Linked List Constructor ***/  
        private node root_node;  
        private node current_node;  
        private node parent_node;  
        private node new_node;  
     
        public D06_Binary_Tree()  
        {  
            // TODO Auto-generated constructor stub  
        }  
     
     
     
        /*** Insert Method ***/  
        public void insert(String n, int a)  
        {  
            current_node = root_node;  
            parent_node = root_node;  
     
            if(root_node == null)  //if root is empty  
            {  
                root_node = new node(n, a);  
                root_node.left_child = null;  
                root_node.right_child = null;  
            }  
            else  
            {  
                while(true)  
                {  
                    parent_node = current_node; //follow current_node in loop  
                    if(current_node.age > a)    //LEFT HAND SIDE  
                    {  
                        current_node = current_node.left_child;  //move current node  
                        if(current_node == null)                 //if empty  
                        {  
                            new_node = new node(n, a);            //create new node  
                            parent_node.left_child = new_node;    //parent is same as current cude  
                            break;  
                        }  
                    }  
                    else /*** RIGHT SIDE OF TREE ***/  
                    {  
                        current_node = current_node.right_child;  
                        if(current_node == null)  
                        {  
                            new_node = new node(n, a);  
                            parent_node.right_child = new_node;  
                            break;  
                        }  
                    }  
                }  
            }  
        }/*** End of Insert Method ***/  
     
     
     
     
     
        /*** IN ORDER TRAVERSAL METHOD ***/  
        //in order traversal - print all value lowest to heightest  
        public void inOrderTraversal(node current_node)  
        {  
            current_node = root_node;  
     
            if(current_node == null)  
            {  
                System.out.println("Binary Tree is empty!");  
            }  
            else   
            {  
                inOrderTraversal(current_node.left_child);  
                System.out.println(current_node);  
                inOrderTraversal(current_node.right_child);  
            }  
        }/*** END OF IN ORDER TRAVERSAL METHOD ***/  
     
     
     
     
        /*** Main Method ***/  
        public static void main(String[] args)  
        {  
            D06_Binary_Tree m = new D06_Binary_Tree();  
     
            m.insert("a", 50);  
            m.insert("b", 25);  
            m.insert("c", 15);  
            m.insert("d", 30);  
            m.insert("5", 75);  
            m.insert("4", 85);  
            m.inOrderTraversal(m.current_node);  
     
        }  
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: binary trees

    Is there a recursive call that is using up all the stack? Look at the stack trace and see if where the method calls are coming from.
    More of the stack trace needs to be posted.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: January 23rd, 2013, 09:04 PM
  2. [SOLVED] Data Structures: Symbolic Algebra with Trees
    By Staticity in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2012, 01:42 AM
  3. Help in printing trees
    By cool_97 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 23rd, 2011, 04:46 PM
  4. Merging of Trees..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2010, 11:39 PM
  5. PQ Trees
    By copeg in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 19th, 2010, 09:40 PM