【二叉查找树】c实现

来源:互联网 发布:爱有来生知乎 编辑:程序博客网 时间:2024/05/16 11:52

二叉查找树虽然不是很复杂,练练手还是不错的。


#include <stdio.h>#include <stdlib.h>//一棵n节点二叉查找树的期望高度h = log(n)struct node{    node *parent;    node *left;    node *right;    int key;};struct tree{    node *root;};node *creat_node(int key){    node *n = (node *)malloc(sizeof(node));    n->parent = NULL;    n->left = NULL;    n->right = NULL;    n->key = key;    return n;}node *tree_search(tree &t, int key){    //算法复杂度为O(h)    node *x = t.root;    while(x != NULL && x->key != key)    {        if (key < x->key)            x = x->left;        else            x = x->right;    }    return x;}void tree_insert(tree &t, node *i){    //算法复杂度为O(h)    //2种情况    //情况1、T为NULL--任务1.1、T->root = i 任务1.2、i->parent = NULL    //情况2、T中至少有一个点-- 任务2.1、找到i的插入后的父点x, 任务2.2、x->child = i 任务2.3、i->parent = x    node *y = NULL;    node *x;    x = t.root;    while (x != NULL) //循环结束的时候,x就是插入位置,y就是插入位置的父节点。完成任务2.1    {        y = x;        if (i->key < x->key)            x = x->left;        else            x = x->right;    }    i->parent = y; //设置插入点的父节点 完成任务2.3,任务1.2    if (NULL == y) //y为NULL,树必然为空,将插入点插入树根,完成任务1.1        t.root = i;    else     {        if (i->key < y->key)//确定i为左节点还是右节点,完成任务2.2            y->left = i;        else             y->right = i;    }}node *tree_maxnum(node *x){    //最大点必然在树的最右边    while(x->right != NULL)        x = x->right;    return x;}node *tree_minnum(node *x){    //最小点必然在树的最左边    while(x->left != NULL)        x = x->left;    return x;}node *tree_successor(node *x){    //寻找x的后继点    //算法复杂度为O(h)    //2中情况,x含有右子节点和x不含有右字节点。    if (x->right != NULL)        return tree_minnum(x->right);    node *y;    y = x->parent;    node *z = x;    while (y != NULL && z == y->right)    {        z = y;        y = y->parent;    }    return y;}node *tree_predecessor(node *x){    //求前继,方法和求后继一样。    if (x->left != NULL)        return tree_maxnum(x->left);    node *y = x->parent;    node *z = x;    while (y != NULL && z == y->left)    {        z = y;        y = y->parent;    }    return y;}void tree_delete(tree &t, node *z){    //算法复杂度为O(h),因为寻找后继的算法复杂度为O(h)    //3种大的情况,每种情况右可以分成几种小的情况讨论。    //情况1:z没有子节点。    //情况1.1:T不只一个点,z为T的一个叶节点(z为左节点或者z为右节点)    //情况1.1.1:z为左子节点--任务1、(z->parent)->left = NULL    //情况1.1.2:z为右子节点--任务2、(z->parent)->right = NULL    //情况1.2:T只有一个点,z就是T的唯一叶节点,也是根节点--任务3、T->root = NULL    //情况2:z只有一个叶子节点。    //情况2.1:z为根节点    //情况2.1.1:z只有一个左节点--任务4.1、T->root = z->left 任务4.2 、(z->left)->parent = NULL    //情况2.1.2:z只有一个右节点--任务5.1、T->root = z->right 任务5.2、 (z->right)->parent = NULL    //情况2.2:z不为根节点    //情况2.2.1:z只有一个左节点,--任务6.1、(z->parent)->left = z->left 任务6.2、(z->left)->parent = z->parent    //情况2.2.2:z只有一个右节点。--任务7.1、(z->parent)->right = z->right 任务7.2、(z->right)->parent = z->parent    //情况3:z有两个叶子节点,下面两种情况其实不用细分考虑--任务8.1、((z->successor)->parent)->left = NULL 任务8.2、(z->key = z->successor)->key    //情况3.1:z为根节点    //情况3.2:z不是根节点    node *y = NULL;    node *x = NULL;        //得到的y就是要删除的点    if (z->left == NULL || z->right == NULL) //对应情况1和情况2        y = z;    else         y = tree_successor(z); //对应情况3,这种情况下就是把x的后继拷贝到z的位置,然后删除y    //对于情况2,最后就是要将x于y->parent连接,对于情况1和情况3,最后就是要将y->parent和x = NULL连接。这里得到的x都能满足这种需求     if (y->left != NULL)         x = y->left;    else        x = y->right;    //此时x存在2中情况,a、指向一个NULL(针对情况1和情况3)。b、x指向某个子节点(针对情况2)    //这个就是对情况2,将x域y->parent进行连接。这句在情况2.2下好理解,在情况2.1下x->parent就是NULL,x就变成了root    if (x != NULL)               x->parent = y->parent;    if (y->parent == NULL) //对应情况1.2、情况2.1。在1.2时,T指向了一个NULL;在2.1时,T指向的就是y->child.在情况3.1情况下不会出现这种情况        t.root = x;    else     {        //对应情况1.1(x=NULL),情况2.2(x=y->child),情况3(x = NULL).        if (y == (x->parent)->left)            (y->parent)->left = x;        else            (y->parent)->right = x;        //让被删除点y的父点的对应子节点指向正确节点。    }    if (y != z) //针对情况3,进行值的复制。        z->key = y->key;    free(y);}void inorder_tree_walk(node *x){    if (x == NULL)        return;    inorder_tree_walk(x->left);    printf("%d\t", x->key);    inorder_tree_walk(x->right);}int main(){    tree t;    t.root = NULL;    tree_insert(t, creat_node(50));    for (int i = 1; i < 100; i++)    {        tree_insert(t, creat_node(i));    }        inorder_tree_walk(t.root);    node *max = tree_maxnum(t.root);    printf("\nmax = %d", max->key);    node *min = tree_minnum(t.root);    printf("\nmin = %d", min->key);    node *f = tree_search(t, 45);    printf("\nfind node: %d", f->key);    node *l = (t.root)->left->right;    node *s = tree_successor(l);    printf("\n%d successor %d",l->key, s->key);    s = tree_predecessor(l);    printf("\n%d successor %d\n",l->key, s->key);    tree_delete(t, l);    inorder_tree_walk(t.root);}