二叉树的创建,遍历和销毁

#include<iostream>
#include<stack>
#include<queue>
#include<string>
using namespace std;

// 二叉树的定义

typedef struct _BiNode
{
	char data;  // 数据域 
	struct _BiNode*LChlid;  // 左孩子指针
	struct _BiNode*RChlid;   // 右孩子指针
}BiNode,*BiTree;


// 递归先序遍历

void PreOrder(BiTree bt)
{
	if (bt!=NULL)
	{
		cout << bt->data << " ";
		PreOrder(bt->LChlid);
		PreOrder(bt->RChlid);
	}
}

// 递归中序遍历

void InOrder(BiTree bt)
{
	if (bt!=NULL)
	{
		InOrder(bt->LChlid);
		cout <<bt->data <<" ";
		InOrder(bt->RChlid);
	}
}

// 递归后序遍历

void PostOrder(BiTree bt)
{
	if (bt!=NULL)
	{
		PostOrder(bt->LChlid);
		PostOrder(bt->RChlid);
		cout << bt->data<<" ";
	}
}

// 后序求树的深度

int PostBiTreeDepth(BiTree bt)
{
	int hr = 0, hl = 0, max = 0;
	if (bt != NULL)
	{
		hl = PostBiTreeDepth(bt->LChlid);
		hr = PostBiTreeDepth(bt->RChlid);
		max = hr > hl ? hr : hl;
		return max + 1;
	}
	return 0;
}

// 递归先序创建二叉树

void CreatBiTree(BiTree *bt)
{
	char ch;
	cin >> ch;
	if (ch=='.')
	{
		*bt = NULL;
	}
	else
	{
		*bt = new BiNode;
		if (NULL==bt)
		{
			cout << "动态申请内存失败!" << endl;
			return;
		}
		(*bt)->data = ch;
		CreatBiTree(&((*bt)->LChlid));
		CreatBiTree(&((*bt)->RChlid));
	}
}

// 非递归先序遍历创建二叉树

void PreOrder1(BiTree bt)
{
	stack<BiNode*>s;
	BiNode *p = bt;

	while (p!=NULL||!s.empty())
	{
		if (p!=NULL)
		{
			cout <<p->data <<"  ";
			s.push(p);
			p = p->LChlid;
		}
		else
		{
			p = s.top();
			s.pop();
			p = p->RChlid;
		}
	}




}

// 非递归中序遍历二叉树

void InOrder1(BiTree bt)
{
	stack<BiNode *>s;
	BiNode *p = bt;
	while (p != NULL || !s.empty())
	{
		if (p!=NULL)
		{
			s.push(p);
			p = p->LChlid;
		}
		else
		{
			p = s.top();
			s.pop();
			cout <<p->data << "  ";
			p = p->RChlid;
		}
	}
}

// 非递归后序遍历二叉树

void PostOrder1(BiTree bt)
{
	stack<BiNode*>s;
	BiNode *p = bt;
	BiNode *q = NULL;

	while(p!=NULL||!s.empty())
	{
		while (p!=NULL)
		{
			s.push(p);
			p = p->LChlid;
		}
		if (!s.empty())
		{
			p = s.top();
			if (p->RChlid==NULL||p->RChlid==q)
			{
				cout << p->data << "  ";
				s.pop();
				q = p;
				p = NULL;
			}
			else
			{
				p = p->RChlid;
			}
		}

	
	}




}

//  二叉树的叶子结点个数

int GetLeafNodeNumber(BiTree bt)
{
	int NodeCount = 0;
	if (bt==NULL)
	{
		return 0;
	}
	else if (bt->LChlid==NULL&&bt->RChlid==NULL)
	{
		return 1;
	}
	else
	{
		return GetLeafNodeNumber(bt->LChlid)+GetLeafNodeNumber(bt->RChlid);
	}
}

// 二叉树的层序遍历

void LayerOrder(BiTree bt)
{
	queue<BiNode*>q;
	BiNode* p = NULL;
	if (NULL == bt)
	{
		return;   // 若树为空的话,则为空树,结束遍历
	}
	else
	{
		q.push(bt);  // 先把对头元素入队列
		while (!q.empty())   // 若队列为非空的话,则遍历未结束,继续进行
		{
			p = q.front();   // 对头元素出队并访问
			q.pop();
			cout << p->data<<"  ";
			if (p->LChlid!=NULL)
			{
				q.push(p->LChlid);
			}
			if (p->RChlid!=NULL)
			{
				q.push(p->RChlid);
			}
		}
	}

}

// 后序递归销毁二叉树

void PostDeStoryBiTree(BiTree *bt)
{
	if (*bt==NULL)
	{
		return;
	}
	else
	{
		PostDeStoryBiTree(&(*bt)->LChlid);
		PostDeStoryBiTree(&(*bt)->RChlid);
		delete *bt;
		*bt = NULL;
	}
}

// 给先序和中序创建二叉树

char *PreArray = "abdfgceh"; //先序序列
char InArray[] = "bfdgaceh";//中序序列

void CreatPreInOrderBiTree(BiTree &bt,int PreIndex,int InIndex,int subTreeLen)
{
	if (subTreeLen<=0)
	{
		bt = NULL;
		return;
	}
	else
	{
		// 创建先序对应的根节点
		bt = new BiNode;
		if (NULL==bt)
		{
			cout <<"动态申请内存失败!" <<endl;
			exit(1);
		}
		bt->data = PreArray[PreIndex];

		// 找到该节点在中序序列中的位置,并将中序序列划分为两个子树
		// strchr(InArray,PreArray[PreIndex]) 返回的是结点在中序序列中的地址

		int index = strchr(InArray,PreArray[PreIndex])-InArray;

		// 左子树的长度
		int LenF = index - InIndex;
		// 创建左子树

		CreatPreInOrderBiTree(bt->LChlid,PreIndex+1,InIndex,LenF);

		// 右子树的长度=总长度-左子树的长度-一个根节点
		int LenR = subTreeLen - LenF - 1;

		// 创建右子树,先序的字符位置从左子树和根结点后面开始,中序的位置从当前根节点的后面的一个位置开始
		CreatPreInOrderBiTree(bt->RChlid,PreIndex+LenF+1,index+1,LenR);
	}

}

int main(void)
{
	BiTree bt = NULL;
	CreatBiTree(&bt);
	PreOrder(bt);
	cout << endl;
	InOrder(bt);
	cout << endl;
	PostOrder(bt);
	cout << endl;

	int n = PostBiTreeDepth(bt);
	cout << n << endl;

	PreOrder1( bt);
	cout <<endl;
	InOrder1(bt);
	cout << endl;
	PostOrder1(bt);
	cout << endl;
	cout << GetLeafNodeNumber(bt) << endl;

	LayerOrder(bt);
	cout << endl;
	// AB.DF..G..C.E.H..

	int i = 0;

	PostDeStoryBiTree(&bt);


	BiTree bt1 = NULL;
//	PreInCreateTree1(bt1,0,0,strlen(PreArray));
	CreatPreInOrderBiTree(bt1, 0, 0, strlen(PreArray));
	
	PostOrder(bt1);
	cout << endl;
	PostDeStoryBiTree(&bt1);


	return 0;
}
#endif // !BiTree


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值