Created
April 1, 2018 05:41
-
-
Save abhijeet2096/728fecd2553d127b985774542d34fa78 to your computer and use it in GitHub Desktop.
Binary Search Tree Implementation in Prolog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Topic : Binary Search Tree Implementation in Prolog | |
%% Author : Abhijeet Sharma | Akhil Singhal | |
%% Date : 26/03/2018 | |
%% Course : Paradigms of Programming | |
%% Sample Inputs | |
%% Input inl([3,6,1,2,4,7,9],nil,X). | |
%% X = t(3, t(1, nil, t(2, nil, nil)), t(6, t(4, nil, nil), t(7, nil, t(9, nil, nil)))) | |
%% del(2,t(3, t(1, nil, t(2, nil, nil)), t(6, t(4, nil, nil), t(7, nil, t(9, nil, nil)))),X). | |
%% X = t(3, t(1, nil, nil), t(6, t(4, nil, nil), t(7, nil, t(9, nil, nil)))) | |
%% lookup(27,t(3, t(1, nil, t(2, nil, nil)), t(6, t(4, nil, nil), t(7, nil, t(9, nil, nil))))). | |
%% false. | |
ins(I, nil, t(I, nil, nil)). | |
ins(I, t(X, L, R), t(Y, P, Q)) :- | |
( I < X | |
-> ins(I, L, U), | |
(P, Y, Q) = (U, X, R) | |
; I > X | |
-> ins(I, R, U), | |
(P, Y, Q) = (L, X, U) | |
; (P, Y, Q) = (L, X, R) | |
). | |
lookup(X,t(X,_,_)). | |
lookup(X,t(V,L,_)) :- X < V, lookup(X,L). | |
lookup(X,t(V,_,R)) :- X > V, lookup(X,R). | |
del(V,t(V,nil,R),R). | |
del(V,t(V,L,nil),L). | |
del(V,t(NVal,nil,nil),t(NVal,nil,nil)) :- V \= NVal. | |
del(V,t(NVal,L,R),t(NVal,L,DRight)) :- V > NVal, del(V,R,DRight). | |
del(V,t(NVal,L,R),t(NVal,DLeft,R)) :- V < NVal, del(V,L,DLeft). | |
del(V,t(V,L,R),t(NewVal,L,DRight)) :- getLeftMost(R,NewVal), del(NewVal,R,DRight). | |
getLeftMost(t(V,nil,_),V). | |
getLeftMost(t(_,L,_),NVal) :- getLeftMost(L,NVal). | |
inl([N|Ns], T0, T) :- | |
ins(N, T0, T1), | |
inl(Ns, T1, T). | |
inl([], T, T). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, this was certainly the best example of a tree I've encountered so far, but I'm still having some difficulty understanding the use of some variables, could you elaborate a bit more?
Despite this, thank you. S
orry about my English