Computer Science 510 |
Due Sept 23 |
Rules for collaboration: You may discuss the problem sets with other students to increase your understanding of the material. However, what you write down and turn in should be your own work.
All problem sets are due at 11:00 a.m. in class on the due date.
get_pivot: int list -> int * int list partition: int * int list -> int list * int list sort: int list -> int listsuch that
sort: ('a * 'a -> bool) -> 'a list -> 'a listDemonstrate the polymorphism by sorting a list of strings into reverse alphabetical order; and by sorting a list of integers so that all the odd numbers come before all the even numbers (with the order otherwise arbitrary).
As part of your demonstration, show the ML declarations that execute sort on these two lists, and the transcript of your ML session.
(If you were not able to get your quicksort working, you may implement any polymorphic sorting algorithm (insertion sort, bubble sort, etc.) that takes a sort function as an argument.)
datatype color = R | B
datatype tree = Empty | T of color * tree * int * tree
Red-black trees also maintain the following 3 invariants
- In any node T (c,t1,x,t2), x is greater than any element in t1 and any element in t2 is greater than x.
- No red node has a red parent.
- Every path from the route to an empty node contains the same number of black nodes.
Implement the following operations on red-black trees, making sure to maintain the three invariants above:
empty : tree (* an empty tree *)
member : int -> tree -> bool (* member x t is true if x is in t *)
insert : int -> tree -> tree (* insert x t returns a new tree
containing the elements of t union {x} *)
You may use any algorithms textbook you choose to help you implement these functions (or you can try to figure it out yourself). Hint: the key to insertion is in the definition of an auxiliary function that rebalances trees. There is a solution in which the core of the balance function can be written in five lines of code. This question will be graded both on correctness and on elegance of the final solution. (Of course, both correctness and elegance are important in all of the answers that you give in this course.)