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 4 of 4

Thread: BinarySearchTree - Having trouble adding elements to a vector from the tree. Recursive method! Help :)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BinarySearchTree - Having trouble adding elements to a vector from the tree. Recursive method! Help :)

    Hi, im trying to implement code for a binary search tree. This method should take all the elements in the tree and add them to a vector sorted with lowest first, the elements in the tree are inorder.

    It gives me java.lang.ArrayIndexOutOfBoundsException at a[temp]=n.element;

    /*
    	 * Adds all elements from the tree rooted at n in inorder to the array a
    	 * starting at a[index].
    	 * Returns the index of the last inserted element + 1 (the first empty
    	 * position in a).
    	 */
    	private int toArray(BinaryNode<E> n, E[] a, int index) {
     
    		return addArray(n, a, index);
     
    	}
     
    	private int addArray(BinaryNode<E> n, E[] a, int index){
    		if(n != null && index>=0 ){
    			int temp = index;
    		    temp = addArray(n.left, a, temp);
    			a[temp]=n.element;
    			temp++;
    			temp = addArray(n.right, a, temp);
    			return temp;
    		} else {
    			return index;
    	}}

    Its a part of another public method that rebuilds the tree. The purpose of the rebuild method is to make the tree balanced.

    /** 
    	 * Builds a complete tree from the elements in the tree.
    	 */
    	public void rebuild() {
    		E[] a = (E[]) new Comparable[size];
    		int first= 0;
    		int last = toArray(root, a, first);
    		root= buildTree(a, first, last-1 );
     
    	}

    And this is my main program where i test to add elements (ints) to the tree and then rebuild it:

    public static void main(String[] args){
     
    	BSTVisualizer bstv = new BSTVisualizer("Fint träd", 500, 500);
    	BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
    //	for(int i=3; i<10; i++){
    //	tree.add(i);
    //	tree.add(i);
    //	}
    	tree.add(4);
    	tree.add(5);
    	tree.add(3);
    	tree.add(1);
    	tree.add(1);
    	tree.add(2);
    	tree.add(8);
    	tree.add(7);
    	tree.rebuild();
    	bstv.drawTree(tree);
     
    	System.out.println(tree.height());
    	System.out.println("//");
    	System.out.println(tree.size());
    	System.out.println("//");
    	tree.printTree();
     
     
    }
    }


    I appreciate any help i receive! Ty!


  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: BinarySearchTree - Having trouble adding elements to a vector from the tree. Recursive method! Help :)

    its not working
    Please explain what "not working" means. There are many ways for code to not work.

    How can the code be compiled and executed for testing?


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BinarySearchTree - Having trouble adding elements to a vector from the tree. Recursive method! Help :)

    Quote Originally Posted by Norm View Post
    Please explain what "not working" means.
    Sorry, ive edited the post now

  4. #4
    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: BinarySearchTree - Having trouble adding elements to a vector from the tree. Recursive method! Help :)

    java.lang.ArrayIndexOutOfBoundsException
    Why is the index to the array past the end of the array?
    Remember that the range of index for an array is 0 to the length-1.

    The code should test for invalid indexes and not use them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 01:08 AM
  2. Binary Tree Longest Increasing Path (Recursive)
    By varsis in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 13th, 2012, 10:19 PM
  3. Replies: 3
    Last Post: January 6th, 2012, 09:18 AM
  4. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM