Practical Lab I: Review of Data Structures
Practical Lab I: Review of Data Structures
1)
Explain the following terms and give examples of each
2) Distinguish between data abstraction and data structures with the aid of appropriate
examples
I. Data Abstraction is like using a black box. You don't need to know how it
works inside, just what it does. It hides the complicated details and focuses on what
you can do with the data, not how it's stored or manipulated.
Example of Data Abstraction: Imagine a TV remote control. You press buttons to
change channels, volume, or power on/off. You don't need to know how the remote
works internally; you abstract away the complexity. All you care about is the
functions it provides.
II. Data Structures, on the other hand, are about organizing and storing data in a
specific way to make it efficient to access and manipulate. They are the tools and
mechanisms for dealing with data in a more structured manner.
Example of Data Structures: Think of a bookshelf. It's a structure designed to store
books efficiently. Each shelf can hold a specific number of books, and you can easily
find and access a book by its position. In this case, the bookshelf is the data structure,
and the books are the data.
3)
i) Discuss the motivation behind the use of recursion in programming
I. Recursion can offer simpler and more elegant solutions compared to iteration.
Take the factorial problem, for instance. The recursive approach is cleaner and
more straightforward than the iterative one.
II. Recursion can provide better efficiency in certain cases. Consider the merge
sort algorithm; its recursive version outperforms the iterative one for large arrays.
III. Recursion is ideal for solving complex problems that are challenging to
address iteratively. Think about tree traversal: the recursive solution is far more
readable and intuitive than an iterative one.
n+1 if m=0
Acker (m, n) = Acker (m-1, 1) if n=0
Acker (m-1, Acker (m, n-1)) otherwise
(2) Write a program in C/C++/Java to implement the function and test Acker (1,2).
In C
#include <stdio.h>
int main() {
int m = 1;
int n = 2;
int result = Acker(m, n);
return 0;
}
b) Show the order of visiting its nodes during the depth-first search starting at A and
using the same neighbor ordering. Draw the resulting depth-first search tree.
Starting from A, we explore as far as possible along each branch before backtracking.
Visit A, Visit B, Visit E, Visit F (because it's the next neighbor based on alphabetic
order),Visit G, Backtrack to F, Visit C (next neighbor after F, Visit C (next neighbor after
F, Backtrack to B,
Visit D.