This lab is designed to help you practice
For these exercises, we will work with the following data definition for binary trees:
;; A bintree[alpha] is either ;; - false, or ;; - (make-node alpha bintree bintree) ;; the data definition for node appears above in that for bintree (define-struct node (item left right))
Write the template for programs over bintrees.
Write a program sign-tree
that consumes a
bintree[number] (i.e., a bintree where the items at each node are
numbers) and returns a bintree[symbol] with the same structure as the
input tree, but with each number replaced with one of the symbols
'positive, 'negative, or 'zero, as appropriate.
Write a program flip-tree
that consumes a
bintree[posn] and returns a bintree[posn]. The returned tree should
have the same structure as the input tree, but each posn should have
its x- and y-coordinates flipped [i.e., (make-posn 3 4) would become
(make-posn 4 3)].
Recall that (define-struct posn (x y)) is built-in to DrScheme.
The programs sign-tree
and flip-tree
have a similar structure. Write a function tree-map
that
takes a bintree and a function and applies the function to each node
in the tree. Show how to implement sign-tree
and
flip-tree
using tree-map
. Be sure to
include a correct contract for tree-map
.
Everybody should be able to finish up to this point
Write a program tree-andmap
that consumes a
function of type (alpha -> boolean) and a bintree[alpha] and returns a
boolean indicating whether the given function returned true at every
node. Can you implement tree-andmap
using a simple call
to tree-map
? By "a simple call", I mean can you just
fill in the ... in the code below to implement this function? If not,
why not?
(define (tree-andmap func abt) (tree-map ...))
Note: to check whether a value is false, you can use
(and (boolean? value) (not value))
Using tree-andmap
, implement pos-tree?
that consumes a bintree[number] and determines whether all numbers in
the tree are positive.
A tennis tournament consists of a series of elimination matches: after each match, the player who wins advances to the next round and the player who loses is out of the tournament. Assume the number of initial players is a power of two. One possible player match-up would look like:
player1 \ player2 player2 / \ -- player 2 player3 \ / player3 player4 /
For each match, we need to store the names of the two players and the match score. In tennis, a match score is given as a series of scores for units of play (sets, if you know tennis lingo). A sample score might look like:
player1 : 6 7 3 6 player2 : 4 6 6 2
Where each column (pair) of numbers is one set.
Develop a data model for individual matches.
Write a program player-wins
that consumes a
player's name and a tournment tree (a bintree[match]) and returns a
number indicating how many matches that player won in the tournament.
Assume that the tournament properly advances the winning player to the
next round (i.e., you shouldn't have to check the numeric scores for
this problem).
Many students should be able to finish up to this point
A score is valid under the following conditions:
Write a program scores-valid?
that consumes a
tournament and returns a boolean indicating whether all of the scores
in the tree meet these restrictions.
(More difficult) Write a program player-earnings
that consumes a tournament, an earnings function, and a player's name
and returns a number indicating how much the player won in the
tournament. The earnings function takes the number of a round and one
of the symbols 'win or 'lose and returns a number indicating how much
the winner/loser earned for that round.
An example earning function might give the winner $10,000 in each of the first two rounds and $25,000 in any subsequent rounds, with the loser getting half as much as the winner in each round.