class BinaryNode{ // Constructors BinaryNode(int theElement){ this

advertisement
1: class BinaryNode{
2:
// Constructors
3:
BinaryNode(int theElement){
4:
this( theElement, null, null );
5:
}
6:
7:
BinaryNode(int theElement, BinaryNode lt, BinaryNode rt ){
8:
element = theElement;
9:
left
= lt;
10:
right
= rt;
11:
}
12:
13:
14:
int element;
15:
BinaryNode left;
16:
BinaryNode right;
17:
}
18:
19:
public class BinarySearchTree
20:
{
21:
private BinaryNode root;
22:
23:
/**
24:
* Construct the tree.
25:
*/
26:
public BinarySearchTree( )
27:
{
28:
root = null;
29:
}
30:
31:
/**
32:
* Insert into the tree; duplicates are ignored.
33:
* @param x the item to insert.
34:
*/
35:
public void insert( int x );
36:
37:
/**
38:
* Remove from the tree. Nothing is done if x is not found.
39:
* @param x the item to remove.
40:
*/
41:
public void remove( int x );
42:
43:
/**
44:
* Find the smallest item in the tree.
45:
* @return smallest item or throw an exception if empty.
46:
*/
47:
public int findMin( );
48:
49:
/**
50:
* Internal method to find the smallest item in a subtree.
51:
* @param t the node that roots the tree.
52:
* @return node containing the smallest item.
53:
*/
54:
public BinaryNode findMin( BinaryNode t );
55:
56:
/**
57:
* Find the largest item in the tree.
58:
* @return the largest item or throw an exception if empty.
59:
*/
60:
public int findMax( );
61:
62:
63:
/**
64:
* Internal method to find the largest item in a subtree.
65:
* @param t the node that roots the tree.
66:
* @return node containing the largest item.
67:
*/
68:
private BinaryNode findMax( BinaryNode t );
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
/**
* Find an item in the tree.
* @param x the item to search for.
* @return matching item or throw exception if not found.
*/
public int find( int x );
/**
* Internal method to find an item in a subtree.
* @param x is item to search for.
* @param t the node that roots the tree.
* @return node containing the matched item.
*/
private BinaryNode find( int x, BinaryNode t );
/**
* Internal method to get element field.
* @param t the node.
* @return the element or throw exception if t is null.
*/
private int elementAt( BinaryNode t );
/**
* Internal method to insert into a subtree.
* @param x the item to insert.
* @param t the node that roots the tree we are working on.
* @return the new root.
*/
private BinaryNode insert( int x, BinaryNode t )
{
if( t == null )
t = new BinaryNode( x, null, null );
else if( x< t.element )
t.left = insert( x, t.left );
else if( x > t.element )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
Figure 1. Binary Search Tree methods.
1: public class Stack{
2:
private ListNode top;
3:
4:
public Stack( ){
5:
top = null;
6:
}
7:
8:
/**
9:
* Test if the stack is empty.
10:
* @return true if empty, otherwise return false.
11:
*/
12:
public boolean isEmpty( );
13:
14:
/**
15:
* Make the stack empty.
16:
*/
17:
public void makeEmpty( );
18:
19:
/**
20:
* Return the top element of stack. Does not change the stack.
21:
* @return the top element of stack.
22:
* @throw Underflow if the stack is empty.
23:
*/
24:
public Object top( ) throws Underflow;
}
25:
26:
/**
27:
* throw away the top element of the stack.
28:
* @exception Underflow if the stack is empty.
29:
*/
30:
public void pop( ) throws Underflow;
31:
32:
/**
33:
* throw away the top element of the stack and return it.
34:
* @return the top element before it gets removed.
35:
* @exception Underflow if the stack is empty.
36:
*/
37:
public Object topAndPop( ) throws Underflow;
38:
39:
/**
40:
* push a new element on top of stack.
41:
* @param x object to put in stack.
42:
*/
43:
public void push( Object x );
Figure 2. Stack Methods.
1. (2 marks) In class BinarySearchTree in figure 1, write method public void
removeMin(), which removes the smallest integer from the tree. If the tree is
empty, this method does nothing. Assume that the tree does not have duplicated
elements.
2.
(5 marks) Given Binary Search Tree Code in figure 1, write method
public int average() of the BinarySearchTree class. This method returns the mean
value (let it be an integer mean) of all integers stored in the tree. You can write
additional methods.
3. (9 marks) Let Stack have usable methods defined in figure 2. If we have stack1
and stack2, we want to use stack1 to represent a queue and use stack2 for any
bookkeeping (see a picture below).
back of queue = top of stack
Front of queue
stack1
stack2

(4 marks) Explain how we can use these 2 stacks to implement enqueue and
dequeue. Stacks can only be manipulated by popping and pushing.

(5 marks) Write the code for such enqueue and dequeue.

(5 marks) Write the code for such enqueue and dequeue.
4. (5 marks) Assuming that we have class Queue of integer with methods:
 public void enqueue(int x); put x at the end of the queue.
 public int dequeue(); remove the value at the front of the queue. Return that
removed value.
Explain how we can modify a queue’s content so that only even number remains
(drawing can help). Write method public void removeOdd() of class Queue that
performs this task.
5. (8 marks) Write method public BinaryNode findParentNode(BinaryNode n) for
class BinarySearchTree. Given the node n, this method returns the parent node of
n. If n is null, or n has no parent, or n does not exist in this tree, the method returns
null.
6. (4 marks) write method public void swapWithParent(BinaryNode n) for class
BinarySearchTree. This method swaps the element value of node n with element
value of its parent node. If n is null, or n has no parent, or n does not exist in this
tree, this method does nothing. (You don’t need to worry if the tree is no longer a
binary search tree).
Download