FAQ: Trees: Conceptual - Binary Search Tree

This community-built FAQ covers the “Binary Search Tree” exercise from the lesson “Trees: Conceptual”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Binary Search Tree

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Something I think the written part is missing an explanation ( I think) is that the numbers need to be greater or lesser than the direct parent above it, but also not greater (if on left side) than any parent upwards and not lesser( if on right side) than any parents upstream.

For example for 23, you can’t have 19 and 50 as children? Is this right? It’s implied in the example but not clearly stated.

tree

Any thoughts from others?

This is a valid binary search tree, so no that’s not right:

   23
  /  \
19    50

not lesser( if on right side) than any parents upstream.

That’s also not right. If you look at your image 35 is a right child and is lesser than its grandparent, same for 22 and 38

All you need is that smaller values are in the left tree, and larger values are in the right tree, this is what causes the searching to narrow down the candidates by half at each step, because you know which child the sought-for value is in because it’s either smaller or larger than the current node.

Yes, this is what I was stating but the opposite, instead of saying what it should be, what it shouldn’t be. I meant if you were on the right side ( meaning 70 and down) you can’t be lesser than any parent upstream ( can’t be less than 39) . I think we are in agreement.

Thanks, I was guessing through the example, but it’s nice to get validation.

At any point in time you’ll only be looking at a small part of the tree.

And, indeed, it may all be easier to make sense of if you stop trying to think about what happens overall and instead look at what happens at any given moment.

1 Like

BTW, what will happen to a Binary Search Tree if we have two equal values in our “list”?
e.g. [15, 19, 19, 22, 23, 31, 35]
Will the BST look like this?:

                                   22
                               /        \
                             19         23
                           /   \       /     \
                          15   19     31     35

And what if the length of our list is an even number (the number of elements is even)?
How will the BST look like?
Which element to pick as a root node for a list [15, 19, 22, 23, 31, 35]?
22 or 23?

i think this is deep into the maths of the graph theory… Binary trees are artificially created in CS to store data . I think what you guys are talking about is bubbling up values and bubbling down values. I’m sure they will cover it later. You might find William Fiset on Youtube stuff on data structures on trees helpful. He gets a bit more math-y.