3 views

Uploaded by Batman

- B-symbols
- hw1-solns
- Mining Interesting Rules
- mcqs
- 12_computer Science (Set - 1)
- IT Final Upto 4th Year Syllabus 09.01.14
- Descriptiondd
- DS QB
- umn_dlu_oagw
- Game of 20
- CS_3_8_syllabus 07102015
- JK
- Handout 9735 OG9735-Doc
- aos buddy
- prac4Spec
- Tree Data Structures
- Lecture Notes COMP3506
- Data and file structure lab manual
- Tree Data Structure
- 16-DOMTree (1).pptx

You are on page 1of 6

Lab 4

3 February 2017

1 Introduction

The goal for the this lab is to make you more comfortable writing functions that operate on

trees.

Please take advantage of this opportunity to practice writing functions and proofs with

the assistance of the TAs and your classmates. You are encouraged to collaborate with your

classmates and to ask the TAs for help.

Update your clone of the git repository to get the files for this weeks lab as usual by running

git pull

1.2 Methodology

You must use the five step methodology for writing functions for every function you write on

this assignment. In particular, every function you write should have a purpose and tests.

1

2 Trees Are Never Sad Look At Them Every Once In

Awhile Theyre Quite Beautiful

Jaden Smith (@officialjaden), 9/19/2013

We will take a look at some trees (notice, they are indeed quite beautiful). Recall the

definition of trees from lecture:

datatype tree = Empty

| Node of (tree * int * tree)

As with any datatype, we can case on a tree like this:

case t of

Empty => ...

| Node (l, x, r) => ...

Similarly, we could pattern match for a clausal function like this:

fun foo (Empty : tree) : t = ...

| foo (Node (l,x,r)) = ...

In the following two tasks, assume that the tree you are working with represents a com-

putation tree. That is, each node contains an integer corresponding to the cost of making

that computation, and the left and right sub-trees of the nodes are computation trees for

dependent sub-expressions.

Task 2.1 Define the function

work : tree -> int

that computes the work of evaluating an expression given its computation tree.

Task 2.2 Define the function

span : tree -> int

that computes the span of evaluating an expression given its computation tree. Hint: You

will probably find the function max : int * int -> int useful. We have provided an

implementation of this function for you.

Jaden Smith (@officialjaden), 3/23/2013

Unfortunately, we have no way to represent the color of a tree (in a manner relevant to the

data structure described thus far in the course). We can, however, work with the shape of a

tree. For this problem, instead of creating blue trees, we will create balanced trees.

For testing, it is useful to be able to create a tree from a list of integers. To make things

interesting, we will ask you to return a balanced tree: one where the depths of any two leaves

differ by no more than 1.

Task 3.1 Define the function

2

listToTree : int list -> tree

that transforms the input list into a balanced tree. Hint: You may use the split function

provided in the support code, whose spec is as follows:

If l is non-empty, then there exist l1,x,l2 such that

split l == (l1,x,l2) and

l == l1 @ x::l2 and

length(l1) and length(l2) differ by no more than 1

Jaden Smith (@officialjaden), 5/01/2013

Regardless of whether our eyes are real or not, we can certainly mirror the shape of a tree.

Here is a function similar to the flatten function from lecture, which computes an in-order

traversal of a tree:

fun treeToList (Empty : tree) : int list = []

| treeToList (Node (l,x,r)) = treeToList l @ (x :: (treeToList r))

Observe that treeToList is total.

In this problem, you will define a function to mirror, or reverse a tree, so that the in-order

traversal of the reverse comes out backwards:

treeToList (revT t) = reverse (treeToList t)

Code

revT : tree -> tree

according to the above spec.

Task 4.2 Explain why revT is total.

Have the TAs check your code for reverse before proceeding!

Analysis

Task 4.3 Determine the recurrence for the work of your revT function, in terms of the size

(number of elements) of the tree. You may assume the tree is balanced.

Task 4.4 Use the closed form to determine the big-O of WrevT .

Task 4.5 Determine the recurrence for the span of your revT function, in terms of the size

of the tree. You may assume the tree is balanced.

Task 4.6 Use the closed form to give a big-O for SrevT .

3

Correctness

Prove the following:

= reverse (treeToList t).

You may use the following lemmas about reverse on lists:

reverse []

= []

For all expressions l and r of type int list such that l and r both reduce to values,

reverse (l @ (x::r))

= (reverse r) @ (x::(reverse l))

In your justifications, be careful to prove that expressions evaluate to values when this

is necessary. Follow the template on the following page.

4

Case for Empty

To show:

Two Inductive hypotheses:

To show:

5

Have the TAs check your analysis and proof before proceeding!

Jaden Smith (@officialjaden), 9/29/2013

At this point, it behooves us to introduce another of SMLs built-in datatypes: order. order

is a very simple datatypeit has precisely three values: GREATER, EQUAL, and LESS, and is

defined as follows:

As you may have guessed, order represents the relative ordering of two values. At present,

we care only about the relative ordering of ints. SML provides a function Int.compare :

int * int -> order which compares two ints and calculates whether the first is GREATER

than, EQUAL to, or LESS than the second respectively. This allows us to implement tri-valued

comparisons, as follows:

GREATER => (* x1 > x2 *)

| EQUAL => (* x1 = x2 *)

| LESS => (* x1 < x2 *)

that, assuming the tree is sorted, returns true if and only if the tree contains the given

number. Your implementation should have work and span proportional to the depth of the

tree. You should use Int.compare, rather than <, in your solution.

- B-symbolsUploaded by0x0FF0
- hw1-solnsUploaded bysiddiquentc
- Mining Interesting RulesUploaded bySanethel Maan
- mcqsUploaded byGaurav Khullar
- 12_computer Science (Set - 1)Uploaded bysaksham sharma
- IT Final Upto 4th Year Syllabus 09.01.14Uploaded byসন্দীপ চন্দ্র
- DescriptionddUploaded byJacobKrupski
- DS QBUploaded bySharma Divya
- umn_dlu_oagwUploaded by6600i
- Game of 20Uploaded byspygen123
- CS_3_8_syllabus 07102015Uploaded byAnonymous HyCiDJwJm
- JKUploaded byHamilton Pinheiro
- Handout 9735 OG9735-DocUploaded bymarius_doman87
- aos buddyUploaded bypraveena
- prac4SpecUploaded byEitze Wouda
- Tree Data StructuresUploaded bysam456789123
- Lecture Notes COMP3506Uploaded byJack
- Data and file structure lab manualUploaded byRajendra Shetty
- Tree Data StructureUploaded byTanmay Baranwal
- 16-DOMTree (1).pptxUploaded byParandaman Sampathkumar S
- Lab TestUploaded byAmalia 夏美
- Data Structure Lec35 HandoutUploaded byJʋŋaiɗ Aĸɓar
- idmUploaded byJallaRk
- Frmwebshowdoc Rep 128094Uploaded byAamir Khan
- 18-avlUploaded byapi-323349664
- ExamplesUploaded byMohanakrishna
- Aads_10 Tree Data Structures and Tree AdtUploaded byLeo George
- GeometricUploaded byPriya Wali
- UNIT-V_QAUploaded byChakkaravarthiEr
- vulunerability classificationUploaded byanon-500134

- tshLabUploaded byBatman
- 15122written9Uploaded byBatman
- syllabusUploaded byBatman
- W8 (1)Uploaded byBatman
- WK6_planUploaded byBatman
- newell-simon-hall-floor2.pdfUploaded byBatman
- Talking-Esports-FINAL-2.pdfUploaded byBatman
- 322AFall13-syllabusUploaded byBatman
- 322AFall13-syllabusUploaded byBatman
- Mar 09 18 Analysis FormulationUploaded byBatman
- Newell Simon Hall Floor2Uploaded byBatman
- S18_Week_2Uploaded byBatman
- Ourobros PaperUploaded bycijone
- Polling BoundsUploaded byBatman
- Excel s16 m1Uploaded byBatman
- 02Chapter_ComputerMusicTutorial.pdfUploaded byBatman
- Scribd 11Uploaded byBatman
- Short LabUploaded byBatman
- Scribd 10Uploaded byBatman
- TempUploaded byBatman
- Scribd 12Uploaded byBatman
- Scribd 15Uploaded byBatman
- Metric Spaces (1)Uploaded byBatman
- Scribd 13Uploaded byBatman
- Scribd 16Uploaded byBatman
- Scribd Test 2Uploaded byBatman
- 15251 Homework 1Uploaded byBatman
- Scribd 17Uploaded byBatman
- 7 Feb 16Uploaded byBatman
- Scribd TestUploaded byBatman

- ANSYS Short TutorialUploaded byNono_geotec
- ACDLabs NMR Data ProcessingUploaded byYongping Zha
- AutoForm-OneStep.pdfUploaded bywulfgang66
- Embedded MATLABUploaded byDhanya Ramaswamy
- Functions Assignment with Solutions.docUploaded bySiddhartha Choudhury
- Cpp Functions.pdfUploaded bynithish
- You Fuzzy in With MeUploaded byArttacla
- xpp_docUploaded byMarco Borges
- Eksponensial Dan Fungsi LogaritmaUploaded byroby_banik
- dasd1123Uploaded byreal bas
- QDM Intro CompleteUploaded byH
- gams tutorialUploaded byoptoergo
- (Science Networks · Historical Studies 7) Professor Dr. Fyodor A. Medvedev (auth.)-Scenes from the History of Real Functions-Birkhäuser Basel (1991).pdfUploaded byHoward Mok
- --ROLLE’S THEOREM & LAGRANGE’S THEOREM.pdfUploaded byNipun Mittal
- Faust Tutorial2Uploaded byyonodio
- Tutorial ReadingWritingDataUploaded byWilliam Javier
- Function in cUploaded byGoldy Batra
- Pine Script Language TutorialUploaded byArinze Anozie
- Cal11 Functions and ChangeUploaded bymarchelo_chelo
- budget-of-work.xlsxUploaded byChristine De San Jose
- Quick Introduction of RacketUploaded byLeLivros
- Modules PythonUploaded byVlad Lungu
- Rough Set TheoryUploaded byregister
- Handbook of Ma Them 1964 AbraUploaded byAswin Alex
- Calculus SyllabusUploaded byobama12345678
- OOCPUploaded bypappupager123456
- Psd Random Vibration Tutorial for Femap and Nx NastranUploaded bybrpnaidu2157
- Ordinary Differential Equations (For Ordinary People) - Waterman, G.Uploaded byroycetaft
- A Friendly Mathematics Competition - 35 Years of Teamwork in Indiana [Gillman] 0883858088.pdfUploaded byAliBenMessaoud
- Tao Nonlinear Dispersive PDEsUploaded bynislam57