Which node satisfies this property?

  • Context:
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Property
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
20 replies · 4K views
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Smirk)

It is a given binary tree $T$, for each node $ n$ of which , all the keys of the nodes of the left subtree of $n$ are greater than the key of $n$ and all the keys of the nodes of the right subtree of $n$ are smaller than the key of $n$.

We suppose that $T$ contains the nodes $3,5,7,9,11,13,15,17$ and we are given that the pre-order traversal of the tree, gives as result the numbers in this row: $9,17,11,15,13,5,7,3$.
I have to draw the tree.

How can I find the first node $n$, which satisfies the first property? (Thinking)
 
Physics news on Phys.org
evinda said:
Hi! (Smirk)

It is a given binary tree $T$, for each node $ n$ of which , all the keys of the nodes of the left subtree of $n$ are greater than the key of $n$ and all the keys of the nodes of the right subtree of $n$ are smaller than the key of $n$.

We suppose that $T$ contains the nodes $3,5,7,9,11,13,15,17$ and we are given that the pre-order traversal of the tree, gives as result the numbers in this row: $9,17,11,15,13,5,7,3$.
I have to draw the tree.

How can I find the first node $n$, which satisfies the first property? (Thinking)

Hey! (Happy)

A pre-order visits first the root of each node, then the left subtree (beginning with its root), and then the right subtree. (Nerd)

So we can already tell that $9$ is the root of the tree.
To which subtree will $17$ belong? (Wondering)
 
I like Serena said:
Hey! (Happy)

A pre-order visits first the root of each node, then the left subtree (beginning with its root), and then the right subtree. (Nerd)

So we can already tell that $9$ is the root of the tree.
To which subtree will $17$ belong? (Wondering)

So, does this means that $17$ and $11$ will be the children of $9$?
If so, can we conclude from a pre-order which will be the left child and which the right? (Thinking)
 
evinda said:
So, does this means that $17$ and $11$ will be the children of $9$?
If so, can we conclude from a pre-order which will be the left child and which the right? (Thinking)

$17$ will definitely be a child of $9$.
What does the problem statement say about whether it should be left or right? (Wondering)

As for $11$, that comes later, but I don't think it can be a child of $9$. (Shake)
 
I like Serena said:
$17$ will definitely be a child of $9$.
What does the problem statement say about whether it should be left or right? (Wondering)
Since the keys of the nodes of the left subtree are greater than the key, does this mean that 17 is the left child of 9?

I like Serena said:
As for $11$, that comes later, but I don't think it can be a child of $9$. (Shake)

How can we find then the right child of 9? :confused:

What do we conclude then from the fact that 11 is at the third position of the pre-order? (Thinking)
 
evinda said:
Since the keys of the nodes of the left subtree are greater than the key, does this mean that 17 is the left child of 9?

Yes. (Nod)
How can we find then the right child of 9? :confused:

What do we conclude then from the fact that 11 is at the third position of the pre-order? (Thinking)

Suppose we look at the tree:
Code:
    9
  /   \
17     11
What is then its pre-order? (Wondering)
Does the tree satisfy the problem statement criteria?

Can you think of another tree that has the same pre-order? (Wondering)
 
I like Serena said:
Suppose we look at the tree:
Code:
    9
  /   \
17     11
What is then its pre-order? (Wondering)

It is 9,17,11, right? (Thinking)

I like Serena said:
Does the tree satisfy the problem statement criteria?
Yes, the problem statement criteria are satisfied.. (Nod) Or am I wrong? (Thinking)

I like Serena said:
Can you think of another tree that has the same pre-order? (Wondering)

Isn't this the only one? (Thinking)
 
evinda said:
It is 9,17,11, right? (Thinking)

Yep! (Nod)
evinda said:
and all the keys of the nodes of the right subtree of $n$ are smaller than the key of $n$.
Yes, the problem statement criteria are satisfied.. (Nod) Or am I wrong? (Thinking)

Since $11$ is in the right sub tree of $9$, is it smaller? (Wondering)
Isn't this the only one? (Thinking)

How about:
Code:
     9
    /
  17
 /
11

Or:
Code:
     9
    /
  17
    \
    11
(Wondering)
 
I like Serena said:
Since $11$ is in the right sub tree of $9$, is it smaller? (Wondering)

Oh no! I am sorry! (Blush)

I like Serena said:
How about:
Code:
     9
    /
  17
 /
11

Or:
Code:
     9
    /
  17
    \
    11
(Wondering)

Oh yes, you are right! (Tmi)
 
So, the first number of the pre-order is always the root, right? (Thinking)
Then, do we know that the next two numbers are the children of the root?
Or do we know this only for the second one? Or for none of them? (Thinking)
 
I like Serena said:
Which of these satisfies the criteria of the problem statement? (Wondering)

This:

Code:
     9
    /
  17
    \
    11

satisfies the criteria of the problem statement, right? (Thinking)
 
I like Serena said:
Yep! (Nod)

Nice! And 15 will be now the left child of 11? Or am I wrong? (Thinking)
 

Attachments

  • tree7.png
    tree7.png
    3.1 KB · Views: 135
I like Serena said:
Yep! (Party)

Great! (Party) And this is the only tree, that can be found, right? (Thinking)
 
I like Serena said:
When you were building up the tree, did you have any choices to make? (Wondering)

No (Shake) So, it is the only possible tree, right? (Smile)
 
I like Serena said:
Yes! (Nod)

Nice, thank you very much! (Smirk)