二叉树的遍历

                                                                                  二叉树的遍历

对于一个二叉树定义如下:

typedef char ElemType;
#define END '#'

typedef struct BtNode  
{
	BtNode *leftchild;
	BtNode *rightchild;
	ElemType data;
}BtNode, *BinaryTree;

 

我们需要实现以下的遍历:

1.前序遍历:

       1.1递归

      1.2非递归

2.中序遍历:

       2.1递归

       2.2非递归

3.后序遍历:

       3.1递归

       3.2非递归

4.层次遍历:

5.工字型遍历:

 

TO1.前序遍历(以“根左右”的形式遍历一遍二叉树)输出:A B C D E F G H

       To1.1:递归

void PreOrder(BtNode *p)
{
	if (p != NULL)
	{
		cout << p->data << " ";
		PreOrder(p->leftchild);
		PreOrder(p->rightchild);
	}
}

       To1.2:非递归 

void NicePreOrder_2(BtNode *ptr)
{
	if (NULL == ptr) return;
	stack<BtNode *> st;
	st.push(ptr);
	while (!st.empty())
	{
		ptr = st.top(); st.pop();
		cout << ptr->data << " ";
		if (ptr->rightchild != NULL)
		{
			st.push(ptr->rightchild);
		}
		if (ptr->leftchild != NULL)
		{
			st.push(ptr->leftchild);
		}
	}
	cout << endl;
}


void NicePreOrder(BtNode *ptr)
{
	if (NULL == ptr) return;
	stack<BtNode *> st;
	while (ptr != NULL || !st.empty())
	{
		while (ptr != NULL)
		{
			cout << ptr->data << " ";
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		ptr = ptr->rightchild;
	}
	cout << endl;
}

TO2.中序遍历(以“左根右”的形式遍历一遍二叉树)输出:C B E D F A G H

       To2.1:递归

void InOrder(BtNode *p)
{
	if (p != NULL)
	{
		InOrder(p->leftchild);
		cout << p->data << " ";
		InOrder(p->rightchild);
	}
}

       To2.2:非递归 

void NiceInOrder(BtNode * ptr)
{
	if (NULL == ptr) return;
	stack<BtNode *> st;
	while (ptr != NULL || !st.empty())
	{
		while (ptr != NULL)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		cout << ptr->data << " ";
		ptr = ptr->rightchild;
	}
	cout << endl;
}

TO3.后序遍历(以“左右根”的形式遍历一遍二叉树)输出:C E F D B H G A

       To3.1:递归

void PastOrder(BtNode *p)
{
	if (p != NULL)
	{
		PastOrder(p->leftchild);
		PastOrder(p->rightchild);
		cout << p->data << " ";
	}
}

       To3.2:非递归 

void NicePastOrder(BtNode * ptr)
{
	if (NULL == ptr) return;
	stack<BtNode *> st;
	BtNode *tag = NULL;
	while (ptr != NULL || !st.empty())
	{
		while (ptr != NULL)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		if (ptr->rightchild == NULL || ptr->rightchild == tag)
		{
			cout << ptr->data << " ";
			tag = ptr;
			ptr = NULL;
		}
		else
		{
			st.push(ptr);
			ptr = ptr->rightchild;
		}
	}
	cout << endl;
}

TO4.层次遍历(一层一层的遍历一遍二叉树)输出:A B G C D H E F

void LevelOrder(BtNode *ptr)
{
	if (NULL == ptr) return;
	queue<BtNode *> qt;
	qt.push(ptr);
	while (!qt.empty())
	{
		ptr = qt.front(); qt.pop();
		cout << ptr->data << " ";
		if (ptr->leftchild != NULL)
		{
			qt.push(ptr->leftchild);
		}
		if (ptr->rightchild != NULL)
		{
			qt.push(ptr->rightchild);
		}
	}
	cout << endl;
}

TO5.工字型遍历(以s型方式遍历一遍二叉树)输出:A G B C D H F E

void NiceGongOrder(BtNode *ptr)
{
	if (NULL == ptr) return;
	stack<BtNode*> st;
	stack<BtNode*> qt;
	int tag = 1;
	qt.push(ptr);
	while (!st.empty() || !qt.empty())
	{
		if (1 == tag)
		{
			while (!qt.empty())
			{
				ptr = qt.top(); qt.pop();
				cout << ptr->data << " ";
				if (ptr->leftchild != NULL)
				{
					st.push(ptr->leftchild);
				}
				if (ptr->rightchild != NULL)
				{
					st.push(ptr->rightchild);
				}
			}
			tag = 0;
		}
		if (0 == tag)
		{
			while (!st.empty())
			{
				ptr = st.top(); st.pop();
				cout << ptr->data << " ";
				if (ptr->rightchild != NULL)
				{
					qt.push(ptr->rightchild);
				}
				if (ptr->leftchild != NULL)
				{
					qt.push(ptr->leftchild);
				}

			}
			tag = 1;
		}
	}
	cout << endl;
}

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值