Homework 5: Binary Trees in Haskell, Assignments of Computer Science

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-f76
koofers-user-f76 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 5 CIS 252 (Spring 2009) Introduction to Computer Science
Coverage
This assignment focuses on material in Chapter 14 of Haskell: The Craft of Func-
tional Programming.
Logistics
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.
Background: Binary Trees
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
Abinary 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):
7


BBBBy y
8


@@
13


BB
20


BBBB
12


BBBB
y yy
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:
Aleaf is a node that has no children. The nodes labeled 7, 12, and 20 above
are leaves; the nodes labeled 8 and 13 are not leaves.
The left child (respectively, right child) of the node labeled by 8 is the
node labeled by 13 (respectively, 20). The node labeled by 13 has a right
child, but not a left child.
The left subtree (respectively, right subtree) of a node nis 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 depth of a node is its distance to the root of the tree (i.e., the topmost
node). Thus the depth of the node labeled 7 is 0, while the depth of the
node labeled 12 is 2.
The height of a tree is the length of path from the root to the deepest leaf
of the tree. The height of the tree rooted at 8 is 2.
A binary tree is full if it has no nodes with only one child (i.e., all of its
nodes are either leaves or have two children).
1The distinction between children and subtrees is blurred by our representation of binary trees; you don’t need to worry about the distinction for this assignment.
Page 1
pf3

Partial preview of the text

Download Homework 5: Binary Trees in Haskell and more Assignments Computer Science in PDF only on Docsity!

Coverage

This assignment focuses on material in Chapter 14 of Haskell: The Craft of Func-

tional Programming.

Logistics

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.

Background: Binary Trees

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):

B

B

y BBy

B

B

B

B

12 BB

B

B

BB

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:

  • A leaf is a node that has no children. The nodes labeled 7, 12, and 20 above are leaves; the nodes labeled 8 and 13 are not leaves.
  • The left child (respectively, right child ) of the node labeled by 8 is the node labeled by 13 (respectively, 20). The node labeled by 13 has a right child, but not a left child.

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 depth of a node is its distance to the root of the tree (i.e., the topmost node). Thus the depth of the node labeled 7 is 0, while the depth of the node labeled 12 is 2.
  • The height of a tree is the length of path from the root to the deepest leaf of the tree. The height of the tree rooted at 8 is 2.
  • A binary tree is full if it has no nodes with only one child (i.e., all of its nodes are either leaves or have two children). (^1) The distinction between children and subtrees is blurred by our representation of binary trees; you don’t need to worry about the distinction for this assignment.

The tree rooted at 7 is full; the tree rooted at 8 is not full, because the node labeled by 13 has one child.

  • A perfect binary tree is a full binary tree in which all leaves have the same depth. Thus, a perfect tree of height h has 2h+^1 − 1 nodes: 2h^ are leaves, and 2h^ − 1 are internal nodes.

For example, the following is a perfect tree of height 3:

^1

H

H

H

H

H

HH

L

LL

L

LL

L

LL

L

LL

B

BB

B

BB

B

BB

B

BB

B

BB

B

BB

B

BB

B

w w w w w w w w w w w w w w wBBw

Exercises

  1. (15 points) Write a Haskell function

height :: BTree a -> Int

such that height tree calculates the height of tree. For an empty tree, return -1.

  1. (15 points) Write a Haskell function

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.

  1. (15 points) Write a Haskell function

full :: BTree a -> Bool

such that full tree determines whether tree is a full binary tree.

  1. (15 points) Write a Haskell function

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)

  1. (15 points) Write a Haskell function

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.

  1. (15 points) Write a Haskell function

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).

  1. (15 points) Write a Haskell function

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: