Almost complete binary tree of height h

  • For h = 0, just a single node
  • For h = 1, left child or two children
  • For h ≥ 2, either:
    • the left subtree of the root is complete with height h-1 and the right is almost complete with height h-1, OR
    • the left is almost complete with height h-1 and the right is complete with height h-2

push() expected running time

  • How far to move up?
    • Half of the nodes are leaves, so half of the inserts will only move up one level
    • A quarter of the nodes are one level above the leaves, so one quarter of the inserts will move up two levels
    • One eighth will require moving up 3 levels
    • One sixteenth will require moving up 4 levels
    • Expected running time:
       
      • 1/2 * 1 + 1/4 * 2 + 1/8 * 3 + ... = Sum((1/(2^i))*i = 2