BinaryNode类
/**
* Created by root on 16-3-5.
*/
public class BinaryNode<E extends Comparable> implements Parent {
private E item;
private BinaryNode left;
private BinaryNode right;
public BinaryNode(){
this(null);
}
public BinaryNode(E item){
this.item=item;
}
public BinaryNode getChild(int comparsion){
if(comparsion<0){
return left;
}
else {
return right;
}
}
public void setChild(int comparison,BinaryNode child){
if(comparison<0){
left=child;
}
else {
right=child;
}
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public E getItem() {
return item;
}
public void setItem(E item) {
this.item = item;
}
}
Parent interface
/**
* Created by root on 16-3-5.
*/
public interface Parent {
public BinaryNode getChild(int comparison);
public void setChild(int comparison,BinaryNode child);
}
SearchTree类
/**
* Created by root on 16-3-5.
*/
public class SearchTree<E extends Comparable> implements Parent {
private BinaryNode root;
public BinaryNode getChild(int comparison){
return root;
}
public void setChild(int comparison,BinaryNode child){
root=child;
}
public SearchTree(E item){
root=new BinaryNode(item);
}
public SearchTree(){
root=null;
}
public boolean add(E target){
Parent prev=this;
BinaryNode node=root;
int comparison=0;
while(node!=null){
comparison =target.compareTo(node.getItem());
if(comparison==0){
return false;
}
else {
prev=node;
node=node.getChild(comparison);
}
}
prev.setChild(comparison,new BinaryNode(target));
return true;
}
public boolean contains(E target){
BinaryNode node =root;
while(node!=null){
int comparison=target.compareTo(node.getItem());
if(comparison==0){
return true;
}
else {
node=node.getChild(comparison);
}
}
return false;
}
public boolean remove(E target){
Parent prev=this;
BinaryNode node=root;
while(node!=null){
int comparison=target.compareTo(node.getItem());
if(comparison==0){
spilt((BinaryNode) prev,node);
}
else {
prev=node;
node=node.getChild(comparison);
}
}
return false;
}
protected void spilt(BinaryNode prev,BinaryNode node){
int direction=node.getItem().compareTo(prev.getItem());
if(node.getLeft()==null){
prev.setChild(direction,node.getRight());
}
else if(node.getRight()==null){
prev.setChild(direction,node.getLeft());
}
else {
BinaryNode inorder=node.getRight();
while(inorder.getLeft()!=null){
inorder=inorder.getLeft();
}
node.setItem(inorder.getItem());
inorder=null;
}
}
public E findMax() {
return findMax(root);
}
public E findMin() {
return findMin(root);
}
public E findMax(BinaryNode node) {
if (node.getRight() == null) {
return (E) node.getItem();
} else {
return findMax(node.getRight());
}
}
public E findMin(BinaryNode node) {
if (node.getLeft() == null) {
return (E) node.getItem();
} else {
return findMin(node.getLeft());
}
}
}
测试类
/**
* Created by root on 16-3-5.
*/
public class Test {
public static void main(String[] args){
SearchTree tree=new SearchTree();
tree.add(7);
tree.add(3);
tree.add(9);
tree.add(13);
tree.add(1);
tree.add(5);
tree.add(6);
tree.add(4);
System.out.println(tree.contains(5));
tree.remove(3);
tree.remove(5);
}
}