二叉树的遍历

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct BinTree
{
    char data;
    struct BinTree *Lchild, *Rchild;
};

void CreateTree(BinTree *&T)//创建二叉树
{
    char ch;
    cin >> ch;
    if (ch == '#')
    {
        T = NULL;
    }
    else
    {
        T = new BinTree;
        T->data=ch;
        CreateTree(T->Lchild);
        CreateTree(T->Rchild);
    }
}
void dlr(BinTree *T)//先序递归遍历
{
    if (T)
    
    {
        cout << T->data;
        dlr(T->Lchild);
        dlr(T->Rchild);
    }
}
void ldr(BinTree *T)//中序递归遍历
{
    if (T)
    
    {
        dlr(T->Lchild);
        cout << T->data;
        dlr(T->Rchild);
    }
}
void lrd(BinTree *T)//后序递归遍历
{
    if (T)
    {
        lrd(T->Lchild);
        lrd(T->Rchild);
        cout << T->data;
    }
}
void DLR(BinTree* T)//先序非递归遍历
{
    if (!T)
        return;

    BinTree* stack[10];
    int top = -1;
    stack[++top] = T;

    while (top > -1)
    {
        BinTree* temp = stack[top--];
        cout<< temp->data;

        if (temp->Rchild)
            stack[++top] = temp->Rchild;
        if (temp->Lchild)
            stack[++top] = temp->Lchild;
    }

}
void LDR(BinTree *T)//中序非递归遍历

{
    BinTree *stack[10];
    int top = -1;
    BinTree *p = T;
    while (top > -1 || p != NULL)
    {
        while (p != NULL)
        {
            stack[++top] = p;
            p = p->Lchild;
        }
        if (top > -1)
        {
            p = stack[top--];
            cout << p->data;
            p = p->Rchild;
        }
    }
}
void LRD(BinTree *T)//非递归后序遍历
{
    if (T == NULL) return;

    stack<BinTree *> stk, stk2;
    stk.push(T);
    while (!stk.empty())
    {
        BinTree *p = stk.top(); stk.pop();
        stk2.push(p);
        if (p->Lchild) stk.push(p->Lchild);
        if (p->Rchild) stk.push(p->Rchild);
    }
    while (!stk2.empty())
    {
        cout<< stk2.top()->data;
        stk2.pop();
    }
}

void level(BinTree *T)//层次遍历
{
    int rear, front;
    rear = front = 0;
    BinTree *q;
    BinTree *que[10];

    if (T)
    {
        rear = (rear + 1) % 10;
        que[rear] = T;
        while (front != rear)
        {
            front = (front + 1) % 10;
            q =que[front];
            cout << q->data;

            if (q->Lchild != NULL)
            {
                rear = (rear + 1) % 10;
                que[rear] = q->Lchild;
            }
            if (q->Rchild != NULL)
            {
                rear = (rear + 1) % 10;
                que[rear] = q->Rchild;
            }
        }

    }
}


int choice()

    cout << "***************欢迎来到二叉树遍历界面***************" << endl;
    cout << "1-显示前序非递归遍历结果" <<endl;
    cout << "2-显示中序非递归遍历结果" << endl;
    cout << "3-显示后序非递归遍历结果" << endl;
    cout << "4-显示前序递归遍历结果" << endl; 
    cout << "5-显示中序递归遍历结果" << endl;
    cout << "6-显示后序递归遍历结果" << endl;
    cout << "7-层次遍历结果" << endl;
    cout << "0-退出" << endl;
    cout << "请输入: ";
    int n;
    cin >> n;
    return n;
}

int main()
{
    BinTree *T;
    cout << "请输入二叉树:";
    CreateTree(T);
    int n;
    while (1)
    {
        n = choice();
        if (!(n >= 0 && n <= 7))
        {
            cout << "菜单选择错误";
            continue;
        }
        if (n == 0)
            break;
        switch (n)
        {
        case 1:
            cout << "前序递归遍历结果:";
            dlr(T);
            break;
        case 2:
            cout << "中序递归遍历结果:";
            ldr(T);
            break;
        case 3:
            cout << "后序递归遍历结果:";
            lrd(T);
            break;
        case 4:
            cout << "前序非递归遍历结果:";
            DLR(T);
            break;
        case 5:
            cout << "中序非递归遍历结果:";
            LDR(T);
            break;
        case 6:
            cout << "后序非递归遍历结果:";
            LRD(T);
            break;
        case 7:
            cout << "层序遍历结果:";
            level(T);
            break;
        
            
            }
            cout << endl;
            system("pause");
            system("cls");

        }

        return 0;
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值