The notes

Definition and property

For each node: left subtree keys < node key < right subtree keys.

  • Example tree: root 50, with children 30 and 70, and so on
  • Terms shown: root, parent, children (left and right), leaves, edges

Key operations

Insertion (example: insert 35)

  • Is 35 < 50? Yes, go left
  • Is 35 > 30? Yes, go right
  • Found the spot: attach as the right child

Searching (finding)

  • Finding 60: 60 > 50, go right; 60 < 70, go left; find 60
  • Finding 25 (not found): go left and right down to a NULL position, so it is not in the tree

Traversals

  • In-order (left, root, right): visits in sorted order, e.g. [20, 30, 40, 50, 60, 70, 80]
  • Pre-order (root, left, right): useful for copying, e.g. [50, 30, 20, 40, 70, 60, 80]
  • Post-order (left, right, root): useful for deletion, e.g. [20, 40, 30, 60, 80, 70, 50]

Balance is key

  • Balanced tree: efficient search, O(log N)
  • Degenerate / skewed tree: slow search, O(N) (like a linked list)

Key takeaway. Balance is key: a balanced BST searches in O(log N), while a skewed one degrades to O(N).

Data structures deck

  1. 1. Linear vs non-linear
  2. 2. Arrays
  3. 3. Hashmaps
  4. 4. Stacks
  5. 5. Queue
  6. 6. Linked list
  7. 7. Binary tree
  8. 8. BST
  9. 9. Graphs
  10. 10. Heaps

Watch the lesson

Data structures, Lesson 20, from 5:11: What is a Binary Search Tree