C/C++ 二叉树创建,查找,删除,插入

#include<cstdio>
#include<cstdlib>
#include<queue>

using namespace std;

typedef struct BTNode *Position;
typedef Position BTree;
struct BTNode
{
    char data;
    Position lChild, rChild;
};

BTree CreateBTree(BTree bt, bool isRoot)
{
    char ch;
    if (isRoot)
        printf("Root: ");
    scanf_s("%c", &ch);
    getchar();
    if (ch != '#') //输入'#'表示该节点为空
    {
        isRoot = false;
        bt = new BTNode;
        bt->data = ch;
        bt->lChild = NULL;
        bt->rChild = NULL;
        printf("%c's left child is: ", bt->data);
        bt->lChild = CreateBTree(bt->lChild, isRoot);
        printf("%c's right child is: ", bt->data);
        bt->rChild = CreateBTree(bt->rChild, isRoot);
        
    }
    return bt;
}
//前序遍历打印在最前,先左子树,后右子树,每个节点经过一次即打印
void PreOrderTraverse(BTree T)
{
    if (T == NULL)
        return;
    printf("%c", T->data);
    PreOrderTraverse(T->lChild);
    PreOrderTraverse(T->rChild);
}
//中序遍历打印在中间,先左子树,后右子树,每个节点经过两次才打印
void InOrderTraverse(BTree T)
{
    if (T == NULL)
        return;
    
    InOrderTraverse(T->lChild);
    printf("%c", T->data);
    InOrderTraverse(T->rChild);
}
//前序遍历打印在最后,先左子树,后右子树,每个节点经过三次才打印
void PostOrderTraverse(BTree T)
{
    if (T == NULL)
        return;

    PostOrderTraverse(T->lChild);
    PostOrderTraverse(T->rChild);
    printf("%c", T->data);
}

void LayerOrderTraverse(BTree T)
{
    queue<BTree> q;
    if (T != NULL)
    {
        q.push(T);
    }
    while (q.empty() == false)
    {
        printf("%c", q.front()->data);
        if (q.front()->lChild != NULL)
        {
            q.push(q.front()->lChild);
        }
        if (q.front()->rChild != NULL)
        {
            q.push(q.front()->rChild);
        }
        q.pop();
    }
}

void Insert(BTree T, char value)
{
    BTree parent = NULL;
    BTree head = T;
    BTree p = (BTree)malloc(sizeof(BTree));
    p->data = value;
    p->lChild = p->rChild = NULL;
    while (head)
    {
        parent = head;
        if (value < head->data)
            head = head->lChild;
        else
            head = head->rChild;
    }
    if (value < parent->data)
        parent->lChild = p;
    else
        parent->rChild = p;
}

BTree SearchParentofSNode(BTree Pnod, BTree nod)
{
    while (1)
    {
        if (nod->lChild != NULL)
        {
            Pnod = nod;
            nod = nod->lChild;
        }
        else
        {
            break;
        }
    }
    return Pnod;
}

BTree SearchSuccessorNode(BTree nod)
{
    while (1)
    {
        if (nod->lChild != NULL)
        {
            nod = nod->lChild;
        }
        else
        {
            break;
        }
    }
    return nod;
}

BTree DeleteData(BTree parent, BTree cur, char value)
{
    BTree SNode = NULL;
    BTree PSNode = NULL;
    BTree temp = NULL;

    if (cur == NULL)
    {
        printf("\n删除节点不存在\n");
        return NULL;
    }
    else if (value > cur->data)
    {
        cur->rChild = DeleteData(cur, cur->rChild, value);
    }
    else if (value < cur->data)
    {
        cur->lChild = DeleteData(cur, cur->lChild, value);
    }
    else if (value == cur->data)
    {
        if (cur->lChild == NULL && cur->rChild == NULL)
        {
            free(cur);
            return NULL;
        }
        else if (cur->lChild != NULL && cur->rChild == NULL)
        {
            temp = cur->lChild;
            free(cur);
            return temp;
        }
        else if (cur->lChild == NULL && cur->rChild != NULL)
        {
            temp = cur->rChild;
            free(cur);
            return cur->rChild;
        }
        else if (cur->lChild != NULL && cur->rChild != NULL)
        {
            SNode = SearchSuccessorNode(cur->rChild);
            PSNode = SearchParentofSNode(cur->rChild, cur->rChild);
            if (cur->rChild == SNode)
            {
                SNode->lChild = cur->lChild;
                free(cur);
                return SNode;
            }
            else if (cur->rChild != SNode && SNode->rChild == NULL)
            {
                SNode->lChild = cur->lChild;
                SNode->rChild = cur->rChild;
                PSNode->lChild = NULL;
                free(cur);
                return SNode;
            }
            else if (cur->rChild != SNode && SNode->rChild != NULL)
            {
                PSNode->lChild = SNode->rChild;
                SNode->lChild = cur->lChild;
                SNode->rChild = cur->rChild;
                free(cur);
                return SNode;
            }
        }
    }
    return cur;
}

void SearchData(BTree T, char value)
{
    if (T != NULL)
    {
        if (T->data == value)
        {
            printf("\n查找值存在,值为%c\n", T->data);
        }
        else if (T->data > value)
        {
            SearchData(T->lChild, value);
        }
        else if (T->data < value)
        {
            SearchData(T->rChild, value);
        }
    }
    else if (T == NULL)
    {
        printf("\n查找值不存在\n");
    }
}
int main()
{
    BTree nod = NULL;
    BTree bt = NULL;
    bt = CreateBTree(bt, true);
    printf("前序遍历: \n");
    PreOrderTraverse(bt);

    printf("\n中序遍历: \n");
    InOrderTraverse(bt);

    printf("\n后序遍历: \n");
    PostOrderTraverse(bt);

    printf("\n层次遍历: \n");
    LayerOrderTraverse(bt);

    printf("\n插入值: \n");
    Insert(bt, 'M');
    LayerOrderTraverse(bt);

    printf("\n查找值:");
    SearchData(bt, 'A');

    printf("删除值: \n");
    nod = DeleteData(bt, bt, 'A');
    LayerOrderTraverse(nod);

    getchar();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kuangbao9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值