数据结构-day7

二叉树创建、遍历、计算结点、计算深度

head.h

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

typedef char datatype;
typedef struct Btree{
	datatype data;
	struct Btree *lchild;
	struct Btree *rchild;
}*btree;

btree create();
void insert_child(datatype e,btree tree);
void first(btree tree);
void mid(btree tree);
void last(btree tree);
void count(btree tree,int *n0,int *n1,int *n2);
int high(btree tree);

main.c

#include "head.h"
int main(int argc, const char *argv[]){
        btree tr=create();
        first(tr);
        puts("");
        mid(tr);
        puts("");
        last(tr);
        puts("");
        int *n0,*n1,*n2,N0,N1,N2;
        n0=&N0,n1=&N1,n2=&N2;
        *n0=0,*n1=0,*n2=0;
        count(tr,n0,n1,n2);
        printf("%d %d %d\n",*n0,*n1,*n2);
        printf("深度为%d\n",high(tr));
        return 0;
}

test.c

#include "head.h"

btree create_node(){
	btree p=(btree)malloc(sizeof(struct Btree));
	p->data=0;
	p->lchild=NULL;
	p->rchild=NULL;
}
btree create(){
	datatype e;
	printf("please input e:");
	scanf("%c",&e);
	getchar();
	if(e=='#')
		return NULL;
	btree tr=create_node();
	tr->data=e;
	puts("创建左孩子");
	tr->lchild=create();
	puts("创建右孩子");
	tr->rchild=create();
	return tr;
}
void first(btree tree){
	if(!tree)
		return;
	printf("%c ",tree->data);
	first(tree->lchild);
	first(tree->rchild);
}
void mid(btree tree){
        if(!tree)
                return;
        mid(tree->lchild);
	printf("%c ",tree->data);
        mid(tree->rchild);
}
void last(btree tree){
        if(!tree)
                return;
        last(tree->lchild);
	last(tree->rchild);
	printf("%c ",tree->data);
}

void count(btree tree,int *n0,int *n1,int *n2){
	if(!tree)
		return;
	if(!tree->lchild&&!tree->rchild)
		++*n0;
	else if(tree->lchild&&tree->rchild)
		++*n2;
	else
		++*n1;
	count(tree->lchild,n0,n1,n2);
	count(tree->rchild,n0,n1,n2);
}

int high(btree tree){
	if(!tree)
		return 0;
	int left=1+high(tree->lchild);
	int right=1+high(tree->rchild);
	return left>right?left:right;
}

结果展示:

相关推荐

  1. 数据结构day2

    2024-02-03 10:40:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-03 10:40:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-03 10:40:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-03 10:40:04       82 阅读
  4. Python语言-面向对象

    2024-02-03 10:40:04       91 阅读

热门阅读

  1. 【CS3CA】Computer Animation

    2024-02-03 10:40:04       50 阅读
  2. 《每天一分钟学习C语言·十三》

    2024-02-03 10:40:04       57 阅读
  3. linux 内核升级

    2024-02-03 10:40:04       55 阅读
  4. 基于机器学习的无损缺陷检测技术研究进展

    2024-02-03 10:40:04       56 阅读
  5. 2.2作业

    2.2作业

    2024-02-03 10:40:04      53 阅读
  6. Vue 图片加载失败处理

    2024-02-03 10:40:04       44 阅读
  7. Vue3快速上手

    2024-02-03 10:40:04       42 阅读
  8. react+ts

    react+ts

    2024-02-03 10:40:04      49 阅读
  9. 115.工业相机海康SDK开发指南(阅读)

    2024-02-03 10:40:04       33 阅读
  10. 探索ChatGPT:AI技术的新篇章与人类的共舞

    2024-02-03 10:40:04       46 阅读
  11. 【硬件产品经理】锂电池充电时间怎么计算?

    2024-02-03 10:40:04       52 阅读
  12. MySQL的备份与恢复

    2024-02-03 10:40:04       49 阅读