SHARE
TWEET

Untitled

Bch_50 May 20th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. merge(null, Root2, Root2) :- !.
  2. merge(Root1, null, Root1) :- !.
  3. merge((Val1, Key1, Pr1, Left1, Right1), (Val2, Key2, Pr2, Left2, Right2), (Val1, Key1, Pr1, Left1, NewRight1)) :- Pr1 > Pr2, merge(Right1, (Val2, Key2, Pr2, Left2, Right2), NewRight1).
  4. merge((Val1, Key1, Pr1, Left1, Right1), (Val2, Key2, Pr2, Left2, Right2), (Val2, Key2, Pr2, NewLeft2, Right2)) :- Pr1 =< Pr2, merge((Val1, Key1, Pr1, Left1, Right1), Left2, NewLeft2).
  5.  
  6. split(null, _, null, null) :- !.
  7. split((XVal, Key, Pr, Left, Right), Val, (XVal, Key, Pr, Left, NewRight), Res2) :- XVal =< Val, split(Right, Val, NewRight, Res2), !.
  8. split((XVal, Key, Pr, Left, Right), Val, Res1, (XVal, Key, Pr, NewLeft, Right)) :- XVal > Val, split(Left, Val, Res1, NewLeft), !.
  9.  
  10. tree_build([], null) :- !.
  11. tree_build([(HVal, HKey) | []], (HVal, HKey, HPr, null, null)) :-  rand_int(100, HPr), !.
  12. tree_build([(HVal, HKey) | T], V) :- T \= [], tree_build(T, Res), rand_int(100, HPr), merge((HVal, HKey, HPr, null, null), Res, V).
  13.  
  14. map_get((Val, Key, Pr, Left, Right), Val, Key) :- !.
  15. map_get((XVal, XKey, Pr, Left, Right), Val, Key) :- (XVal > Val, map_get(Left, Val, Key)) ; (XVal < Val, map_get(Right, Val, Key)).
  16.  
  17. map_replace([], _, _, []) :- !.
  18. map_replace(null, _, _, null) :- !.
  19. map_replace((Val, Key, Pr, Left, Right), Val, NewKey, (Val, NewKey, Pr, Left, Right)) :- !.
  20. map_replace((XVal, XKey, Pr, Left, Right), Val, NewKey, (XVal, XKey, Pr, Res, Right)) :- XVal > Val, map_replace(Left, Val, NewKey, Res).
  21. map_replace((XVal, XKey, Pr, Left, Right), Val, NewKey, (XVal, XKey, Pr, Left, Res)) :- XVal < Val, map_replace(Right, Val, NewKey, Res).
  22.  
  23. change((Val, Key, Pr, Left, Right), NewKey, (Val, NewKey, Pr, Left, Right)).
  24. map_put(null, Val, NewKey, (Val, NewKey, Pr, null, null)) :- rand_int(100, Pr), !.
  25. map_put((XVal, XKey, Pr, Left, Right), Val, NewKey, Res) :-
  26.                                                     split((XVal, XKey, Pr, Left, Right), Val, ResLeft, ResRight),
  27.                                                     split(ResLeft, Val - 1, LeftLeft, Middle),
  28.                                                     ((Middle == null,
  29.                                                     rand_int(100, NewPr),
  30.                                                     merge(LeftLeft, (Val, NewKey, NewPr, null, null), CurRes));
  31.                                                     (Middle \= null,
  32.                                                      change(Middle, NewKey, NewNode),
  33.                                                      merge(LeftLeft, NewNode, CurRes))),
  34.                                                      merge(CurRes, ResRight, Res).
  35. map_remove(null, _, null) :- !.
  36. map_remove((XVal, XKey, Pr, Left, Right), Val, Res) :-
  37.                                                     split((XVal, XKey, Pr, Left, Right), Val, ResLeft, ResRight),
  38.                                                     Val1 is Val - 1,
  39.                                                     split(ResLeft, Val1, LeftLeft, Middle),
  40.                                                     merge(LeftLeft, ResRight, Res).
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top