Data Structures19 sections · 729 units
Open in Course

Tree Properties

Counting nodes and edges

Some fundamental properties of trees: A tree with nn nodes has exactly n1n - 1 edges. Each node except the root has exactly one incoming edge from its parent.

A binary tree can be unbalanced. In the worst case (a "stick"), height equals n1n - 1. In the best case (perfect balance), height is log2n\lfloor \log_2 n \rfloor.

For a complete binary tree stored in an array (index starting at 0):

  • Parent of node ii: (i1)/2\lfloor (i-1)/2 \rfloor
  • Left child of node ii: 2i+12i + 1
  • Right child of node ii: 2i+22i + 2

This array representation is how heaps work. You'll return to it later.