二叉树各种操作


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<iomanip>
#include<stack>
#include <queue>
using namespace std;
typedef struct BitNode
{
	char data;
	BitNode* lchild;
	BitNode* rchild;

}BitNode, *BitTree;
typedef struct
{
	BitTree link;
	int tag;
}treeType;

void visit(BitTree &T)
{
	cout << T->data<<" ";
}
//二叉树遍历,递归实现
void PreOrder(BitTree& T)
{
	if (T!=NULL)
	{
		visit(T);
		PreOrder(T->lchild);
		PreOrder(T->rchild);
	}
}
void InOrder(BitTree &T)
{
	if (T != NULL)
	{
		InOrder(T->lchild);
		visit(T);
		InOrder(T->rchild);
	}
}
void PostOrder(BitTree &T)
{
	if (T != NULL)
	{
		PostOrder(T->lchild);
		PostOrder(T->rchild);
		visit(T);

	}
}
//二叉树遍历 ,非递归实现
void preOrder(BitTree &T)
{
	stack<BitTree> s;
	BitTree p = T;
	if (p == NULL)
		cout << "二叉树为空!" << endl;
	while (!((p == NULL) && s.empty()))
	{
		if (p != NULL)
		{
			visit(p);
			s.push(p);
			p = p->lchild;
		}
		else
		{
			p = s.top();
			 s.pop();
			p = p->rchild;
		}
	}
}
void inOrder(BitTree &T)
{
	stack<BitNode> s;
	BitTree p = T;
	if (p == NULL)
		cout << "二叉树为空!" << endl;
	while (!((p == NULL) && s.empty()))
	{
		if (p != NULL)
		{
			
			s.push(*p);
			p = p->lchild;
		}
		else
		{
			p = &s.top();
			visit(p);
			s.pop();
			p = p->rchild;
		}
	}
}
void postOrder(BitTree &T)
{
	BitTree p, post;
	post = NULL;
	p = T;
	stack<BitTree> s;
	if (T == NULL)
	{
		cout << "二叉树为空!" << endl;
		return;
	}
		

	while (p != NULL)
	{
		s.push(p);
		p = p->lchild;
	}

	while (!s.empty())
	{
		p = s.top();
		s.pop();
			
		
		if ((p)->rchild == NULL || (p)->rchild == post)
		{
			visit(p);
			post = p;
		}
		else
		{
			s.push(p);
			p = p->rchild;
			while (p)
			{
				s.push(p);
				p = p->lchild;
			}
		}
	}
}
void postOrder2(BitTree &T)
{
	stack<treeType> s;
	BitTree p=T;
	if (T == NULL)
	{
		cout << "二叉树为空!" << endl;
		return;
	}
	while (!((p == NULL) && s.empty()))
	{
		if (p != NULL)
		{
			treeType t;
			t.link = p;
			t.tag = 1;
			s.push(t);
			p = p->lchild;
		}
		else
		{
			treeType tt = s.top();
			s.pop();

			if (tt.tag == 1)
			{
				tt.tag = 2;
				s.push(tt);
				p = tt.link->rchild;
			}
			else
			{
				visit(tt.link);
				p = NULL;//必须设置成空
			}
		}
	}
}
//二叉树层次遍历
void layerOrder(BitTree& T)
{
	queue<BitTree> q;
	BitTree p = T;
	if (T == NULL)
	{
		cout << "二叉树为空!" << endl;
		return;
	}
	q.push(T);
	while (!(q.empty()))
	{
		BitTree temp = q.front();
		q.pop();
		visit(temp);
		if (temp->lchild)
			q.push(temp->lchild);
		if (temp->rchild)
			q.push(temp->rchild);
	}
}
//构造完全二叉树,根据节点数目
BitTree genTree(int num)
{
	BitTree p,tree=NULL;
	
	int DATA = 'A';
	queue<BitTree> q;
	if (num == 0)
		return tree;
	p = new BitNode();
	p->data = DATA;
	p->lchild = NULL;
	p->rchild = NULL;
	tree = p;
	num--;
	q.push(p);
	while (num != 0)
	{
		BitTree temp = q.front();
		q.pop();
		p = new BitNode();
		DATA++;
		p->data = DATA;
		p->lchild = NULL;
		p->rchild = NULL;
		temp->lchild = p;
		q.push(p);
		num--;
		if (num != 0)
		{
			p = new BitNode();
			DATA++;
			p->data = DATA ;
			p->lchild = NULL;
			p->rchild = NULL;
			temp->rchild = p;
			q.push(p);
			num--;
		}
	}
	return tree;	
}

