Binary sorting tree: either an empty tree, or a binary tree with the following properties: the left subtree is all smaller than its root node, the values of the right subtree nodes are all greater than the value of the root node, and its left and right subtrees are also Binary sorted tree.
java implementation:
package cn.stone.algorithm; public class BinarySortTree{ class BiTree{ int data; BiTree lchild; BiTree rchild; public BiTree(int data,BiTree lchild,BiTree rchild){ this.data=data; this.lchild=lchild; this.rchild=rchild; } } //Inquire public BiTree search(BiTree tree,int key){ if((tree==null) || key==tree.data) return tree; else if(key<tree.data) return search(tree.lchild, key); else return search(tree.rchild, key); } //Increase BiTree p=null; public boolean getInsertNode(BiTree tree,int key,BiTree f){ if(tree==null){ p=f; return false; }else if(tree.data==key){ p=tree; return false; }else if(tree.data>key){ return getInsertNode(tree.lchild,key,tree); }else{ return getInsertNode(tree.rchild,key,tree); } } public boolean insert(BiTree tree,int key){ if(!getInsertNode(tree,key,null)){ BiTree node=new BiTree(key,null,null); if(p==null){ tree=node; }else if(p.data<key){ p.rchild=node; }else{ p.lchild=node; } return true; } return false; } //delete public boolean deleteBST(BiTree tree,int key){ if(tree==null){ return false; }else if(tree.data==key){ return delete(tree); }else if(tree.data>key){ return deleteBST(tree.lchild,key); }else{ return deleteBST(tree.rchild,key); } } public boolean delete(BiTree tree){ if(tree==null){ return true; }else if(tree.lchild==null){ tree=tree.lchild; }else if(tree.rchild==null){ tree=tree.rchild; }else{ BiTree p=tree; BiTree s=tree.lchild; while(s.rchild!=null){ p=s; s=s.rchild; } tree.data=s.data; if(p!=tree){ p.rchild=s.lchild; }else{ p.lchild=s.lchild; } } return true; } }