C语言数据结构学习笔记(11)-二叉排序树的创建遍历及删除

/*
二叉排序树的创建遍历及删除
输出结果:
中序遍历:1   2   4   5   6   7   9   10  14  15  17  18  19  20  25  30  35  40
层序遍历:20  14  25  5   15  35  4   10  17  30  40  2   6   19  1   9   18  7
Treeheight = 7
请输入待寻找的数字:5
找到了5.
请输入待寻找父节点的数字:5
5的父节点值为14
请输入要删除的节点值:5
删除后root = 20 proot = 00A8FA6C
中序遍历:1   2   4   6   7   9   10  14  15  17  18  19  20  25  30  35  40
层序遍历:20  14  25  4   15  35  2   10  17  30  40  1   6   19  9   18  7
请输入要删除的节点值:7
删除后root = 20 proot = 00A8FA6C
中序遍历:1   2   4   6   9   10  14  15  17  18  19  20  25  30  35  40
层序遍历:20  14  25  4   15  35  2   10  17  30  40  1   6   19  9   18
请输入要删除的节点值:17
删除后root = 20 proot = 00A8FA6C
中序遍历:1   2   4   6   9   10  14  15  18  19  20  25  30  35  40
层序遍历:20  14  25  4   15  35  2   10  19  30  40  1   6   18  9
请输入要删除的节点值:20
删除后root = 19 proot = 00A8FA6C
中序遍历:1   2   4   6   9   10  14  15  18  19  25  30  35  40
层序遍历:19  14  25  4   15  35  2   10  18  30  40  1   6   9
请输入要删除的节点值:14
删除后root = 19 proot = 00A8FA6C
中序遍历:1   2   4   6   9   10  15  18  19  25  30  35  40
层序遍历:19  10  25  4   15  35  2   6   18  30  40  1   9
******
*/
# include <stdio.h>
# include <stdlib.h>
# define ElemType int
# define MAX_SIZE 50
typedef struct BSTreeNode{
    ElemType data;
    struct BSTreeNode * lchild;
    struct BSTreeNode * rchild;
}Node, *pNode;
void InOrderTree(pNode root);//中序遍历二叉排序树
void LevelOrder(pNode root);//层序遍历二叉排序树
pNode CreateTreeNode(ElemType data);//创建二叉排序树的一个节点
void InsertTreeNode(pNode * proot, ElemType data);//向二叉排序树中依序插入一个节点
int TreeHeight(pNode root);//二叉排序树的高度
pNode FindTree(pNode root, ElemType data);//二叉排序树的查找
pNode FindParent(pNode root, ElemType data);//二叉排序树查找父节点
void DeleteTree(pNode * proot, ElemType data);//删除二叉排序树节点
void DestoryTree(pNode * proot);//销毁二叉排序树

int main(void)
{
    pNode root = NULL;
    ElemType test[] = {20, 14, 5, 15, 25, 35, 30, 17, 10, 19, 6, 4 ,40, 18, 2, 9, 1, 7};
    //ElemType test[] = {3,4,2};
    int len = sizeof(test)/sizeof(ElemType);
    for(int i = 0; i < len; i++)
    {
        InsertTreeNode(&root, test[i]);
    }
    printf("中序遍历:");
    InOrderTree(root);
    printf("\n");
    printf("层序遍历:");
    LevelOrder(root);
    printf("\n");
    printf("Treeheight = %d\n", TreeHeight(root));
    printf("请输入待寻找的数字:");
    ElemType x;
    scanf("%d", &x);
    pNode find = FindTree(root, x);
    if(find)
        printf("找到了%d.\n", find->data);
    else
        printf("没找到.\n");
    printf("请输入待寻找父节点的数字:");
    ElemType y;
    scanf("%d", &y);
    pNode findparent &

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值