-1

For a HW assignment, I was tasked to add a bunch of methods into a BinarySearchTree class. Two methods I have are balance and InsertTree (I think it should've been named InsertNode). The authors from the textbook provided pseudo code of what the methods should look like. Both methods work with each other; balance is supposed to take an unbalanced tree and insert each element into an array. I believe InsertTree is supposed to take the elements from the array and put them back into the new formed tree.

The BST Class itself is quite large so I don't think posting it is a good idea. But you can find the Source code here under Sample Materials. The code in reference is in the ch07.trees package.

This is my interpretation of the authors pseudo code so far:

ArrayList<T> array = new ArrayList<T>();

public void balance()
// Will read, store, and recreate the tree
{
      Iterator<T> iter = this.iterator();
      int index = 0;
      while(iter.hasNext())
      {
          array.add(iter.next());
                  index++;
      }
      System.out.println(array.toString());
      System.out.println(index);

      tree = new BinarySearchTree<T>();
      tree.InsertTree(0, index -1);
  }

public void InsertTree(int low, int high)
// Will find the mid-point and insert other elements into left and right subtrees
  {
      if (low == high)
      {
          tree.add(array.get(low));
      }
      else if((low + 1) == high)
      {
          tree.add(array.get(low));
          tree.add(array.get(high));
      }
      else
      {
            int mid = (low + high)/2; 
            tree.add(array.get(mid));
            tree.InsertTree(low,mid-1);
            tree.InsertTree(mid+1,high);
      }
  }

I have to use ArrayList because all of the methods are generics of type T. In my driver class I am simply adding an unbalanced set of elements [A, B, C, D, E, F] and index will correctly show I have incremented index to 6. But, when the new tree calls for InsertTree( 0, index - 1), I get this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ch07.trees.BinarySearchTree.InsertTree(BinarySearchTree.java:180)
at ch07.trees.BinarySearchTree.balance(BinarySearchTree.java:163)
at ch07.trees.HWDriver.main(HWDriver.java:67)

Line 163 is tree.InsertTree(0, index -1); and Line 180 is tree.add(array.get(mid));

Seems the problem is involved with the mid-point, but I'm not sure what the issue could be. I'm not an expert at using ArrayLists, so any help on solving this would be much appreciated.

edit:

I believe the problem has been fixed. I put the array that I created back into the balance method instead of outside the method, and added the array to the InsertTree methods arguments. Then, I had to change every conditional output from this.tree.add to this.add. I also moved my BinarySearchTree tree back into the balanced method because before I was getting a NullPointerException.

Whether or not my method is working as intended is still to be determined.

GGz
  • 3
  • 5

2 Answers2

1

Look what happens when you have an empty collection...

int index = 0;
[...]
tree = new BinarySearchTree<T>();
tree.InsertTree(0, index -1);

You are trying to insert something at index (-1). That is not legal.

LowKeyEnergy
  • 652
  • 4
  • 11
  • I have index increment in my while loop and the printout will tell me 6, so shouldn't it bet that I am inserting something at index ( (6) - 1)? – GGz Oct 31 '19 at 19:37
  • @GGz I see no evidence of that, and you also have no main method. Your exception says that you are trying to read from index 2, so I don't believe that you have added 6 elements. – LowKeyEnergy Oct 31 '19 at 20:19
  • I have updated it with my printout statement. Am I not taking the values stored in my array with me when I call for InsertTree? – GGz Oct 31 '19 at 20:33
  • Where is your main method? The output is meaningless without the code that produces it. – LowKeyEnergy Oct 31 '19 at 20:38
  • When you say main method, do you mean my driver class? If so, that's irrelevant. I have a bunch of characters added to a new tree. Then I call for tree.balance(); to balance my tree. – GGz Oct 31 '19 at 20:44
  • I'm not an expert and I never claimed to be. I can post the main method if you insist. – GGz Oct 31 '19 at 20:47
  • I'm not insisting anything. It's a matter of whether or not you want anyone to be able to help you. – LowKeyEnergy Oct 31 '19 at 20:48
  • My driver class has been added – GGz Oct 31 '19 at 20:50
  • How does this even compile? Your recAdd() method only has a return statement under certain conditions. – LowKeyEnergy Oct 31 '19 at 20:57
  • I might have made an error with pasting the code into the editor, return node should be outside of the else statement – GGz Oct 31 '19 at 21:02
  • I can see that you are having some difficulty here, so I am trying to be patient... but obviously you can understand that it is difficult to help you with your code if we can't see what you are doing. We can't guess what your code really looks like. We can only try to trust that you are showing us accurately. Please review all of your code, copy and paste from a source that compiles, format/indent carefully, and then we can try again. – LowKeyEnergy Oct 31 '19 at 21:06
  • If it helps, I did provide a link to the website for my textbook, the sample materials section has the source code that you can see every detail in the ch07.trees package. They intentionally have errors in the code for students to fix. – GGz Oct 31 '19 at 21:09
  • That's a lot of work to expect someone else to do for you. – LowKeyEnergy Oct 31 '19 at 21:12
  • I have added the entire BinarySearchTree class. I removed my hw methods and left the balance and InsertTree methods. – GGz Oct 31 '19 at 21:31
  • OK here is what I see... You have a class called BinarySearchTree. In one or more cases, you refer to the instance using "this" . That is fine, but you also have a member variable of the class called "tree". What is the reason for this? Did you intend to have your BinarySearchTree contain another BinarySearchTree that is not part of the tree's structure? At one point, you perform: tree = new BinarySearchTree(); Is this intentional? You are also using a member variable called "array" to pass values to the balance method. Potentially, any other line of the code can change the value. – LowKeyEnergy Oct 31 '19 at 21:54
  • I am taking the tree I create in my main class and calling balance. Then I am taking the elements from that tree and placing them in an array for temp storage. The new tree I create is for rebuilding the old tree so that it isn't a degenerate tree. I want to find the middle element that I can set for the root and then build the left and right subtrees. At the moment, balance is set to void because I have been testing my array to see if it is actually taking my elements from iterator. I think I need to change void back to "BinarySearchTree balance()" so I can return the new tree. – GGz Oct 31 '19 at 22:06
  • Rather than creating member variables to pass data between methods, it would be better if you create the variables within the methods, and pass them when necessary using method arguments. Then there is no question of if/when they variables are intercepted or modified elsewhere. – LowKeyEnergy Oct 31 '19 at 22:08
0

Here is your answer more concisely:

this.tree = new BinarySearchTree<T>();
this.tree.InsertTree(0, index-1);

So you have created a new, empty tree and stored it in the member variable "tree". You then attempt tell your new, empty tree to insertTree(0, 5) .

LowKeyEnergy
  • 652
  • 4
  • 11
  • OK... now I see it... You have your main BST called "this", and inside it is a new tree called "tree". Each of these BST has an ArrayList called "array"... but you are calling this.tree.InsertTree(0,5). Inside that method, you are expecting array to have 6 elements (because you printed the 6 elements of this.array), but you are wrong about this, because in an instance method of this.tree, you are looking at this.tree.array. It is the "array" of the new tree, which is an empty ArrayList. – LowKeyEnergy Oct 31 '19 at 22:15
  • If you understand and except this answer, please edit your question to remove the portions of your code that are not relevant. It will make the question easier to read and more useful to another user having a similar problem. – LowKeyEnergy Oct 31 '19 at 22:16
  • Sorry no, I still don't quite understand what my problem is. Are you saying that any BST I make will have an empty ArrayList? – GGz Oct 31 '19 at 22:29
  • Yes. You defined "array" as a member of the BST class, so every time you make a new BST, that BST will be born with an empty array. Your mistake was trying to call this.tree.InstertTree and expecting that BST (this.tree) to see the printed array (this.array), but you didn't pass the array in... you allowed the InsertTree method (which should begin with a lowercase letter, by the way) to read the variable "array", which is ITS OWN "array", as in this.tree.array, NOT this.array. You should avoid using array as a member function of BST. Declare it in your balance function and pass to insertTree() – LowKeyEnergy Oct 31 '19 at 22:35
  • Thank you for your patience. I updated the post and added what I did to fix my issue. Have a good day. Happy Halloween! – GGz Oct 31 '19 at 23:18