|
101 | 101 | ;; Balancing ;;
|
102 | 102 | ;;-----------;;
|
103 | 103 |
|
104 |
| -;; Balance: -2 |
105 |
| -;; Left Rotation |
106 |
| -;; a c |
107 |
| -;; / \ / \ |
108 |
| -;; b c a e |
109 |
| -;; / \ / \ / \ |
110 |
| -;; d e b d f g |
111 |
| -;; / \ |
112 |
| -;; f g |
113 |
| - |
114 |
| -;; Balance: 2 |
115 |
| -;; Right Rotation |
116 |
| -;; a b |
117 |
| -;; / \ / \ |
118 |
| -;; b c d a |
119 |
| -;; / \ / \ / \ |
120 |
| -;; d e f g e c |
121 |
| -;; / \ |
122 |
| -;; f g |
123 |
| - |
124 | 104 | (defgeneric balance-factor (rope)
|
125 | 105 | (:method ((rope leaf))
|
126 | 106 | 0)
|
|
160 | 140 | (with-slots (left right) rope
|
161 | 141 | (let ((bf (balance-factor rope)))
|
162 | 142 | (cond ((< 0 bf)
|
163 |
| - ;; (balance-children) |
164 | 143 | (if (minusp (balance-factor left))
|
165 | 144 | (rotate-left-right rope)
|
166 | 145 | (rotate-right rope)))
|
167 | 146 | ((> 0 bf)
|
168 |
| - ;; (balance-children) |
169 | 147 | (if (plusp (balance-factor right))
|
170 | 148 | (rotate-right-left rope)
|
171 | 149 | (rotate-left rope)))
|
172 | 150 | (t
|
173 |
| - rope)))) |
174 |
| - ;; (with-slots (left right) rope |
175 |
| - ;; (let ((left (if (zerop (balance-factor left)) |
176 |
| - ;; left |
177 |
| - ;; (balance-rope left))) |
178 |
| - ;; (right (if (zerop (balance-factor right)) |
179 |
| - ;; right |
180 |
| - ;; (balance-rope right))) |
181 |
| - ;; (rope (concat-rope* left right)) |
182 |
| - ;; (bf (balance-factor rope))) |
183 |
| - ;; (format t "root: ~a, left: ~a, right: ~a~%" |
184 |
| - ;; bf |
185 |
| - ;; (balance-factor left) |
186 |
| - ;; (balance-factor right) |
187 |
| - ;; ) |
188 |
| - ;; (cond ((< 0 bf) |
189 |
| - ;; (if (minusp (balance-factor left)) |
190 |
| - ;; (rotate-left-right rope) |
191 |
| - ;; (rotate-right rope))) |
192 |
| - ;; ((> 0 bf) |
193 |
| - ;; (if (plusp (balance-factor right)) |
194 |
| - ;; (rotate-right-left rope) |
195 |
| - ;; (rotate-left rope))) |
196 |
| - ;; (t |
197 |
| - ;; rope)))) |
198 |
| - ;; (with-slots (left right) rope |
199 |
| - ;; (let* ((left (if (balance-direction left) (balance-rope left) left)) |
200 |
| - ;; (right (if (balance-direction right) (balance-rope right) right)) |
201 |
| - ;; (rope (concat-rope* left right)) |
202 |
| - ;; (balance (balance rope))) |
203 |
| - ;; (cond ((< 1 balance) (rotate rope :right)) |
204 |
| - ;; ((> -1 balance) (rotate rope :left)) |
205 |
| - ;; (t rope)))) |
206 |
| - )) |
| 151 | + rope)))))) |
207 | 152 |
|
208 | 153 | ;;---------;;
|
209 | 154 | ;; Rebuild ;;
|
|
0 commit comments