软基作业:二叉树的创建与遍历

本文详细探讨了如何在计算机程序中创建和遍历二叉树,包括二叉树的基本概念、构造方法以及前序、中序和后序遍历的实现过程。通过实例解析,帮助读者深入理解二叉树的数据结构及其应用。
摘要由CSDN通过智能技术生成

题目要求:

ex4_1:

1)二叉树结点类型定义为:
typedefstructbnode
{  intdata;
   structbnode*lc,*rc;
}bnode_type;
2)编写中序遍历函数;
3)编写后序遍历函数;
4)编写先序遍历函数;
5)编写create函数建立二叉排序树;
6)编写main()函数,先调用create函数,建立一颗二叉排序树;然后分别调用中序、后序、先序遍历函数,将二叉树的先序、中序和后序遍历序列输出到屏幕上。

程序如下:

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

typedef struct bnode
{
	int data;
	struct bnode *lc, *rc;
}bnode_type;

bnode_type *create(void);
void preorder(bnode_type *root);
void inorder(bnode_type *root);
void postorder(bnode_type *root);

int main(void)
{
	bnode_type *tree;

	printf("Please input your datas(ended by -1):");
	tree = create();

	printf("Preorder:\n");
	preorder(tree);

	printf("\nInorder:\n");
	inorder(tree);

	printf("\nPostorder:\n");
	postorder(tree);
	printf("
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值