Lecture 14, July 19
The lecture notes I post serve two purposes: to remind you of the topics we discussed and to provide any interesting code examples I did during lecture. I would describe these notes as an outline, not a summary, of what I talked about during lecture. You are not expected to be able to learn the material simply from examining these notes, nor is reading these notes a reasonable substitute for attending lecture.
2-3-4 Trees
Be sure you can perform insertions and deletions by hand. Be sure you understand the valid? function and how it works.
;; A 2-3-4 tree is either (Empty) or
;; - (2-node (a l m))
;; - (3-node (a b l m n))
;; - (4-node (a b c l m n o))
;; Where a, b, c are numbers and l, m, n, o are 2-3-4 trees
;; A 2-3-4 tree must always be perfectly balanced
;; A 2-3-4 tree must always satisfy the condition that l < a < m < b < n < o
(define-struct Empty ())
(define-struct 2-node
([e0 : Real]
[t0 : Tree]
[t1 : Tree]))
(define-struct 3-node
([e0 : Real]
[e1 : Real]
[t0 : Tree]
[t1 : Tree]
[t2 : Tree]))
(define-struct 4-node
([e0 : Real]
[e1 : Real]
[e2 : Real]
[t0 : Tree]
[t1 : Tree]
[t2 : Tree]
[t3 : Tree]))
(define-type Tree (U Empty 2-node 3-node 4-node))
(: 2-3-4-insert (Real Tree -> Tree))
(define (2-3-4-insert i t)
(match t
[(Empty) (2-node i (Empty) (Empty))]
[(2-node a (Empty) (Empty))
(cond
[(< i a) (3-node i a (Empty) (Empty) (Empty))]
[(= i a) t]
[else (3-node a i (Empty) (Empty) (Empty))])]
[(2-node a l m)
(cond
[(< i a)
(match l
[(4-node sa sb sc sl sm sn so)
(cond
[(< i sb)
(3-node sb a (2-3-4-insert i (2-node sa sl sm)) (2-node sc sn so) m)]
[(= i sb) t]
[else
(3-node sb a (2-node sa sl sm) (2-3-4-insert i (2-node sc sn so)) m)])]
[_ (2-node a (2-3-4-insert i l) m)])]
[(= i a) t]
[else
(match m
[(4-node sa sb sc sl sm sn so)
(cond
[(< i sb)
(3-node a sb l (2-3-4-insert i (2-node sa sl sm)) (2-node sc sn so))]
[(= i sb) t]
[else
(3-node a sb l (2-node sa sl sm) (2-3-4-insert i (2-node sc sn so)))])]
[_ (2-node a l (2-3-4-insert i m))])])]
[(3-node a b (Empty) (Empty) (Empty))
(cond
[(< i a) (4-node i a b (Empty) (Empty) (Empty) (Empty))]
[(= i a) t]
[(< i b) (4-node a i b (Empty) (Empty) (Empty) (Empty))]
[(= i b) t]
[else (4-node a b i (Empty) (Empty) (Empty) (Empty))])]
[(3-node a b l m n)
(cond
[(< i a)
(match l
[(4-node sa sb sc sl sm sn so)
(cond
[(< i sb)
(4-node sb a b (2-3-4-insert i (2-node sa sl sm)) (2-node sc sn so) m n)]
[(= i sb) t]
[else
(4-node sb a b (2-node sa sl sm) (2-3-4-insert i (2-node sc sn so)) m n)])]
[_ (3-node a b (2-3-4-insert i l) m n)])]
[(= i a) t]
[(< i b)
(match m
[(4-node sa sb sc sl sm sn so)
(cond
[(< i sb)
(4-node a sb b l (2-3-4-insert i (2-node sa sl sm)) (2-node sc sn so) n)]
[(= i sb) t]
[else
(4-node a sb b l (2-node sa sl sm) (2-3-4-insert i (2-node sc sn so)) n)])]
[_ (3-node a b l (2-3-4-insert i m) n)])]
[(= i b) t]
[else
(match n
[(4-node sa sb sc sl sm sn so)
(cond
[(< i sb)
(4-node a b sb l m (2-3-4-insert i (2-node sa sl sm)) (2-node sc sn so))]
[(= i sb) t]
[else
(4-node a b sb l m (2-node sa sl sm) (2-3-4-insert i (2-node sc sn so)))])]
[_ (3-node a b l m (2-3-4-insert i n))])])]
[(4-node a b c l m n o)
(cond
[(< i b) (2-node b (2-3-4-insert i (2-node a l m)) (2-node c n o))]
[(= i b) t]
[else (2-node b (2-node a l m) (2-3-4-insert i (2-node c n o)))])]))
(: size (Tree -> Nonnegative-Integer))
(define (size t)
(match t
[(Empty) 0]
[(2-node _ l r) (+ 1 (size l) (size r))]
[(3-node _ _ l m r) (+ 1 (size l) (size m) (size r))]
[(4-node _ _ _ l ml mr r) (+ 1 (size l) (size ml) (size mr) (size r))]))
(: search : (Real Tree -> Boolean))
(define (search a t)
(match t
[(Empty) #f]
[(2-node v l r)
(cond
[(< a v) (search a l)]
[(= a v) #t]
[else (search a r)])]
[(3-node v w l m r)
(cond
[(< a v) (search a l)]
[(= a v) #t]
[(< a w) (search a m)]
[(= a w) #t]
[else (search a r)])]
[(4-node v w x l ml mr r)
(cond
[(< a v) (search a l)]
[(= a v) #t]
[(< a w) (search a ml)]
[(= a w) #t]
[(< a x) (search a mr)]
[(= a x) #t]
[else (search a r)])]))
(: min (Tree -> Real))
(define (min t)
(match t
[(Empty) +inf.0]
[(2-node v (Empty) _) v]
[(2-node _ l _) (min l)]
[(3-node v _ (Empty) _ _) v]
[(3-node _ _ s _ _) (min s)]
[(4-node v _ _ (Empty) _ _ _) v]
[(4-node _ _ _ s _ _ _) (min s)]))
(: max (Tree -> Real))
(define (max t)
(match t
[(Empty) -inf.0]
[(2-node v _ (Empty)) v]
[(2-node _ _ r) (max r)]
[(3-node _ v _ _ (Empty)) v]
[(3-node _ _ _ _ r) (max r)]
[(4-node _ _ v _ _ _ (Empty)) v]
[(4-node _ _ _ _ _ _ r) (max r)]))
(: height (Tree -> Nonnegative-Integer))
(define (height t)
(match t
[(Empty) 0]
[(2-node _ l _) (add1 (height l))]
[(3-node _ _ l _ _) (add1 (height l))]
[(4-node _ _ _ l _ _ _) (add1 (height l))]))
(: valid-tree (Tree -> Boolean))
(define (valid-tree t)
(match t
[(Empty) #t]
[(2-node v l m)
(and (< (max l) v)
(< v (min m))
(= (height l) (height m))
(valid-tree l)
(valid-tree m))]
[(3-node v w l m n)
(and (< (max l) v)
(< v (min m))
(< (max m) w)
(< w (min n))
(= (height l) (height m))
(= (height m) (height n))
(valid-tree l)
(valid-tree m)
(valid-tree n))]
[(4-node v w x l m n o)
(and (< (max l) v)
(< v (min m))
(< (max m) w)
(< w (min n))
(< (max n) x)
(< x (min o))
(= (height l) (height m))
(= (height m) (height n))
(= (height n) (height o))
(valid-tree l)
(valid-tree m)
(valid-tree n)
(valid-tree o))]))
(: inorder (Tree -> (Listof Real)))
(define (inorder t)
(match t
[(Empty) '()]
[(2-node v l m)
(append (inorder l)
(cons v (inorder m)))]
[(3-node v w l m n)
(append (inorder l)
(append (cons v (inorder m))
(cons w (inorder n))))]
[(4-node v w x l m n o)
(append (inorder l)
(append (cons v (inorder m))
(append (cons w (inorder n))
(cons x (inorder o)))))]))
(foldl 2-3-4-insert (Empty) (build-list 40 (λ ([n : Integer]) (random 100))))
Exam Topics
New Material:
- 2-3-4 trees
- efficiency
- tail recursion
- Sorting Algorithms: insert, merge, quick
On Quiz 4:
- Trees: expression trees, binary trees, non-binary trees, polymorphic trees, binary search trees
- Match
- Lambda
- Higher Order Programming: build-list map filter foldr foldl
On Quiz 3:
- Polymorphism: functions, structures, and types
- local
- Recursively defined structures: Nat, dolls, list
- Subtypes
- Singleton types (Zero, One, Void, Null)
- Lists: empty, cons, first, rest, emmpty?, cons?, list?
Quiz 2:
- Structures: define-struct, constructors, selectors, predicates
- Union types
- Simple recursion
Quiz 1:
- Prefix notation
- Function definition
- Simple types
- Function types
- Expression trees
- Truth tables