

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


The goal of this assignment is to write your own implementation of AVL trees in Haskell. A sample imple- mentation of part of the functionality is provided on the course web page, but you are encouraged not to look at this implementation unless you get stuck: You’ll get the most out of the assignment if you try each step yourself, from scratch, and peek at the sample implementation only as necessary.
data AVL e = E -- empty tree | N -- non-empty tree Int -- balance factor (right size - left size) (AVL e) -- left subtree e -- value (AVL e) -- right subtree deriving Show
t1 = ... t2 = ... t3 = ...
... and some others that do not...
bad1 = ... bad2 = ... bad3 = ...
check :: (Ord e, Show e) => AVL e -> IO ()
that, given an AVL tree e, verifies that e satisfies the following properties:
(a) The branching factor at each node is correctly calculated. (You’ll need a helper function height for this.) (b) The branching factor at each node is between -1 and +1. (c) The items stored in e are in strictly increasing order. (One easy way to do this is to define a helper function avlToList that returns a list of values encountered during an inorder traversal of e and another function that checks whether a list is in strictly increasing order.)
If any of these conditions fail, the check function should print out a message saying what is wrong.
avlInsert :: Ord e => e -> AVL e -> AVL e
that simply returns its second argument unchanged.
checkInsert :: Ord e => e -> AVL e -> IO (AVL e)