二叉树 - 二叉查找树

0. 定义

又叫二叉排序树(Binary Sort Tree)、二叉搜索树(Binary Search Tree), 即BST

二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:

  1. 若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
  2. 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
  3. 左、右子树也分别为二叉排序树;

1. 插入节点

插入的节点一定是叶子节点,可以直接和节点的key对比,然后递归搜索左/右子树即可

BiTree *insertBST(BiTree *t, int key) {
    if (t == NULL) {
        t = new BiTree();
        t->lchild = t->rchild = NULL;
        t->key = key;
        return t;
    }
    if (key < t->key) {
        t->lchild = insertBST(t->lchild, key);
    } else {
        t->rchild = insertBST(t->rchild, key);
    }

    return t;
}

2. 删除节点

在二叉排序树删去一个结点,分三种情况讨论:

  1. *p结点为叶子结点,即PL(左子树)和PR(右子树)均为空树。由于删去叶子结点不破坏整棵树的结构,则可以直接删除此子结点。
  2. *p结点只有左子树PL或右子树PR,此时只要令PL或PR直接成为其双亲结点*f的左子树(当*p是左子树)或右子树(当*p是右子树)即可,作此修改也不破坏二叉排序树的特性。
  3. *p结点的左子树和右子树均不空。在删去*p之后,为保持其它元素之间的相对位置不变,可按中序遍历保持有序进行调整,可以有两种做法:
    • 3.1 其一是令*p的左子树为*f的左/右(依*p*f的左子树还是右子树而定)子树,*s*p左子树的最右下的结点,而*p的右子树为*s的右子树;
    • 3.2 其二是令*p的直接前驱(或直接后继)替代*p,然后再从二叉排序树中删去它的直接前驱(或直接后继)-即让*f的左子树(如果有的话)成为*p左子树的最左下结点(如果有的话),再让*f成为*p的左右结点的父结点。
bool deleteBST(BiTree *t) {
    BiTree *q, *s;
    //如果只有右子树
    if (t->lchild == NULL) {
        q = t;
        t = t->rchild;
        delete q;
    //如果只有左子树
    } else if (t->rchild == NULL) {
        q = t;
        t = t->lchild;
        delete q;
    //左右子树都不为空
    } else {
        q = t;
        s = t->lchild;
        //找到节点t的前驱
        while (s->rchild) {
            q = s;
            s = s->rchild;
        }
        //保存s的值
        t->key = s->key;
        //此时s的右子树为空,续接s的左子树
        if (q != t) {
            q->rchild = s->lchild;
        } else {
            q->lchild = s->lchild;
        }
        delete s;
    }
    return true;
}

3. 优化

  • avl 树
  • 二叉树 - 红黑树

3. 实现示例

完整代码


#include 

using namespace std;

typedef struct BiTree {
    int key;
    BiTree *lchild;
    BiTree *rchild;
};

//插入节点
BiTree *insertBST(BiTree *t, int key) {
    if (t == NULL) {
        t = new BiTree();
        t->lchild = t->rchild = NULL;
        t->key = key;
        return t;
    }
    if (key < t->key) {
        t->lchild = insertBST(t->lchild, key);
    } else {
        t->rchild = insertBST(t->rchild, key);
    }

    return t;
}

//删除节点
bool deleteBST(BiTree *t) {
    BiTree *q, *s;
    if (t->lchild == NULL) {
        q = t;
        t = t->rchild;
        delete q;
    } else if (t->rchild == NULL) {
        q = t;
        t = t->lchild;
        delete q;
    } else {
        q = t;
        s = t->lchild;
        while (s->rchild) {
            q = s;
            s = s->rchild;
        }
        t->key = s->key;
        if (q != t) {
            q->rchild = s->lchild;
        } else {
            q->lchild = s->lchild;
        }
        delete s;
    }
    return true;
}

//中序遍历
void print(BiTree *t) {
    if (t != NULL) {
        if (t->lchild != NULL) {
            print(t->lchild);
        }
        printf("%3d", t->key);
        if (t->rchild != NULL) {
            print(t->rchild);
        }
    }
}

int main(int argc, const char *argv[]) {
    int n, m;
    BiTree *t = NULL;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> m;
        t = insertBST(t, m);
    }
    print(t);
    return 0;
}

你可能感兴趣的:(二叉树 - 二叉查找树)