二叉排序树

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

//二叉排序树结点
typedef struct BSTreeNode {
    int val;
    struct BSTreeNode *lchild;
    struct BSTreeNode *rchild;

} BSTreeNode, *BSTree;

BSTree insert(BSTree root, int val) {
    if (root == NULL) {
        BSTree node = (BSTree) malloc(sizeof(BSTreeNode));
        node->val = val;
        node->lchild = NULL;
        node->rchild = NULL;
        root = node;
    }
    else if (val < root->val) {
        root->lchild = insert(root->lchild, val);
    }
    else {
        root->rchild = insert(root->rchild, val);
    }
    return root;
}

BSTree buildBSTree(BSTree root, int *arr, int n) {
    int i;
    for (i = 0; i < n; i++) {
        root = insert(root, arr[i]);
    }
    return root;
}

//查找元素key,找到返回key的结点指针,没找到返回NULL
BSTree search(BSTree root, int key) {
    if (root == NULL) {
        return NULL;
    }
    if (key > root->val)//查找右子树
    {
        return search(root->rchild, key);
    }
    else if (key < root->val)//查找左子树
    {
        return search(root->lchild, key);
    }
    else {
        return root;
    }
}

int delNode(BSTree *root) {
    BSTree q, s;
    //root为叶子结点
    if (!(*root)->lchild && !(*root)->rchild) {
        *root = NULL;
    }
    else if (!(*root)->lchild) {//左子树为空,重接右子树
        q = *root;
        *root = (*root)->rchild;
        free(q);
    } else if (!(*root)->rchild) {//右子树为空,重接左子树
        q = *root;
        *root = (*root)->rchild;
        free(q);
    } else {//左右子树均不为空
        q = *root;
        s = (*root)->lchild;
        while (s->rchild) {
            q = s;
            s = s->rchild;//转左,然后向右走到尽头
        }
        (*root)->val = s->val;
        if (q != *root) {//判断是否执行上述while循环
            q->rchild = s->lchild;//执行上述while循环,重接右子树
        } else {
            q->lchild = s->lchild;//未执行上述while循环,重接左子树
        }
        free(s);
    }
    return 1;
}

//若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素结点
int delBST(BSTree *root, int key) {
    if (!(*root)) {
        return 0;
    }
    else {
        if (key == (*root)->val) {
            return delNode(root);
        } else if (key < (*root)->val) {
            return delBST(&(*root)->lchild, key);
        } else {
            return delBST(&(*root)->rchild, key);
        }
    }
}

void visit(BSTree root) {
    printf("%d ", root->val);
}

//前序遍历
void preOrder(BSTree root) {
    if (root != NULL) {
        visit(root);//访问根结点
        preOrder(root->lchild);
        preOrder(root->rchild);
    }
}

//中序遍历
void inOrder(BSTree root) {
    if (root != NULL) {
        inOrder(root->lchild);
        visit(root);
        inOrder(root->rchild);
    }
}

//后序遍历
void postOrder(BSTree root) {
    if (root != NULL) {
        postOrder(root->lchild);
        postOrder(root->rchild);
        visit(root);
    }
}

int main() {
    int arr[] = {10, 6, 15, 2, 7, 12, 4, 13};
    BSTree root = NULL;
    root = buildBSTree(root, arr, 8);
    printf("先序遍历二叉树: ");
    preOrder(root);
    printf("\n");
    /*printf("中序遍历二叉树: ");
    inOrder(root);
    printf("\n");
    printf("后序遍历二叉树: ");
    postOrder(root);
    printf("\n");*/
    //查找元素7
    /*BSTree node = search(root,7);
    if (node != NULL){
        printf("二叉排序树存在结点值为%d的结点\n",node->val);
    } else {
        printf("二叉排序树不存在结点值为7的结点\n");

    }*/
    /*//查找元素25
    BSTree node1 = search(root,25);
    if (node1 != NULL){
        printf("二叉排序树存在结点值为%d的结点\n",node1->val);
    } else {
        printf("二叉排序树不存在结点值为25的结点\n");

    }*/
    if (delBST(&root,2)) {
        printf("删除结点元素为2的结点\n先序遍历二叉树: ");
        preOrder(root);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值