package main
import (
"flag"
"fmt"
"math"
"time"
"algo/metrics"
)
// cmdBalance is lesson 11's `shape` run again, with the balanced tree beside
// the plain one. Same libraries, same two insertion orders, one extra structure.
func cmdBalance(args []string) error {
fs := flag.NewFlagSet("balance", flag.ContinueOnError)
shuf := fs.String("shuffled", "library.jsonl", "library in random order")
sorted := fs.String("sorted", "sorted.jsonl", "the same library, pre-sorted")
if err := fs.Parse(args); err != nil {
return err
}
a, err := load(*shuf)
if err != nil {
return err
}
b, err := load(*sorted)
if err != nil {
return err
}
fmt.Printf("%-10s %-10s %8s %8s %10s %14s %12s\n",
"structure", "insert", "n", "height", "log2 n", "cmps/lookup", "lookup time")
for _, tc := range []struct {
name string
items []Item
}{{"shuffled", a}, {"-sorted", b}} {
bst := NewTreeIndex()
var c metrics.Counter
for _, it := range tc.items {
bst.Put(it, &c)
}
var l metrics.Counter
t0 := time.Now()
for _, it := range tc.items {
bst.Get(it.Title, &l)
}
el := time.Since(t0)
n := float64(bst.Len())
fmt.Printf("%-10s %-10s %8d %8d %10.1f %14.1f %12s\n",
"BST (11)", tc.name, bst.Len(), bst.Height(), math.Log2(n), float64(l.N())/n, el.Round(time.Microsecond))
}
for _, tc := range []struct {
name string
items []Item
}{{"shuffled", a}, {"-sorted", b}} {
avl := NewAVLIndex()
var c metrics.Counter
for _, it := range tc.items {
avl.Put(it, &c)
}
var l metrics.Counter
t0 := time.Now()
for _, it := range tc.items {
avl.Get(it.Title, &l)
}
el := time.Since(t0)
n := float64(avl.Len())
fmt.Printf("%-10s %-10s %8d %8d %10.1f %14.1f %12s\n",
"AVL (12)", tc.name, avl.Len(), avl.Height(), math.Log2(n), float64(l.N())/n, el.Round(time.Microsecond))
}
return nil
}
// cmdRotate is the price: what balancing costs on input the plain BST already
// handled perfectly well.
func cmdRotate(args []string) error {
fs := flag.NewFlagSet("rotate", flag.ContinueOnError)
in := fs.String("in", "library.jsonl", "library file")
if err := fs.Parse(args); err != nil {
return err
}
items, err := load(*in)
if err != nil {
return err
}
bst := NewTreeIndex()
var bc metrics.Counter
t0 := time.Now()
for _, it := range items {
bst.Put(it, &bc)
}
bBuild := time.Since(t0)
avl := NewAVLIndex()
var ac metrics.Counter
t0 = time.Now()
for _, it := range items {
avl.Put(it, &ac)
}
aBuild := time.Since(t0)
n := float64(len(items))
fmt.Printf("n = %d, inserted in the order given\n\n", len(items))
fmt.Printf(" %-10s %14s %12s %12s %10s\n", "", "build cmps", "build time", "rotations", "height")
fmt.Printf(" %-10s %14d %12s %12s %10d\n", "BST (11)", bc.N(), bBuild.Round(time.Microsecond), "-", bst.Height())
fmt.Printf(" %-10s %14d %12s %12d %10d\n", "AVL (12)", ac.N(), aBuild.Round(time.Microsecond), avl.Rotations, avl.Height())
fmt.Printf("\n rotations per insert: %.3f\n", float64(avl.Rotations)/n)
fmt.Printf(" worst |balance| anywhere in the AVL tree: %d\n", avl.WorstBalance())
var bl, al metrics.Counter
for _, it := range items {
bst.Get(it.Title, &bl)
avl.Get(it.Title, &al)
}
fmt.Printf("\n lookups: BST %.3f cmps, AVL %.3f cmps\n", float64(bl.N())/n, float64(al.N())/n)
return nil
}
// cmdReload answers lesson 11's drill 1: is the naive save-and-reload safe now?
func cmdReload(args []string) error {
fs := flag.NewFlagSet("reload", flag.ContinueOnError)
in := fs.String("in", "library.jsonl", "library file")
if err := fs.Parse(args); err != nil {
return err
}
items, err := load(*in)
if err != nil {
return err
}
var c metrics.Counter
bst := NewTreeIndex()
avl := NewAVLIndex()
for _, it := range items {
bst.Put(it, &c)
avl.Put(it, &c)
}
fmt.Printf("original: BST height %5d, AVL height %5d\n", bst.Height(), avl.Height())
// Save with Keys() — sorted — and reload by re-inserting, as the drill did.
bst2 := NewTreeIndex()
for _, k := range bst.Keys() {
bst2.Put(Item{Title: k}, &c)
}
avl2 := NewAVLIndex()
for _, k := range avl.Keys() {
avl2.Put(Item{Title: k}, &c)
}
fmt.Printf("reloaded from Keys(): BST height %5d, AVL height %5d\n", bst2.Height(), avl2.Height())
fmt.Printf("\nAVL rotations during that reload: %d (%.3f per insert)\n",
avl2.Rotations, float64(avl2.Rotations)/float64(avl2.Len()))
return nil
}