14:30 There are many different BSTs that correspond to the same set of keys.
The number of compares depends on the order in which the keys come in.
In [1]:
# Setup for running examples
import sys
import os
sys.path.insert(0, '{GIT}/PrincetonAlgorithms/py'.format(GIT=os.environ['GIT']))
from AlgsSedgewickWayne.BST import BST
# Function to convert keys to key-value pairs where
# 1. the key is the letter and
# 2. the value is the index into the key list
get_kv = lambda keys: [(k, v) for v, k in enumerate(keys)]
In [2]:
# All of these will create the same best case tree shape
# Each example has the same keys, but different values
BST(get_kv(['H', 'C', 'S', 'A', 'E', 'R', 'X'])).wr_png("BST_bc0.png")
BST(get_kv(['H', 'S', 'X', 'R', 'C', 'E', 'A'])).wr_png("BST_bc1.png")
BST(get_kv(['H', 'C', 'A', 'E', 'S', 'R', 'X'])).wr_png("BST_bc2.png")
In [3]:
# These will create worst case tree shapes
BST(get_kv(['A', 'C', 'E', 'H', 'R', 'S', 'X'])).wr_png("BST_wc_fwd.png")
BST(get_kv(['X', 'S', 'R', 'H', 'E', 'C', 'A'])).wr_png("BST_wc_rev.png")