Last active
          April 21, 2018 06:19 
        
      - 
      
- 
        Save hsinewu/a141a401ec704eeaaa0f23489fd4ec01 to your computer and use it in GitHub Desktop. 
    Naive (unbalanced) binary search tree in haskell
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | data BinarySearchTree = Null | Node Int (BinarySearchTree) (BinarySearchTree) | |
| deriving Show | |
| insert Null val = Node val Null Null | |
| insert (Node x left right) val | |
| | x > val = Node x left' right | |
| | otherwise = Node x left right' | |
| where | |
| left' = insert left val | |
| right' = insert right val | |
| fromList xs = foldl insert Null xs | |
| toPreorderArray Null = [] | |
| toPreorderArray (Node x left right) = toPreorderArray left ++ [x] ++ toPreorderArray right | |
| main = do | |
| print $ tree1 | |
| print $ tree2 | |
| print $ toPreorderArray tree2 | |
| where | |
| a = insert Null 7 | |
| b = insert a 3 | |
| tree1 = insert b 9 | |
| tree2 = fromList [4,1,2,3,45,6,2,4] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment