Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. implementation module BinSearchTree
  2.  
  3. import StdEnv
  4. import BinTree
  5. import BinTreePrint
  6. //(Node :: a (Tree a) (Tree a) -> (Tree a)
  7. // Leaf is a data constructor of arity zero (Leaf :: Tree a)
  8. z0 = Leaf
  9. z1 = insertTree 50 z0
  10. z2 = insertTree 10 z1
  11. z3 = insertTree 75 z2
  12. z4 = insertTree 80 z3
  13. z5 = insertTree 77 z4
  14. z6 = insertTree 10 z5
  15. z7 = insertTree 75 z6
  16. z8 = deleteTree 50 z7
  17.  
  18. Start0 = toString z7
  19. Start1 = toString z8
  20. Start2 = toString t7
  21.  
  22. // Uit het diktaat, blz. 73:
  23. insertTree :: a (Tree a) -> Tree a | Ord a
  24. insertTree e Leaf = Node e Leaf Leaf
  25. insertTree e (Node x le ri)
  26. | e <= x = Node x (insertTree e le) ri
  27. | e > x = Node x le (insertTree e ri)
  28.  
  29. deleteTree :: a (Tree a) -> (Tree a) | Eq, Ord a
  30. deleteTree e Leaf = Leaf
  31. deleteTree e (Node x le ri)
  32. | e < x = Node x (deleteTree e le) ri
  33. | e == x = join le ri
  34. | e > x = Node x le (deleteTree e ri)
  35. where
  36. join :: (Tree a) (Tree a) -> (Tree a)
  37. join Leaf b2 = b2
  38. join b1 b2 = Node x b1` b2
  39. where
  40. (x,b1`) = largest b1
  41.  
  42. largest :: (Tree a) -> (a,(Tree a))
  43. largest (Node x b1 Leaf) = (x,b1)
  44. largest (Node x b1 b2) = (y,Node x b1 b2`)
  45. where
  46. (y,b2`) = largest b2
  47.  
  48. full_tree_check :: (a a -> Bool) a (Tree a) -> Bool | Ord a
  49. full_tree_check _ _ Leaf = True
  50. full_tree_check f x (Node n l r)
  51. |f n x = (full_tree_check f x l) && (full_tree_check f x r)
  52. |otherwise = False
  53.  
  54. is_geordend :: (Tree a) -> Bool | Ord a
  55. is_geordend Leaf = True
  56. is_geordend (Node x Leaf Leaf) = True
  57. is_geordend (Node x Leaf ri)
  58. | full_tree_check (>=) x ri = is_geordend ri
  59. | otherwise = False
  60. is_geordend (Node x le Leaf)
  61. | full_tree_check (<=) x le = is_geordend le
  62. | otherwise = False
  63. is_geordend (Node x le ri)
  64. | (full_tree_check (<=) x le) && (full_tree_check (>=) x ri)= is_geordend le && is_geordend ri
  65. | otherwise = False
  66.  
  67.  
  68. Start3 = map is_geordend [t0,t1,t2,t3,t4,t5,t6,t7]
  69.  
  70.  
  71. is_gebalanceerd :: (Tree a) -> Bool
  72. is_gebalanceerd Leaf = True
  73. is_gebalanceerd (Node _ l r)
  74. | (d <= 1) && (d >= -1) = True
  75. | otherwise = False where d = (diepte l) - (diepte r)
  76.  
  77. Start4 = map is_gebalanceerd [t0,t1,t2,t3,t4,t5,t6,t7]
  78. Start = (
  79. //Start0,'\n',
  80. //Start1,'\n',
  81. Start2,'\n',
  82. Start3,'\n',
  83. Start4,'\n'
  84. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement