How do you draw a binary tree using pre-order and in-order traversal?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Binary Drawing Tree
Click For Summary

Discussion Overview

The discussion revolves around constructing a binary tree using given pre-order and in-order traversal sequences. Participants explore the methodology for drawing the tree and clarify the relationships between the nodes based on traversal orders. The exercise involves both theoretical understanding and practical application of tree construction algorithms.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant outlines the process of identifying the root from the pre-order sequence and locating it in the in-order sequence to determine the left and right subtrees.
  • Another participant suggests an alternative in-order sequence, which differs from the provided one, raising questions about the accuracy of the initial data.
  • A participant proposes writing a recursive algorithm to construct the binary tree from the pre-order and in-order sequences, indicating a standard approach to the problem.
  • Some participants express uncertainty about the uniqueness of the tree structure when duplicate values are present in the traversal sequences.
  • There is a discussion about the implications of having duplicate values in the in-order sequence and how it affects the identification of subtrees.
  • Several participants inquire about the possibility of constructing trees from different traversal combinations, such as post-order and in-order, and seek clarification on the steps involved.
  • One participant shares a code snippet for building a binary tree from in-order and post-order sequences, suggesting a similar approach could be applied to pre-order and in-order sequences.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the uniqueness of the binary tree structure when duplicate values are involved. There are competing views on how to handle such cases, and the discussion remains unresolved regarding the implications of duplicates in the traversal sequences.

Contextual Notes

Some participants express confusion about the steps needed to construct the tree, particularly in the context of duplicate values, and there is a lack of clarity on how to proceed with the construction in such cases.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Smile)

I am looking at the following exercise:

It is given a binary tree with 8 nodes and keys 2,4,6,8,10,12,14,16.The in-order tree traversal gives this order: 2,4,6,8,10,12,14,16.
The pre-order traversal gives the order: 10,8,2,6,4,16,14,12. Draw the tree. Explain how you drawed it.

That's what I have tried:

View attachment 3608

In a pre-order sequence, the leftmost element is the root of the tree.
By searching the root in the in-order sequence, we can find which elements are on the left side of the root, in the left subtree and which are on the right side of the root, in the right subtree. Could you tell me if it is right? (Thinking)
 

Attachments

  • btree.png
    btree.png
    3 KB · Views: 121
Technology news on Phys.org
evinda said:
Hi! (Smile)

I am looking at the following exercise:

It is given a binary tree with 8 nodes and keys 2,4,6,8,10,12,14,16.The in-order tree traversal gives this order: 2,4,6,8,10,12,14,16.
The pre-order traversal gives the order: 10,8,2,6,4,16,14,12. Draw the tree. Explain how you drawed it.

That's what I have tried:

View attachment 3608

In a pre-order sequence, the leftmost element is the root of the tree.
By searching the root in the in-order sequence, we can find which elements are on the left side of the root, in the left subtree and which are on the right side of the root, in the right subtree. Could you tell me if it is right? (Thinking)

Hey! (Wave)

I believe the in-order of that tree is: 2, 8, 4, 6, 10, 14, 16, 12... but that is not what was given! (Worried)
 
View attachment 3611
You know the root is 10. Now find 10 in the in order sequence. You then know the left subtree has nodes 2,4,6,8 and the right has 12,14. Do it again. The left subtree has root 8. Find 8 in the inorder sequence 2,4,6,8. So you know the left sub tree from 8 has nodes 2,4,6 and the right subtree from 8 is empty. Continue.
It's a standard easy exercise to formalize the above and write a recursive algorithm to construct a binary tree given the in order and pre order sequences. I suggest writing such.
 

Attachments

  • MHBcs3.png
    MHBcs3.png
    3.6 KB · Views: 135
So, is this the binary tree? (Thinking)

View attachment 3613

Could you give me also two other orders, for example post-order and in-order, so that I find the binary tree? (Thinking)
 

