二叉排序树或者是一棵空树,或者是具有下列性质的
二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的
根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于或
等于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的节点。
插入和创建过程比较简单,我就不说了,这里讨论下删除操作:
在二叉排序树删去一个结点,分三种情况讨论:
-
若*p结点为叶子结点,即PL(左子树)和PR(右子树)均为空树。由于删去叶子结点不破坏整棵树的结构,则可以直接删除此子结点。
-
若*p结点只有左子树PL或右子树PR,此时只要令PL或PR直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,作此修改也不破坏二叉排序树的特性。
-
若*p结点的左子树和右子树均不空。在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整,可以有两种做法:其一是令*p的左子树为*f的左/右(依*p是*f的左子树还是右子树而定)子树,*s为*p左子树的最右下的结点,而*p的右子树为*s的右子树;其二是令*p的直接前驱(或直接后继)替代*p,然后再从二叉排序树中删去它的直接前驱(或直接后继)-即让*f的左子树(如果有的话)成为*p左子树的最左下结点(如果有的话),再让*f成为*p的左右结点的父结点。
我们废话就不多说了,直接上代码
package oj;
public class BinarySortTree {
private BST root;
class BST{
int data;
BST lchild;
BST rchild;
public BST(){}
public BST(int data,BST lchild,BST rchild) {
this.data=data;
this.lchild=lchild;
this.rchild=rchild;
}
public BST(int data) {
this.data=data;
this.lchild=null;
this.rchild=null;
}
}
public void createBST(int []d){
for(int i:d)
insertBST(i);
}
/**
* 插入新节点
* 注意该算法要求新节点的值不能与已有节点的值重复
* @param d
*/
public void insertBST(int d){
BST p=this.root,parent=this.root;
if(p==null){
this.root=new BST(d,null,null);
return;
}
while(p!=null){
parent=p;
if(d<p.data){
p=p.lchild;
}
else if(d>p.data){
p=p.rchild;
}
}
if(d<parent.data){
parent.lchild=new BST(d,null,null);
}
else{
parent.rchild=new BST(d,null,null);
}
System.out.println("insert "+d+" over");
}
public BST searchBST(BST root,int key){
if(root!=null){
if(root.data==key)
return root;
else{
if(key<root.data){
return searchBST(root.lchild,key);
}
else
return searchBST(root.rchild,key);
}
}
else
return null;
}
public void delete(int x){
BST p=root;
BST parent=null;
if(p!=null){
while(p!=null&&p.data!=x){
parent=p;
if(x<p.data){
p=p.lchild;
}
else{
p=p.rchild;
}
}
if(p==null){
System.out.println("没有找到该节点");
return ;
}
else{
//表明已找到该节点。接下来需要分四种情况来讨论
//1、左右子树全部为空
if(p.lchild==null&&p.rchild==null){
if (parent==null){ //此时树只有一个根节点
root=null;
}
else{
if(parent.data>x)
parent.lchild=null;
if(parent.data<x)
parent.rchild=null;
}
}
//2、左子树为空右子树非空
if(p.lchild==null&&p.rchild!=null){
if(p==root){
root=p.rchild;
}
else{
if(parent.data>x)
parent.lchild=p.rchild;
if(parent.data<x)
parent.rchild=p.rchild;
}
}
//3、左子树非空右子树为空
if(p.lchild!=null&&p.rchild==null){
if(p==root){
root=p.lchild;
}
else{
if(parent.data>x)
parent.lchild=p.lchild;
if(parent.data<x)
parent.rchild=p.lchild;
}
}
//4、此时左右子树非空,我们的策略是找出右子树中最小的元素(即左下节点),将该节点的值复制到待删除节点
// 若该节点尚有右孩子,则将该孩子挂到父亲节点的左子树上,若父节点恰好是待删除节点,那么就将该孩子节点挂到待删节点的右子树上
if(p.lchild!=null&&p.rchild!=null){
BST pre=p;
BST s=p.rchild;
while(s.lchild!=null){
pre=s;
s=s.lchild; //这样就可以找到右子树的最左下节点
}
p.data=s.data;
if(s.rchild!=null){
if(pre==p)
p.rchild=s.rchild;
else{
pre.lchild=s.rchild;
}
}
}
}
}
else{
System.out.println("这是一棵空树");
return ;
}
}
public void inOrder(BST root){
if(root!=null){
inOrder(root.lchild);
System.out.print(root.data+" ");
inOrder(root.rchild);
}
}
public static void main(String []args){
BinarySortTree bTree=new BinarySortTree();
int []d={2,6,8,4,12,45,25,14,28};
bTree.createBST(d);
bTree.delete(2);
bTree.inOrder(bTree.root);
}
}
需要注意的是,在C++语言中,我们还能见到一种利用递归进行插入和删除的方法,但那种方法虽然代码较简单,但理解与维护都比较困难,而且效率也不高,因此我们不妨干脆选用非递归方式。