views:

121

answers:

2

So I am currently getting a strange stack overflow exception when i try to run this program, which reads numbers from a list in a data/text file and inserts it into a binary search tree. The weird thing is that when the program works when I have a list of 4095 numbers in random order. However when i have a list of 4095 numbers in increasing order (so it makes a linear search tree), it throws a stack overflow message. The problem is not the static count variable because even when i removed it, and put t=new BinaryNode(x,1) it still gave a stack overflow exception. I tried debugging it, and it broke at if (t == NULL){ t = new BinaryNode(x,count); Here is the insert function.

BinaryNode *BinarySearchTree::insert(int x, BinaryNode *t) {
 static long count=0;
 count++;

 if (t == NULL){ 
  t = new BinaryNode(x,count);
  count=0;
 }
 else if (x < t->key){
  t->left = insert(x, t->left);
 }
 else if (x > t->key){
  t->right = insert(x, t->right);
 }
 else
  throw DuplicateItem();
 return t;
}
+1  A: 

In a language like C++, you cannot use recursive algorithms on tall trees because each function call uses additional space on a limited stack. You must either change your algorithm (use iteration rather than recursion) or use a balanced binary tree structure.

If you have a bounded input (as it appears you do in this case), you can relieve stack pressure by either making the stack bigger (as Andreas suggests) or put less data on the stack. It seems as though insert is a member function of the BinarySearchTree class even though it doesn't reference any other members of the class. If you make insert a static method (or a regular function not in a class), it won't have to push a this pointer on the stack for every function call, and you will be able to get more iterations before overflowing.

Gabe
The problem is i'm supposed to test this program with the 4095 in increasing order. I guess what i'm trying to ask is if there is a way to avoid the stack overflow, while still using recursion?
Jay
You already did test the program, and it crashed. Are you supposed to get a different result? This sounds like homework. Why not just post your actual requirements?
Gabe
I'm supposed to test 36 different data files, write down the total number of nodes and the average search cost for the tree each file makes. My code works for 35 of those data files, except for the one with 4095 numbers in increasing order. It works for 1024 number in increasing order, and 4095 in random order. The count variable that the node is instantiated with is the search cost. The code for insert was already given. I'm hesitant to change it completely just for one data file that is crashing =/
Jay
Unfortunately I tried making the stack bigger..Oddly it didn't do anything. Its difficult to make it static because it references a member of the BinarySearchTree class, called root=/ There is another function member function..that one that i actually call to insert it.void insert(int x) throw(DuplicateItem){ root = insert(x, root); }
Jay
Ah, i had to increase the stack reserve size to 10,000,000 for it to finally work.
Jay
A: 

You can increase the size of the stack. Depending on which compiler you're working with this is done in different ways. For instance in Visual Studio the stack size can be set with the command line option:

/F stacksize
Andreas Brinck
I tried increasing it 200,000, 2000,000 and both times it had the stack overflow exception at the same spot..i.e. it only got up to 747 on both instead of increasing when i increased the size =/
Jay
Jay, are you saying that you are in fact using Visual Studio and that you set the stack size with `/F 2000000`, and yet it ran with fewer iterations? Please tell me you didn't use `/F 200,000`.
Gabe
I increased the stack reserve size in linker to 10,000,000 for it to work(without the commas)
Jay