Solution Midterm DSA
Solution Midterm DSA
Solution:
Q1 a). Describe, as clearly and simply as possible, your proposed strategy to
implement such a stack using a fixed-capacity array so that each update
operation runs in O (1) time.
Answer:
As we know, when we involve an array, then we need the time complexity O(n) to
add or retrieve an element from an if we do not know the position of that particular
element in the array.
So, for the array-based implementation of a stack that has a predefined size, we
maintain an array `arr` of a predefined size that must be greater than 1, the variable
`top` that points to the last added element in the stack, and the variable `size` that
refers to the array size. The Variable `top` changes from -1 to size-1. If the new
element is added to the stack, the `top` variable will be incremented by 1. If the
stack is empty then the variable `top` will be -1 and if the stack is full then the
variable `top` will be size-1.
In fixed-size stack that is implemented using an array the `size` remains
unchanged, therefore when top reaches to size. The stack will throw a Stack-Empty
Exception.
By implementing this strategy, we can run all the operation on stack in O (1) time.
Q1 b). Write the pseudocode for the push operation of such a stack.
Answer:
The pseudocode for push operation of such a stack is as follows.
public void push(AnyType element)
{
if (top == size)
throw new StackException("Stack is full");
else
top++;
arr[top] = element;
}
In this pseudocode firstly we are checking if the top is equal to the size then throws
an exception that stack is full otherwise increment the top and add the element on
the top location of the stack.
Q1 c). Write the pseudocode for the pop operation of such a stack.
Answer:
The pseudocode for pop operation of such a stack is as follows.
public void pop()
{
if (top == -1)
throw new StackException("Stack is empty");
else
element = arr[top];
arr[top] = null;
top--;
return element;
}
In this pseudocode firstly we are checking if the top is equal to the -1 then throws
an exception that stack is empty otherwise copy the top element to a variable, then
replace the top position of the stack with a null, after that decrement the top by 1
and return the variable in which we copy the value of the top element.
Q2. Write an efficient, recursive isbst function in pseudocode to check that a
binary tree is a valid Binary Search Tree, based on checking that subtrees are
within closed intervals starting with [MIN INT, MAX INT] and calculate the
complexity, in terms of O(g(n)), with respect to the size of the tree. Justify
your calculation of the complexity with a short explanation.
Answer:
The efficient recursive pseudocode of function isbst that validate that tree is a BST
or not is as follows.
boolean isbst(Node node, int minvalue, int maxvalue)
{
if (node == null)
return true;
if (node.data < minvalue || node.data > maxvalue)
return false;
return (isbst(node.left, minvalue, node.data-1) && isbst(node.right,
node.data+1, maxvalue));
}
In this pseudocode firstly we are checking if the node is null return the true. After
that we are checking that if the data of the node is less than minimum value or
greater than maximum value return false means that tree is not an BST and this is
happening recursively by changing the node, min and max value accordingly.
The time complexity of this isbst function will be O(g(n)) where `n` depends upon
the height of the tree.
As we know that f(n) = O(g(n)) means there are positive constant such that 0 ≤ f(n)
≤ g(n). The value of the `n` must be positive constant that will be the height of the
tree.
Suppose the height of the tree is 3 means that this function finds the two BSTs and
compare total 4 nodes. So, by this convention the time complexity of this scenario
will be O(g(3)) which is equal to O(g(n)) as `n` is the height of the tree.
Q3. Construct an AVL tree of positive integers and of height 3 such that
insertion of the value 7 will trigger a double rotation (i.e. a right rotation
followed by a left rotation or a left rotation followed by a right rotation) and,
following the insertion of 7, an insertion of the value 8 will also trigger a
double rotation.
(a) Draw the valid AVL tree before the specified insertions.
The valid AVL tree before the specified insertion is as follows.
This is the valid AVL tree before the insertion of the specified values.
(b) Draw the valid AVL tree after insertion of 7 but before insertion of 8.
The valid AVL tree after insertion of 7 but before insertion of 8 is as follows.