Trees and Binary Search Trees

17. Trees and Binary Search Trees#

A hierarchy of categories or an ordered set can be represented through nodes and child subtrees. This chapter introduces recursive models before using them to traverse trees, compute results, and maintain a binary search tree. You will connect each update to the ordering invariant and analyze work in terms of tree shape and height.

  • Hierarchy: Trees represent parent-child relationships with one root and no cycles.

  • Recursive structure: Count nodes, leaves, and height by combining child-subtree results.

  • Traversal: Trace depth-first and level-order visits and choose an order for a task.

  • Binary search tree: Maintain a unique-key ordering invariant during search, insertion, and removal.

  • Shape and cost: Explain how height controls path operations and how pruning supports range reports.

Chapter Video

Data Structures - Full Course for Beginners (3:11:08) by Tech With Tim.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

This is a full course, not a required single viewing. Use its topic navigation to review this chapter’s concepts; the reading sequence below defines the chapter scope.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Explain base cases, smaller recursive calls, and suspended call frames.

  2. Distinguish a general tree, a binary tree, and a binary search tree.

  3. Trace preorder, inorder, postorder, and level-order traversal.

  4. Compute node count, leaf count, and height using explicit base cases.

  5. Implement and explain BST search, insertion, and all three removal cases.

  6. Validate ancestor bounds and test updates against an ordered-set model.

  7. Analyze path operations in terms of height and explain range-query pruning.

Chapter Flow

Suggested three-meeting pace (110 minutes): read the relevant explanations and predict the selected example before class.

Meeting

Minutes

Guided focus

1

35

recursive cases, traversal, and returned values.

2

40

BST search, updates, and the ordering invariant.

3

35

height, pruned queries, and verification.

Class uses selected traces, runs, and checks. Use the remaining worked examples and detailed API tables for practice and reference. Supporting readings are selected separately; lab and homework follow the core lessons.

Glossary

#

Term

Explanation

1

Tree

A data structure made of nodes connected by parent-child relationships.

2

Node

One stored item and its references to child nodes.

3

Root

The top node of a tree, with no parent.

4

Parent

A node that has a child beneath it.

5

Child

A node directly below another node.

6

Leaf

A node with no children.

7

Edge

A connection between a parent and child node.

8

Subtree

A node together with all nodes below it.

9

Depth

The number of edges from the root to a node.

10

Height

The longest downward path from a node to a leaf.

11

Binary tree

A tree where each node has at most two children.

12

Left child

The child reference conventionally drawn on the left.

13

Right child

The child reference conventionally drawn on the right.

14

Traversal

A rule for visiting every node in a tree.

15

Preorder

Visit node, then left subtree, then right subtree.

16

Inorder

Visit left subtree, then node, then right subtree.

17

Postorder

Visit left subtree, then right subtree, then node.

18

Binary search tree

A binary tree with an ordering rule that supports search.

19

BST invariant

Values in the left subtree are smaller; values in the right subtree are larger.

20

Balanced tree

A tree whose height stays small relative to its number of nodes.

21

Inorder successor

The smallest key in a node’s right subtree when that subtree exists.

22

Exclusive bound

A limit that the key may not equal.

23

Level order

A traversal that processes nodes in increasing depth using a queue.

24

Pruning

Skipping a subtree after proving it cannot contain a match.

25

Ordered set

A collection of unique keys that supports sorted iteration.

26

Base case

A recursive input whose answer is returned directly.

27

Call stack

Active method frames waiting for their called methods to return.

28

Subtree summary

A returned result combining measurements of one subtree, such as count, leaves and height.

Lecture Slides

Chapter 17 Slides