Deleting from Binary Search Tree and Sequentional Hash

In this homework you will write two functions. The first implements a deleting function over a binary search tree, and the second implements Hashing function stored in a array with sequential search to diambiguate collissions.

What to do

Instructions

Start with deleteTree.

deleteMin works by following the longest left chain without an empty.

Linear hashing works as follows.


-- Author : _________________

module InsertAndHash where

import ArrCommands
import Test.HUnit(Test(TestCase,TestList),assertEqual,runTestTT)
import HashFunction

data BinSearchTree a
   = Empty
   | Branch (BinSearchTree a) a (BinSearchTree a)

deleteTree :: (Ord key) => key -> BinSearchTree (key,value) ->  BinSearchTree (key,value)
deleteTree key x = undefined

deleteMin :: BinSearchTree a -> (a,BinSearchTree a)
deleteMin tree = undefined

testD = do { print testTree; putStrLn "\n"; print t1 }
  where testTree:: BinSearchTree (Int,Int)
        testTree = createTests [(7,1),(4,6),(9,2),(1,3),(6,5),(8,4)]
        t1 = deleteTree  6 testTree

------------------------------------------------------
-- Linear hashing

insertHash:: (String,value) -> Array (Maybe(String,value)) -> IO ()
insertHash (key,value) arr =
  do { (low,hi) <- boundsArr arr
     ; let hashLoc = undefined
     ; insert (low,hi) hashLoc hashLoc (key,value) arr
     }


insert:: (Int,Int) -> Int -> Int -> (String,value) -> Array (Maybe(String,value)) -> IO ()
insert (low,hi) hashLoc current (k,v) arr = undefined


testH =
  do { table <- newArr (3,7) Nothing
     ; printArr table
     ; undefined -- include tests here
     }


findHash:: String -> Array (Maybe(String,value)) -> IO (Maybe value)
findHash key arr = undefined


findH:: (Int,Int) -> Int -> Int -> String -> Array (Maybe(String,value)) -> IO(Maybe value)
findH (low,hi) hashLoc current k arr = undefined


------------------------------------------------------------
-- This section Included so you can use it to create test
-- data easily, and print out Trees.

insertTree :: (Ord key) => (key,value) -> BinSearchTree (key,value) ->  BinSearchTree (key,value)
insertTree (k,v) Empty = Branch Empty (k,v) Empty
insertTree (k,v) (Branch left (key,satelite) right)
  | k>=key =  Branch left (key,satelite) (insertTree (k,v) right)
  | k < key =  Branch (insertTree (k,v) left) (key,satelite) right

createTests [] = Empty
createTests ((k,v):more) = insertTree (k,v) (createTests more)

showT n Empty = ""
showT n (Branch left x right) = showT (n+3) right ++
                                replicate n ' ' ++ show x ++ "\n" ++
                                showT (n+3) left

instance Show t => Show (BinSearchTree t) where
  show x = showT 0 x

-------------------------------------------------------

main = undefined


What to turn in.

Create a file by cutting and pasting the template between the dark black horizontal lines. Be sure you do the following

Back to the Daily Record.

Back to the class web-page.