erchashubianli.txt

来自「数据结构中二叉树遍历算法」· 文本 代码 · 共 83 行

TXT
83
字号
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
#include <iostream>
using namespace std;
typedef struct node
{
	char date;
	struct node *Lchild,*Rchild;
}*bitreptr;
void CreateBtr(bitreptr &t)
{
	bitreptr p;
	char x;
	cin>>x;
	if (x=='#')
		t=NULL;
	else
	{
		p=new node;
		p->date=x;
		t=p;
		CreateBtr(t->Lchild);
		CreateBtr(t->Rchild);
	}
}
void Preorder (bitreptr p)
{
	if (p)
	{
		cout<<p->date;
		Preorder(p->Lchild);
		Preorder(p->Rchild);
	}
}
void Inorder (bitreptr p)
{
	if (p)
	{
		Inorder(p->Lchild);
		cout<<p->date;
		Inorder(p->Rchild);
	}
}
void Postorder (bitreptr p)
{
	if (p)
	{
		Postorder(p->Lchild);
		Postorder(p->Rchild);
		cout<<p->date;
	}
}
int main()
{
	bitreptr p;
	p=new node;
	cout<<"输入树的各个元素:"<<endl;
	CreateBtr(p);
	cout<<"前序遍历:";
	Preorder(p);
	cout<<endl;
	cout<<"中序遍历:";
	Inorder(p);
	cout<<endl;
	cout<<"后序遍历:";
	Postorder(p);
	cout<<endl;
	return 0;
}

⌨️ 快捷键说明

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