Get Instant Help From 5000+ Experts For

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Implementing Stack Data Structure, Converting Doubly Linked List to Binary Search Tree, Exercise 12.

Variant of Stack Data Structure with MAX Primitive

1. [20%] Implement a variant of the stack data structure, so that on top of the three primitives, PUSH, POP, and TOP, there is now a new primitive, MAX.

â€¢ MAX(S): returns the maximum key currently in the stack S.

It is assumed that the data type of the keys supports comparisons (i.e., â€œ?â€). Other than that, we do not know anything about the data type of the keys. Your solution is evaluated by how competitive its performance (running time) is.

2. [20%] Design a linear-time algorithm that converts a sorted, doubly linked list into a balanced binary search tree. The only thing we know about the data type of the keys is that they are comparable (i.e., â€œ?â€). You may further assume that the keys in the linked list are distinct. It is okay for the algorithm to be destructive (meaning it modifies the original linked list or even distroys it). It is also okay to use additional space (so long as the amount is reasonable).

3. [20%] Exercise 12.3-5 on page 299 of CLRS.

4. [40%] Implement a red-black tree class in Java.

(a) The keys are of type int.

(b) Your class shall be named as follows:Â

ca.ucalgary.cpsc331.RedBlackTree

(c) Your class shall implement the following Java interface:
package ca.ucalgary.cpsc331;
public interface Dictionary {
boolean empty();
void insert(int key);
void delete(int key);
boolean member(int key);
}

(d) Your implementation shall raise an appropriate run-time exception when the user attempts to delete from an empty tree.Â

Â

(e) The class shall provide a constructor that does not take any arguments. This constructor initializes the tree to an empty one.

(f) The class shall override the toString() method. Your implementation of to String() shall return a String in the following format:
â€¢ The string consists of as many â€œlinesâ€ as there are internal nodes (i.e., a node that is not a leaf/NIL). If there are ten internal nodes, then there are ten lines. An empty tree is represented by an empty string.
â€¢ Each â€œlineâ€ is terminated by a newline character. No line shall contain leading or trailing whitespaces.
â€¢ Each line is a representation of an internal node. The order of the lines corresponds to a preorder tree walk.
â€¢ A line has the following format: