点灯笼的二叉树视频实现
//二叉树的创建与遍历
#include<iostream>
using namespace std;
//二叉树的类型声明
typedef struct node
{
int data;
struct node *left;
struct node *right;
}tree;
//先序遍历
void pre_order(tree *p)
{
if(p!=NULL)
{
cout<<p->data<<endl;
pre_order(p->left);
pre_order(p->right);
}
}
//中序遍历
void in_order(tree *p)
{
if(p!=NULL)
{
in_order(p->left);
cout<<p->data<<endl;
in_order(p->right);
}
}
//后序遍历
void post_order(tree *p)
{
if(p!=NULL)
{
post_order(p->left);
post_order(p->right);
cout<<p->data<<endl;
}
}
int main()
{
tree *p1=new tree;
tree *p2=new tree;
tree *p3=new tree;
tree *p4=new tree;
tree *p5=new tree;
tree *p6=new tree;
p1->data=7;
p1->left=p2;
p1->right=p3;
p2->data=2;
p2->left=p4;
p2->right=p5;
p3->data=6;
p3->left=p6;
p3->right=NULL;
p4->data=5;
p4->left=NULL;
p4->right=NULL;
p5->data=4;
p5->left=NULL;
p5->right=NULL;
p6->data=8;
p6->left=NULL;
p4->right=NULL;
pre_order(p1);
in_order(p1);
post_order(p1);
return 0;
}