【数据结构】二叉排序树
二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree),亦称二叉搜索树。
特点
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
1、若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
2、若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
3、左、右子树也分别为二叉排序树;
4、没有键值相等的节点。
特性
二叉排序树通常采用二叉链表作为存储结构。中序遍历二叉排序树可得到一个依据关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程即是对无序序列进行排序的过程。每次插入的新的结点都是二叉排序树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。搜索、插入、删除的时间复杂度等于树高,期望O(logn),最坏O(n)(数列有序,树退化成线性表,如右斜树)。
查找算法
步骤:
1、若子树为空,查找不成功。
2、二叉树若根结点的关键字值等于查找的关键字,成功。
3、否则,若小于根结点的关键字值,递归查左子树。
4、若大于根结点的关键字值,递归查右子树。
插入算法
步骤:
1、执行查找算法,找出被插结点的父亲结点。
2、判断被插结点是其父亲结点的左、右儿子。将被插结点作为叶子结点插入。
3、若二叉树为空。则首先单独生成根结点。
删除算法
步骤:
1.若*p结点为叶子结点,即PL(左子树)和PR(右子树)均为空树。由于删去叶子结点不破坏整棵树的结构,则只需修改其双亲结点的指针即可。
2.若*p结点只有左子树PL或右子树PR,此时只要令PL或PR直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,作此修改也不破坏二叉排序树的特性。
3.若*p结点的左子树和右子树均不空。在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整。比较好的做法是,找到*p的直接前驱(或直接后继)*s,用*s来替换结点*p,然后再删除结点*s。
二叉排序树的实现练习(Java)
public class BinarySortTree {
private TreeNode root=null;
/**
* 获取树的高度
* @param subTree
* @return
*/
private int height(TreeNode subTree){
if(subTree == null){
return 0;
}else{
int i = height(subTree.leftChild);
int j = height(subTree.rightChild);
return (i>j)?(i+1):(j+1);
}
}
/**
* 获取树的节点数
* @param subTree
* @return
*/
private int size(TreeNode subTree){
if(subTree == null){
return 0;
}else{
return size(subTree.leftChild)+size(subTree.rightChild)+1;
}
}
/**
* 前序遍历
* @param subTree
*/
public void preOrder(TreeNode subTree){
if(subTree!=null){
visted(subTree);
preOrder(subTree.leftChild);
preOrder(subTree.rightChild);
}
}
/**
* 中序遍历
* @param subTree
*/
public void inOrder(TreeNode subTree){
if(subTree!=null){
inOrder(subTree.leftChild);
visted(subTree);
inOrder(subTree.rightChild);
}
}
/**
* 后续遍历
* @param subTree
*/
public void postOrder(TreeNode subTree) {
if (subTree != null) {
postOrder(subTree.leftChild);
postOrder(subTree.rightChild);
visted(subTree);
}
}
public void visted(TreeNode subTree){
System.out.print(subTree.data+",");
}
/**
* 插入
* @param subTree
* @param iv
*/
public void insertNote(TreeNode subTree, int iv){
TreeNode newNode = new TreeNode(iv);
if(subTree == null){
this.root = newNode;
}else if(subTree.data > iv){
if(subTree.leftChild == null){
subTree.leftChild = newNode;
newNode.parent = subTree;
}else{
insertNote(subTree.leftChild, iv);
}
}else if(subTree.data < iv){
if(subTree.rightChild == null){
subTree.rightChild = newNode;
newNode.parent = subTree;
}else{
insertNote(subTree.rightChild, iv);
}
}else{
System.out.println("node has exist.");
}
}
/**
* 查询
* @param subTree
* @param fv
* @return
*/
public boolean findNote(TreeNode subTree, int fv){
if(subTree == null){
return false;
}else if(subTree.data > fv){
return findNote(subTree.leftChild, fv);
}else if(subTree.data < fv){
return findNote(subTree.rightChild, fv);
}else{
return true;
}
}
/**
* 删除节点
* @param subTree
* @param iv
*/
public void deleteNote(TreeNode subTree, int dv){
if(subTree == null){
System.out.println("BST is empty.");
}else if(subTree.data > dv){
deleteNote(subTree.leftChild, dv);
}else if(subTree.data < dv){
deleteNote(subTree.rightChild, dv);
}else{
if(subTree.leftChild == null && subTree.rightChild == null){
/*如果左右子树为空,怎直接删除该节点*/
if(subTree.parent == null){
this.root = null;
}else if(subTree.parent.leftChild == subTree){
subTree.parent.leftChild = null;
subTree.parent = null;
}else if(subTree.parent.rightChild == subTree){
subTree.parent.rightChild = null;
subTree.parent = null;
}
}else if(subTree.leftChild != null && subTree.rightChild == null){
/*如果左子树不为空而右子树为空,则直接用左子树根节点替换删除节点*/
if(subTree.parent == null){
this.root = subTree.leftChild;
}else if(subTree.parent.leftChild == subTree){
subTree.parent.leftChild = subTree.leftChild;
}else if(subTree.parent.rightChild == subTree){
subTree.parent.rightChild = subTree.leftChild;
}
subTree.leftChild.parent = subTree.parent;
subTree.parent = null;
subTree.leftChild = null;
subTree = null;
}else if(subTree.leftChild == null && subTree.rightChild != null){
/*如果左子树为空而右子树不为空,则直接用右子树根节点替换删除节点*/
if(subTree.parent == null){
this.root = subTree.leftChild;
}else if(subTree.parent.leftChild == subTree){
subTree.parent.leftChild = subTree.rightChild;
}else if(subTree.parent.rightChild == subTree){
subTree.parent.rightChild = subTree.rightChild;
}
subTree.rightChild.parent = subTree.parent;
subTree.parent = null;
subTree.rightChild = null;
subTree = null;
}else{
/*左右子树都不为空的情况下,直接找前驱替代P,并释放*/
TreeNode p = subTree.leftChild;
if(p.rightChild == null){
/*P是删除节点的左子树最大值,即前驱,替换删除节点*/
if(subTree.parent == null){
this.root = p;
}else if(subTree.parent.leftChild == subTree){
subTree.parent.leftChild = p;
}else if(subTree.parent.rightChild == subTree){
subTree.parent.rightChild = p;
}
p.parent = subTree.parent;
p.rightChild = subTree.rightChild;
subTree.rightChild.parent = p;
}else{
while(p.rightChild != null){
p = p.rightChild;
}
if(p.leftChild != null){
p.parent.rightChild = p.leftChild;
p.leftChild.parent = p.parent;
p.parent = null;
p.leftChild = null;
}else{
p.parent.rightChild = null;
p.parent = null;
}
/*P是删除节点的左子树最大值(即前驱),替换删除节点*/
if(subTree.parent == null){
this.root = p;
}else if(subTree.parent.leftChild == subTree){
subTree.parent.leftChild = p;
}else if(subTree.parent.rightChild == subTree){
subTree.parent.rightChild = p;
}
p.parent = subTree.parent;
p.leftChild = subTree.leftChild;
subTree.leftChild.parent = p;
p.rightChild = subTree.rightChild;
subTree.rightChild.parent = p;
}
subTree.parent = null;
subTree.leftChild = null;
subTree.rightChild = null;
subTree = null;
}
}
}
/**
* 二叉树的节点数据结构
*/
private class TreeNode{
private int data;
private TreeNode parent = null;
private TreeNode leftChild=null;
private TreeNode rightChild=null;
public TreeNode(int data){
this.data=data;
}
}
public static void main(String[] args) {
int[] tns = {1,3,4,6,7,8,10,13,14};
for(int dv:tns){
BinarySortTree bst = createBST();
System.out.println("===============delete "+dv+" demo=====================");
System.out.println("findNote("+dv+"):"+bst.findNote(bst.root, dv));
System.out.println("before delete inOrder:");
bst.inOrder(bst.root);
System.out.println("");
bst.deleteNote(bst.root, dv);
System.out.println("after delete inOrder:");
bst.inOrder(bst.root);
System.out.println("");
System.out.println("");
}
}
public static BinarySortTree createBST(){
BinarySortTree bst = new BinarySortTree();
bst.insertNote(bst.root, 8);
bst.insertNote(bst.root, 3);
bst.insertNote(bst.root, 10);
bst.insertNote(bst.root, 1);
bst.insertNote(bst.root, 6);
bst.insertNote(bst.root, 14);
bst.insertNote(bst.root, 4);
bst.insertNote(bst.root, 7);
bst.insertNote(bst.root, 13);
return bst;
}
}
运行结果:
===============delete 1 demo=====================
findNote(1):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
3,4,6,7,8,10,13,14,
===============delete 3 demo=====================
findNote(3):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,4,6,7,8,10,13,14,
===============delete 4 demo=====================
findNote(4):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,6,7,8,10,13,14,
===============delete 6 demo=====================
findNote(6):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,7,8,10,13,14,
===============delete 7 demo=====================
findNote(7):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,6,8,10,13,14,
===============delete 8 demo=====================
findNote(8):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,6,7,10,13,14,
===============delete 10 demo=====================
findNote(10):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,6,7,8,13,14,
===============delete 13 demo=====================
findNote(13):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,6,7,8,10,14,
===============delete 14 demo=====================
findNote(14):true
before delete inOrder:
1,3,4,6,7,8,10,13,14,
after delete inOrder:
1,3,4,6,7,8,10,13,