给定中序和后序遍历,如何输出树的前序遍历?

发布于 2024-09-05 01:01:32 字数 1481 浏览 2 评论 0原文

给出当我在整数数组中具有先序和中序遍历时输出树的后序遍历的代码。如何使用给定的中序和后序数组来类似地获取前序?

void postorder( int preorder[], int prestart, int inorder[], int inostart, int length)
{ 
  if(length==0) return; //terminating condition
  int i;
  for(i=inostart; i<inostart+length; i++)
    if(preorder[prestart]==inorder[i])//break when found root in inorder array
      break;
  postorder(preorder, prestart+1, inorder, inostart, i-inostart);
  postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);
  cout<<preorder[prestart]<<" ";
}

这是 preorder() 的原型

void preorder( int inorderorder[], int inostart, int postorder[], int poststart, int length)

使用 postorder() 它将输出,

int preorder[6]={6,4,1,5,8,9};
int inorder[6]={1,4,5,6,8,9};
postorder( preorder,0,inorder,0,6);

下面是

1 5 4 9 8 6

print_preorder( 的错误代码),下面仍然无法工作

void print_preorder( int inorder[], int inostart, int postorder[], int poststart, int length)
    {
      if(length==0) return; //terminating condition
      int i;
      for(i=inostart; i<inostart+length; i++)
        if(postorder[poststart+length-1]==inorder[i])
          break; 
      cout<<postorder[poststart+length-1]<<" ";
      print_preorder(inorder, inostart , postorder, poststart, i-inostart);
      print_preorder(inorder, inostart+i-poststart+1, postorder, i+1, length-i+inostart-1);
    }

Given the code for outputing the postorder traversal of a tree when I have the preorder and the inorder traversal in an integer array. How do I similarly get the preorder with the inorder and postorder array given?

void postorder( int preorder[], int prestart, int inorder[], int inostart, int length)
{ 
  if(length==0) return; //terminating condition
  int i;
  for(i=inostart; i<inostart+length; i++)
    if(preorder[prestart]==inorder[i])//break when found root in inorder array
      break;
  postorder(preorder, prestart+1, inorder, inostart, i-inostart);
  postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);
  cout<<preorder[prestart]<<" ";
}

Here is the prototype for preorder()

void preorder( int inorderorder[], int inostart, int postorder[], int poststart, int length)

to use postorder() it will be

int preorder[6]={6,4,1,5,8,9};
int inorder[6]={1,4,5,6,8,9};
postorder( preorder,0,inorder,0,6);

out put will be

1 5 4 9 8 6

below is the incorrect code for print_preorder(), still not working below

void print_preorder( int inorder[], int inostart, int postorder[], int poststart, int length)
    {
      if(length==0) return; //terminating condition
      int i;
      for(i=inostart; i<inostart+length; i++)
        if(postorder[poststart+length-1]==inorder[i])
          break; 
      cout<<postorder[poststart+length-1]<<" ";
      print_preorder(inorder, inostart , postorder, poststart, i-inostart);
      print_preorder(inorder, inostart+i-poststart+1, postorder, i+1, length-i+inostart-1);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-09-12 01:01:33

这里有一些提示:

  • postorder 子数组中的最后一个元素是新的 preorder 根。
  • inorder 数组可以在新 preorder 根的两侧分成两部分。
  • 您可以在这两个 inorder 子数组上递归调用 print_preorder 函数。
  • 当调用print_preorder函数时,inorderpostorder数组的大小将相同。
  • 您的数组访问超出范围:postorder[poststart+length] 超出了数组末尾。要获取最后一个元素,您需要 postorder[poststart+length-1]
  • 您的第一个递归 print_preorder 函数选择了错误的长度。请记住,length 是子数组的长度,但 inostartinorder 数组中的绝对位置。您的函数可能会以负长度进行调用。
  • 您的第二个递归函数对于转换边界和长度来说还很遥远。将其画在纸上并跟踪您的算法可能会有所帮助。

画出这棵树可能会有所帮助:

     6
   /   \
  4     8
 / \     \
1   5     9

然后写出三个遍历:

// index:         0 1 2 3 4 5
int postorder[6]={1,5,4,9,8,6};
int inorder[6]=  {1,4,5,6,8,9};
int preorder[6]= {6,4,1,5,8,9};

现在,放下电脑,拿出笔,然后写出三个遍历。纸并思考问题:)

想象一下这个调用堆栈(新的根打印在左侧):

6 print_preorder(len=6, in=[1 4 5 6 8 9], post=[1 5 4 9 8 6])
4 |-> print_preorder(len=3, in=[1 4 5], post=[1 5 4])
1 |   |-> print_preorder(len=1, in=[1], post=[1])
  |   |   |-> print_preorder(len=0, in=[], post=[])
  |   |   |-> print_preorder(len=0, in=[], post=[])
