

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for a homework assignment in cis 252 (introduction to computer science) focused on binary trees using haskell. It includes background information on binary trees, definitions, and examples. Students are required to write functions to calculate the height of a binary tree, check if an item is in a binary tree, determine if a binary tree is full, shift labels in a binary tree, replace leaves with empty nodes, and create a perfect binary tree.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


This assignment focuses on material in Chapter 14 of Haskell: The Craft of Func-
tional Programming.
This homework is officially due in the bin in CST 3-212 by noon on Thursday,
March 5. However, it comes with an automatic extension: anything submitted
by noon on Friday, March 6 will be accepted as being on time.
You may work singly or in pairs on this assignment.
As always, you’re expected to follow the design recipe discussed in lecture.
Turn in a hard copy of your source code and a transcript demonstrating con-
vincingly that your code is correct. Also, submit your code (but not transcript)
electronically.
Trees are ubiquitous data structures in Computer Science: they’re used to order
data (in what are known as search trees), to represent syntactic objects for mini-
languages, and to provide a hierarchical structure on (say) file directories. This
write-up should include all the definitions you need for this homework. If you
want more information, however, check out:
http://en.wikipedia.org/wiki/Binary_tree
A binary tree is a tree in which every node has at most two children, and the
children are ordered (that is, there is a left child and a right child). We can define
(polymorphic) binary trees in Haskell using the following type definition:
data BTree a = Empty | BNode a (BTree a) (BTree a) deriving (Show)
According to this definition, each node has a label: every label in a given tree
has the same type, just as every element in a list has the same type.
As an example, (Node 7 Empty Empty) is the BTree Int representation of the
tree on the left below (the filled-in circles represent empty trees):
y BBy
y y y
y y
The BTree Int representation of the tree on the right is: BNode 8 (BNode 13 Empty (BNode 12 Empty Empty)) (BNode 20 Empty Empty).
Here are some common definitions and examples:
The left subtree (respectively, right subtree ) of a node n is the tree rooted by n’s left child (respectively, right child). That is, a child is a single node; a subtree is a node, plus all of its descendants (ie, its children’s children, and their children, and so on).^1
The tree rooted at 7 is full; the tree rooted at 8 is not full, because the node labeled by 13 has one child.
For example, the following is a perfect tree of height 3:
w w w w w w w w w w w w w w wBBw
height :: BTree a -> Int
such that height tree calculates the height of tree. For an empty tree, return -1.
inTree :: Eq a => a -> BTree a -> Bool
such that inTree item tree determines whether or not item appears as a label for any of the nodes in tree.
full :: BTree a -> Bool
such that full tree determines whether tree is a full binary tree.
shift :: Int -> BTree Int -> BTree Int
such that (shift n tr) creates a new version of tr in which every node label is increased by n. For example,
shift 90 (BNode 3 Empty (BNode 9 Empty Empty))
should return:
BNode 93 Empty (BNode 99 Empty Empty)
fall :: BTree a -> BTree a
such that (fall tree) returns a new tree that is the result of replacing each leaf in tree with Empty.
spring :: a -> BTree a -> BTree a
such that (spring sprout tree) returns the new tree that is the result of replacing each Empty subtree in tree with (BNode sprout Empty Empty).
makePerfect :: Int -> a -> BTree a
such that makePerfect h item creates a perfect tree of height h, with each node labeled by item. If h is negative, simply return Empty. For example: