先说几句废话,发现自己有点眼高手低,以为有些东西懂了,但是当真的要去做的时候,漏洞百出,满眼的纰漏,让人不堪入目,对不起,我认真的对自己道歉,一定要沉下心来做事,认认真真的掌握,实实在在的做出来,并且想明白每一个点,踏踏实实的做好每一件事。
从今天起开始向树的算法进军,加油!
言归正传。今天学习树的建成,和树的遍历
/*
Author : silence
Time : 2012/6/9
Description : 二叉树的生成与遍历
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
//定义树节点的结构体
typedef struct node
{
char data;
struct node *lchild,*rchild;
}Node,*Tree;
Tree create()
{
Tree t=NULL;
char input;
cout<<"请输入"<<endl;
cin>>input;
if(input =='*')
//t=NULL;
return NULL;
else
{
t = (Tree)malloc(sizeof(Node));
t->data = input;
t->lchild = create();
t->rchild = create();
}
return t;
}
void pre(Tree t)
{
if(t!=NULL)
{
cout<<t->data<<endl;
pre(t->lchild);
pre(t->rchild);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"创建二叉树"<<endl;
Tree t = create();
cout<<"输入完毕"<<endl;
cout<<"线序遍历二叉树"<<endl;
pre(t);
//play();
system("pause");
return 0;
}
/*以下为测试数据
输入:
A
B
C
*
D
*
*
E
*
*
F
*
H
*
*
输出:
A
B
C
D
E
F
G
*/