数据结构-Tree Traversals Again(中序遍历建树,后序遍历输出)

题目

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1

运行结果

CaseHintResultRun TimeMemory
0sample 有单边有双边结点Accepted3 ms512 KB
1单边喇叭张开形Accepted2 ms424 KB
2交错Accepted2 ms384 KB
3N=1Accepted3 ms512 KB
4N=30,复杂组合Accepted2 ms360 KB

程序


#include<iostream>
using namespace std;

#define Tree int
#define ElementType int
#define MaxNodes 30
#define Null 0

struct Node{
    ElementType Element=Null;
    Tree Left = Null;
    Tree Right = Null;
}BT[MaxNodes];
typedef struct Node *PtrToNode;
//测试用例-中序遍历思路
//Tree MiddleOrderBuildTree_Test(struct Node BT[]);

Tree MiddleOrderBuildTree(struct Node BT[]);
int *PostOrderTraversals(struct Node BT[], int root);

int main()
{
    int root;
    int *Results;
    /*
    //测试用例-中序遍历思路
    root = MiddleOrderBuildTree_Test(BT);
    */
    root = MiddleOrderBuildTree(BT);
    //后序遍历思路
    Results = PostOrderTraversals(BT, root);

    //输出结果个数
    int NResults;
    for(NResults=0; NResults<MaxNodes; NResults++)
        if(Results[NResults] == 0) break;

    //安装输出格式
    for(int i=0; i< NResults-1; i++)
        cout << Results[i] << " ";
    cout << Results[NResults-1] <<endl;

    return 0;
}

int *PostOrderTraversals(struct Node BT[], int root)
{
    static int Results[MaxNodes];
    int Results_i = 0;
    int stack_[MaxNodes];
    int stack_i = -1;//栈顶下标
    int CurrData;
    PtrToNode CurrNode;//当前指向的结点
    int pre = Null;

    for(int i=0; i<MaxNodes; i++) Results[i] = 0;

    CurrNode = &BT[root];

    while(CurrNode->Element >0 || stack_i>=0){
        while(CurrNode->Element > 0){
            //压入当前元素到堆栈
            ++stack_i;
            stack_[stack_i] = CurrNode->Element;
            CurrNode = &BT[CurrNode->Left];
        }
        if(stack_i >= 0){
            //取出栈顶元素
            CurrData = stack_[stack_i];
            --stack_i;
            CurrNode = &BT[CurrData];
            //向回走
            if((CurrNode->Right == Null) || CurrNode->Right == pre){
                Results[Results_i] = CurrData;
                ++Results_i;
                pre = CurrNode->Element;
                CurrNode = &BT[0];
            }
            //向下走
            else{
                ++stack_i;
                stack_[stack_i] = CurrNode->Element;
                CurrNode = &BT[CurrNode->Right];
            }
        }
    }
    return Results;
}


Tree MiddleOrderBuildTree(struct Node BT[])
{
    int N;
    int root = Null;
    int stack_[MaxNodes];
    int stack_i = -1;//栈顶下标
    int CurrData = Null;
    PtrToNode CurrNode;//当前指向的结点

    cin >> N;
    string operations[2*N];
    int InputData[2*N];

    //输入格式要求
    for(int i=0; i<2*N; i++){
        cin >> operations[i];
        if(operations[i] == "Push")
            cin >> InputData[i];
        else if(operations[i] == "Pop")
            InputData[i] = 0;
    }

    //取出当前操作数值
    CurrData = InputData[0];
    //创建根结点
    BT[CurrData].Element = CurrData;
    //将第一个push值压入堆栈
    ++stack_i;
    stack_[stack_i] = CurrData;
    //指定根结点
    root = CurrData;
    CurrNode = &BT[CurrData];

    for(int i=1; i<2*N; i++){

        if(operations[i] == "Push"){
            //读取当前操作数值
            CurrData = InputData[i];
            //将新的操作数值压入堆栈
            ++stack_i;
            stack_[stack_i] = CurrData;
            //左孩子为空,则优先填补
            if(CurrNode->Left == Null)
                CurrNode->Left = CurrData;
            else
                CurrNode->Right = CurrData;
            //创建新结点
            BT[CurrData].Element = CurrData;
            CurrNode = &BT[CurrData];
        }
        else if(operations[i] == "Pop"){
            //将栈顶元素取出
            CurrData = stack_[stack_i];
            --stack_i;
            //指向当前结点
            CurrNode = &BT[CurrData];
        }
    }
    return root;
}

Tree MiddleOrderBuildTree_Test(struct Node BT[])
{
    static int N = 6;
    int root = Null;
    int stack_[MaxNodes];
    int stack_i = -1;//栈顶下标
    int CurrData = Null;
    PtrToNode CurrNode;//当前指向的结点

    string operations[2*N] = \
    {"Push", "Push", "Push", "Pop", "Pop", "Push", "Pop", "Pop", \
     "Push", "Push", "Pop", "Pop"};

    int InputData[2*N] = {1,2,3,0,0,4,0,0,5,6,0,0};

    //取出当前操作数值
    CurrData = InputData[0];
    //创建根结点
    BT[CurrData].Element = CurrData;
    //将第一个push值压入堆栈
    ++stack_i;
    stack_[stack_i] = CurrData;
    //指定根结点
    root = CurrData;
    CurrNode = &BT[CurrData];

    for(int i=1; i<2*N; i++){

        if(operations[i] == "Push"){
            //读取当前操作数值
            CurrData = InputData[i];
            //将新的操作数值压入堆栈
            ++stack_i;
            stack_[stack_i] = CurrData;
            //左孩子为空,则优先填补
            if(CurrNode->Left == Null)
                CurrNode->Left = CurrData;
            else
                CurrNode->Right = CurrData;
            //创建新结点
            BT[CurrData].Element = CurrData;
            CurrNode = &BT[CurrData];
        }
        else if(operations[i] == "Pop"){
            //将栈顶元素取出
            CurrData = stack_[stack_i];
            --stack_i;
            //指向当前结点
            CurrNode = &BT[CurrData];
        }
    }
    return root;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值