I want to create a binary search tree of custom data type Book. Book has two attributes, name and page. I want to use attribute page as the node of the tree. I've stuck at defining the tree. Can anyone help me with any resource? Here is the code I've tried (it's not working)
import System.IO
import Data.List
data Book = Book{
name:: String,
page::Int
}deriving (Show)
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
singleton :: (Book _ x) -> Tree x
singleton (Book _ x) = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert (Book _ x) EmptyTree = singleton (Book _ x)
treeInsert (Book _ x) (Node a left right)
| x == a = Node x left right
| x < a = Node a (treeInsert (Book _ x) left) right
| x > a = Node a left (treeInsert (Book _ x) right)
Bookdoes not take a type variable, andsingletonreturns aTree Int.Treeto hold onlyBooks, or do you want it to hold any type? Right now, you are somewhere in between.