FAQ: Trees: Conceptual - Binary Search Tree


#1

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!


#2

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.


#3

Any thoughts from others?


#4

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.


#5

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.


#6

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.