Originally intended to use CPP, Ada, and python to rewrite one copy, I think this behavior is relatively 2.
Because scheme is the most elegant expression.
# Lang Scheme
(Define nil '())
(Define (root tree) (Car tree ))
(Define (left-tree) (CADR tree ))
(Define (right-tree) (caddr tree ))
(Define (height tree)
(Cond [(null? Tree) 0]
[Else (cadddr tree)])
(Define (make-leaf ELEM) (List ELEM nil 1 ))
(Define (make-AVL-tree root left right)
(List root left right (+ 1 (max (height left)
(Height right )))))
(Define (contains-ELEM? ELEM tree)
(Cond [(null? Tree) False]
[(= ELEM (root tree) True]
[(<ELEM (root tree ))
(Contains-ELEM? ELEM (left-tree)]
[(> ELEM (root tree ))
(Contains-ELEM? ELEM (right-tree)])
(Define (rotate-left Tree)
(Cond [(null? Tree) tree]
[Else (make-AVL-tree (root (left-tree ))
(Left-tree ))
(Make-AVL-tree (root tree)
(Right-tree (left-tree ))
(Right-tree)])
(Define (rotate-right tree)
(Cond [(null? Tree) tree]
[Else (make-AVL-tree (root (right-tree ))
(Make-AVL-tree (root tree)
(Left-tree)
(Left-tree (right-tree )))
(Right-tree)])
(Define (rotate-right-left Tree)
(Cond [(null? Tree) tree]
[Else (make-AVL-tree (left-tree (right-tree ))
(Make-AVL-tree (root tree)
(Left-tree)
(Left-tree (right-tree ))))
(Make-AVL-tree (root (right-tree ))
(Right-tree (left-tree (right-tree )))
(Right-tree)])
(Define (rotate-left-right tree)
(Cond [(null? Tree) tree]
[Else (make-AVL-tree (root (right-tree (left-tree )))
(Make-AVL-tree (root (left-tree ))
(Left-tree ))
(Left-tree (right-tree (left-tree ))))
(Make-AVL-tree (root tree)
(Right-tree (left-tree )))
(Right-tree)])
(Define (balance-AVL-tree)
(Define (factor tree)
(-(Height (right-tree ))
(Height (left-tree ))))
(Let ([F (factor tree)])
(Cond [(= F 2)
(Cond [(<(factor (right-tree) 0)
(Rotate-right-left Tree)]
[Else (rotate-right tree)]
[(= F-2)
(Cond [(> (factor (left-tree) 0)
(Rotate-left-right tree)]
[Else (rotate-left Tree)]
[Else tree])
(Define (insert-ELEM tree)
(Define (insert-in-son ELEM tree)
(Cond [(null? Tree)
(Make-leaf ELEM)]
[(<ELEM (root tree ))
(Let * ([newlefttree (insert-in-son ELEM (left-tree)]
[Newavltree (make-AVL-tree (root tree)
Newlefttree
(Right-tree)])
(Balance-AVL-tree newavltree)]
[(> ELEM (root tree ))
(Let * ([newrighttree (insert-in-son ELEM (right-tree)]
[Newavltree (make-AVL-tree (root tree)
(Left-tree)
Newrighttree)])
(Balance-AVL-tree newavltree)]
[Else tree])
(Cond [(contains-ELEM? ELEM tree) tree]
[Else (insert-in-son ELEM tree)])
(Define (delete-ELEM tree)
(Define (delete-left-most tree)
(Cond [(left-Empty? Tree) tree]
[Else (let * ([leftmost (delete-left-most (left-tree)]
[Newrighttree (make-AVL-tree (root tree)
(Right-tree leftmost)
(Right-tree)])
(Make-AVL-tree (root leftmost)
Nil
(Balance-AVL-tree newrighttree)])
(Define (delete-in-son ELEM tree)
(Cond [(<ELEM (root tree ))
(Let * ([newlefttree (delete-in-son ELEM (left-tree)]
[Newavltree (make-AVL-tree (root tree)
Newlefttree
(Right-tree)])
(Balance-AVL-tree newavltree)]
[(> ELEM (root tree ))
(Let * ([newrighttree (delete-in-son ELEM (right-tree)]
[Newavltree (make-AVL-tree (root tree)
(Left-tree)
Newrighttree)])
(Balance-AVL-tree newavltree)]
[(= ELEM (root tree ))
(Cond [(and (right-Empty? Tree)
(Left-Empty? Tree ))
Nil]
[(Right-Empty? Tree)
(Left-tree)]
[(Left-Empty? Tree)
(Right-tree)]
[Else (LET ([leftmost (delete-left-most (right-tree)])
(Make-AVL-tree (root leftmost)
(Left-tree)
(Right-tree leftmost)])
(Define (left-Empty? Tree) (null? (Left-tree )))
(Define (right-Empty? Tree) (null? (Right-tree )))
(Cond [(contains-ELEM? ELEM tree)
(Delete-in-son ELEM tree)]
[Else tree])
(Define (list-> AVL elems)
(Define (ITER elems tree)
(Cond [(null? Elems) tree]
[Else (ITER (CDR elems)
(Insert-ELEM (CAR elems) Tree)])
(Cond [(null? Elems) '()]
[Else (LET ([AVL (make-leaf (CAR elems)])
(ITER (CDR elems) AVL)])