Comp 258: Assignment 2
Comp 258: Assignment 2
Question 1
1. Code Reuse: With help of Generics, one needs to write a method/class/interface only once and
use it for any type whereas, in non-generics, the code needs to be written again and again whenever
needed. Java Generics helps the programmer to reuse the code for whatever type he/she wishes. For
instance, a programmer writes a generic method for sorting an array of objects. Generics allow the
programmer to use the same method for Integer arrays, Double arrays, and even String arrays.
2. Type Safety: Generics make errors to appear compile time than at run time (It’s always better to
know problems in your code at compile time rather than making your code fail at run time).
Suppose you want to create an ArrayList that store name of students and if by mistake the
programmer adds an integer object instead of a string, the compiler allows it. But, when we retrieve
this data from ArrayList, it causes problems at runtime.
3. Individual Type Casting is not needed: If we do not use generics, then, in the above example
every time we retrieve data from ArrayList, we have to typecast it. Typecasting at every retrieval
operation is a big headache. If we already know that our list only holds string data then we need not
typecast it every time.
5. Implementing generic algorithms: By using generics, we can implement algorithms that work
on different types of objects and at the same, they are type-safe too. It allows us to implement non-
generic algorithms.
Code that uses generics has many benefits over non-generic code:
Question 2
a) Given the following list of numbers: 50, 25, 75, 13, 33, 60, 88, 7, 19, 80, 101. Create a
graphic displaying the tree these numbers were inserted into a binary search tree in
the order above.
Java Code:
package binarysearchtree;
class Node {
int data;
Node left, right;
Node(int d) {
data = d;
left = right = null;
}
}
/* A function that constructs Balanced Binary Search Tree from a sorted array */
/* Base Case */
if (start > end) {
return null;
}
/* Recursively construct the left subtree and make it left child of root */
node.left = sortedArrayToBST(arr, start, mid - 1);
/* Recursively construct the right subtree and make it right child of root */
node.right = sortedArrayToBST(arr, mid + 1, end);
return node;
}
Output:
b) Do you consider the tree to be an efficiently structured tree? If yes, provide a
sequence that would create an inefficiently structured tree.
The idea of a binary search tree is that data is stored according to order so that it can be
retrieved very efficiently. By creating the Binary Search Tree according to the given list of
numbers and properly using the appropriate data structure, we consider it to be an efficiently
structured tree. Now, for creating the inefficiently structured Binary Search Tree one should
follow the sequence of creating an unbalanced Binary Search Tree using an unsorted array
data structure because the moment when a binary tree becomes unbalanced, it loses its
efficiency. Based on everything that we already know about binary search trees, we know
that they are incredibly powerful because of their logarithmic runtime, which is exactly
what makes them so fast and efficient.
Question 3
Describe the two features required for a recursive method. When does a recursive
method behave like an infinite loop?
In recursion, the recursive function calls itself repeatedly until a base condition is satisfied. The memory
for the called function is pushed onto the stack at the top of the memory for the calling function. For
each function call, a separate copy of local variables is made.
methodName (T parameters…)
{
if (precondition == true)
//precondition or base condition
{
return result;
}
return methodName (T parameters…);
//recursive call
}
The precondition is also called the base condition. The basic idea behind using recursion is to express
the bigger problem in terms of smaller problems. Also, we need to add one or more base conditions so
that we can come out of recursion.
Note that a precondition or a base condition is necessary for any recursive method as in the case if we do
not break the recursion then it will keep on running infinitely (behave like an infinite loop) and result in
a stack overflow.
Question 4
package binarysearchtree;
class BinarySearchTree {
Node root;
return res;
}
public static void main(String args[])
{
BinarySearchTree tree = new BinarySearchTree();
tree.root = new Node(50);
tree.root.left = new Node(25);
tree.root.left.right = new Node(75);
tree.root.left.right.left = new Node(13);
tree.root.left.right.right = new Node(33);
tree.root.left.right.left = new Node(60);
tree.root.right = new Node(88);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(19);
tree.root.right.right.left = new Node(80);
tree.root.right.right.right = new Node(101);
// Function call
System.out.println("Maximum element of the Tree is: "
+ tree.findMax(tree.root));
}
}
Output:
References:
https://docs.oracle.com/javase/tutorial/java/generics/why.html