//由先序和中序构造二叉树
BitTree preInGen(char pre[],char in[], int m, int n,int p,int q)
{//麻烦之一是flag的错误;
	//麻烦之二是调试递归的时候第一次跳到rchild时候是从最深的栈开始的
	//当前递归栈范围的变量在当前递归栈是不变的,不同递归栈之间的同名变量是内存不同的
	if (n<m||q<p)
		return NULL;//结束条件1
	BitTree root, temp=NULL;
	int flag=0;
	
	temp = new BitNode();
	temp->data = pre[m];
	temp->lchild = NULL;
	temp->rchild = NULL;
	root = temp;
	
	if (m == n||p==q)//结束条件2
		return root;
	for (int i = p;i <= q;i++)//确定中间元素是从当前串左数第几个
	{
		if (in[i] != pre[m])
		{
			flag++;
		}
		else
			break;
	}
	
	temp->lchild = preInGen(pre,in,m+1,m+flag,p,p+flag-1);	
	temp->rchild= preInGen(pre, in, m + flag+1, n, p+flag+1,q);
	return root;
}
//由后序遍历和中序遍历恢复二叉树
BitTree postInGen( char post[], char in[], int m, int n, int p, int q)
{
	if (n<m || q<p)
		return NULL;//结束条件1
	BitTree root, temp = NULL;
	int flag = 0;
	

	temp = new BitNode();
	temp->data = post[n];
	temp->lchild = NULL;
	temp->rchild = NULL;
	root = temp;

	if (m == n || p == q)//结束条件2
		return root;
	for (int i = p;i <= q;i++)//确定中间元素是从当前串左数第几个
	{
		if (in[i] != post[n])
		{
			flag++;
		}
		else
			break;
	}
	temp->lchild = postInGen(post, in, m , m + flag-1, p, p + flag - 1);
	temp->rchild = postInGen(post, in, m + flag , n-1, p + flag + 1, q);
	return root;
}
int main()
{
	BitTree t=genTree(10);
	cout << "先序遍历,递归: ";
	PreOrder(t);	cout << endl;
	cout << "先序遍历,非递归: ";
	preOrder(t);cout << endl;
	cout << "中序遍历,递归: ";
	InOrder(t);cout << endl;
	cout << "中序遍历,非递归: ";
	inOrder(t);cout << endl;
	cout << "后序遍历,递归: ";
	PostOrder(t);cout << endl;
	cout << "后序遍历,非递归: ";
	postOrder(t);cout << endl;
	cout << "后序遍历,非递归2: ";
	postOrder2(t);cout << endl;
	cout << "层次遍历: ";
	layerOrder(t);cout << endl;

	
	char pre[9] = {'A','B','C','D','E','F','G','H','I'};
	char in[9] = {'B','C','A','E','D','G','H','F','I'};
	char post[9] = { 'C','B','E','H','G','I','F','D','A' };
	cout << "先序中序还原二叉树测试";
	BitTree tr = preInGen(pre, in, 0, 8, 0, 8);
	PostOrder(tr);cout << endl;
	cout << "后序中序还原二叉树测试";
	BitTree trr = postInGen(post, in, 0, 8, 0, 8);
	PreOrder(trr);
	int aa;
	cin >> aa;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值