, 9.7. X , , . X
(Left Right) , : , , , , 9.8. Right ( Y) , X. . , , Left.
, 9.5. : delminl Tree, Y, Treel)
- (.. ) Tree, a Treel - Tree Y.
9,
RfgM |
Right |
flight |
rue. n.. , X
9,5.
% del(Tree, X, NewTree):
% X ;'"-'. Tree del(t(nil, X, Right), X, Right). dell t(Left, x, nil), X, Left).
del(t(Left, X, Right), X, t(Left, Y, Rightl)):-
delmin(Right, Y, Rightl). del (t(Left, Root, Right), X, t(Leftl, Root, Right)) Root, X), del! Left, x, Leftl).
del (t(Left, Root, Right), X, t(Left, Root, Rightl)) gt X, Root), del(Right, X, Rightl),
-
: -
% delminl Tree, ■, NewTree):
* NewTree
Tree
delraint t(nil,
R)
delmini t(Left, Root, Right), X, t(Leftl, Root, Right)):-delmint Left, Y, Leftl).
. add , , , . .
X D, .
X D (, X ).
D , X D, X D.
D. : addroot; D, X, D1)
X , D, a D1 , X. X, D D1
I. Prolog
|
|
. 9.9. . Li L2 . 9.9 (, , R1 R2). , .
L1 L2 .
L2 L.
L1 X I X.
._ /V |
<
Ycx
D1
. 9.9. X
addrcot- , X L , L1 L2. Prolog L1 L2 : addrootf L, X, t(Lb X, L2
- R2: addroot! Rr X, tl Rl, X, R2))
9.6 "" .
9.6.
i add(Tree, X, HewTree): i X Tree % add[ Tree, X, HewTree):- addroot; Tree, X, NewTree). % X |
addf t(L, Y, R), Y,, t (LI, i, R)) gt(Y, x), add(L, X, LI).
add{ t (L, Y, RJ, X, 1 (L, Y, Rl)
:;■ X, Y),
% X
9.
add(, X, Rl).
4 addrootf Tree, X, HewTree):
Tree HewTree
addrootf nil, X, t[ nil, X, nil)). %
addroot (tt L, Y, ", X, t(. X, t(L2, Y, R))):-
gt(Y, X), addroott L, X, t(1, X, L2>>.
addroott t(L, Y, R), X, t(t(L, Y, Rl), X, R2)):-
gt(X, Y),
addroott R, X, t(Rl, X, R2)).
, . add , . , :. 3, 5, 1, 6, 5, DD
add(nil, 3, Di), add (Dl, 5, D2), add f D2, 1, D3), add(D3, 6, D), add f DD, 5, D)