标准建立二叉树NEW

时间:2024-04-06 21:03:06
#include<iostream>
#include<sstream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<time.h>
#define LEN 1000
#define INF 99999
using namespace std; struct node{
string data;
node *left;
node *right;
};
typedef node *BiTree;//定义新类型 看成结点!! void createtree(BiTree *t)//传递给我结点的指针t
{
string s; cin>>s; if(s=="#")
{
(*t)=NULL; // 既然t是结点的指针 // (*t)就是对应结点 下面同样如此
return;
} (*t)=new node; (*t)->data=s;
createtree(&(*t)->left);
createtree(&(*t)->right);
} void lastvisit(BiTree t)//后序遍历不用解释
{
if(t==NULL) return; lastvisit(t->right);
lastvisit(t->left);
cout<<t->data<<endl;
} int main()
{
BiTree T;
createtree(&T);//主意这里容易出错 &T//由于函数的原理,只有传递指针类型才能对树进行创立
lastvisit(T); return 0;
}

  

相关文章