⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp1.cpp

📁 数据结构课程的实验
💻 CPP
字号:
#include<iostream.h>
#include<malloc.h>
struct bitree
{
	char data;
	struct bitree *lchild,*rchild;
};
bitree *s,*root;
bitree *Creat()
{
		int front,rear;
		bitree *p[100];//存储数据
		char ch;
		root=NULL;
		front=1;
		rear=0;
		cout<<"输入二叉树的数据"<<endl;
		cin>>ch;
		while(ch!='!')
		{
			s=NULL;
			if(ch!='!')
			{
				s=(struct bitree*)malloc(sizeof(bitree));
				s->data=ch;
				s->lchild=NULL;
				s->rchild=NULL;
			}
			rear++;
			p[rear]=s;
			if(rear==1)
				root=s;
			else
			{
			if(s&&p[rear])
				if(rear%2==0) p[front]->lchild=s;
				else
					p[front]->rchild=s;
				if(rear%2==1)
					front++;
			}
		cin>>ch;
		}
		return root;
}	
Preorder(bitree *t)
	{
		if(t)
		{
			cout<<t->data<<endl;
			Preorder(t->lchild);
			Preorder(t->rchild);
		}
	}
Inorder(bitree *t)
	{
		if(t)
		{
			Inorder(t->lchild);
			cout<<t->data<<endl;
			Inorder(t->rchild);
		}
	}
Postorder(bitree *t)
	{
		if(t)
		{
			Postorder(t->lchild);
			Postorder(t->rchild);
			cout<<t->data<<endl;
		}

	}
void main()
{
	int k;
	cout<<"输入1进入程序,其它退出程序"<<endl;
	cin>>k;
	while(k==1)
	{
	bitree *m;
	char n;
	int i,j;
	m=Creat();
	cout<<"输入I"<<endl;
		cin>>i;
	cout<<"输入1进行前续输出,2进行中续,3进行后续,否则输入错误"<<endl;
	s=root;
	if(i==1)
	{
		cout<<"前续输出"<<endl;
		n=Preorder(s);
	}
	if(i==2)
	{
		cout<<"中续输出"<<endl;
		n=Inorder(s);
	}
	if(i==3)
	{
		cout<<"后续输出"<<endl;
		n=Postorder(s);
	}
	if(i>3)
		cout<<"输入错误"<<endl;
	cout<<"继续输入k"<<endl;
	cin>>k;
	}
	cout<<"退出程序"<<endl;
	
}
	

	

		



	
	
	








	

		



	
	
	







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -