I am trying to use a recursive method to create a binary search tree with 1,000,000 nodes, each containing a number from 0 to 1,000,000. The root must contain 500,000, with its left child containing 250,000 and right child containing 750,000. This pattern will continue with each node until all numbers are used. I need to then return the root to allow the program to search for a specific number. However, I keep getting a stack overflow error. When I change the numbers to 0 and 2 to test it on a smaller scale, it keeps returning the node with 2, rather than with 1. Any ideas?
public static BTNode treeMaker(int minimum, int maximum)
{
int currentNum = (minimum + maximum)/2;
BTNode node = new BTNode(currentNum, null, null);
if(minimum == maximum)
return node;
else
{
node.setLeft(treeMaker(minimum, currentNum - 1));
node.setRight(treeMaker(currentNum + 1, maximum));
return node;
}
}
Edit: Here is the BTNode class:
public class BTNode
{
private static int data;
private static BTNode left, right;
public BTNode(int newData, BTNode newLeft, BTNode newRight)
{
data = newData;
left = newLeft;
right = newRight;
}
public static BTNode getLeft()
{
return left;
}
public static BTNode getRight()
{
return right;
}
public static int getData()
{
return data;
}
public static void setLeft(BTNode newLeft)
{
left = newLeft;
}
public static void setRight(BTNode newRight)
{
right = newRight;
}
}