Wednesday, October 21, 2009

Binary Search tree using in Java

public class BinarySearchTree_temp {
private BSTNode root;

public BinarySearchTree_temp(){
this(null);
}


public BinarySearchTree_temp(BSTNode node){
root = node;
}

public BSTNode getRoot(){
return root;
}


public BSTNode find(Comparable val){
if(root != null){
return root.findNode(val);
}
return null;
}


public BSTNode removeNode(Comparable val){
return removeNode(new BSTNode(val));
}

public BSTNode removeNode(BSTNode node){
return removeNodeHelper(root, node);
}


private BSTNode removeNodeHelper(BSTNode start, BSTNode node){
if(start == null){
return null;
}
else{
int delta = node.getValue().compareTo(start.getValue());

if(delta <> 0){
return removeNodeHelper(start.getRightChild(), node);
}
else{
if(start.isLeaf()){
return this.removeLeaf(start);
}
else if(start.isTree()){
return this.removeTree(start);
}
else{
return this.removeOneSubTree(start);
}
}
}
}

private BSTNode removeLeaf(BSTNode leaf){
if(leaf.hasParent()){
BSTNode leafParent = leaf.getParent();

leaf.setParent(null);

if(leafParent.hasLeftChild() && leafParent.getLeftChild().equals(leaf)){
leafParent.setLeftChild(null);
}
else{
leafParent.setRightChild(null);
}
}
else{
root = null;
}
return leaf;
}

private BSTNode removeTree(BSTNode tree){
BSTNode replacementNode;
if(heightHelper(tree.getLeftChild()) > heightHelper(tree.getRightChild())){
replacementNode = getMaxHelper(tree.getLeftChild());
}
else{
replacementNode = getMinHelper(tree.getRightChild());
}

if(tree.hasParent()){

if(tree.hasRightChild() && !tree.getRightChild().equals(replacementNode)){
replacementNode.setRightChild(tree.getRightChild());
}

if(tree.hasLeftChild() && !tree.getLeftChild().equals(replacementNode)){
replacementNode.setLeftChild(tree.getLeftChild());
}

BSTNode treeParent = tree.getParent();

if(treeParent.hasLeftChild() && treeParent.getLeftChild().equals(tree)){
treeParent.setLeftChild(replacementNode);
}
else{
treeParent.setRightChild(replacementNode);
}

BSTNode replaceNodeParent = replacementNode.getParent();

if(replaceNodeParent.hasLeftChild()
&& replaceNodeParent.getLeftChild().equals(replacementNode)){
replaceNodeParent.setLeftChild(null);
}
else{
replaceNodeParent.setRightChild(null);
}

replacementNode.setParent(treeParent);
}
else{
BSTNode replaceNodeParent = replacementNode.getParent();

if(replaceNodeParent.hasLeftChild()
&& replaceNodeParent.getLeftChild().equals(replacementNode)){
replaceNodeParent.setLeftChild(null);
}
else{
replaceNodeParent.setRightChild(null);
}

replacementNode.setParent(null);
replacementNode.setRightChild(tree.getRightChild());
replacementNode.setLeftChild(tree.getLeftChild());
tree.getRightChild().setParent(replacementNode);
tree.getLeftChild().setParent(replacementNode);
root = replacementNode;
}
tree.setParent(null);
tree.setRightChild(null);
tree.setLeftChild(null);
return tree;
}

private BSTNode removeOneSubTree(BSTNode start){
if(start.hasParent()){
BSTNode startParent = start.getParent();

if(startParent.hasLeftChild() && startParent.getLeftChild().equals(start)){
BSTNode n = start.hasLeftChild()? start.getLeftChild(): start.getRightChild();
n.setParent(startParent);
startParent.setLeftChild(n);
}
else{
BSTNode n = start.hasLeftChild()? start.getLeftChild(): start.getRightChild();
n.setParent(startParent);
startParent.setRightChild(n);
}
}
else{
if(start.hasLeftChild()){
root = start.getLeftChild();
start.getLeftChild().setParent(null);
}
else{
root = start.getRightChild();
start.getRightChild().setParent(null);
}
}
start.setLeftChild(null);
start.setRightChild(null);
start.setParent(null);
return start;
}


public void insertNode(BSTNode node){
if(root != null){
root.insertNode(node);
}
}


private BSTNode getMaxHelper(BSTNode node){
// if this node has a right child, it is not the maximum
// return the helper function executed on the right child
if(node.hasRightChild()){
return getMaxHelper(node.getRightChild());
}
return node;
}

private BSTNode getMinHelper(BSTNode node){
if(node.hasLeftChild()){
return getMinHelper(node.getLeftChild());
}
return node;
}


public boolean isEmpty(){
return root == null;
}

public void makeEmpty(){
root = null;
}

public int getHeigth(){
return heightHelper(root);
}

private int heightHelper(BSTNode start){
if (start == null){
return 0;
}
else{
return Math.max(heightHelper(start.getLeftChild()), heightHelper(start.getRightChild())) + 1;
}
}

public String toString(){
if(root == null)
return "{EmptyTree}\n";
else
return "\n" + showSub(root, 1) + "\n";
}

private String showSub(BSTNode node, int level){

StringBuffer sb = new StringBuffer();

if(node != null){
sb.append(showSub(node.getRightChild(), level+1));
for(int j = 0; j < tree =" new" tree =" new" i =" 0;" i =" 0;" hash =" 0;" value =" elem;" left =" lf;" right =" rt;" left =" node;" right =" node;" parent =" node;" left ="="" right ="="" delta =" node.getValue().compareTo(value);" left ="="" left =" node;"> 0){
if(right == null){
right = node;
right.setParent(this);
}
else{
right.insertNode(node);
}
}
}

public BSTNode findNode(Comparable val){
int delta = val.compareTo(value);
if(delta <> 0){
return (right != null)? right.findNode(val): null;
}
return this;
}

public BSTNode findNode(BSTNode node){
return findNode(node.getValue());
}


public boolean equals(Object o){
if(o instanceof BSTNode){
BSTNode b = (BSTNode) o;
return b.value.equals(value);
}
return false;
}

public int hashCode(){
if(hash == 0){
hash = value.hashCode();
}
return hash;
}

public String toString(){
return "{" + value.toString() + "}";
}
}

1 comment: