数据结构 二叉排序树

二叉排序树

定义

  • 二叉排序树也叫二叉搜索树,二叉排序树的左子树上的节点都小于根节点,右子树上的节点都大于根节点,每棵子树都是二叉排序树

二叉排序树实现

结构体定义

#define SUCCESS 0
#define FAILURE -1

typedef int ElemType;

struct BSTNode {
    ElemType elem;
    struct BSTNode *lchild, *rchild; // 儿子结点
};

typedef struct BSTNode* BSTree;

静态函数列表

// 创建一个节点
static struct BSTNode* create_bstnode(ElemType elem);

// 删除ptree节点
static int remove_node(BSTree *ptree);

基本函数声明

// 初始化为空 (好看)
BSTree init_empty_bstree(void); // 返回NULL

// 非递归插入元素
int insert_bstree(BSTree *ptree, ElemType elem);
// 递归插入元素
int insert_recursion_bstree(BSTree *ptree, ElemType elem);

// 非递归删除元素
int remove_bstree(BSTree *ptree, ElemType elem);
// 递归删除元素
int remove_recursion_bstree(BSTree *ptree, ElemType elem);

// 非递归查找
int search_bstree(BSTree tree, ElemType elem);
// 递归查找
int search_recursion_bstree(BSTree tree, ElemType elem);

// 树结点数
size_t size_bstree(BSTree tree);

// 树高度
size_t height_bstree(BSTree tree);

// 树是否为空
bool empty_bstree(BSTree tree);

// 清除
void clear_bstree(BSTree *ptree);

// 递归前序遍历
void prev_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
// 非递归前序遍历
void prev_bstree(BSTree tree, void (*foreach)(ElemType));

// 递归中序遍历
void mid_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
// 非递归中序遍历
void mid_bstree(BSTree tree, void (*foreach)(ElemType));

// 递归后序遍历
void back_foreach_bstree(BSTree tree, void (*foreach)(ElemType));
// 非递归后序遍历
void back_bstree(BSTree tree, void (*foreach)(ElemType));

// 层序遍历
void layer_foreach_bstree(BSTree tree, void (*foreach)(ElemType));

静态函数实现

创建一个节点 create_bstnode
static struct BSTNode* create_bstnode(ElemType elem)
{
    struct BSTNode *node = (struct BSTNode*)malloc(sizeof(struct BSTNode));
    if (node != NULL) {
        node->elem = elem;
        node->lchild = NULL;
        node->rchild = NULL;
    }
    return node;
}
删除ptree节点 remove_node
static int remove_node(BSTree *ptree)
{
    if ((*ptree)->lchild == NULL && (*ptree)->rchild == NULL) { // 没有子结点 直接删除
        free(*ptree);
        *ptree = NULL;
    } else if ((*ptree)->lchild == NULL || (*ptree)->rchild == NULL) { // 有一个子结点
        struct BSTNode *node = *ptree;
        *ptree = node->lchild != NULL ? node->lchild : node->rchild; // 使子结点代替要删除元素的位置
        free(node);
    } else { // 有2个子结点  找到左子树中大元素 与 要删除的元素 替换
        struct BSTNode *node = *ptree;
        for (ptree = &(*ptree)->lchild; (*ptree)->rchild != NULL; ptree = &(*ptree)->rchild);
        node->elem = (*ptree)->elem; // 替换
        remove_node(ptree); // 只会递归一次
    }
    return SUCCESS;
}

基本函数实现

