I have been given this interface,
interface BinarySearchTree {
public void insert(Integer data);
public int size();
public int height();
public boolean contains(Integer target);
}
and I have to implement BST with all these functions. I have implemented the first insert and size like this way -
class Node {
Node left, right, next;
Integer data;
Node () {
left = right = null;
data = 0;
}
}
public class BSTree extends Node implements BinarySearchTree {
static Node root;
static int countNode;
/**
* Creates a new instance of BSTree
*/
public BSTree() {
root = null;
}
public void insert(Integer data) {
if (root == null) {
root.data = data;
countNode++;
} else {
Node temp = new Node();
temp = root;
while (temp != null) {
if (temp.data < data) temp = temp.right;
else {
temp = temp.left;
}
temp.data = data;
countNode++;
}
}
}
public int size () {
return countNode;
}
public int height() {
Node temp = new Node();
/* could have used these for recursion purposes
final boolean flag = true;
if (flag) */
temp = root;
if (temp == null) {
return 0;
} else {
/* would have been easy to find the height with recursion in the following way
return 1 + max(height(temp.left), height(temp.right)); */
}
}
public boolean contains (Integer target) {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BSTree bs = new BSTree();
bs.insert(12);
bs.insert(3);
bs.insert(14);
}
}
The objective requires that the height be implemented without using an argument. Do you have some ideas?