SWUST数据结构--交换二叉树的孩子节点

#include<iostream>
#include<cstdlib>
using namespace std;

typedef struct node
{
	char data;
	struct node *l,*r;
}Tree;

void Init(Tree *&T)
{
	char str;
	cin>>str;
	if(str!='#')
	{
		T = (Tree *)malloc(sizeof(Tree));
		T->data = str;
		Init(T->l);
		Init(T->r);
	}
	else T = NULL;
}

void Exchange(Tree *&T)
{
	Tree *t;
	if(T!=NULL)
	{
		t = T->l;
		T->l = T->r;
		T->r = t;
		Exchange(T->l);
		Exchange(T->r);
	}
}

void Mid(Tree *&T)
{
	if(T!=NULL)
	{
		Mid(T->l);
		cout<<T->data;
		Mid(T->r);
	}
}

void Pro(Tree *&T)
{
	if(T!=NULL)
	{
		cout<<T->data;
		Pro(T->l);	
		Pro(T->r);
	}
}

int main()
{
	Tree *T;
	Init(T);
	Exchange(T);
	Mid(T);
	cout<<endl;
	Pro(T);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41681743/article/details/80378602