I am making Binary Search Tree and AVL Tree for an assignment.
I am having problem when I try to add 1,000,000 elements to Binary Search Tree but I can add key-> value pairs to AVL Tree.(There is no problem with AVLTree)
If I Balance Binary Search Tree , there would be NO difference with AVL Tree ??(What is the point if I balance binary search tree it becomes a AVLTree?)
I'm getting error from Binary Search Tree after 15,000 elements inserted:
Exception in thread "main" java.lang.StackOverflowError
Project Definition:
Using these tests and the code created for naive binary search trees, compare
the performance of AVL trees and naive binary search trees for both the time
of search, insert and delete and for tree balance on long sequences. You should
run tests with up to 1000000 elements in your trees.
public class BinaryTreeExample {
public static void main(String[] args) {
new BinaryTreeExample().run();
}
static class Node
{
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
}
public void run() {
Node rootnode = new Node(25);
insert3(rootnode, 50_000);
for (int i = 0; i < 150_000; i++)
insert3(rootnode, i);
System.out.println("Bittaaa");
// System.out.println(getNodesCount(rootnode));
}
protected int getNodesCount(Node root) {
if (root != null) {
int counter = 1;
counter += getNodesCount(root.left);
counter += getNodesCount(root.right);
return counter;
} else
return 0;
}
void insert3(Node node, int value) {
if (value < node.value) {
if (node.left == null)
node.left = new Node(value);
else
insert3(node.left, value);
} else {
if (node.right == null)
node.right = new Node(value);
else
insert3(node.right, value);
}
}
public void printInOrder(Node node) {
if (node != null) {
printInOrder(node.left);
System.out.println(" Traversed " + node.value);
printInOrder(node.right);
}
}
}