Tutelage

Lessons › coding and computer science · taught by Leo

Trees and BSTs explained simply

Learn tree basics, binary trees, and how binary search trees keep values ordered for fast searching and insertion.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

A tree is a connected family

A tree is a way to organize things with one top node and links below it. Think of it like a family chart, or a folder tree.

Each connection is called an edge, and the circles are nodes. The ones at the bottom with no children are leaves.

Why trees are useful

Trees help us store data in a shape that makes searching and organizing easier.

They are used in file systems, games, decision making, and databases.

The big idea is simple: each node can lead to smaller groups underneath it.

Predict a tree property

If a node has no children, what is it called?

  1. Leaf
  2. Root
  3. Edge
Show answer

Leaf: Yes. A node with no children is a leaf.

A tree has one parent path

Here is a tree. Every node except the root has exactly one parent.

Notice there is only one path from the root to any node. That is a key tree rule.

Tree terms to know

Two more words matter a lot: parent and child.

A parent points to nodes below it. A child is the node below its parent.

Nodes at the same level are called siblings.

Quick check on tree terms

What do we call nodes that share the same parent?

  1. Siblings
  2. Leaves
  3. Roots
Show answer

Siblings: Correct. Nodes with the same parent are siblings.

Binary tree means two children max

A binary tree is a tree where each node can have at most two children.

We usually call them the left child and the right child.

That left-right structure is what makes binary trees so useful in computing.

≤ 2 children

Binary search trees stay ordered

A binary search tree, or BST, keeps values in order.

Everything on the left is smaller than the node. Everything on the right is bigger.

That rule repeats at every node in the tree.

left < node < right

Predict BST placement

In a BST with root 7, where should 5 go?

  1. Left of 7
  2. Right of 7
  3. It can go anywhere
Show answer

Left of 7: Yes. Five is smaller than 7, so it goes left.

Insert 6 into a BST

Let’s insert 6 into a BST with root 8 and left child 3.

Compare 6 with 8. Since 6 is smaller, go left.

Then compare 6 with 3. Since 6 is bigger, go right and place it there.

Search by comparing once

Searching in a BST is fast because each comparison removes half the tree path.

If the target is smaller, go left. If it is bigger, go right.

You never need to check every node in a well-shaped BST.

Quick check on searching

You are searching for 12 in a BST. At node 9, what do you do next?

  1. Go right
  2. Go left
  3. Stop immediately
Show answer

Go right: Correct. Twelve is bigger than 9, so you go right.

Watch out for duplicates

One common mistake is forgetting what to do with equal values.

A BST needs a clear rule for duplicates, like always sending equals to one side.

The exact duplicate rule can vary, but it must stay consistent.

Your turn: insert 4

In a BST with root 9 and left child 5, where does 4 go?

  1. Left of 5
  2. Right of 5
  3. Left of 9
Show answer

Left of 5: Good. Four is smaller than 9, then smaller than 5, so it goes left of 5.

Tree and BST recap

Here is your checklist.

First, a tree is connected and has one root.

Second, a binary tree has at most two children. Third, a BST keeps smaller values left and larger values right.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app