Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.65 KB | None | 0 0
  1. %%Горохова Юлия
  2. %%5081/12
  3. %%Программа, написанная по предоставленным модульным тестам
  4. %%Часть 2.Деревья
  5.  
  6.   -module(assoc2).
  7.   -export([new/0,lookup/3, insert/3,delete/2, from_list/1, to_list/1, fold/3, filter/2]).
  8.        
  9. new() -> {nil,nil,nil,nil}.
  10.  
  11. %Поиск заданного ключа в дереве
  12. lookup(_, _, {nil, 0, nil, nil}) -> [];
  13. lookup(Key, _, {Key, H, S, B}) -> {Key, H, S, B};
  14. lookup(Key,Value, {Key1, _, Smaller, Bigger}) ->
  15.     if
  16.         Key < Key1 -> lookup(Key,Value,Smaller);
  17.         Key > Key1 -> lookup(Key,Value,Bigger)
  18.     end.
  19.    
  20. %Вставка ключа в дерево
  21. insert(Key, Value, {nil, nil, nil, nil}) ->
  22.      E = new(),
  23.      {Key, Value, E, E};
  24. insert(Key, Value, {K, _, S, B}) when Key == K -> {Key, Value, S, B};
  25. insert(Key, Value, {K, V, S, B}) when Key < K ->
  26.      T = insert(Key, Value, S),
  27.      {K, V, T, B};
  28. insert(Key, Value, {K, V, S, B}) when Key > K ->
  29.      T = insert(Key, Value, B),
  30.      {K, V, S, T}.
  31.  
  32. %Удаление элемента из дерева
  33. delete(_,{nil, 0, nil, nil}) -> {nil, 0, nil, nil};
  34. delete(Key, {Key, 1, {nil, 0, nil, nil}, {nil, 0, nil, nil}}) -> {nil, 0, nil, nil};
  35. delete(Key,{Key, _, {nil, 0, nil, nil}, B}) -> B;
  36. delete(Key, {Key, _, S, {nil, 0, nil, nil}}) -> S.
  37.  
  38. %реализация функций теста    
  39. from_list([]) -> new();
  40. from_list(Arg) -> list(Arg, new()).
  41. list([], Tree) -> Tree;
  42. list([{Arg1,Arg2}|Xs], Tree) ->
  43.     T = insert(Arg1, Arg2, Tree),
  44.     list(Xs, T).
  45.  
  46. to_list({nil, nil, nil, nil}) -> [];
  47. to_list(Tree) -> list2(Tree, []).
  48. list2({nil, nil, nil, nil}, Arg) -> Arg;
  49. list2({Key, Value, {nil, nil, nil, nil}, B}, Arg) -> list2(B, [{Key, Value}|Arg]);
  50. list2({Key, Value, S, _}, Arg) -> list2(S, [{Key, Value}|Arg]).
  51.  
  52. fold(_Fun, Q, {nil, nil, nil, nil}) -> Q;
  53. fold(Fun, Q, {Key, Value, {nil, nil, nil, nil}, B}) -> Fun(Key, Value, fold(Fun, Q, B));
  54. fold(Fun, Q, {Key, Value, S, _}) -> Fun(Key, Value, fold(Fun, Q, S)).
  55.  
  56. filter(_, {nil, nil, nil, nil}) -> {nil, nil, nil, nil};
  57. filter(Fun, Tree) -> filter(Fun, Tree, new()).
  58. filter(_Fun,{nil, nil, nil, nil}, Tree2) -> Tree2;
  59. filter(Fun, {Key, Value, {nil, nil, nil, nil}, B}, Tree2) ->
  60.                 case Fun(Key, Value) of
  61.                    true-> T = insert(Key, Value, Tree2),filter(Fun, B, T);
  62.                    false -> filter(Fun, B, Tree2)
  63.                 end;
  64. filter(Fun, {Key, Value, S, _}, Tree2) ->
  65.                 case Fun(Key, Value) of
  66.                    true-> T = insert(Key, Value, Tree2),filter(Fun, S, T);
  67.                    false -> filter(Fun, S, Tree2)
  68.                 end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement