数据结构学习笔记五:二叉查找树

数据结构学习笔记五:二叉查找树

二叉查找树简介

二叉查找树(Binary Search Tree),又被称为二叉搜索树。
它是特殊的二叉树:对于二叉树,假设x为二叉树中的任意一个结点,x节点包含关键字key,节点x的key值记为key[x]。如果y是x的左子树中的一个结点,则key[y] <= key[x];如果y是x的右子树的一个结点,则key[y] >= key[x]。那么,这棵树就是二叉查找树。如下图所示:
数据结构学习笔记五:二叉查找树_第1张图片
在二叉查找树中:
(01) 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(02) 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(03) 任意节点的左、右子树也分别为二叉查找树。
(04) 没有键值相等的节点(no duplicate nodes)。

二叉查找树的实现

1. 二叉查找树节点的定义

public class BSTree>{
	private BSTNode mRoot;

	public class BSTNode>{
		T key;
		BSTNode left;
		BSTNode right;
		BSTNode parent;
		
		public BSTNode(T key, BSTNode parent, BSTNode left, BSTNode right){
			this.key = key;
			this.parent = parent;
			this.left = left;
			this.right =right;
		}
	}
}

BSTree是二叉树,它保护了二叉树的根节点mRoot;mRoot是BSTNode类型,而BSTNode是二叉查找树的节点,它是BSTree的内部类。BSTNode包含二叉查找树的几个基本信息:
(01) key – 它是关键字,是用来对二叉查找树的节点进行排序的。
(02) left – 它指向当前节点的左孩子。
(03) right – 它指向当前节点的右孩子。
(04) parent – 它指向当前节点的父结点。

2.遍历

前序遍历

若二叉树非空,则执行以下操作:
(01) 访问根结点;
(02) 先序遍历左子树;
(03) 先序遍历右子树。
根–左--右

private void preOrder(BSTNode tree){
	if(tree != null){
		System.out.print(tree.key + " ");
		preOrder(tree.left);
		preOrder(tree.right);
	}
}

public void preOrder(){
	preOrder(mRoot);
}

中序遍历

若二叉树非空,则执行以下操作:
(01) 中序遍历左子树;
(02) 访问根结点;
(03) 中序遍历右子树。
左–根--右

private void inOrder(BSTNode tree) {
    if(tree != null) {
        inOrder(tree.left);
        System.out.print(tree.key+" ");
        inOrder(tree.right);
    }
}

public void inOrder() {
    inOrder(mRoot);
}

后序遍历

若二叉树非空,则执行以下操作:
(01) 后序遍历左子树;
(02) 后序遍历右子树;
(03) 访问根结点。
左–右--根

private void postOrder(BSTNode tree) {
    if(tree != null)
    {
        postOrder(tree.left);
        postOrder(tree.right);
        System.out.print(tree.key+" ");
    }
}

public void postOrder() {
    postOrder(mRoot);
}

3.查找

递归:

private BSTNode search(BSTNode x, T key){
	if(x==null){
		return x;
	}

	int cmp = key.compareTo(x.key);
	if(cmp < 0){
		return search(x.left, key);
	}else if(cmp > 0){
		return search(x.right, key);
	}else{
		return x;
	}
}

public BSTNode search(T key){
	return search(mRoot, key);
}

非递归:

private BSTNode iterativeSearch(BSTNode x, T key){
	while(x!=null){
		int cmp = key.compareTo(x.key);

		if(cmp<0){
			x = x.left;
		}else if(cmp>0){
			x = x.right;
		}else{
			return x;
		}
		return x;
}

public BSTNode iterativeSearch(T key){
	return iterativeSearch(mRoot, key);
}

4.最大值和最小值

最大值:

private BSTNode maximum(BSTNode tree){
	if(tree==null){
		return null;
	}
	while(tree.right!=null){
		tree = tree.right;
	}
	return tree;
}

public T maximum(){
	BSTNode p = maximum(mRoot);
	if(p!=null){
		return p.key;
	}
	return null;
}

最小值

private BSTNode minimum(BSTNode tree){
	if(tree==null){
		return null;
	}
	while(tree.left!=null){
		tree = tree.left;
	}
	return tree;
}

public T minimum(){
	BSTNode p = minimum(mRoot);
	if(p!=null){
		return p.key;
	}
	return null;
}

5.前驱和后继

节点的前驱:是该节点的左子树中的最大节点。
节点的后继:是该节点的右子树中的最小节点。

查找前驱节点:

public BSTNode predecessor(BSTNode x){
	if(x.left!=null){
		return maximum(x.left);
	} // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。

	// 如果x没有左孩子。则x有以下两种可能:
    // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。
    // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。
    BSTNode y = x.parent;
    while((y!=null)&&(x==y.left)){
    	x = y;
    	y = y.parent;
    }
    return y;

查找后继节点:

public BSTNode successor(BSTNode x){
	// 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。
	if(x.right!=null){
		return minimum(x.right);
	}

    // 如果x没有右孩子。则x有以下两种可能:
    // (01) x是"一个左孩子",则"x的后继结点"为 "它的父结点"。
    // (02) x是"一个右孩子",则查找"x的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"x的后继结点"。
    BSTNode y = x.parent;
    while ((y!=null) && (x==y.right)) {
        x = y;
        y = y.parent;
    }

    return y;
 }

6.插入

private void insert(BSTree bst, BSTNode z) {
    int cmp;
    BSTNode y = null;
    BSTNode x = bst.mRoot;

    // 查找z的插入位置
    while (x != null) {
        y = x;
        cmp = z.key.compareTo(x.key);
        if (cmp < 0)
            x = x.left;
        else
            x = x.right;
    }

    z.parent = y;
    if (y==null)
        bst.mRoot = z;
    else {
        cmp = z.key.compareTo(y.key);
        if (cmp < 0)
            y.left = z;
        else
            y.right = z;
    }
}

/* 
 * 新建结点(key),并将其插入到二叉树中
 *
 * 参数说明:
 *     tree 二叉树的根结点
 *     key 插入结点的键值
 */
public void insert(T key) {
    BSTNode z=new BSTNode(key,null,null,null);

    // 如果新建结点失败,则返回。
    if (z != null)
        insert(this, z);
}

7.删除

/* 
 * 删除结点(z),并返回被删除的结点
 *
 * 参数说明:
 *     bst 二叉树
 *     z 删除的结点
 */
private BSTNode remove(BSTree bst, BSTNode z) {
    BSTNode x=null;
    BSTNode y=null;

    if ((z.left == null) || (z.right == null) )
        y = z;
    else
        y = successor(z);

    if (y.left != null)
        x = y.left;
    else
        x = y.right;

    if (x != null)
        x.parent = y.parent;

    if (y.parent == null)
        bst.mRoot = x;
    else if (y == y.parent.left)
        y.parent.left = x;
    else
        y.parent.right = x;

    if (y != z) 
        z.key = y.key;

    return y;
}

/* 
 * 删除结点(z),并返回被删除的结点
 *
 * 参数说明:
 *     tree 二叉树的根结点
 *     z 删除的结点
 */
public void remove(T key) {
    BSTNode z, node; 

    if ((z = search(mRoot, key)) != null)
        if ( (node = remove(this, z)) != null)
            node = null;
}

8.打印

/*
 * 打印"二叉查找树"
 *
 * key        -- 节点的键值 
 * direction  --  0,表示该节点是根节点;
 *               -1,表示该节点是它的父结点的左孩子;
 *                1,表示该节点是它的父结点的右孩子。
 */
private void print(BSTNode tree, T key, int direction) {

    if(tree != null) {

        if(direction==0)    // tree是根节点
            System.out.printf("%2d is root\n", tree.key);
        else                // tree是分支节点
            System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction==1?"right" : "left");

        print(tree.left, tree.key, -1);
        print(tree.right,tree.key,  1);
    }
}

public void print() {
    if (mRoot != null)
        print(mRoot, mRoot.key, 0);
}

9.销毁

/*
 * 销毁二叉树
 */
private void destroy(BSTNode tree) {
    if (tree==null)
        return ;

    if (tree.left != null)
        destroy(tree.left);
    if (tree.right != null)
        destroy(tree.right);

    tree=null;
}

public void clear() {
    destroy(mRoot);
    mRoot = null;
}

你可能感兴趣的:(DataStructure)