コンテンツへスキップ

Lesson 81: Binary tree

English

Left is earlier, right is later

  • In a binary tree each node has up to two children: a left pointer and a right pointer. The first node is the root.
  • In an ordered binary tree, everything in the left subtree comes before the node, and everything in the right subtree comes after it.
  • In-order traversal, left, node, right, visits the data in order. The function calls itself for each subtree: recursion.
Index leftPtr data rightPtr
0 1 Red 2
1 3 Green 4
2 6 Yellow -1
3 -1 Blue -1
4 5 Orange -1
5 -1 Indigo -1
6 -1 Violet -1

The ordered binary tree: Red is the root (index 0). Its left child is Green (1) and its right child Yellow (2). Green has Blue (3) on the left and Orange (4) on the right; Orange has Indigo (5) on the left; Yellow has Violet (6) on the left. Left means earlier in the alphabet, right means later

  • rootPtr = 0. Lessons 76 to 78 show the other array-based structures: linked list, stack and queue.

ログインまたはアカウント作成

IGCSE, A-Level & AP