I am trying to define a simple binary search tree. It is stored in lists like so: [Key, Left Tree, Right Tree]. I'm having issue with my logic. This is what I have
bstadd(K, [], [K,[],[]]).
bstadd(K, [X|_], [X, [], [K, [], []]]) :- K > X.
bstadd(K, [X, [], [K, [], []]], [X|_]) :- K < X.
This is what I query
1 ?- bstadd(19, [],T1), bstadd(20,T1,T2), bstadd(21,T2,T3).
this is what I get out
T1 = [19, [], []],
T2 = [19, [], [20, [], []]],
T3 = [19, [], [21, [], []]]
and this is what I need
[19, [], [20, [], [21, [], []]]]
Any help would be wonderful. I have been banging my head against the wall for days.
t(K,L,R)