2024-2-7-复习作业

源代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef char datatype;
typedef struct Node
{
	datatype data;
	struct Node *lchild;

	struct Node *rchild;
}*Btree;
Btree creat_node()
{
	Btree s=(Btree)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data='\0';
	s->lchild=NULL;
	s->rchild=NULL;
	return s;
}
Btree creat_tree()
{
	datatype element;
	printf("please enter element:");
	scanf(" %c",&element);
	if(element=='#')
		return NULL;
	Btree tree =creat_node();
	tree->data=element;
	tree->lchild=creat_tree();
	tree->rchild=creat_tree();
	return tree;

}
void first_tree(Btree tree)
{
	if(tree==NULL)
		return ;
	
	printf("%-4c",tree->data);
	first_tree(tree->lchild);
	first_tree(tree->rchild);

}
void mid_tree(Btree tree)
{
		if(tree==NULL)
		return ;
	
	mid_tree(tree->lchild);
	printf("%-4c",tree->data);
	mid_tree(tree->rchild);

}
void last_tree(Btree tree)
{
		if(tree==NULL)
		return ;
	
	mid_tree(tree->lchild);
	mid_tree(tree->rchild);
	printf("%-4c",tree->data);
}
void Count(Btree tree ,int *n0,int *n1 ,int *n2)
{
	if(tree==NULL)
		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==NULL)
		return 0;
	int left=1+high(tree->lchild);
	int right=1+high(tree->rchild);
	return left>right?left:right;
}

int main(int argc,char *argv[])
{
	Btree tree =creat_tree();
	first_tree(tree);
		puts("");
	mid_tree(tree);
		puts("");
	last_tree(tree);
		puts("");
	int n0,n1,n2;
	n0=n1=n2=0;
	Count(tree,&n0,&n1,&n2);
	printf("n0=%d,n1=%d,n2=%d\n",n0,n1,n2);
	int high_tree=high(tree);
	printf("high=%d\n",high_tree);



	return 0;
}

效果图:

相关推荐

  1. 作业2024/2/2

    2024-02-08 08:14:04       32 阅读
  2. 2.7作业

    2024-02-08 08:14:04       29 阅读
  3. 作业2024/2/6

    2024-02-08 08:14:04       28 阅读
  4. 作业2024/2/5

    2024-02-08 08:14:04       29 阅读

最近更新

  1. TCP协议是安全的吗?

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

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

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

    2024-02-08 08:14:04       18 阅读

热门阅读

  1. 1971 - 大小写转换

    2024-02-08 08:14:04       32 阅读
  2. Rust-AI todo list 开发体验

    2024-02-08 08:14:04       34 阅读
  3. Vista —— a magazine I will read along the rest of my life

    2024-02-08 08:14:04       31 阅读
  4. #Z2294. 打印树的直径

    2024-02-08 08:14:04       33 阅读
  5. nohup基本使用

    2024-02-08 08:14:04       36 阅读
  6. SQL 注入 - http头注入之UA头注入探测

    2024-02-08 08:14:04       36 阅读
  7. Android 自定义BaseActivity

    2024-02-08 08:14:04       30 阅读
  8. django的基本使用(一)

    2024-02-08 08:14:04       28 阅读