5 |   |-> print_preorder(len=1, in=[5], post=[5])
  |       |-> print_preorder(len=0, in=[], post=[])
  |       |-> print_preorder(len=0, in=[], post=[])
8 |-> print_preorder(len=2, in=[8 9], post=[9 8])
      |-> print_preorder(len=0, in=[], post=[])
9     |-> print_preorder(len=1, in=[9], post=[9])
          |-> print_preorder(len=0, in=[], post=[])
          |-> print_preorder(len=0, in=[], post=[])

祝你好运:)

Here's a few hints:

  • The last element in the postorder subarray is your new preorder root.
  • The inorder array can be split in two on either side of the new preorder root.
  • You can call recursively call the print_preorder function on those two inorder subarrays.
  • When calling the print_preorder function, the inorder and postorder arrays will be the same size.
  • You have an out-of-bounds array access: postorder[poststart+length] is past the end of the array. To get the last element, you want postorder[poststart+length-1]
  • Your first recursive print_preorder function chooses the wrong length. Remember that length is the length of the subarray, but inostart is the absolute position within the inorder array. Your function will probably call with a negative length.
  • Your second recursive function is pretty far off for translating the bounds and length. It'll probably help to draw it on paper and trace your algorithm.

It may help to draw the tree:

     6
   /   \
  4     8
 / \     \
1   5     9

Then write out the three traversals:

// index:         0 1 2 3 4 5
int postorder[6]={1,5,4,9,8,6};
int inorder[6]=  {1,4,5,6,8,9};
int preorder[6]= {6,4,1,5,8,9};

Now, put down the computer, get out a pen & paper and think about the problem :)

Imagine this call stack (the new root is printed on the left):

6 print_preorder(len=6, in=[1 4 5 6 8 9], post=[1 5 4 9 8 6])
4 |-> print_preorder(len=3, in=[1 4 5], post=[1 5 4])
1 |   |-> print_preorder(len=1, in=[1], post=[1])
  |   |   |-> print_preorder(len=0, in=[], post=[])
  |   |   |-> print_preorder(len=0, in=[], post=[])
5 |   |-> print_preorder(len=1, in=[5], post=[5])
  |       |-> print_preorder(len=0, in=[], post=[])
  |       |-> print_preorder(len=0, in=[], post=[])
8 |-> print_preorder(len=2, in=[8 9], post=[9 8])
      |-> print_preorder(len=0, in=[], post=[])
9     |-> print_preorder(len=1, in=[9], post=[9])
          |-> print_preorder(len=0, in=[], post=[])
          |-> print_preorder(len=0, in=[], post=[])

Good luck :)

水溶 2024-09-12 01:01:33
  1. 后序中的最后一个元素将是树的根。

  2. 之后,我们将查看 Inorder 数组以确定根的位置。数组的左侧是左子树,右侧是右子树。

  3. 通过使用该索引,我们将确定 left 中作为根的元素。

    通过使用

  4. 我们对右子树也是如此,主要思想是通过查找中序数组来确定左子树和右子树的索引。希望我说清楚了..

    public static void printpreorder(char []inorder,int i_start,int i_end,char[] postorder,int p_start,int p_end)
    {
      if(i_start > i_end || p_start > p_end)
             返回 ; 
      char root = postorder[p_end];
      System.out.print(root);
      System.out.print("(");
        整数k=0;
          for(int i=0; i< inorder.length; i++){
              if(inorder[i]==root){
                k = 我;
                休息;
              }
          }
      printpreorder(inorder, i_start, k-1, postorder, p_start, p_start+k-(i_start+1));
      System.out.print(")(");
      printpreorder(inorder, k+1, i_end, postorder, p_start+k-i_start, p_end-1);
      System.out.print(")");    
    }
    

这是我的作业。感谢@Stephen 的好回答

  1. The last element in post order will be the root of the tree.

  2. After that we will look in Inorder array to determine the position of the root. left side of the array is left sub tree and right side is right sub tree.

  3. By using that index we shall determine the element in left which is the root.

  4. similarly we do for the right sub tree, The main idea is we determine the indexes of left sub tree and right sub tree by looking in inorder array. hope i was clear..

    public static void printpreorder(char []inorder,int i_start,int i_end,char[] postorder,int p_start,int p_end)
    {
      if(i_start > i_end || p_start > p_end)
             return ; 
      char root = postorder[p_end];
      System.out.print(root);
      System.out.print("(");
        int k=0;
          for(int i=0; i< inorder.length; i++){
              if(inorder[i]==root){
                k = i;
                break;
              }
          }
      printpreorder(inorder, i_start, k-1, postorder, p_start, p_start+k-(i_start+1));
      System.out.print(")(");
      printpreorder(inorder, k+1, i_end, postorder, p_start+k-i_start, p_end-1);
      System.out.print(")");    
    }
    

This was hommework for me. Thanks to @Stephen for a good answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文