一、定义
著作权归pdai.tech所有。 链接:www.pdai.tech/md/algorith…
在二叉查找树中:
- 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 任意节点的右子树不空,则右子树上所有结点的值均大于(或等于)它的根结点的值;
- 任意节点的左、右子树也分别为二叉查找树。
- 没有键值相等的节点。
二、实现
- 构造节点
BSTree是二叉树,它保存了二叉树的根节点mRoot;mRoot是BSTNode类型,而BSTNode是二叉查找树的节点,它是BSTree的内部类。BSTNode包含二叉查找树的几个基本信息:
- key -- 它是关键字,是用来对二叉查找树的节点进行排序的。
- left -- 它指向当前节点的左孩子。
- right -- 它指向当前节点的右孩子。
- parent -- 它指向当前节点的父结点。
public class BSTree<T extends Comparable<T>> {
private BSTNode<T> mRoot; // 根结点
public class BSTNode<T extends Comparable<T>> {
T key; // 关键字(键值)
BSTNode<T> left; // 左孩子
BSTNode<T> right; // 右孩子
BSTNode<T> parent; // 父结点
public BSTNode(T key, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right) {
this.key = key;
this.parent = parent;
this.left = left;
this.right = right;
}
}
......
}
-
遍历
- 先序遍历
若二叉树非空,则执行以下操作:
访问根结点;先序遍历左子树;先序遍历右子树。
遍历顺序:ABDECF
从上到下,每一个小二叉数都是:根->左->右
/* * 前序遍历"二叉树" */ private void preOrder(BSTNode<T> tree) { if(tree != null) { System.out.print(tree.key+" "); preOrder(tree.left); preOrder(tree.right); } } public void preOrder() { preOrder(mRoot); }- 中序遍历
若二叉树非空,则执行以下操作:
中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树
遍历结果:DBEAFC
从小到大排序(只对BST)
/* * 中序遍历"二叉树" */ private void inOrder(BSTNode<T> tree) { if(tree != null) { inOrder(tree.left); System.out.print(tree.key+" "); inOrder(tree.right); } } public void inOrder() { inOrder(mRoot); }-
后序遍历
若二叉树非空,则执行以下操作:
先遍历左子树,然后遍历右子树,最后访问根结点
遍历结果:DEBFCA
从最底层往上读取(从下到上从左到右)
/* * 后序遍历"二叉树" */ private void postOrder(BSTNode<T> tree) { if(tree != null) { postOrder(tree.left); postOrder(tree.right); System.out.print(tree.key+" "); } } public void postOrder() { postOrder(mRoot); } -
查找
- 递归版本的代码
/* * (递归实现)查找"二叉树x"中键值为key的节点 */ private BSTNode<T> search(BSTNode<T> 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<T> search(T key) { return search(mRoot, key); }- 非递归版本的代码
/* * (非递归实现)查找"二叉树x"中键值为key的节点 */ private BSTNode<T> iterativeSearch(BSTNode<T> 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<T> iterativeSearch(T key) { return iterativeSearch(mRoot, key); } -
最大值和最小值
查找最大值,就是找最右边的结点
查找最小值,就是找最左边的结点
/* * 查找最大结点: 返回tree为根结点的二叉树的最大结点。 */ private BSTNode<T> maximum(BSTNode<T> tree) { if (tree == null) return null; while(tree.right != null) tree = tree.right; return tree; } public T maximum() { BSTNode<T> p = maximum(mRoot); if (p != null) return p.key; return null; } /* * 查找最小结点: 返回tree为根结点的二叉树的最小结点。 */ private BSTNode<T> minimum(BSTNode<T> tree) { if (tree == null) return null; while(tree.left != null) tree = tree.left; return tree; } public T minimum() { BSTNode<T> p = minimum(mRoot); if (p != null) return p.key; return null; } -
前驱和后继
前驱图:
后继图:跟前驱类似
/* * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 */ public BSTNode<T> successor(BSTNode<T> x) { // 如果x存在右孩子,则"x的后继结点"为 "以其右孩子为根的子树的最小结点"。 if (x.right != null) return minimum(x.right); // 如果x没有右孩子。则x有以下两种可能: // (01) x是"一个左孩子" // (02) x是"一个右孩子" //但无论哪种情况,后继节点的寻找方法都是,不断地找父节点,直到当前节点是父节点的左节点 BSTNode<T> y = x.parent; while ((y!=null) && (x==y.right)) { x = y; y = y.parent; } return y; } /* * 找结点(x)的前驱结点。即,查找"二叉树中数据值小于该结点"的"最大结点"。 */ public BSTNode<T> predecessor(BSTNode<T> x) { // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。 if (x.left != null) return maximum(x.left); // 如果x没有左孩子。则x有以下两种可能: // (01) x是"一个右孩子" // (02) x是"一个左孩子" //但无论哪种情况,前驱节点的寻找方法都是,不断地找父节点,直到当前节点是父节点的右节点 BSTNode<T> y = x.parent; while ((y!=null) && (x==y.left)) { x = y; y = y.parent; } return y; } -
插入
/* * 将结点插入到二叉树中 * * 参数说明: * bst 整个tree * z 插入的结点 */ private void insert(BSTree<T> bst, BSTNode<T> z) { int cmp; BSTNode<T> y = null; BSTNode<T> 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; } } -
删除
情况一:被删除的节点有左右子树--前驱顶替
情况二:被删除的节点只有左子树(或右子树)--孩子顶替
情况三:被删除的节点没有子树--直接删除
/* * 删除结点(z),并返回被删除的结点 * 删除并不是真的把节点位置删除了,需要先去寻找真正删除的位置,然后把对应的节点重新绑定(赋值) * 参数说明: * bst 二叉树 * z 删除的结点 */ private BSTNode<T> remove(BSTree<T> bst, BSTNode<T> z) { //暂存节点信息 BSTNode<T> x=null; //真正被删除的节点位置 BSTNode<T> y=null; //1、寻找被删除的位置 if ((z.left == null) || (z.right == null) ) //被删除的节点只有左子树或右子树或者没有子树 y = z; else //被删除的节点有左右子树--先寻找前驱节点 //删除的就是前驱的位置 y = successor(z); //2、重新构造新的节点信息 //有左节点信息,一定是左节点替换(前驱节点替换删除的位置),没有的话就只有右节点了 if (y.left != null) x = y.left; else x = y.right; if (x != null) x.parent = y.parent; //3、重新连接节点(删除需要删除的节点) //如果y的parent为null,那么y就是根 if (y.parent == null) bst.mRoot = x; //被删除的节点只有左子树 else if (y == y.parent.left) //重新连接节点信息 y.parent.left = x; else //重新连接节点信息 y.parent.right = x; //4、复制值x if (y != z) z.key = y.key; //System.out.println("真正删除的节点:"+y); return y; } -
打印
/* * 打印"二叉查找树" * * key -- 节点的键值 * direction -- 0,表示该节点是根节点; * -1,表示该节点是它的父结点的左孩子; * 1,表示该节点是它的父结点的右孩子。 */ private void print(BSTNode<T> 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); } -
销毁
/* * 销毁二叉树 */ private void destroy(BSTNode<T> 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; }
三、代码和测试
完整代码和测试参考:www.pdai.tech/md/algorith…