树的所有实现

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 1000
typedef struct Linklist
{
    char data;
    struct Linklist *lchild,*rchild;
}Node;
Linklist* init()
{
    Linklist* tmp=new Node;
    tmp->lchild=NULL;
    tmp->rchild=NULL;
    return tmp;
}
void creat(Linklist* &root)
{
    char c;
    scanf("%c",&c);
    if(c=='#')
    {
        root=NULL;
    }
    else
    {
        root=new Node;
        root->data=c;
        creat(root->lchild);
        creat(root->rchild);
    }
}

void forder(Linklist* &root)
{
    if(root)
    {
        cout<<root->data<<' ';
        forder(root->lchild);
        forder(root->rchild);
    }
}
void morder(Linklist* &root)
{
    if(root)
    {
        morder(root->lchild);
        cout<<root->data<<' ';
        morder(root->rchild);
    }
}
void border(Linklist* &root)
{
    if(root)
    {
        border(root->lchild);
        border(root->rchild);
        cout<<root->data<<' ';
    }
}

void Morder(Linklist* &root)
{
    Linklist* Stack[MAXSIZE];
    Linklist* p=root;
    int top=0;
    if(root==NULL)
    {
        cout<<"该二叉树为空树"<<endl;
        return;
    }
    while(p!=NULL||top!=0)
    {
        while(p!=NULL)
        {
            if(top<MAXSIZE)///压栈
            {
                Stack[top++]=p;
            }
            else
            {
                cout<<"栈溢出"<<endl;
            }
            p=p->lchild;
        }
        if(top<=0)///栈为空结束
        {
            return;
        }
        else
        {
            p=Stack[--top];
            cout<<p->data<<' ';
            p=p->rchild;
        }
    }
}

int depth(Linklist* L)
{
    if(L==NULL)
    {
        return 0;
    }
    int m=depth(L->lchild);
    int n=depth(L->rchild);
    return max(m+1,n+1);
}

int cntnode(Linklist* L)
{
    if(L==NULL)
    {
        return 0;
    }
    return cntnode(L->lchild)+cntnode(L->rchild)+1;
}
int main()
{
    int menu;
    Linklist* root=NULL;
        printf("---------------------菜单-----------------
");
        printf("-----1、前序遍历生成二叉树----------------
");
        printf("-----2、前序遍历上述生成的二叉树----------
");
        printf("-----3、中序遍历上述生成的二叉树----------
");
        printf("-----4、后序遍历上述生成的二叉树----------
");
        printf("-----5、使用非递归方式中序遍历二叉树;----
");
        printf("-----6、输出二叉树的深度------------------
");
        printf("-----7、输出二叉树的节点个数--------------
");
        printf("-----0、退出------------------------------
");
        ///ABC##DE#G##F###
    while (1)
    {
        printf("请输入:");
        scanf("%d",&menu);
        getchar();
        if(menu==1)
        {
            root=init();
            cout<<"请按照前序排列输入二叉树(以#结束)"<<endl;
            creat(root);
        }
        else if(menu==2)
        {
            if(root==NULL)
            {
                cout<<"二叉树为空"<<endl;
            }
            else
            {
                cout<<"前序为:";
                forder(root);
                cout<<'
';
            }
        }
        else if(menu==3)
        {
            if(root==NULL)
            {
                cout<<"二叉树为空"<<endl;
            }
            else
            {
                cout<<"中序为:";
                morder(root);
                cout<<'
';
            }
        }
        else if(menu==4)
        {
            if(root==NULL)
            {
                cout<<"二叉树为空"<<endl;
            }
            else
            {
                cout<<"后序为:";
                border(root);
                cout<<'
';
            }
        }
        else if(menu==5)
        {
            if(root==NULL)
            {
                cout<<"二叉树为空"<<endl;
            }
            else
            {
                cout<<"使用非递归方法计算中序为:";
                Morder(root);
                cout<<'
';
            }
        }
        else if(menu==6)
        {
            cout<<"二叉树的深度为:"<<depth(root)<<'
';
        }
        else if(menu==7)
        {
            cout<<"二叉树的节点数为:"<<cntnode(root)<<'
';
        }
        if(menu==0)
        {
            break;
        }
    }
}
原文地址:https://www.cnblogs.com/ranzhong/p/13792915.html