# 二叉树的深度优先和广度优先遍历

8966人阅读 评论(5)

/*************************** bintree.h文件 *****************************/

#ifndef _BINTREE_H

#define _BINTREE_H

template <class T>

class CBintree

{

public:

CBintree();

bool Depth_RF();//先根深度遍历

bool Depth_RM();//中根深度遍历

bool WidthFS(); //层次遍历

protected:

private:

struct TreeNode

{

T va;

TreeNode *plchild;

TreeNode *prchild;

};

TreeNode *m_pBintree;

};

#endif

template <class T>

CBintree<T>::CBintree()

{

m_pBintree = new TreeNode;

TreeNode *pNode2 = new TreeNode;

TreeNode *pNode3 = new TreeNode;

TreeNode *pNode4 = new TreeNode;

TreeNode *pNode5 = new TreeNode;

TreeNode *pNode6 = new TreeNode;

TreeNode *pNode7 = new TreeNode;

m_pBintree->va = (T)1;

m_pBintree->plchild = pNode2;

m_pBintree->prchild = pNode3;

pNode2->va = (T)2;

pNode2->plchild = pNode4;

pNode2->prchild = pNode5;

pNode3->va = (T)3;

pNode3->plchild = NULL;

pNode3->prchild = pNode6;

pNode4->va = (T)4;

pNode4->plchild = NULL;

pNode4->prchild = NULL;

pNode5->va = (T)5;

pNode5->plchild = pNode7;

pNode5->prchild = NULL;

pNode6->va = (T)6;

pNode6->plchild = NULL;

pNode6->prchild = NULL;

pNode7->va = (T)7;

pNode7->plchild = NULL;

pNode7->prchild = NULL;

}

template <class T>

bool CBintree<T>::Depth_RF() //先根深度遍历

{

cout << "先根遍历序列:/n";

stack <TreeNode> s;

s.push(*m_pBintree);

while (!s.empty())

{

TreeNode node_s = s.top();

cout << node_s.va << "/n";

s.pop();

if (node_s.prchild != NULL)

{

s.push(*node_s.prchild);

}

if (node_s.plchild != NULL)

{

s.push(*node_s.plchild);

}

}

return true;

}

template <class T>

bool CBintree<T>::Depth_RM() //中根深度遍历

{

cout << "中根遍历序列:/n";

stack <TreeNode> s;

TreeNode *pNode = m_pBintree;

while (pNode != NULL || !s.empty())

{

while (pNode != NULL)

{

s.push(*pNode);

pNode = pNode->plchild;

}

if (!s.empty())

{

TreeNode node_s = s.top();

cout << node_s.va << "/n";

s.pop();

pNode = node_s.prchild;

}

}

return true;

}

template <class T>

bool CBintree<T>::WidthFS() //层次遍历

{

cout << "层次遍历序列:/n";

queue <TreeNode> q;

q.push(*m_pBintree);

while (!q.empty())

{

TreeNode *pNode = &q.front();

cout << pNode->va << "/n";

if (pNode->plchild != NULL)

{

q.push(*pNode->plchild);

}

if (pNode->prchild != NULL)

{

q.push(*pNode->prchild);

}

q.pop();

}

return true;

}

/************************ main.cpp 文件 ****************************/

#include <iostream>

#include <stack>

#include <queue>

using namespace std;

#include "bintree.h"

int main()

{

CBintree <int> bt;

bt.Depth_RF();

bt.Depth_RM();

bt.WidthFS();

return 0;

}

/***************** 教科书标准算法及优化算法（转）*******************/
1.

void PreOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;

while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL)             //

{
visite(p->data);
push(s,p);
p=p->lchild;
}

if (!StackEmpty(s))         //

{
p=pop(s);
p=p->rchild;
}//endif

}//endwhile
}

2.

void InOrderUnrec(Bitree *t)
{
Stack s;
StackInit(s);
Bitree *p=t;

while (p!=NULL || !StackEmpty(s))
{
while (p!=NULL)             //

{
push(s,p);
p=p->lchild;
}

if (!StackEmpty(s))
{
p=pop(s);
visite(p->data);        //

p=p->rchild;            //

}//endif

}//endwhile
}

3.

typedef enum{L,R} tagtype;
typedef struct
{
Bitree ptr;
tagtype tag;
}stacknode;

typedef struct
{
stacknode Elem[maxsize];
int top;
}SqStack;

void PostOrderUnrec(Bitree t)
{
SqStack s;
stacknode x;
StackInit(s);
p=t;

do
{
while (p!=null)        //

{
x.ptr = p;
x.tag = L;         //

push(s,x);
p=p->lchild;
}

while (!StackEmpty(s) && s.Elem[s.top].tag==R)
{
x = pop(s);
p = x.ptr;
visite(p->data);   //tag
R，表示右子树访问完毕，故访问根结点
}

if (!StackEmpty(s))
{
s.Elem[s.top].tag =R;     //

p=s.Elem[s.top].ptr->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec

4.前序最简洁非递归算法

void PreOrderUnrec(Bitree *t)

{

Bitree *p;

Stack s;

s.push(t);

while (!s.IsEmpty())

{

s.pop(p);

visit(p->data);

if (p->rchild != NULL) s.push(p->rchild);

if (p->lchild != NULL) s.push(p->lchild);

}

}

5.后序算法之二

void BT_PostOrderNoRec(pTreeT root)

{

stack<treeT *> s;

pTreeT pre=NULL;

while ((NULL != root) || !s.empty())

{

if (NULL != root)

{

s.push(root);

root = root->left;

}

else

{

root = s.top();

if (root->right!=NULL && pre!=root->right){

root=root->right;

}

else{

root=pre=s.top();

visit(root);

s.pop();

root=NULL;

}

}

}

}

/**************************** *******************************/

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：531837次
• 积分：6321
• 等级：
• 排名：第4010名
• 原创：14篇
• 转载：458篇
• 译文：0篇
• 评论：63条
评论排行
最新评论