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

📄 twoembranchmenttree.cpp

📁 TwoEmbranchmentTree:As ancestor has its children , a tree has its leaves. we could check its leaves
💻 CPP
字号:
#include<iostream>

using namespace std;

struct node
{
	char data;
	node *lchild;
	node *rchild;
}*r,*root,*root2;

char ch,c;
node *create();
node *copy(node *);
void pre1(node *);
void pre2(node *);

void main()
{
    create();

	cout<<endl<<"the copy tree is:"<<endl;
	copy(root);

	cout<<endl<<endl;
    pre1(root2);
	cout<<endl<<"preorder1 is ok"<<endl;

	cout<<endl;
	pre2(root2);
    cout<<endl<<"preorder2 is ok"<<endl;
}

node *create()
{
	node *q;
	cin>>ch;

	if(ch=='%')
	{
		return (0);
	}

	else
	{
		q=new node;
		q->data=ch;
		q->lchild=create();
		q->rchild=create();
        root=q;
		return (q);
	}
}

node *copy(node *r)
{
	node *p;
	p=new node;
	
	if(r=='\0')
	{
		cout<<'%';
		return 0;
	}

	else 
	{
		p->data=r->data;
		cout<<p->data;
        p->lchild=copy(r->lchild);
		p->rchild=copy(r->rchild);
		root2=p;
		return p;
	}
}

void pre1(node *r)                       //后序检验
{
	if(r)
	{	
		pre1(r->lchild);
		pre1(r->rchild);
        cout<<r->data<<' ';
	}
}

void pre2(node *r)                       //中序检验
{
	if(r)
	{
		pre2(r->lchild);
        cout<<r->data<<' ';
		pre2(r->rchild);
	}
}

⌨️ 快捷键说明

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