二叉树的遍历+同构判断

二叉树的遍历

最近数据结构学到树了,从电脑里把过去码的东西扒出来温习一下。
(今年5月份的时候在做团队程序设计天梯赛GPLT相关题目,碰到level2中关于二叉树的题目发现自己还没学过这些玩意,赶紧马不停蹄地去自学,码下以下代码。大部分代码要么是抄书的,要么是网上的,当时没有这个意识,都没有及时记录引用源,抱歉。)
数据结构中,树这块也是我学得最透彻的,感谢我那时候的倾情付出——纸上编程,纸上推演,再结合离散数学的学习,各种敲代码。

遍历

#include 
#include 
#include
#include
#include
#define MAX 20
#include
#define type char
using namespace std;
//typedef struct BTNode *Position;
//typedef Position btree;
typedef struct BTNode
{
	type data;
	BTNode* lchild, *rchild;
} btree;
btree* CreateBTree(btree *bt, bool isRoot)
{
	char ch;
	if (isRoot)
		printf("Root: ");
	fflush(stdin);         /* 清空缓存区 */
	cin>>ch;
	fflush(stdin);
	if (ch != '#')
	{
		isRoot = false;
		bt = new BTNode;
		bt->data = ch;
		bt->lchild = NULL;
		bt->rchild = NULL;
		printf("%c's lchild child is: ", bt->data);
		bt->lchild = CreateBTree(bt->lchild, isRoot);
		printf("%c's rchild child is: ", bt->data);
		bt->rchild = CreateBTree(bt->rchild, isRoot);
	}
	return bt;
}
void preorder1(btree *node)
{
	if(node)
	{
		cout<<node->data<<" ";
		preorder1(node->lchild);
		preorder1(node->rchild);
	}
}
void preorder2(btree *root)
{
	btree* temp;
	stack<btree*> S;
	temp=root;
	while(temp||!S.empty())
	{
		while(temp)
		{
			cout<<temp->data<<" ";
			S.push(temp);
			temp=temp->lchild;
		}
		if(!S.empty())
		{
			temp=S.top();
			S.pop();
			temp=temp->rchild;
		}
	}
}
void inorder1(btree *node)
{
	if(node)
	{
		inorder1(node->lchild);
		cout<<node->data<<" ";
		inorder1(node->rchild);
	}
}
void inorder2(btree *root)
{
	btree* temp;
	stack<btree*> S;
	temp=root;
	while(temp||!S.empty())
	{
		while(temp)
		{
			S.push(temp);
			temp=temp->lchild;
		}
		if(!S.empty())
		{
			temp=S.top();
			S.pop();
			cout<<temp->data<<" ";
			temp=temp->rchild;
		}
	}
}
void postorder1(btree* node)
{
	if(node)
	{
		postorder1(node->lchild);
		postorder1(node->rchild);
		cout<<node->data<<" ";
	}
}
void postorder2(btree *root)
{
	if (!root)
		return;
	stack<btree*> s;
	stack<btree*> output;
	s.push(root);
	while (!s.empty())
	{
		btree *curr= s.top();
		output.push(curr);
		s.pop();
		if (curr->lchild)
			s.push(curr->lchild);
		if (curr->rchild)
			s.push(curr->rchild);
	}
	//cout<<"output.size()"<
	while (!output.empty())
	{
		cout << output.top()->data << " ";
		output.pop();
	}
}
void postorder3(btree *root)
{
	if (!root)
		return;
	stack<btree*> s;
	s.push(root);
	btree *prev = NULL;
	while (!s.empty())
	{
		btree *curr= s.top();
		// we are traversing down the tree
		if (!prev||prev->lchild== curr|| prev->rchild == curr)
		{
			if (curr->lchild)
			{
				s.push(curr->lchild);
			}
			else if (curr->rchild)
			{
				s.push(curr->rchild);
			}
			else
			{
				cout << curr->data << " ";
				s.pop();
			}
		}
		// we are traversing up the tree from the lchild
		else if (curr->lchild == prev)
		{
			if (curr->rchild)
			{
				s.push(curr->rchild);
			}
			else
			{
				cout << curr->data << " ";
				s.pop();
			}
		}
		// we are traversing up the tree from the rchild
		else if (curr->rchild == prev)
		{
			cout << curr->data << " ";
			s.pop();
		}
		prev = curr;  // record previously traversed node
	}
}
void levelorder(btree* root)
{
	queue<btree*> q;
	btree *temp;
	if(!root)
		return;
	q.push(root);
	while(!q.empty())
	{
		temp=q.front();
		q.pop();
		cout<<temp->data<<" ";
		if(temp->lchild)
			q.push(temp->lchild);
		if(temp->rchild)
			q.push(temp->rchild);
	}
	return;
}
int height(btree *root)
{
	int HL,HR,MAXH;
	if(root)
	{
		HL=height(root->lchild);
		HR=height(root->rchild);
		MAXH=(HL>HR)?HL:HR;
		return (MAXH+1);
	}
	else
		return 0;
}
int main()
{
	btree *bt;
	bt = CreateBTree(bt, true);//递归创建
	cout<<"前序遍历序列"<<endl;
	preorder1(bt);//	递归前序遍历
	cout<<endl;
	cout<<"前序遍历序列(非递归)"<<endl;
	preorder2(bt);//	非递归前序遍历
	cout<<endl;
	cout<<"中序遍历序列"<<endl;
	inorder1(bt);//		递归中序遍历
	cout<<endl;
	cout<<"中序遍历序列(非递归)"<<endl;
	inorder2(bt);//		非递归中序遍历
	cout<<endl;
	cout<<"后序遍历序列"<<endl;
	postorder1(bt);//		递归后序遍历
	cout<<endl;
	cout<<"后序遍历序列(非递归)"<<endl;
	//postorder2(bt);
	postorder3(bt);//		非递归后序遍历 3
	cout<<endl;
	cout<<"后序遍历序列(非递归双堆栈)"<<endl;
	postorder2(bt);//		非递归后序遍历 2
	cout<<endl;
	cout<<"层序遍历序列"<<endl;
	levelorder(bt);//	非递归层序遍历
	cout<<endl;
	cout<<"这棵树的高度为(递归): ";
	cout<<height(bt)<<endl;//	递归后序遍历求树高
	return 0;
}

