设为首页 加入收藏

TOP

AVL树C语言完整实现(一)
2014-07-19 22:52:54 来源: 作者: 【 】 浏览:114
Tags:AVL 语言 完整 实现

    采用非递归方式,效率较好,经过常规测试,不过,在删除节点的方法中,与《数据结构》中删除步骤有点出入,可能会有bug,待进一步测试

    #include <stdio.h>

    #include <string.h>

    #include <stdlib.h>

    #include <errno.h>

    #include <assert.h>

    typedef enum

    {

    EH = 0,

    LH = 1,

    RH = -1

    }bh_t;

    typedef enum

    {

    FALSE = 0,

    TRUE = 1

    }bool_t;

    typedef int ElemType;

    typedef struct BSTNode

    {

    ElemType key;

    int bf;

    struct BSTNode *lchild,*rchild,*parent;

    }BSTNode,*BSTree;

    bool_t  searchAVL(BSTree root,ElemType key, BSTNode **parent)

    {

    BSTNode *tmp = NULL;

    assert(root != NULL);

    *parent = root->parent;

    tmp = root;

    while(NULL != tmp)

    {

    if(tmp->key == key)

    {

    return TRUE;

    }

    *parent = tmp;

    if(tmp->key > key)

    {

    tmp = tmp->lchild;

    }

    else

    {

    tmp = tmp->rchild;

    }

    }

    return FALSE;

    }

    /* get the minimum node*/

    BSTNode* minNode(BSTNode* root)

    {

    if (root == NULL)

    {

    return NULL;

    }

    while (root->lchild != NULL)

    {

    root = root->lchild;

    }

    return root;

    }

    /* get the maximum node*/

    BSTNode* maxNode(BSTNode* root)

    {

    if (root == NULL)

    {

    return NULL;

    }

    while (root->rchild != NULL)

    {

    root = root->rchild;

    }

    return root;

    }

    /* get the previous node*/

    BSTNode* preNode(BSTNode* target)

    {

    if (target == NULL)

    {

    return NULL;

    }

    if (target->lchild != NULL)

    {

    return maxNode(target->lchild);

    }

    while ((target->parent!=NULL) && (target!=target->parent->rchild))

    {

    target = target->parent;

    }

    return target->parent;

    }

    /* get the next node*/

    BSTNode* nextNode(BSTNode* target)

    {

    if (target == NULL)

    {

    return NULL;

    }

    if (target->rchild != NULL)

    {

    return minNode(target->rchild);

    }

    while ((target->parent!=NULL) && (target!=target->parent->lchild))

    {

    target = target->parent;

    }

    return target->parent;

    }

     

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++构造函数:函数参数传递 下一篇C语言新建文件读出文件内容

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)
·C语言指针的定义和使 (2025-12-26 05:21:31)
·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)