Advertisement
Guest User

Untitled

a guest
May 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. print_tree(null) :- !.
  2. print_tree(tree(K, V, L, R)) :-
  3. print_tree(L),
  4. write(K), write(V), nl,
  5. print_tree(R).
  6.  
  7. mega_build([], [], null) :- write(1), nl, !.
  8. mega_build([], [(K1, V1)], tree(K1, V1, null, null)):- write(2), nl, !.
  9. mega_build(T, [(K, V) | Map], tree(K, V, L, R)):-
  10. append(T, [(K, V)], NewT),
  11. length(NewT, L1), length(Map, L2), L1 >= L2, write(5), nl,
  12. mega_build([], T, L),
  13. mega_build([], Map, R), !.
  14. mega_build(T, [(K1, V1) | Map], tree(K, V, L, R)):-
  15. append(T, [(K1, V1)], NewT),
  16. write(NewT), nl,
  17. mega_build(NewT, Map, tree(K, V, L, R)).
  18.  
  19. split(null, _, null, null) :- !.
  20. split(tree(K, V, P, L, R), Val, tree(K, V, P, L, L1), Right) :-
  21. K < Val, split(R, Val, L1, Right),!.
  22. split(tree(K, V, P, L, R), Val, Left, tree(K, V, P, R1, R)) :-
  23. K >= Val, split(L, Val, Left, R1),!.
  24.  
  25. merge(null, T, T) :- !.
  26. merge(T, null, T) :- !.
  27. merge(tree(K1, V1, P1, L1, R1), tree(K2, V2, P2, L2, R2), tree(K1, V1, P1, L1, Right)) :-
  28. P1 < P2, merge(R1, tree(K2, V2, P2, L2, R2), Right),!.
  29. merge(tree(K1, V1, P1, L1, R1), tree(K2, V2, P2, L2, R2), tree(K2, V2, P2, Left, R2)) :-
  30. P1 >= P2, merge(tree(K1, V1, P1, L1, R1), L2, Left),write(R2),!.
  31.  
  32. map_remove(Tree, Key, NewAns) :-
  33. split(Tree, Key, Left1, Right1),
  34. split(Right1, Key + 1, _, Right2),
  35. merge(Left1, Right2, NewAns),!.
  36. map_put(null, K, V, tree(K, V, P, null, null)) :- rand_int(1000000000, P), !.
  37. map_put(Tree, Key, Value, NewAns) :-
  38. split(Tree, Key, Left1, Right1),
  39. split(Right1, Key + 1, _, Right2),
  40. rand_int(1000000000, P),
  41. merge(Left1, tree(Key, Value, P, null, null), Merg),
  42. merge(Merg, Right2, NewAns), !.
  43.  
  44. build([], Tree, Tree) :- !.
  45. build([(K, V)], Tree, Ans) :- map_put(Tree, K, V, Ans),!.
  46. build([(K, V) | Map], Tree, Ans) :-
  47. map_put(Tree, K, V, A),
  48. build(Map, A, Ans),!.
  49.  
  50. tree_build(Map, Tree) :- %mega_build([], Map, Tree).
  51. build(Map, null, Tree),!.
  52.  
  53. map_get(tree(K, V, _, _, _), K, V) :- !.
  54. map_get(tree(Key, _, _, L, _), K, V) :-
  55. K < Key, map_get(L, K, V),!.
  56. map_get(tree(Key, _, _, _, R), K, V) :-
  57. K > Key, map_get(R, K, V),!.
  58.  
  59. map_next(null, K, K, null).
  60.  
  61. map_next(tree(K, _, _, R), Key, K2, V2) :-
  62. K < Key, map_next(R, Key, K2, V2),!.
  63.  
  64. map_next(tree(K, V, L, _), Key, K, V) :-
  65. K > Key, map_next(L, Key, K3, _), (K < K3 ; K3 == Key), !.
  66.  
  67. map_next(tree(K, _, L, _), Key, K3, V3) :-
  68. K > Key, map_next(L, Key, K3, V3), K >= K3, !.
  69.  
  70. map_next(tree(K, _, _, R), K, K3, V3) :-
  71. map_next(R, K, K3, V3),!.
  72.  
  73. map_prev(null, K, K, null).
  74.  
  75. map_prev(tree(K, _, L, _), Key, K2, V2) :-
  76. K > Key, map_prev(L, Key, K2, V2),!.
  77.  
  78. map_prev(tree(K, V, _, R), Key, K, V) :-
  79. K < Key, map_prev(R, Key, K3, _), (K > K3 ; K3 == Key), !.
  80.  
  81. map_prev(tree(K, _, L, _), Key, K3, V3) :-
  82. K < Key, map_prev(L, Key, K3, V3), K =< K3, !.
  83.  
  84. map_prev(tree(K, _, L, _), K, K3, V3) :-
  85. map_prev(L, K, K3, V3),!.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement