作者 Anonymous [actionscript] 2012-01-14 13:57 (点击下载)

  1. -- file: splay.hs
  2.  
  3. import Prelude hiding (null)
  4.  
  5. data (Ord n) => Splay n = Leaf
  6. | Bin n (Splay n) (Splay n)
  7. deriving (Show)
  8.  
  9. null Leaf = True
  10. null (Bin {}) = False
  11.  
  12. singleton k = Bin k Leaf Leaf
  13.  
  14. splay Leaf _ = Leaf
  15. splay tr k =
  16. let
  17. (kk, ll, rr) = lookup tr [] []
  18. in
  19. Bin kk (connl ll) (connr rr)
  20. where
  21. connl [] = Leaf
  22. connl lts = foldl1 ( acc (Bin k l _) -> (Bin k l acc)) lts
  23. connr [] = Leaf
  24. connr rts = foldl1 ( acc (Bin k _ r) -> (Bin k acc r)) rts
  25. lookup tr@(Bin tk lt rt) lts rts =
  26. if k == tk then
  27. (tk, lt : lts, rt : rts)
  28. else if k < tk then
  29. if null lt then
  30. (tk, lt : lts, rt : rts)
  31. else
  32. let
  33. (Bin ltk _ _) = lt
  34. tr1@(Bin tk1 lt1 rt1) =
  35. if k < ltk then
  36. zig tr
  37. else
  38. tr
  39. in
  40. if null lt then
  41. (tk1, lt1 : lts, rt1 : rts)
  42. else
  43. lookup lt1 lts (tr1 : rts)
  44. else
  45. if null rt then
  46. (tk, lt : lts, rt : rts)
  47. else
  48. let
  49. (Bin rtk _ _) = rt
  50. tr1@(Bin tk1 lt1 rt1) =
  51. if k > rtk then
  52. zag tr
  53. else
  54. tr
  55. in
  56. if null rt then
  57. (tk1, lt1 : lts, rt1 : rts)
  58. else
  59. lookup lt1 (tr1 : lts) rts
  60. where
  61. zig (Bin x (Bin y at bt) ct) = Bin y at (Bin x bt ct)
  62. zag (Bin y at (Bin x bt ct)) = Bin x (Bin y at bt) ct

提交下面的校正或者修改. (点击这里开始一个新的帖子)
姓名: 在 cookie 中记住我的名字

屏幕抓图:(jpeg 或 png)