14.二叉树 中序遍历 先序遍历 的非递归实现 以及 二叉树 的复制 及判断二叉树的等价性


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#define OK 0
#define ERROR 1
using namespace std;
typedef char ElemType;
FILE *fp;
void InitFile()
{
	bool e;
	fopen_s(&fp, "data.txt", "r");
	//fopen_s(&fp, "tdata.txt", "w+");
	if (!fp) exit(ERROR);
	return;
}
typedef bool Status;


/******
file input:
abc00d00ef00g00
*******/

//二叉树的 链接式存储表示
typedef  struct node
{
	ElemType data;
	struct node *lchild, *rchild;

}TreeNode, *tree_ptr;

Status CreateBitTree(tree_ptr &t)//创建一颗二叉树
{
	char c;
	//c = fgetchar();
	fscanf_s(fp, "%c", &c);
	printf("%c", c);
	if (c == '0'){ t = NULL;  return OK; }
	else
	{
		t = (tree_ptr)malloc(sizeof(TreeNode));
		if (!t) exit(ERROR);
		t->data = c;
		CreateBitTree(t->lchild);
		CreateBitTree(t->rchild);
	}
}
//分别 用 递归和非递归 实现二叉树的遍历 
Status Inorder(tree_ptr t)
{
	if (t)
	{
		Inorder(t->lchild);
		printf("%c ", t->data);
		Inorder(t->rchild);
	}
	return OK;
}

stack<tree_ptr> S;
Status Inorder_n(tree_ptr t)//中序遍历
{
	if (t)	S.push(t);
	while (!S.empty())
	{
		tree_ptr t = S.top();
		while (t)
		{
			S.push(t->lchild);
			t = t->lchild;
		}
		S.pop();
		if (!S.empty())
		{
			t = S.top(); S.pop();
			printf("%c ", t->data);
			S.push(t->rchild);
		}
	}
	return OK;
}//按照访问的急迫程度  压入栈中 对每个节点的访问 都分为  访问左子树  同时将该节点压入栈中  
//对每个从栈中pop出来的节点  visit该节点 并同时把该节点的右子树压入栈中

Status Preorder(tree_ptr t)
{
	if (t)
	{
		printf("%c ", t->data);
		Preorder(t->lchild);
		Preorder(t->rchild);
	}
	return OK;
}


Status Preorder_n(tree_ptr t)
{
	stack<tree_ptr> S2;
	if(t) S2.push(t);
	while (!S2.empty())
	{
		tree_ptr t;
		t = S2.top(); S2.pop();
		printf("%c ", t->data);
		if(t->rchild) S2.push(t->rchild);
		if(t->lchild) S2.push(t->lchild);
	}
	return OK;
}
Status Postorder(tree_ptr t)
{
	if (t)
	{
		Postorder(t->lchild);
		Postorder(t->rchild);
		printf("%c ", t->data);
	}
	return OK;
}

Status Copy_Bittree(tree_ptr &t, tree_ptr &c)//实现二叉树的复制  使c树的结构与t树一样
{
	if (t == NULL)  c = NULL;
	else
	{
		c = (tree_ptr)malloc(sizeof(TreeNode));
		if (!c) exit(ERROR);
		c->data = t->data;
		Copy_Bittree(t->lchild, c->lchild);
		Copy_Bittree(t->rchild, c->rchild);
	}
	return OK;
}
  
Status Equal_Bittree(tree_ptr a, tree_ptr b)
{
	return   (!a&&!b) || (a &&  b && a->data == b->data  &&  Equal_Bittree(a->lchild, b->lchild) && Equal_Bittree(a->rchild, b->rchild));
}
//by zhaoyang 2014.4.19
int main()
{
	InitFile();
	/*fp = fopen("data.txt", "r+");
	if (!fp) exit(ERROR);*/
	tree_ptr A;
	CreateBitTree(A);

	printf("\n先序遍历:\n");
	Preorder(A);

	printf("\n递归先序遍历:\n");
	Preorder_n(A);

	printf("\n中序遍历:\n");
	Inorder(A);
	printf("\n递归中序遍历:\n");
	Inorder_n(A);

	printf("\n后序遍历:\n");
	Postorder(A);

	printf("\n二叉树复制阶段:-----------------\n");
	tree_ptr B;
	Copy_Bittree(A, B);
	printf("\n先序遍历B树:\n");
	Preorder(B);
	printf("\n判断等价树:\n");
	if (Equal_Bittree(A, B))  printf("A树与B树等价");


	printf("\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值