Académique Documents
Professionnel Documents
Culture Documents
a.
b.
c. Distinguish between AVL and Red-Black trees [5 marks]
Difference:
1. AVL trees provide faster lookups than Red Black Trees because they are more strictly
balanced.
2. Red Black Trees provide faster insertion and removal operations than AVL trees as
fewer rotations are done due to relatively relaxed balancing.
3. AVL trees store balance factors or heights for each node, thus requires O(N) extra
spacewhereas Red Black Tree requires only 1 bit of information per node, thus
require O(1) extra space.
4. Red Black Trees are used in most of the language libraries
like map, multimap, multiset in C++ whereas AVL trees are used in databases where
faster retrievals are required
You are given the following binary search tree.
d. Colour the nodes of the tree red and black so that it becomes a valid red-black tree. If
you don't have a coloured pen, you could e.g. draw a circle for red nodes and a square
for black nodes. [15
marks]
e. Insert 60 into the tree using the red-black insertion algorithm. Write down the final tree.
Please use this simple rules to color your tree
a. what is the difference between dynamic programming and divide and conquer
[6]
b. Explain what you understand by greedy algorithm [4]
A greedy algorithm works by choosing the best possible answer in each step and then
moving on to the next step until it reaches the end, without regard for the overall solution.
It only hopes that the path it takes is the globally optimum one, but as proven time and
again, this method does not often come up with a globally optimum solution. In fact, it is
entirely possible that the most optimal short-term solutions lead to the worst possible
global outcome.
For example: Take the path with the largest sum overall. A greedy algorithm would take the
blue path, as a result of shortsightedness, rather than the orange path, which yields the
largest sum.
c. Describe the merge sort algorithm [5]
Merge Sort is a divide and conquer algorithm. It works by recursively breaking down a problem
into two or more sub-problems of the same or related type, until these become simple enough
to be solved directly. The solutions to the sub-problems are then combined to give a solution
to the original problem. So Merge Sort first divides the array into equal halves and then
combines them in a sorted manner