小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

输入一颗二元树,从上向下按层打印树的每个节点,同一层中,按照从左到右的顺序打印。

输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印,具体实现如下:

#include <iostream>
using namespace std;

typedef struct BSTNode
{
	int nValue;
	struct BSTNode *pLChild;
	struct BSTNode *pRChild;
}*PBSTree;


PBSTree Insert(PBSTree pRoot, int value)
{
	if (pRoot == NULL)
	{
		pRoot = new BSTNode;
		pRoot->nValue = value;
		pRoot->pLChild = NULL;
		pRoot->pRChild = NULL;
	}
	else
	{
		if (value < pRoot->nValue)
		{
			pRoot->pLChild = Insert(pRoot->pLChild, value);
		}
		else if (value > pRoot->nValue)
		{
			pRoot->pRChild = Insert(pRoot->pRChild, value);
		}
	}
	return pRoot;
}

//非递归实现插入
void Insert1(PBSTree *pRoot, int value)
{
	if ((*pRoot) == NULL)
	{
		(*pRoot) = new BSTNode;
		(*pRoot)->nValue = value;
		(*pRoot)->pLChild = NULL;
		(*pRoot)->pRChild = NULL;
	}
	else
	{
		PBSTree pCur = (*pRoot);
		while (1)
		{
			if (value == pCur->nValue)
			{
				return;
			}
			else if (value < pCur->nValue)
			{
				if (pCur->pLChild == NULL)
				{
					PBSTree pTmp = new BSTNode;
					pTmp->nValue = value;
					pTmp->pLChild = pTmp->pRChild = NULL;
					pCur->pLChild = pTmp;

					return;
				}
				else
				{
					pCur = pCur->pLChild;
				}

			}
			else
			{
				if (pCur->pRChild == NULL)
				{
					PBSTree pTmp = new BSTNode;
					pTmp->nValue = value;
					pTmp->pLChild = pTmp->pRChild = NULL;
					pCur->pRChild = pTmp;

					return;
				}
				else
				{
					pCur = pCur->pRChild;
				}
			}
		}
	}
}

void Visit(PBSTree pRoot)
{
	cout << pRoot->nValue << " ";
}

//该树转换为它的镜像    递归实现
PBSTree Change(PBSTree pRoot)
{
	if (pRoot->pLChild == NULL && pRoot->pRChild == NULL)
	{
		return NULL;
	}
	else
	{
		PBSTree pTmp = pRoot->pLChild;
		pRoot->pLChild = pRoot->pRChild;
		pRoot->pRChild = pTmp;
		Change(pRoot->pLChild);
		Change(pRoot->pRChild);


	}

	return pRoot;
}

//该树转换为它的镜像    非递归实现
void Change1(PBSTree *pRoot)
{
	PBSTree stack[100];
	int nTop = 0;
	PBSTree pCur = *pRoot;
	while (nTop > 0 || pCur != NULL)
	{
		while (pCur != NULL)
		{
			PBSTree pTmp = pCur->pLChild;
			pCur->pLChild = pCur->pRChild;
			pCur->pRChild = pTmp;
			stack[nTop++] = pCur;
			pCur = pCur->pLChild;
		}

		if (nTop > 0)
		{
			nTop--;
			pCur = stack[nTop];
			pCur = pCur->pRChild;
		}
	}
}



//前序递归实现
void PreOrder(PBSTree pRoot)
{
	if (pRoot == NULL)
	{
		return;
	}
	Visit(pRoot);
	PreOrder(pRoot->pLChild);
	PreOrder(pRoot->pRChild);
}

void PreOrder1(PBSTree pRoot)
{
	PBSTree stack[100];
	int nTop = 0;
	PBSTree pCur = pRoot;
	while (nTop > 0 || pCur != NULL)
	{
		while (pCur != NULL)
		{
			Visit(pCur);
			stack[nTop++] = pCur;
			pCur = pCur->pLChild;
		}


		if (nTop > 0)
		{
			nTop--;
			pCur = stack[nTop];
			pCur = pCur->pRChild;
		}
	}
}
//中序非递归实现
void InOrder1(PBSTree pRoot)
{
	PBSTree stack[100];
	int nTop = 0;
	PBSTree pCur = pRoot;
	while (nTop > 0 || pCur != NULL)
	{
		while (pCur != NULL)
		{

			stack[nTop++] = pCur;
			pCur = pCur->pLChild;
		}
		if (nTop > 0)
		{
			nTop--;
			pCur = stack[nTop];
			Visit(pCur);
			pCur = pCur->pRChild;
		}
	}
}

//中序遍历 递归实现
void InOrder(PBSTree pRoot)
{
	if (pRoot == NULL)
	{
		return;
	}
	InOrder(pRoot->pLChild);
	Visit(pRoot);
	InOrder(pRoot->pRChild);
}

//后序 递归实现
void PostOrder(PBSTree pRoot)
{
	if (pRoot == NULL)
	{
		return;
	}
	PostOrder(pRoot->pLChild);
	PostOrder(pRoot->pRChild);
	Visit(pRoot);
}

//后序 非递归实现
void PostOrder1(PBSTree pRoot)
{

	PBSTree stack[100];
	int nTop = 0;
	PBSTree pCur = pRoot;
	PBSTree IsVisited = NULL;
	while (nTop > 0 || pCur != NULL)
	{
		while (pCur != NULL)
		{
			stack[nTop++] = pCur;
			pCur = pCur->pLChild;
		}

		pCur = stack[nTop - 1];
		if (pCur->pRChild == NULL && IsVisited == pCur->pRChild)
		{
			Visit(pCur);
			nTop--;
			IsVisited = pCur;
			pCur = NULL;
		}
		else
		{
			pCur = pCur->pRChild;
		}
	}
}

//水平遍历二叉搜索树
void LevelOrder(PBSTree pRoot)
{
	PBSTree queue[100];
	int nHead = 0;
	int nTail = 0;

	PBSTree pCur = pRoot;
	if (pCur)
	{
		queue[nTail++] = pCur;
		while (nHead != nTail)
		{
			PBSTree pTmp = queue[nHead++];
			Visit(pTmp);
			if (pTmp->pLChild)
			{
				queue[nTail++] = pTmp->pLChild;
			}
			if (pTmp->pRChild)
			{
				queue[nTail++] = pTmp->pRChild;
			}
		}
	}
}
int main()
{
	PBSTree pRoot = NULL;
	Insert1(&pRoot, 8);
	Insert1(&pRoot, 6);
	Insert1(&pRoot, 5);
	Insert1(&pRoot, 7);
	Insert1(&pRoot, 10);
	Insert1(&pRoot, 9);
	Insert1(&pRoot, 11);
	LevelOrder(pRoot);
	printf("\n");
	system("pause");
	return 0;
}

运行效果如图1所示:

图1 运行效果


posted on 2014-09-14 22:42  牛栏山1  阅读(120)  评论(0编辑  收藏  举报

导航