二叉树遍历的应用--求叶数,非叶数,层数,所有数的和,查找某个数,插入

本文介绍了一种使用C语言实现二叉树基本操作的方法,包括创建节点、查找节点、插入节点、计算节点总数、叶子节点数量、非叶子节点数量及二叉树的层数等。通过具体的函数定义和主函数示例演示了如何构造简单的二叉树。

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

#include <stdio.h>
#include <stdlib.h>
struct node{
    int element;
    struct node* Left;
    struct node* Right;
};
struct node* CreateNode(int data){//创造新节点
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
    if(temp!=NULL){
        temp->element=data;
        temp->Left=NULL;
        temp->Right=NULL;
    }
    return temp;
}
struct node* FindNode(struct node* BinTree,int data)//查找
{
    if(BinTree==NULL)
        return NULL;
    if(BinTree->element==data)
        return BinTree;
    struct node* temp=NULL;
    if(BinTree->Left!=NULL)
        temp=FindNode(BinTree->Left,data);
    if(temp==NULL&&BinTree->Right!=NULL)
        temp=FindNode(BinTree->Right,data);
    return temp;

}
//假设每个节点值不同
#define LEFT 1
#define RIGHT 0
struct node* InsertNode(struct node* BinTree,int loc,int dir,int data)//插入节点,loc代表插入的数的父节点的值,dir表示方向
{
    struct node* parent=FindNode(BinTree,loc);
    if(parent!=NULL){
        struct node* temp=CreateNode(data);
        if(dir==LEFT)
            parent->Left=temp;
        else
            parent->Right=temp;
    }
    return BinTree;
}
int SumOfNode(struct node* BinTree)//求和
{
    if(BinTree==NULL)
        return 0;
    int sum=BinTree->element;
    if(BinTree->Left!=NULL)
        sum+=SumOfNode(BinTree->Left);
    if(BinTree->Right!=NULL)
        sum+=SumOfNode(BinTree->Right);
    return sum;
}
int CountOfLeaves(struct node* BinTree)//叶个数
{
    if(BinTree==NULL)
        return 0;
    if(BinTree->Left==NULL&&BinTree->Right==NULL)
        return 1;
    int count=0;
    if(BinTree->Left!=NULL)
        count+=CountOfLeaves(BinTree->Left);
    if(BinTree->Right!=NULL)
        count+=CountOfLeaves(BinTree->Right);
    return count;
}
int CountOfNotLeaves(struct node* BinTree)//非叶节点个数
{
    if(BinTree==NULL)
        return 0;
    if(BinTree->Left==NULL&&BinTree->Right==NULL)
        return 0;
    int count=1;
    if(BinTree->Left!=NULL)
        count+=CountOfNotLeaves(BinTree->Left);
    if(BinTree->Right!=NULL)
        count+=CountOfNotLeaves(BinTree->Right);
    return count;

}
int CountOfLevels(struct node* BinTree)//求层数
{
    if(BinTree==NULL)
        return 0;
    int count1=0,count2=0;
    if(BinTree->Left!=NULL)
        count1=CountOfLevels(BinTree->Left);
    if(BinTree->Right!=NULL)
        count2=CountOfLevels(BinTree->Right);
    return 1+(count1>count2?count1:count2);
}
int main()
{
    //手工建树,不提倡,之后会写到怎样建树
    struct node* BinTree=CreateNode(10);
    BinTree=InsertNode(BinTree,10,1,20);
    BinTree=InsertNode(BinTree,10,0,30);
    BinTree=InsertNode(BinTree,30,1,40);
    BinTree=InsertNode(BinTree,40,0,50);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值