IB Computer Science HL
Lesson 2: Walking and Changing Trees
Master the logic of tree traversals and learn the rules for adding and removing nodes from a Binary Search Tree (BST).
(15 minutes)
Traversal is a process for visiting every node in a tree exactly once in a specific, predictable order. The name tells you when you "visit" the node relative to its children.
(NLR)
1. Visit Node
2. Go Left
3. Go Right
(LNR)
1. Go Left
2. Visit Node
3. Go Right
(LRN)
1. Go Left
2. Go Right
3. Visit Node
(20 minutes)
(30 minutes)
Let's practice modifying the tree. For each prompt, sketch the resulting tree, then click the button to check your answer.
Add `60` to the original tree.
From the original tree, remove `20`.
First, add `10`. Then, remove `20`.
Rule: Parent adopts the grandchild.
From the original tree, remove `30`.
Rule: Replace with smallest node from right subtree (the inorder successor).
(5-10 minutes)
Using the tree diagram, answer the following: