tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
28
Task/Pattern-matching/Racket/pattern-matching.rkt
Normal file
28
Task/Pattern-matching/Racket/pattern-matching.rkt
Normal 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)]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue