AllRounder.ai
Chapters in this course

Enrol to start learning

Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.

Enrol free

15.4. Predecessor Function

Interactive Audio Lesson

Session 1: Finding Minimum in a Binary Search Tree

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Today we'll learn how to find the minimum value in a binary search tree. Can anyone tell me how we might locate the minimum value?

Noah
Noah

Isn’t it the left-most node?

Sarah
SarahInstructor

That's correct! The minimum value is the left-most node because all values to the left are smaller. We keep traversing left until we can't anymore.

Isabella
Isabella

What if we used a method to avoid recursion?

Sarah
SarahInstructor

We can definitely use an iterative approach by starting at the root and continuously moving left until we reach a nil node. Great thinking! So if we start at a node with value 5 and go left, we would go to 3, then to 1, where we stop.

Akash
Akash

If I understand correctly, if we reach a node where left is nil, that's our minimum?

Sarah
SarahInstructor

Exactly! To recap: finding the minimum involves a continuous left traversal until you can’t anymore. Now let’s summarize this: Minimum is found by always going left! Let's move on to maximum.

Session 2: Finding Maximum in a Binary Search Tree

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Robert
RobertInstructor

Now, what can you tell me about finding the maximum node in a BST?

Ananya
Ananya

Is it the right-most node?

Robert
RobertInstructor

Yes! Just like with the minimum, the maximum is found at the right-most end. All values to the right of a node are larger.

Noah
Noah

So we would follow the right links until we can’t go right anymore?

Robert
RobertInstructor

Great job! We keep moving to the right until reaching a nil node, and that last valid node we reached will be the maximum.

Isabella
Isabella

So if I take a node with value 5 and go to 7, then 9, I can't go right anymore, so 9 is maximum!

Robert
RobertInstructor

Exactly! Now let’s summarize: Maximum is found by always going right! Very good. Let's transition to the topic of predecessor and successor.

Session 3: Understanding Successor

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Now, who can tell me what the successor of a node is?

Akash
Akash

Isn't it the next larger value in order?

Sarah
SarahInstructor

Yes! The successor of a node is the next value in in-order traversal. If the node has a right subtree, its successor is the minimum node of that subtree.

Noah
Noah

What about if it doesn’t have a right subtree?

Sarah
SarahInstructor

Good question! In that case, we need to go up the parent nodes until we find the first left child. That parent becomes the successor.

Isabella
Isabella

So we could say that if there’s no right, we look up to find where we turned left?

Sarah
SarahInstructor

Exactly! Great connection. Let's summarize: If it has a right subtree, it's the right subtree’s minimum; if not, we navigate upward to find where we turned left.

Session 4: Understanding Predecessor

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Robert
RobertInstructor

What can you tell me about the predecessor function?

Ananya
Ananya

Is it the largest value less than a given node?

Robert
RobertInstructor

Correct! For a node with a left subtree, the predecessor is the maximum of that subtree.

Akash
Akash

And if there’s no left subtree?

Robert
RobertInstructor

In that situation, we trace the parent links until we turn right. That node turns out to be the predecessor.

Noah
Noah

So for example, if from node 5 we go left to 4, 4 is predecessor?

Robert
RobertInstructor

Exactly right! And if there’s no left option, we navigate back up to find where we turned right. Let’s wrap this session up by reiterating the major points: Predecessor is found via the left subtree’s maximum or by ascending to where we turned right.