Bitpath

Lesson 1 · Unit 01

Roots and leaves

Nodes, links, and measuring a tree with recursion

11 exercisesabout 3 min

What you practise

  • Vocabulary, a node class, height and size by recursion

Try a question from this lesson

Pick one

In this picture every node hangs from exactly one node above it, with one exception. Which node has nothing above it?

#       8
#      / \
#     3   10
#    / \    \
#   1   6    14
//       8
//      / \
//     3   10
//    / \    \
//   1   6    14
//       8
//      / \
//     3   10
//    / \    \
//   1   6    14

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

A tree is nodes and links

A binary tree is built from nodes. Each node holds a value and two links, left and right. Either link may be empty.

The top node is the root. A node with no children is a leaf. It is a linked list where every node has two "next" pointers instead of one.

class Node:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

root = Node(8)
root.left = Node(3)
root.right = Node(10)
print(root.left.val)   # 3
class Node {
  left = null;
  right = null;
  constructor(val) {
    this.val = val;
  }
}

const root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
console.log(root.left.val); // 3
class Node {
  left: Node | null = null;
  right: Node | null = null;
  constructor(public val: number) {}
}

const root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
console.log(root.left.val); // 3

Python noteNone plays the role of the empty link. if node.left: is the usual way to ask whether a child exists.

TypeScript noteNode | null says a link is either a node or empty. public val: number in the constructor declares the field and assigns it in one step.

Concept

Trees are made of smaller trees

Each child is the root of a smaller tree, so tree functions nearly write themselves. Answer the question for the empty tree, then combine the answers from left and right.

To add up every value: an empty tree totals 0. Otherwise it is this node's value plus the totals of its two subtrees.

def total(node):
    if node is None:
        return 0
    left = total(node.left)
    right = total(node.right)
    return node.val + left + right
function total(node) {
  if (node === null) return 0;
  const left = total(node.left);
  const right = total(node.right);
  return node.val + left + right;
}
type Tree = Node | null;

function total(node: Tree): number {
  if (node === null) return 0;
  const left = total(node.left);
  const right = total(node.right);
  return node.val + left + right;
}

Python noteif node is None is the precise test for the empty tree. if not node works too and is common in short solutions.

TypeScript notetype Tree = Node | null is an alias: a short name for "a node or nothing". After the === null check, TypeScript knows node is a real Node, so node.left is allowed.

8 more exercises in the app

  • 2× type the answer
  • 1× match the pairs
  • 2× multiple choice
  • 1× fill the blanks
  • 1× true or false
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy