Docsity

Exercise Solutions for COSC 3015: Tree Data Structures - Prof. James L. Caldwell, Assignments of Computer Science

Toggle sidebar
bg1
HW 12 Prof. Caldwell
Due: 9 October 2008 COSC 3015
1 Trees
Exercise 1.1. Read chapter 6 of the Bird text.
Recall the BTree datatype and it’s structural induction principle for finite BTrees.
data BTree a = Leaf a | Fork (BTree a) (BTree a)
deriving (Show,Eq)
(∀x:a. P [Leaf x]∧
∀x:a. ∀t1, t2: (BTree a).(P[t1]∧P[t2]) ⇒P[Fork(t1, yt2)])
⇒
∀t: (BTree a). P [t]
Exercise 1.2. Here is a data-type declaration for a type of tree that can store two different types
of values in it’s nodes.
data ABTree a b = Leaf |
ANode a (ABTree a b) (ABTree a b) |
BNode b (ABTree a b) (ABTree a b)
deriving (Show,Eq)
Write the structural induction principle for this data-type.
Exercise 1.3.
i.) Write an recursive datatype declaration (of your choice) for another tree-like data type and,
ii.) write the structural induction principle for properties of finite instanes of your data-type.
Exercise 1.4. consider the following:
size (Leaf x) = 1
size (Fork xt yt) = (size xt) + (size yt)
height (Leaf x) = 0
height (Fork xt yt) = 1 + (height xt ‘max‘ height yt)
perfect (Leaf x) = True
perfect (Fork xt yt) = (height xt == height yt) && perfect xt && perfect yt
Prove1the following theorem hold for all finite BTrees by structural induction.
Theorem 1.1. ∀t:BTree a. (perfect t)⇒size t= 2(height t)
1Hints will be given in class on Thursday.
1

Partial preview of the text

Download Exercise Solutions for COSC 3015: Tree Data Structures - Prof. James L. Caldwell and more Assignments Computer Science in PDF only on Docsity!

HW 12 Prof. Caldwell Due: 9 October 2008 COSC 3015

1 Trees

Exercise 1.1. Read chapter 6 of the Bird text. Recall the BTree datatype and it’s structural induction principle for finite BTrees.

data BTree a = Leaf a | Fork (BTree a) (BTree a) deriving (Show,Eq)

(∀x : a. P [Leaf x] ∧ ∀x : a. ∀t 1 , t 2 : (BTree a). (P [t 1 ] ∧ P [t 2 ]) ⇒ P [Fork(t 1 , yt 2 )]) ⇒ ∀t : (BTree a). P [t]

Exercise 1.2. Here is a data-type declaration for a type of tree that can store two different types of values in it’s nodes.

data ABTree a b = Leaf | ANode a (ABTree a b) (ABTree a b) | BNode b (ABTree a b) (ABTree a b) deriving (Show,Eq)

Write the structural induction principle for this data-type.

Exercise 1.3.

i.) Write an recursive datatype declaration (of your choice) for another tree-like data type and,

ii.) write the structural induction principle for properties of finite instanes of your data-type.

Exercise 1.4. consider the following:

size (Leaf x) = 1 size (Fork xt yt) = (size xt) + (size yt)

height (Leaf x) = 0 height (Fork xt yt) = 1 + (height xt ‘max‘ height yt)

perfect (Leaf x) = True perfect (Fork xt yt) = (height xt == height yt) && perfect xt && perfect yt

Prove^1 the following theorem hold for all finite BTrees by structural induction.

Theorem 1.1. ∀t : BTree a. (perfect t) ⇒ size t = 2(height^ t)

(^1) Hints will be given in class on Thursday.