-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.erl
56 lines (47 loc) · 1.45 KB
/
tree.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-module(tree).
-export([empty/0, insert/3, lookup/2]).
empty() ->
{node, 'nil'}.
%Nil node
insert(Key, Val, {node, 'nil'}) ->
{node, {Key, Val, {node, 'nil'}, {node, 'nil'}}};
%Recurse into the smaller branch
insert(NewKey, NewVal, {node, {Key, Val, Smaller, Larger}}) when NewKey < Key ->
{node, {Key, Val, insert(NewKey, NewVal, Smaller), Larger}};
%Recurse into larger
insert(NewKey, NewVal, {node, {Key, Val, Smaller, Larger}}) when NewKey > Key ->
{node, {Key, Val, Smaller, insert(NewKey, NewVal, Larger)}};
%Equality
insert(Key, Val, {node, {Key, _ , Smaller, Larger}}) ->
{node, {Key, Val, Smaller, Larger}}.
lookup(_,{node,'nil'})->
undefined;
lookup(Key, {node, {Key, Val,_,_}}) ->
{ok,Val};
lookup(Key, {node, {NodeKey, _, Smaller, _}}) when Key < NodeKey ->
lookup(Key, Smaller);
lookup(Key, {node,{_,_,_,Larger}}) ->
lookup(Key, Larger).
%% has_value(_. {node,'nil'}) -> false;
%% has_value(Val, {node, {_, Val, _, _}}) ->
%% true;
%% has_value(Val, node, {_,_, Left, Right}}) ->
%% case has_value(Val, Left) of
%% true -> true;
%% false -> has_value(Val, Right)
%% end.
has_value(Val, Tree) ->
try has_value1(Val,Tree) of
false ->
false
catch
true ->
true
end.
has_value1(_, {node, 'nil'}) ->
false;
has_value1(Val, {node, {_, Val, _, _}}) ->
throw(true);
has_value1(Val, {node, {_, _, Left, Right}}) ->
has_value1(Val, Left),
has_value1(Val, Right).