C语言实现二叉查找树的元素删除功能

接前一篇博文添加二叉查找树的元素删除功能。

#include <stdio.h>
#include <stdlib.h>

//二叉查找树的节点
struct bst_node {
    int value;
    struct bst_node *left;
    struct bst_node *right;
};

typedef struct bst_node Bst_Node;
typedef struct bst_node *P_Bst_Node;
typedef struct bst_node *Bin_Search_Tree;
typedef struct bst_node **P_Bin_Search_Tree;

void clear_bst(P_Bin_Search_Tree);

void insert_bst(P_Bin_Search_Tree, int);

void print_bst(Bin_Search_Tree);

void del_bst(P_Bin_Search_Tree, int, P_Bst_Node);

P_Bst_Node find_min(Bin_Search_Tree);

int main() {
    printf("Hello, World!\n");
    char a[3][4];
    char b = 'a';
    int i, j;
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 4; ++j) {
            a[i][j] = (char) b++;
        }
    }
    for (i = 0; i < 3; ++i) {
        for (j = 0; j < 4; ++j) {
            printf("%c ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    char (*pc)[4];
    char *pd;
    for (i = 0, pc = a; i < 3; ++i) {
        for (j = 0, pd = *(pc + i); j < 4; ++j) {
            printf("%c ", *(pd + j));
        }
        printf("\n");
    }
    printf("\n");

    Bin_Search_Tree bst = NULL;
    P_Bin_Search_Tree p_bst = &bst;
    insert_bst(p_bst, 1);
    insert_bst(p_bst, 2);
    insert_bst(p_bst, 3);
    insert_bst(p_bst, 0);
    insert_bst(p_bst, -1);
    insert_bst(p_bst, 8);
    insert_bst(p_bst, 6);
    insert_bst(p_bst, 5);
    insert_bst(p_bst, 7);
    print_bst(bst);
    P_Bst_Node p_min = find_min(bst);
    printf("\n%d\n", p_min->value);
    del_bst(p_bst, 1, NULL);
    printf("\n");
    print_bst(bst);
    printf("\n");
    del_bst(p_bst, 3, NULL);
    del_bst(p_bst, 6, NULL);
    del_bst(p_bst, -1, NULL);
    printf("\n");
    print_bst(bst);
#if 1
    printf("\n");
    clear_bst(p_bst);
    bst = NULL;
#endif
    return 0;
}

void clear_bst(P_Bin_Search_Tree pt) {
    P_Bst_Node pn = *pt;
    if (pn != NULL) {
        clear_bst(&pn->left);
        clear_bst(&pn->right);
        free(pn);
        pn = NULL;
    }
}

void insert_bst(P_Bin_Search_Tree pt, int value) {
    if (*pt == NULL) {
        P_Bst_Node pn = malloc(sizeof(Bst_Node));
        pn->value = value;
        pn->left = pn->right = NULL;
        *pt = pn;
        return;
    }
    if (value < (*pt)->value) insert_bst(&(*pt)->left, value);
    else if (value > (*pt)->value) insert_bst(&(*pt)->right, value);
    else;
}

void print_bst(Bin_Search_Tree tree) {
    if (tree == NULL) return;
    print_bst(tree->left);
    printf("%d ", tree->value);
    print_bst(tree->right);
}

void del_bst(P_Bin_Search_Tree pt, int x, P_Bst_Node p_parent) {
    if (*pt == NULL) return;
    P_Bst_Node p_del = *pt;
    if (x < p_del->value) del_bst(&p_del->left, x, p_del);
    else if (x > p_del->value) del_bst(&p_del->right, x, p_del);
    else {
#if 0
        printf("\n%d is to be removed!\n", p_del->value);
#endif

        if (p_del->left == NULL) {
            if (p_del->right == NULL) {//树叶
                if (p_parent == NULL) {//p_del是根
                    *pt = NULL;
                } else {
                    if (p_parent->left == p_del) p_parent->left = NULL;
                    else if (p_parent->right == p_del) p_parent->right = NULL;
                }
            } else {//存在右子树
                if (p_parent == NULL) {//p_del是根
                    *pt = p_del->right;
                } else {
                    if (p_parent->left == p_del) p_parent->left = p_del->right;
                    else if (p_parent->right == p_del) p_parent->right = p_del->right;
                }
            }
            free(p_del);
        } else {
            if (p_del->right == NULL) {//存在左子树
                if (p_parent == NULL) {//p_del是根
                    *pt = p_del->left;
                } else {
                    if (p_parent->left == p_del) p_parent->left = p_del->left;
                    else if (p_parent->right == p_del) p_parent->right = p_del->left;
                }
                free(p_del);
            } else {//存在左、右子树
                P_Bst_Node p_right_min = find_min(p_del->right);
                p_del->value = p_right_min->value;
                del_bst(&p_del->right, p_del->value, p_del);
            }
        }
    }
}

P_Bst_Node find_min(Bin_Search_Tree tree) {
    return tree == NULL || tree->left == NULL ? tree : find_min(tree->left);
}

运行结果

-1 0 1 2 3 5 6 7 8 
-1

-1 0 2 3 5 6 7 8 

0 2 5 7 8 

Process finished with exit code 0

总结

关键在于:

  1. 元素的递归删除
  2. 分析待删除的节点是否有孩子,有几个孩子
  3. 何时才是对节点进行 free() 的最佳时机
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言实现二叉排序树的主要步骤如下: 1. 定义二叉排序树的节点结构体,包括左右子树指针和数据域。 2. 实现节点的创建函数,用于新建一个节点并初始化其值。 3. 实现插入函数,将一个元素插入到二叉排序树中。 4. 实现查找函数,查找指定元素是否在二叉排序树中。 5. 实现删除函数,删除指定元素在二叉排序树中的节点。 6. 实现遍历函数,可以按照前序遍历、中序遍历、后序遍历的方式输出二叉排序树中所有节点的值。 下面是一个简单的C语言实现二叉排序树的示例代码: ``` #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 创建一个新节点 TreeNode* createNode(int val) { TreeNode* node = (TreeNode*) malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } // 向二叉排序树中插入一个节点 void insert(TreeNode** root, int val) { if (*root == NULL) { *root = createNode(val); return; } if (val < (*root)->val) { insert(&((*root)->left), val); } else { insert(&((*root)->right), val); } } // 查找指定元素是否在二叉排序树中 TreeNode* search(TreeNode* root, int val) { if (root == NULL || root->val == val) { return root; } if (val < root->val) { return search(root->left, val); } else { return search(root->right, val); } } // 删除指定元素在二叉排序树中的节点 TreeNode* delete(TreeNode* root, int val) { if (root == NULL) { return root; } if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL) { TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { TreeNode* temp = root->left; free(root); return temp; } TreeNode* temp = root->right; while (temp && temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = delete(root->right, temp->val); } return root; } // 前序遍历二叉排序树 void preOrder(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preOrder(root->left); preOrder(root->right); } // 中序遍历二叉排序树 void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); printf("%d ", root->val); inOrder(root->right); } // 后序遍历二叉排序树 void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%d ", root->val); } int main() { TreeNode* root = NULL; // 插入元素 insert(&root, 5); insert(&root, 3); insert(&root, 7); insert(&root, 1); insert(&root, 4); // 查找元素 TreeNode* node = search(root, 7); if (node != NULL) { printf("Found element: %d\n", node->val); } else { printf("Element not found\n"); } // 删除元素 root = delete(root, 3); // 遍历二叉排序树 printf("Pre-order traversal: "); preOrder(root); printf("\n"); printf("In-order traversal: "); inOrder(root); printf("\n"); printf("Post-order traversal: "); postOrder(root); printf("\n"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值