简体   繁体   English

如何使用后序遍历打印排序的数组?

[英]How to print a sorted array using postorder traversal?

I am trying to print the values of all indices of a given sorted array using postorder traversal. 我正在尝试使用后序遍历打印给定排序数组的所有索引的值。 I don't understand what the problem is! 我不明白问题是什么! Please help me with that. 请帮我。 As you know a sorted array is a minheap that here is going to be printed by postorder traversal. 如您所知,排序数组是最小堆,将通过后遍历在此处打印。

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
using namespace std;
int a, b, temp;
char c;
int B[10] = { NULL };

int L(int i) //returning the index of left child
{
    i = ((2 * i) + 1);
    return i;
}

int R(int i) //returning the index of right child
{
    i = ((2 * i) + 2);
    return i;
}

int P(int i) //returning the index of parent
{
    if (i % 2 == 0) { i = ((i - 2) / 2); }
    else { i = ((i - 1) / 2); }
    return i;
}

//printing function
void f(int A[], int i) //printing using post order format 
{
    while (A[L(i)] != NULL && B[L(i)] != 1) 
    {
        i = L(i); 
    }

    cout << A[i] << " A "; 
    B[i] = 1; // B[i] is a kind of flag that checks whether A[i] has already been printed or not 

    if (i == 2) //I noticed that when it approaches the index 2, it only needs to print A[0] and gets out of the function
    {
        cout << A[0] << endl<< endl; 
        return;
    } 

    if (A[L(i+1)] == NULL) 
    {
        cout << A[i + 1] << " "; B[i + 1] = 1; 
    } //checks if the right node hasn't a child, prints it
    else 
    {
        f(A, i + 1); 
    }

        cout << A[P(i)] << " "; B[P(i)] = 1;
// When looking at the parent, if its index is odd, it means that you have already printed a left child so you should go the right index
        if (P(i) % 2 == 1)
        {

            f(A, P(i) + 1);
        }
// When looking at the parent, if its index is even, it means that you have already printed a right child so you should go up to its parent : (P(P(i))
        else 
        {
            f(A, P(P(i))); 
        } 


} 
int main()
{
int A[10]={0,1,2,3,4,5,6,7,8,9};
f(A,0);
return 0;
}

First, I don't think int B[10] = { NULL }; 首先,我不认为int B[10] = { NULL }; is good method for whatever you wanted. 是您所需的好方法。 If you just want do the postorder traversal, the easier way to implement is: 如果只想进行后遍历,则更简单的实现方法是:

void postTraversal(int a[], int n)
{
    if (n <=9  && n >= 0)
    {
        postTraversal(a, L(n));
        postTraversal(a, R(n));
        cout << a[n] << " ";
    }

}
int main()
{
int A[10]={0,1,2,3,4,5,6,7,8,9};
//f(A,0);
postTraversal(A, 0);
cout << endl;
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用一个堆栈的后序遍历 - PostOrder Traversal using one stack 如何在给定深度和后序遍历的情况下构造一棵树,然后打印其前序遍历 - How to construct a tree given its depth and postorder traversal, then print its preorder traversal 创建了一个用于二叉树遍历、中序和后序打印错误序列的程序 - Created a program for binary tree traversal, inorder and postorder print wrong sequences 在stl地图中进行后序遍历 - Postorder traversal in stl map 使用后序遍历递归的深度优先搜索会产生意外的 output - Depth First search using postorder traversal recursion produces unexpected output 使用顺序遍历从相同大小的排序数组中填充现有的BST - Fill an existing BST from sorted array of same size using inorder traversal 在给定顺序和后序遍历的情况下,如何输出树的前序遍历? - How do I output the preorder traversal of a tree given the inorder and postorder tranversal? 二元搜索树postorder迭代遍历 - binary search tree postorder traversal by iteration 为什么写 RETURN 是错误的? (后序遍历) - Why is it wrong to write RETURN ? ( postorder traversal ) 从LevelOrder遍历中查找PostOrder遍历而无需构造树 - Finding PostOrder traversal from LevelOrder traversal without constructing the tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM