|
121 | 121 | ;; / \
|
122 | 122 | ;; f g
|
123 | 123 |
|
124 |
| -(defgeneric rotate (rope direction) |
125 |
| - (:method ((rope branch) (direction (eql :left))) |
126 |
| - (with-slots (left right) rope |
127 |
| - (concat-rope* |
128 |
| - (concat-rope* left (branch-left right)) |
129 |
| - (branch-right right)))) |
130 |
| - (:method ((rope branch) (direction (eql :right))) |
131 |
| - (with-slots (left right) rope |
132 |
| - (concat-rope* |
133 |
| - (branch-left left) |
134 |
| - (concat-rope* (branch-right left) right))))) |
135 |
| - |
136 |
| -(defmethod rotate :around (rope direction) |
137 |
| - (format t "rope: ~a, rotating: ~a~%" (write-rope rope nil) direction) |
138 |
| - (call-next-method)) |
139 |
| - |
140 |
| -(defgeneric balance-direction (rope) |
| 124 | +(defgeneric balance-factor (rope) |
141 | 125 | (:method ((rope leaf))
|
142 |
| - nil) |
| 126 | + 0) |
143 | 127 | (:method ((rope branch))
|
144 |
| - (with-slots (left right) rope |
145 |
| - (let ((balance (- (rope-depth left) (rope-depth right)))) |
146 |
| - (cond ((< 1 balance) :right) |
147 |
| - ((> -1 balance) :left) |
148 |
| - (t nil)))))) |
149 |
| - |
150 |
| -;; (defmethod balance :around (rope) |
151 |
| -;; (let ((bal (call-next-method))) |
152 |
| -;; (format t "rope: ~a, balance: ~a~%" (write-rope rope nil) bal) |
153 |
| -;; bal)) |
| 128 | + (- (rope-depth (branch-left rope)) |
| 129 | + (rope-depth (branch-right rope))))) |
| 130 | + |
| 131 | +(defun rotate-left (rope) |
| 132 | + (with-slots (left right) rope |
| 133 | + (concat-rope* |
| 134 | + (concat-rope left (branch-left right)) |
| 135 | + (branch-right right)))) |
| 136 | + |
| 137 | +(defun rotate-right (rope) |
| 138 | + (with-slots (left right) rope |
| 139 | + (concat-rope* |
| 140 | + (branch-left left) |
| 141 | + (concat-rope (branch-right left) right)))) |
| 142 | + |
| 143 | +(defun rotate-left-right (rope) |
| 144 | + (with-slots (left right) rope |
| 145 | + (rotate-right (concat-rope* (rotate-left left) right)))) |
| 146 | + |
| 147 | +(defun rotate-right-left (rope) |
| 148 | + (with-slots (left right) rope |
| 149 | + (rotate-left (concat-rope* left (rotate-right right))))) |
| 150 | + |
| 151 | +(defun balance-children (rope) |
| 152 | + (with-slots (left right) rope |
| 153 | + (concat-rope* (balance-rope left) |
| 154 | + (balance-rope right)))) |
154 | 155 |
|
155 | 156 | (defgeneric balance-rope (rope)
|
156 | 157 | (:method ((rope leaf))
|
157 | 158 | rope)
|
158 | 159 | (:method ((rope branch))
|
159 | 160 | (with-slots (left right) rope
|
160 |
| - (let* ((left (if (balance-direction left) (balance-rope left) left)) |
161 |
| - (right (if (balance-direction right) (balance-rope right) right)) |
162 |
| - (rope (concat-rope* left right)) |
163 |
| - (balance (balance rope))) |
164 |
| - (cond ((< 1 balance) |
165 |
| - (rotate rope :right)) |
166 |
| - ((> -1 balance) |
167 |
| - (rotate rope :left)) |
| 161 | + (let ((bf (balance-factor rope))) |
| 162 | + (cond ((< 0 bf) |
| 163 | + ;; (balance-children) |
| 164 | + (if (minusp (balance-factor left)) |
| 165 | + (rotate-left-right rope) |
| 166 | + (rotate-right rope))) |
| 167 | + ((> 0 bf) |
| 168 | + ;; (balance-children) |
| 169 | + (if (plusp (balance-factor right)) |
| 170 | + (rotate-right-left rope) |
| 171 | + (rotate-left rope))) |
168 | 172 | (t
|
169 |
| - rope)))))) |
| 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 | + )) |
170 | 207 |
|
171 | 208 | ;;---------;;
|
172 | 209 | ;; Rebuild ;;
|
|
0 commit comments