Attachments

  • treeee.png
    treeee.png
    3.3 KB · Views: 112
Sorry, in my previous post I left out 16.

24g5860.png


Given the in order sequence and the post order sequence, the idea is very similar to the in order and pre order. Here's an algorithm, actual C code:

Code:
/*  Builds the unique binary tree with n nodes given the inorder and post
order traversal sequences of keys in arrays in and post.  Return is
pointer to the tree.
*/
node *build(int *in,int *post,int n)
{
    node *p;
    int i;
    if (!n) {
      return(0);
    }
    p=getNode();
    p->key=post[n-1];
    for (i=0;in[i] != post[n-1];i++)
        ;
    p->left_child=build(in,post,i);
    p->right_child=build(in+i+1,post+i,n-i-1);
    return(p);
}

I repeat. I think you should write such an algorithm for pre order and in order. What about pre order and post order?
 
johng said:
Sorry, in my previous post I left out 16.
Given the in order sequence and the post order sequence, the idea is very similar to the in order and pre order.

Could you explain me the steps we follow, in order to find the binary tree?
Since $2$ is at the first position of the post-order, it means that it is the leftmost leaf, right? How do we continue? (Thinking)
 
evinda said:
So, is this the binary tree? (Thinking)

View attachment 3613

Could you give me also two other orders, for example post-order and in-order, so that I find the binary tree? (Thinking)

Yep! That's it! Good! (Nod)

How about a tree with post-order 8 5 3 1 6 6 8 7 9 5, and in-order 1 8 3 5 6 5 6 9 7 8? (Wondering)
 
I like Serena said:
Yep! That's it! Good! (Nod)

How about a tree with post-order 8 5 3 1 6 6 8 7 9 5, and in-order 1 8 3 5 6 5 6 9 7 8? (Wondering)

Is this one possible tree? (Thinking)

View attachment 3617
 

Attachments

  • extree.png
    extree.png
    3.6 KB · Views: 105
evinda said:
Is this one possible tree? (Thinking)

https://www.physicsforums.com/attachments/3617

Yep!
And I believe it's the only one! (Party)
 
  • #10
I like Serena said:
Yep!
And I believe it's the only one! (Party)

Nice! (Happy) So, couldn't we consider that the root $5$ corresponds to the $5$ of the $4^{th}$ position from the in-order traversal? (Thinking)
 
  • #11
evinda said:
So, couldn't we consider that the root $5$ corresponds to the $5$ of the $4^{th}$ position from the in-order traversal? (Thinking)

Huh? (Wondering)
 
  • #12
I like Serena said:
Huh? (Wondering)

We know that $5$ is the root and so we found it at the in-order to see which of the elements are at the left subtree and which of the right, like that:

View attachment 3618

But, there is two times the number $5$.

So, couldn't we also do it like that? (Thinking)

View attachment 3619
 

Attachments

  • extree.png
    extree.png
    1.8 KB · Views: 113
  • extree.png
    extree.png
    1.8 KB · Views: 111
  • #13
evinda said:
We know that $5$ is the root and so we found it at the in-order to see which of the elements are at the left subtree and which of the right, like that:

But, there is two times the number $5$.

So, couldn't we also do it like that? (Thinking)

Maybe.
Can we? (Wondering) (Thinking) (Sweating)
 
  • #14
I like Serena said:
Maybe.
Can we? (Wondering) (Thinking) (Sweating)

I think that we can't do this, because then it will be like that:

View attachment 3621

We will have to put the number $5$ but there isn't one left... Or am I wrong? (Thinking)
 

Attachments

  • extree.png
    extree.png
    4.3 KB · Views: 119
  • #15
evinda said:
I think that we can't do this, because then it will be like that:

https://www.physicsforums.com/attachments/3621

We will have to put the number $5$ but there isn't one left... Or am I wrong? (Thinking)

I believe you're quite right. (Mmm)
We won't be able to fit in the numbers that are left in the thought balloon. (Angry)
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K