同构

这段是抄浙大数据结构mooc的

#include
#define type char
#define type char
#define MAX 20
using namespace::std;
struct treenode
{
	type data;
	int lchild;
	int rchild;
} tree1[MAX],tree2[MAX];
int buildtree(treenode tree[])
{
	bool book[MAX]= {false};
	int root;
	int N,i;
	char left,right;
	cin>>N;
	if(N)
	{
		for(i=0; i<N; i++)
		{
			cin>>tree[i].data>>left>>right;
			if(left!='-')
			{
				tree[i].lchild=left-'0';
				book[tree[i].lchild]=1;
			}
			else
				tree[i].lchild=-1;
			if(right!='-')
			{
				tree[i].rchild=right-'0';
				book[tree[i].rchild]=true;
			}
			else
				tree[i].rchild=-1;
		}
		for(i=0; i<N; i++)
			if(book[i])
				break;
		root=i;
		return root;
	}
	else
		return -1;
}
bool isomorphic(int R1,int R2)
{
	if(R1==-1&&R2==-1)
		return true;
	if((R1==-1&&R2!=-1)||(R1!=-1&&R2==-1))
		return false;
	if(tree1[R1].data!=tree2[R2].data)
		return false;
	if(tree1[R1].lchild==-1&&tree2[R2].lchild==-1)
		return isomorphic(tree1[R1].rchild,tree2[R2].rchild);
	if(tree1[R1].rchild==-1&&tree2[R2].rchild==-1)
		return isomorphic(tree1[R1].lchild,tree2[R2].lchild);
	if((tree1[R1].lchild!=-1&&tree2[R2].lchild!=-1)
	        &&(tree1[tree1[R1].lchild].data==tree2[tree2[R2].lchild].data))
		return isomorphic(tree1[R1].lchild,tree2[R2].lchild)
		       &&isomorphic(tree1[R1].rchild,tree2[R2].rchild);
}
void display(treenode tree[])
{
	for(int i=0; tree[i].data!='\0'; i++)
	{
		cout<<tree[i].data<<" ";
		cout<<tree[i].lchild<<" ";
		cout<<tree[i].rchild<<endl;
	}
	return;
}
int main()
{
	int root1=buildtree(tree1);
	int root2=buildtree(tree2);
	if(isomorphic(root1,root2))
		cout<<"YES"<<endl;
	else
		cout<<"No"<<endl;
	cout<<"tree1:"<<endl;
	display(tree1);
	cout<<"tree2:"<<endl;
	display(tree2);
	return 0;
}

你可能感兴趣的:(数据结构)