c语言二叉树的存储表示与实现

本文详细介绍了二叉树链表存储结构的类型定义及其基本操作的实现,包括初始化、生成节点、释放节点、销毁树、判断是否为空、获取深度、返回根节点、获取和设置节点数据、插入新子树、删除子树等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
typedef struct node//二叉树链表存储结构的类型定义
{
    int data;//数据域
    struct node* lchild;//指针域,指向左孩子节点
    struct node* rchild;//指针域,指向右孩子节点
}*BiTree, BiTNode;
BiTree InitBiTree(BiTNode* root)//初始化构造一个新的二叉树,传入一个指向节点的指针。
{
    BiTree tree = root;
    return tree;
}
BiTNode* MakeNode(int data, BiTNode* lchild, BiTNode* rchild)//生成节点
{
    BiTNode* pnode = (BiTNode*)malloc(sizeof(BiTNode));
    if (pnode!=NULL)
    {
        pnode->data = data;
        pnode->lchild = lchild;
        pnode->rchild = rchild;
    }
    return pnode;
}
void FreeNode(BiTNode* pnode)//释放节点
{
    if (pnode!=NULL)
    {
        free(pnode);
        pnode = NULL;
    }
    return;
}
void DestroyBiTree(BiTree tree)//销毁一棵二叉树
{
    BiTNode* pnode = tree;
    if (pnode->lchild!=NULL)
    {
        DestroyBiTree(pnode->lchild);
    }
    if (pnode->rchild!=NULL)
    {
        DestroyBiTree(pnode->rchild);
    }
    FreeNode(pnode);
    return;
}
int IsEmpty(BiTree tree)//判断是否为空
{
    if (tree==NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int GetDepth(BiTree tree)//返回树的深度
{
    int cd, ld, rd;
    cd = 0;
    ld = 0;
    rd = 0;
    if (tree!=NULL)
    {
        ld = GetDepth(tree->lchild);
        rd = GetDepth(tree->rchild);
        cd = (ld > rd ? ld : rd);
        return cd + 1;
    }
    else
    {
        return 0;
    }
}
BiTree GetRoot(BiTree tree)//返回根
{
    return tree;
}
int GetData(BiTNode* pnode)//返回节点值
{
    return pnode->data;
}
void SetData(BiTNode* pnode, int data)//设置节点值
{
    pnode->data = data;
    return;
}
BiTree SetLchild(BiTree parent, BiTree lchild)//设置左子树
{
    parent->lchild = lchild;
    return lchild;
}
BiTree SetRchild(BiTree parent, BiTree rchild)//设置右子树
{
    parent->rchild = rchild;
    return rchild;
}
BiTree GetLchild(BiTree tree)//返回左子树
{
    if (tree!=NULL)
    {
        return tree->lchild;
    }
    else
    {
        return NULL;
    }
}
BiTree GetRchild(BiTree tree)//返回右子树
{
    if (tree!=NULL)
    {
        return tree->rchild;
    }
    else
    {
        return NULL;
    }
}
BiTree InsertChild(BiTree parent, int lr, BiTree child)//插入新子树
{
    if (parent!=NULL)
    {
        if (lr==0&&parent->lchild==NULL)
        {
            parent->lchild = child;
            return child;
        }
        if (lr==1&&parent->rchild==NULL)
        {
            parent->rchild = child;
            return child;
        }
    }
    else
    {
        return NULL;
    }
}
void DeleteChild(BiTree parent, int lr)//删除子树
{
    if (parent!=NULL)
    {
        if (lr==0&parent->lchild!=NULL)
        {
            FreeNode(parent->lchild);
            parent->lchild = NULL;
        }
        if (lr==1&&parent->rchild!=NULL)
        {
            FreeNode(parent->rchild);
            parent->rchild = NULL;
        }
    }
    else
    {
        return;
    }
}
int main()
{
    BiTNode* n1 = MakeNode(10, NULL, NULL);
    BiTNode* n2 = MakeNode(20, NULL, NULL);
    BiTNode* n3 = MakeNode(30, n1, n2);
    BiTNode* n4 = MakeNode(40, NULL, NULL);
    BiTNode* n5 = MakeNode(50, NULL, NULL);
    BiTNode* n6 = MakeNode(60, n4, n5);
    BiTNode* n7 = MakeNode(70, NULL, NULL);
    BiTree tree = InitBiTree(n7);
    SetLchild(tree, n3);
    SetRchild(tree, n6);
    printf("树的深度为:%d\n", GetDepth(tree));
    DeleteChild(tree, 0);
    DeleteChild(tree, 1);
    printf("树的深度为:%d\n", GetDepth(tree));

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值