I have implemented the Binary Search Tree in Java and would love to get reviews. I would like to have some comments on multi thread implantation.
Note: Corrected Code is in the Answers
package com.atleyvirdee.myDataStructures.tree.binarytree;
import com.atleyvirdee.myDataStructures.tree.ITree;
import com.atleyvirdee.myDataStructures.tree.ITreeNode;
import com.atleyvirdee.myDataStructures.tree.DFTreeTraverser;
@SuppressWarnings ( {"rawtypes", "unchecked"} )
public class BinaryTree implements ITree {
private ITreeNode root;
public DFTreeTraverser treeTraverser = new DFTreeTraverser(root);
@Override
public void insert( Comparable data ) {
root = insert(data, root);
}
@Override
public void remove( Comparable data ) {
if ( isEmpty() )
System.out.println("Tree Empty");
else if ( find(data) == null )
System.out.println("Sorry " + data + " is not present");
else {
root = remove(data, root);
}
}
@Override
public void removeMin() {
ITreeNode minimumNode = findMin(root);
Comparable data = minimumNode.getData();
remove(data);
}
@Override
public Comparable find( Comparable data ) {
ITreeNode dataNode = find(data, root);
return (dataNode != null) ? dataNode.getData() : null;
}
@Override
public Comparable findMin() {
ITreeNode minimumNode = findMin(root);
return minimumNode.getData();
}
@Override
public Comparable findMax() {
ITreeNode maximumNode = findMax(root);
return maximumNode.getData();
}
@Override
public boolean isEmpty() {
return root == null ? true : false;
}
@Override
public void makeEmpty() {
this.root = null;
}
protected ITreeNode findMax( ITreeNode node ) {
while (node.hasRightNode()) {
node = node.getRightNode();
}
return node;
}
protected ITreeNode findMin( ITreeNode node ) {
while (node.hasLeftNode()) {
node = node.getLeftNode();
}
return node;
}
protected ITreeNode find( Comparable data, ITreeNode node ) {
if ( node == null ) return null;
if ( node.getData().compareTo(data) == 0 ) return node;
if ( node.getData().compareTo(data) < 0 ) return find(data, node.getRightNode());
return find(data, node.getLeftNode());
}
protected ITreeNode insert( Comparable data, ITreeNode node ) {
ITreeNode n;
if ( node == null ) {
node = new BinaryTreeNode(data);
} else if ( data.compareTo(node.getData()) < 0 ) {
n = insert(data, node.getLeftNode());
node.setLeftNode(n);
} else if ( data.compareTo(node.getData()) > 0 ) {
n = insert(data, node.getRightNode());
node.setRightNode(n);
} else {
throw new RuntimeException("Data already in the Tree.");
}
return node;
}
protected ITreeNode remove( Comparable data, ITreeNode node ) {
ITreeNode maxInLeftTree, newLeftChild, n;
if ( node == null ) {
return null;
}
if ( node.getData() == data ) {
ITreeNode leftChild = node.getLeftNode();
ITreeNode rightChild = node.getRightNode();
if ( leftChild == null && rightChild == null ) // BOTH of the Child are null and Value
// matched.
return null;
else if ( leftChild == null ) {
return rightChild;
} else if ( rightChild == null ) {
return leftChild;
} else {
maxInLeftTree = findMax(leftChild);
Comparable value = maxInLeftTree.getData();
newLeftChild = remove(value, leftChild);
node.setData(value);
node.setLeftNode(newLeftChild);
return node;
}
}
if ( data.compareTo(node.getData()) < 0 ) {
n = remove(data, node.getLeftNode());
node.setLeftNode(n);
} else {
n = remove(data, node.getRightNode());
node.setRightNode(n);
}
return node;
}
@Override
public DFTreeTraverser getTraverser() {
treeTraverser.setRoot(root);
return treeTraverser;
}
class BinaryTreeNode implements ITreeNode {
ITreeNode leftNode = null;
ITreeNode rightNode = null;
Comparable data = null;
public void setLeftNode( ITreeNode leftNode ) {
this.leftNode = leftNode;
}
public void setRightNode( ITreeNode rightNode ) {
this.rightNode = rightNode;
}
@Override
public ITreeNode getLeftNode() {
return this.leftNode;
}
@Override
public ITreeNode getRightNode() {
return this.rightNode;
}
@Override
public Comparable getData() {
return this.data;
}
@Override
public boolean hasLeftNode() {
return (this.leftNode != null) ? true : false;
}
@Override
public boolean hasRightNode() {
return (this.rightNode != null) ? true : false;
}
public BinaryTreeNode( Comparable data ) {
this.data = data;
}
@Override
public String toString() {
return this.data.toString();
}
@Override
public void setData( Comparable data ) {
this.data = data;
}
}
@Override
public int size() {
return size(root);
}
public int size(ITreeNode node) {
int leftSize = 0,rightSize =0;
if(node==null)
return 0;
if(node.hasLeftNode())
leftSize = size(node.getLeftNode());
if(node.hasRightNode())
rightSize = size(node.getRightNode());
return leftSize+rightSize+1;
}
@Override
public ITreeNode getRoot() {
return root;
}
}