树的链式存储,及前序中序后序遍历

二叉树链式存储的,提供了两种的建树方式,一。没有返回值的,二。有返回值的,遍历的方法,主要是运用了递归的思想,不是很难可以理解

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define max 100
typedef struct Bitee{
	char data;
   struct Bitee *lchild,*rchild;
}Bitree;
Bitree *creatree()
{
	char c; Bitree *T;
	cin>>c;
	if(c=='*') T=NULL;
	else
	{
		T=(Bitree *)malloc(sizeof(Bitree));
		T->data=c;
		T->lchild=creatree();
		T->rchild=creatree();
	}
	return T;
}
void creatree1(Bitree *&T)
{
	char c;
	cin>>c;
	if(c=='*') T=NULL;
	else
	{
		T=(Bitree *)malloc(sizeof(Bitree));
		T->data=c;
		creatree1(T->lchild);
		creatree1(T->rchild);
	}
	
}
void xianxu(Bitree *T)
{
	if(T!=NULL)
	{
		cout<<T->data<<" ";
		xianxu(T->lchild);
		xianxu(T->rchild);
	}
}
void zhongxu(Bitree *T)
{
	if(T!=NULL)
	{
		zhongxu(T->lchild);
		cout<<T->data<<" ";
		zhongxu(T->rchild);
	}
 }
 void houxu(Bitree *T)
 {
 	if(T!=NULL)
 	{
 		houxu(T->lchild);
 		houxu(T->rchild);
 		cout<<T->data<<" ";
	 }
 }
int main()
{
   Bitree *T;
	cout<<"请按照先序的顺序输入二叉树的存储数据,如果值为空用“*”代替\n" ; 
	T=creatree();
	//creatree1(T);//用第二种方法来建树不需要返回值 
	cout<<"树的前序\n"; 
	xianxu(T);
	cout<<"\n树的中序\n";
	zhongxu(T);
	cout<<"\n树的后序\n";
	houxu(T);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值