ProLog 0123Tree Vrs (Haoh=#)1:

//Prolog// Programming for Artificial Intelligence.//pdf// - Higher Intellect…Printed in Great Britain bv Commercial Colour Press. British Library Cataloguing in Publication Data. Bratko. Ivan. Prolog programming for artificial intelligence.

ProLog 0123Tree Vrs (Haoh=#)1:

[pdf pg 256] pg 238 PROLOG PROGRAMMING FOR ARTIFICIAL INTELLIGENCE
% Insertion and deletion in the vertically balanced 2-3 dictionary

newtree:[0,[]];

ins(n0, X): [n(1,[X])];

Nohd(VaL):[1,[VaL]];.

plus([0,[]],X):[1,[X]];

ins(n1(N), X): [n1(N), X, n1(x)] :- N < X;

ins123(n1(N), X): [n1(x), N, n1(N)] :- X < N;

ob(n1(N), {
ins(X): R :-
N < X, R = [n1(N), X, n1(X)];
X > N, R = [n1(X), N, n1(N)].
}).

ins123(n1(N),X): R :-
N < X, R = [n1(N), X, n1(X)];
X > N, R = [n1(X), N, n1(N)].

plus([1,[OhLd]], New):OwT_LisT :-
OhLd < New, OuTLisT = [ 2, [ [1,OhLd], [1,New] ] ];
New < Ohld, OuTList = [ 2, [ [1,New], [1,OhLd] ] ].

ins( n2(Li,M,Ri), X ): [T] :-
X < M, ins(Li, X): L,
( L=[T1], T=n2(T1,M,RT);
L=[LT, M2, MT], T=t3(LT,M2,MT,M,Ri)
);

ins(n2(L, M, R), X): [n3(LT, M2, MT, M, R)] :-
X < Mi, ins(L, X): [LT, M2, MT].

ins(n2(L, M, R), X): [n3(L, M, MT, M2, RT)] :-
Mi < X, ins(R, X): [MT, M2, RT].

ins(n2(Li, M, Ri), X): [T] :-
( M < X, ins(Ri, X): L);
X < M, ins(Li, X): L)
),(
L=[T], T=n2(L, M, T);
L=[MT,M2,RT], T=n3(Li,M,MT,m2,RT)
).

ins(n3(TL,ML,TM,MR,TR), X): [n3(NL,ML,MT,MR,RT)] :-
x < ML, ins(TL, X): [NL];

ins(n3(TL,ML,TM,MR,TR), X): [n2(n(NL,NM,NR),ML,n2(TM,MR,TR)] :-
x < ML, ins(TL, X): [NL.NM,NR];

ins(n3(TL,ML,TM,MR,TR), X): [n3(TL,ML,MT,MR,NR)] :-
MR < X, ins(TR, X): [NR];

ins(n3(TL,ML,TM,MR,TR), X): [n2(n(TL,ML,TM),ML,n2(NL,NM,NR)] :-
MR < X, ins(TR, X): [NL.NM,NR];

ins(n3(TL,ML,TM,MR,TR), X): [n3(TL,ML,NT,MR,tR)] :-
ML < X, x < MR, ins(TM, X): [NT];

ins(n3(TL,ML,TM,MR,TR), X): [n2(n(TL,ML,NL),NM,n2(NR,Mr,TR)] :-
ML < X, x < MR, ins(TM, X): [NL.NM,NR];

ins123(n3(TL,ML,TM,MR,TR),X): RL :-
X < ML, ins(TL,X):L, (
L=[NL], RL=[n3(NL,ML,MT,MR,RT)];
L=[NL.NM,NR], RL=[n2(n(NL,NM,NR),ML,n2(TM,MR,TR)]
);
MR < X, ins(TR,X): L, (
L=[NR], RL=[n3(TL,ML,MT,MR,NR)];
L=[NL.NM,NR], RL=[n2(n(TL,ML,TM),ML,n2(NL,NM,NR)]
);
ML < X, X < MR, ins(TM, X):L, (
L=[NT], RL=[n3(TL,ML,NT,MR,tR)];
L=[NL.NM,NR], RL=[n2(n(TL,ML,NL),NM,n2(NR,Mr,TR)]
);