寒假作业-day6

1>请编程实现二又树的操作
1.1二又树的创建
1.2二又树的先序遍历
1.3二又树的中序遍历
1.4二又树的后序遍历
1.5二又树各个节点度的个数
1.6二叉树的深度

代码:

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

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

btree node_create(){
	btree p=(btree)malloc(sizeof(struct Btree));
	if(!p)
		return NULL;
	p->data='\0';
	p->lchild=NULL;
	p->rchild=NULL;
	return p;	
}
btree tree_create(){
	datatype e;
	scanf("%c",&e);
	getchar();
	if(e=='#')
		return NULL;
	btree p=node_create();
	p->data=e;
	p->lchild=tree_create();
	p->rchild=tree_create();
	return p;
}
	
void first(btree root){
	if(!root)
		return;
	printf("%c\t",root->data);
	first(root->lchild);
	first(root->rchild);
}
void mid(btree root){
        if(!root)
                return;
        mid(root->lchild);
	printf("%c\t",root->data);
        mid(root->rchild);
}
void last(btree root){
        if(!root)
                return;
        last(root->lchild);
        last(root->rchild);
	printf("%c\t",root->data);
}
int count(btree root,int *n0,int *n1,int *n2){
	if(!root)
		return 0;
	if(!root->lchild&&!root->rchild)
		++*n0;
	else if(root->lchild&&root->rchild)
		++*n2; 
	else
		++*n1;
        count(root->lchild,n0,n1,n2);
	count(root->rchild,n0,n1,n2);
}	
int depth(btree root){
	if(!root)
		return 0;
	int left=1+depth(root->lchild);
	int right=1+depth(root->rchild);
	return left>right?left:right;
}

int main(int argc,const char*argv[]){
	btree root=NULL;
	root=tree_create();
	first(root);
	puts("");
	mid(root);
        puts("");
	last(root);
        puts("");
	int n0=0,n1=0,n2=0;
	count(root,&n0,&n1,&n2);
	printf("n0=%d n1=%d n2=%d sum=%d\n",n0,n1,n2,n0+n1+n2);
	int len=depth(root);
	printf("二叉树深度为%d\n",len);
	return 0;
}

结果:

相关推荐

  1. 寒假作业2月6

    2024-02-08 14:40:02       33 阅读
  2. 作业day6

    2024-02-08 14:40:02       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-08 14:40:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-08 14:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-08 14:40:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-08 14:40:02       20 阅读

热门阅读

  1. 精通Python中的正则表达式

    2024-02-08 14:40:02       34 阅读
  2. 贪心算法入门题(算法村第十七关青铜挑战)

    2024-02-08 14:40:02       32 阅读
  3. redis

    redis

    2024-02-08 14:40:02      35 阅读
  4. leetCode 30天

    2024-02-08 14:40:02       26 阅读
  5. 使用gpu_burn对GPU进行压测

    2024-02-08 14:40:02       30 阅读
  6. 典型数据结构的模板实现

    2024-02-08 14:40:02       39 阅读
  7. chagpt的原理详解

    2024-02-08 14:40:02       29 阅读
  8. WebGPU Inter-stage 变量

    2024-02-08 14:40:02       31 阅读
  9. watch 和 watchEffect 的使用

    2024-02-08 14:40:02       28 阅读
  10. Nginx中proxy_pass指令斜杠的作用

    2024-02-08 14:40:02       33 阅读