1. 程式人生 > >資料結構大實習——二叉樹與數

資料結構大實習——二叉樹與數

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <queue>
#include <cstdio>
#include <stack>

using namespace std;

typedef struct node
{
    char data[100];
    node *lt,*rt;
    bool ltag,rtag;
} tree;

bool flag;

void TreeNodeSet(tree *&t)//初始化指標
{
    t=(tree*)malloc(sizeof(tree));
    t->lt=NULL;
    t->rt=NULL;
    t->ltag=true;
    t->rtag=true;
    return ;
}

void ClearQue(queue<tree**> &q)//清空佇列
{
    while(!q.empty())
    {
        q.pop();
    }
    return ;
}

void CreatBsTree(tree *&t)//按層次建立二叉樹
{
    queue<tree**> q;
    int n;
    cout<<"tree's level:";
    cin>>n;
    TreeNodeSet(t);
    q.push(&t);
    for(int lev=0; lev<n; lev++)
    {
        int len=q.size();
        for(int i=0; i<len; i++)
        {
            tree **tmp=q.front();
            q.pop();
            cout<<"level "<<lev+1<<" no "<<i+1<<":";
            char in[100];
            scanf("%s",in);
            if(strcmp(in,"NULL")==0)
            {
                free(*tmp);
                (*tmp)=NULL;
            }
            else
            {
                strcpy((*tmp)->data,in);
                if(lev!=n-1)
                {
                    TreeNodeSet((*tmp)->lt);
                    q.push(&((*tmp)->lt));
                    TreeNodeSet((*tmp)->rt);
                    q.push(&((*tmp)->rt));
                }
            }
        }
    }
    ClearQue(q);
}

void PreOrderDfs(tree *&t)//遞迴先序遍歷
{
    if(t==NULL)return ;
    flag?cout<<t->data:cout<<" "<<t->data;
    flag=false;
    PreOrderDfs(t->lt);
    PreOrderDfs(t->rt);
    return ;
}

void MidOrderDfs(tree *&t)//遞迴中序遍歷
{
    if(t==NULL)return ;
    MidOrderDfs(t->lt);
    flag?cout<<t->data:cout<<" "<<t->data;
    flag=false;
    MidOrderDfs(t->rt);
    return ;
}

void LastOrderDfs(tree *&t)//遞迴後序遍歷
{
    if(t==NULL)return ;
    LastOrderDfs(t->lt);
    LastOrderDfs(t->rt);
    flag?cout<<t->data:cout<<" "<<t->data;
    flag=false;
    return ;
}

void PreOrderStack(tree *&t)//非遞迴先序遍歷
{
    stack<tree*> s;
    s.push(t);
    while(!s.empty())
    {
        tree *tmp=s.top();
        s.pop();
        flag?cout<<tmp->data:cout<<" "<<tmp->data;
        flag=false;
        if(tmp->rt!=NULL)
            s.push(tmp->rt);
        if(tmp->lt!=NULL)
            s.push(tmp->lt);
    }
    return ;
}

void MidOrderStack(tree *&t)//非遞迴中序遍歷
{
    stack<tree *> s;
    s.push(t);
    while(!s.empty())
    {
        tree *tmp=s.top();
        if(tmp->ltag&&tmp->lt!=NULL)
            s.push(tmp->lt),tmp->ltag=false;
        else
        {
            s.pop();
            flag?cout<<tmp->data:cout<<" "<<tmp->data;
            flag=false;
            if(tmp->rtag&&tmp->rt!=NULL)
                s.push(tmp->rt),tmp->rtag=false;
        }
    }
    return ;
}

void LastOrderStack(tree *&t)
{
    stack<tree*> s;
    
}

int main()
{
    string type;
    while(cout<<"tree's type:"&&cin>>type)
    {
        tree *t;
        switch(type[0])
        {
            case 'b':
            {
                CreatBsTree(t);
                cout<<"DFS:"<<endl;
                cout<<"The PreOrderDfsTraversal:"<<endl<<"{ ";
                flag=true;
                PreOrderDfs(t);
                cout<<" }"<<endl;
                cout<<"The MidOrderDfsTraversal:"<<endl<<"{ ";
                flag=true;
                MidOrderDfs(t);
                cout<<" }"<<endl;
                cout<<"The LastOrderDfsTraversal:"<<endl<<"{ ";
                flag=true;
                LastOrderDfs(t);
                cout<<" }"<<endl;
                cout<<"STACK:"<<endl;
                cout<<"The PreOrderStackTraversal:"<<endl<<"{ ";
                flag=true;
                PreOrderStack(t);
                cout<<" }"<<endl;
                cout<<"The MidOrderStackTraversal:"<<endl<<"{ ";
                flag=true;
                MidOrderStack(t);
                cout<<" }"<<endl;
                /*cout<<"The LastOrderStackTraversal:"<<endl<<"{ ";
                flag=true;
                LastOrderStack(t);
                cout<<" }"<<endl;*/
                break;
            }
        }
    }
    return 0;
}