[数据结构] 二叉树的递归与非递归遍历

输入                                   A

                           B                          C

                D               E           F             G

          H        I       J

以先序遍历,用“#”表示为空,输入ABDH##I##EJ###CF##G##

前序遍历:根->左->右  ABDHIEJCFG

中序遍历:左->根->右  HDIBJEAFCG

后序遍历:左->右->根  HIDJEBFGCA

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

typedef char Elementtype;

typedef struct node  
{  
    struct node *lchild;  
    struct node *rchild;  
    Elementtype data;
}BiTreeNode, *BTREE;  
      
void CreateBTree(BTREE &T) //先序顺序建立二叉树 
{  
    char c;  
    cin >> c;  
    if('#' == c)  
      T = NULL;  
    else  
    {  
        T = new node;  
        T->data = c;
        CreateBTree(T->lchild);  //递归建立 
        CreateBTree(T->rchild);  
    }  
}  

int IsEmpty(BTREE root)
{
    if (root == NULL)
      return 1;
    else
      return 0;
}

void Visit(Elementtype data)
{
    printf("%c ",data);
}
void InOrder(BTREE root)//递归中序 
{
    if (IsEmpty(root) == 0)
    {
        InOrder(root->lchild);
        Visit(root->data);
        InOrder(root->rchild);
    }
}

void PreOrder(BTREE root){//递归前序 
    if(IsEmpty(root) == 0){
        Visit(root->data);
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }
}
void PostOrder(BTREE root){//递归后序 
    if(IsEmpty(root) == 0){
        PostOrder(root->lchild);
        PostOrder(root->rchild);
        Visit(root->data);
    }
}

void N_InOrder(BTREE T)//非递归中序遍历 
{
	stack<BTREE> STACK;
	if(IsEmpty(T))
    {
    	cout << "Empty Tree!" << endl;
    	return;
    }
    while(T || !STACK.empty())
	{
		while(T)
		{
			STACK.push(T);
			T=T->lchild;
		}
		T=STACK.top();
		STACK.pop();
		printf("%c ",T->data);
		T=T->rchild;
	}              
}

void N_PreOrder(BTREE T)
{
	stack<BTREE> STACK;
	if(IsEmpty(T))
    {
    	cout << "Empty Tree!" << endl;
    	return;
    }
    while(T || !STACK.empty())
	{
		while(T)
		{
			STACK.push(T);
			printf("%c ",T->data);
			T=T->lchild;
		}
		T=STACK.top();
		STACK.pop();		
        T=T->rchild;		
	}
} 

void N_PostOrder(BTREE T) // 后序遍历的非递归  
{  
    stack<BTREE> STACK;  
    BTREE curr = T ;           // 指向当前要检查的节点
    BTREE previsited = NULL;    // 指向前一个被访问的节点
    while(curr != NULL || !STACK.empty())  //栈空时结束  
    {  
        while(curr != NULL)            //一直向左走直到为空
        {  
            STACK.push(curr);  
            curr = curr->lchild;  
        }  
        curr = STACK.top();
        
		//当前节点的右孩子如果为空或者已经被访问,则访问当前节点
        if(curr->rchild == NULL || curr->rchild == previsited)  
        {  
            cout<<curr->data<<" ";  
            previsited = curr;  
            STACK.pop();  
            curr = NULL;  
        }  
        else
          curr = curr->rchild;//否则访问右孩子
    }  
} 

void N_PostOrder_S(BTREE T)  //后序遍历双栈法的非递归
{  
    stack<BTREE> s1,s2;  
    BTREE curr ;        //指向当前要检查的节点
    s1.push(T);
    while(!s1.empty())  //栈空时结束  
    {
        curr = s1.top();
        s1.pop();
        s2.push(curr);
        if(curr->lchild)
            s1.push(curr->lchild);
        if(curr->rchild)
            s1.push(curr->rchild);
    }
    while(!s2.empty())
    {
        printf("%c ", s2.top()->data);
        s2.pop();
    }
}

int visit(BTREE T)
{
    if(T)
    {
        printf("%c ",T->data);
        return 1;
    }
    else
        return 0;
}

void Traverse(BTREE T)//非递归层次遍历二叉树
{
    queue<BTREE> Q;
    BTREE p;
    p = T;
    if(visit(p)==1)
        Q.push(p);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        if(visit(p->lchild) == 1)
            Q.push(p->lchild);
        if(visit(p->rchild) == 1)
            Q.push(p->rchild);
    }
}

int main()  
{  
    BTREE T;  
    CreateBTree(T);  
    InOrder(T);
    cout << endl;
    N_InOrder(T);
    cout << endl;
    N_PreOrder(T);
    cout << endl;
    N_PostOrder(T); 
    cout << endl;
    N_PostOrder_S(T);
    cout << endl;
    Traverse(T);
    cout << endl;
    return 0;  
}

你可能感兴趣的:(二叉树,遍历)