tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,28 @@
#lang racket
(struct t-node (color t-left value t-right))
(define (balance t)
(match t
[(t-node 'black (t-node 'red (t-node 'red a x b) y c) z d)
(t-node 'red (t-node 'black a x b) y (t-node 'black c z d))]
[(t-node 'black (t-node 'red a x (t-node 'red b y c)) z d)
(t-node 'red (t-node 'black a x b) y (t-node 'black c z d))]
[(t-node 'black a x (t-node 'red (t-node 'red b y c) z d))
(t-node 'red (t-node 'black a x b) y (t-node 'black c z d))]
[(t-node 'black a x (t-node 'red b y (t-node 'red c z d)))
(t-node 'red (t-node 'black a x b) y (t-node 'black c z d))]
[else t]))
(define (insert x s)
(define (ins t)
(match t
['empty (t-node 'red 'empty x 'empty)]
[(t-node c a y b)
(cond [(< x y)
(balance (t-node c (ins a) y b))]
[(> x y)
(balance (t-node c a y (ins b)))]
[else t])]))
(match (ins s)
[(t-node _ a y b) (t-node 'black a y b)]))