初始化为空 (好看) init_empty_bstree
BSTree init_empty_bstree(void)
{
    return NULL;
}
非递归插入元素 insert_bstree
int insert_bstree(BSTree *ptree, ElemType elem) // ptree为二级指针
{
    assert(ptree != NULL);
    while (*ptree != NULL) {
        if (elem < (*ptree)->elem) { // 判断插入的元素应该在哪个子树
            ptree = &(*ptree)->lchild;
        } else if (elem > (*ptree)->elem) {
            ptree = &(*ptree)->rchild;
        } else {
            return FAILURE; // 重复元素不插入
        }
    }
    *ptree = create_bstnode(elem);
    if (*ptree == NULL) {
        return FAILURE;
    }
    return SUCCESS;
}
递归插入元素 insert_recursion_bstree
int insert_recursion_bstree(BSTree *ptree, ElemType elem)
{
    assert(ptree != NULL);
    if (*ptree == NULL) {
        *ptree = create_bstnode(elem);
        if (*ptree == NULL) {
            return FAILURE;
        }
        return SUCCESS;
    }
    if (elem < (*ptree)->elem) {
        return insert_recursion_bstree(&(*ptree)->lchild, elem);
    } else if (elem > (*ptree)->elem) {
        return insert_recursion_bstree(&(*ptree)->rchild, elem);
    }
    return FAILURE;
}
非递归删除元素 remove_bstree
int remove_bstree(BSTree *ptree, ElemType elem)
{
    assert(ptree != NULL);
    while (*ptree != NULL) {
        if (elem < (*ptree)->elem) { // 找到要删除元素的位置
            ptree = &(*ptree)->lchild;
        } else if (elem > (*ptree)->elem) {
            ptree = &(*ptree)->rchild;
        } else {
            break;
        }
    }
    if (*ptree == NULL) {
        return FAILURE;
    }
    return remove_node(ptree);
}
递归删除元素 remove_recursion_bstree
int remove_recursion_bstree(BSTree *ptree, ElemType elem)
{
    assert(ptree != NULL);
    if (*ptree == NULL) {
        return FAILURE;
    }
    assert(ptree != NULL);
    if (elem < (*ptree)->elem) { // 找到要删除元素的位置
        return remove_recursion_bstree(&(*ptree)->lchild, elem);
    } else if (elem > (*ptree)->elem) {
        return remove_recursion_bstree(&(*ptree)->rchild, elem);
    }
    return remove_node(ptree);
}
非递归查找 search_bstree
int search_bstree(BSTree tree, ElemType elem)
{
    while (tree != NULL) {
        if (elem < tree->elem) { // 根据排序树性质查找
            tree = tree->lchild;
        } else if (elem > tree->elem) {
            tree = tree->rchild;
        } else {
            return SUCCESS;
        }
    }
    return FAILURE;
}
递归查找 search_recursion_bstree
int search_recursion_bstree(BSTree tree, ElemType elem)
{
    if (tree == NULL) {
        return FAILURE;
    }
    if (elem < tree->elem) {
        return search_recursion_bstree(tree->lchild, elem);
    } else if (elem > tree->elem) {
        return search_recursion_bstree(tree->rchild, elem);
    }
    return SUCCESS;
}
树结点数 size_bstree
size_t size_bstree(BSTree tree)
{
    if (tree == NULL) {
        return 0;
    }
    return size_bstree(tree->lchild) + 1 + size_bstree(tree->rchild);
}
树高度 height_bstree
size_t height_bstree(BSTree tree)
{
    if (tree == NULL) {
        return 0;
    }
    size_t lh = height_bstree(tree->lchild) + 1;
    size_t rh = height_bstree(tree->rchild) + 1;
    return lh > rh ? lh : rh;
}
树是否为空 empty_bstree
bool empty_bstree(BSTree tree)
{
    return tree == NULL;
}
清除 clear_bstree
void clear_bstree(BSTree *ptree)
{
    assert(ptree != NULL);
    if (*ptree != NULL) {
        clear_bstree(&(*ptree)->lchild);
        clear_bstree(&(*ptree)->rchild);
        free(*ptree);
        *ptree = NULL;
    }
}
递归前序遍历 prev_foreach_bstree
// 前序遍历     先遍历根结点   然后再遍历左右子树
void prev_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree != NULL) {
        foreach(tree->elem);
        prev_foreach_bstree(tree->lchild, foreach);
        prev_foreach_bstree(tree->rchild, foreach);
    }
}
非递归前序遍历 prev_bstree
void prev_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree == NULL) {
        return;
    }
    ST st = create_stack(sizeof(struct BSTNode*));
    push_stack(st, &tree);
    while (!empty_stack(st)) {
        struct BSTNode *node = NULL;
        pop_stack(st, &node);
        foreach(node->elem); // 先遍历根结点
        if (node->rchild) { // 先进后出 右子树最后遍历
            push_stack(st, &node->rchild);
        }
        if (node->lchild) {
            push_stack(st, &node->lchild);
        }
    }
    destroy_stack(st);
}
递归中序遍历 mid_foreach_bstree
// 中序遍历     先遍左子树 然后遍历根结点  再遍历右子树
void mid_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree != NULL) {
        mid_foreach_bstree(tree->lchild, foreach);
        foreach(tree->elem);
        mid_foreach_bstree(tree->rchild, foreach);
    }
}
非递归中序遍历 mid_bstree
void mid_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree == NULL) {
        return;
    }
    ST st = create_stack(sizeof(struct BSTNode*));
    struct BSTNode *node = tree;
    while (node != NULL || !empty_stack(st)) {
        while (node != NULL) {
            push_stack(st, &node);
            node = node->lchild; // 有左子树就左子树入栈 (中序遍历 小的先进栈)
        }
        pop_stack(st, &node); // 当前一定是最小的元素
        foreach(node->elem);
        node = node->rchild; // 向右走一个(然后子树中小的先入栈)
    }
    destroy_stack(st);
}
递归后序遍历 back_foreach_bstree
// 后序遍历   先遍左子树 然后遍历右子树  再遍历根结点
void back_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree != NULL) {
        back_foreach_bstree(tree->lchild, foreach);
        back_foreach_bstree(tree->rchild, foreach);
        foreach(tree->elem);
    }
}
非递归后序遍历 back_bstree
void back_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree == NULL) {
        return;
    }
    ST st1 = create_stack(sizeof(struct BSTNode*));
    ST st2 = create_stack(sizeof(ElemType));
    struct BSTNode *node = tree;
    push_stack(st1, &node);
    while (!empty_stack(st1)) {
        pop_stack(st1, &node);
        push_stack(st2, &node->elem); // 把父亲结点压入栈中  先进后出 父亲结点最后遍历
        if (node->lchild != NULL) { // 先入第一个栈 则后入第二个栈 后入第二栈 则先出第二个栈 左子树先遍历
            push_stack(st1, &node->lchild);
        }
        if (node->rchild != NULL) {
            push_stack(st1, &node->rchild);
        }
    }
    ElemType elem = 0;
    while (!empty_stack(st2)) {
        pop_stack(st2, &elem);
        foreach(elem);
    }
    destroy_stack(st1);
    destroy_stack(st2);
}
层序遍历 layer_foreach_bstree
// 层序遍历 BFS
void layer_foreach_bstree(BSTree tree, void (*foreach)(ElemType))
{
    assert(foreach != NULL);
    if (tree == NULL) {
        return;
    }
    QE que = create_queue(sizeof(struct BSTNode*));
    push_queue(que, &tree); // 第上一层遍历完才会遍历下一层
    while (!empty_queue(que)) {
        BSTree node = NULL;
        pop_queue(que, &node);
        foreach(node->elem);
        if (node->lchild != NULL) {
            push_queue(que, &(node->lchild));
        }
        if (node->rchild != NULL) {
            push_queue(que, &(node->rchild));
        }    
    }
    destroy_queue(que);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值