Professional Documents
Culture Documents
Data Structure Notes
Data Structure Notes
1.1 Introduction
1.1 Introduction:
In a beginning programming course, variables are introduced that store a single datum. In all
but trivial programs, however, we need to store multiple related data items for convenience. For
example, data items like students records many need to be stored under a single name for ease of
processing. Such convenient structuring of data is called data organization: a container for the
data is called data structure.
A data structure is a means of organizing data in primary memory in a form that is convenient to
process by a program.
In the definition of data structure, structure means a set of rules that holds the data together. In
other words, if we take a group of data and fit them into a structure such that we can define its
relating rules, we have made a data structure.
Data is stored either in main memory or in secondary memory. In order to represent data we
need some models.
The different models (logical or mathematical) to represent/organize/store data in the main
memory are together referred to as data structures.
The data structure is collection of data organized in a specific manner in computers main
memory.
The data structure is a way Abstract Data Type (ADT) can be implemented.
In the beginning of computer programming, if we wanted to read a file , we wrote the code to
read the physical file device. It did not take long to realize that we were writing the same code
over and over again. So, we created what is known as abstract data type(ADT). We wrote the
code to read the file and placed in a library for all programmers to use.
An abstract data type is a data declaration packaged together with the operations that are
meaningful for the data type.
In other words, ADT is the encapsulation of the data and the operations on the data in such a
way that they are hidden from the users.
ADT consists of a set of definitions that allow programmers to use functions while hiding the
implementation.
With an ADT, users are not concerned with how the task is done but rather with what it can do.
Suppose we have to solve a problem that requires queue data structure. The queue data structure
is not available in programming languages. There are two ways to solve this problem:
One way is to write a program that simulates the queue we need. But this is good only for one
application.
Another way is to write a queue ADT that can be used to solve any queue problem.
If we choose the latter way, the other programmers writing other applications can concentrate
on the applications rather than writing the queue data structure.
The different models (logical or mathematical) to represent/organize/store data in the secondary
memory are called file structures or data storage structures.
A data structure could be a programming construct provided in a language or it can be defined
by the programmer. Example of data structure include: Arrays, Linked lists, Stacks, Queues,
Trees, Graphs. Data structures are applied in sorting, searching, hash tables, graph algorithms,
pattern matching, data compressing etc.
The content of this course has not changed much in the last two decades except for inclusion of
some new algorithms.
Good programming and problem solving requires knowledge of data structures. Without a
sufficient understanding of data structures, the level at which a programmer can work will be
severely limited.
Case 1: When more than one field may have unique values.
In that case, there exist multiple keys, but at a time we use only one field as a key, called primary
key. The other key(s) are called as alternate key(s).
Case 2: There is no field that has unique values.
Then a combination of two or more fields can be used to form a key. Such a key is called
composite key.
Case 3: There is no possibility of forming a key from the within the record.
Then an extra field can be added to the record that can be used as a key.
Your friend =
You =
20 times better!!
Time Space Trade-off
The best algorithm to solve a given problem is one that requires less memory space and less time
to run to completion. But in practice, it is not always possible to obtain both of these objectives.
One algorithm may require less memory space but may take more time to complete its execution.
On the other hand, the other algorithm may require more memory space but may take less time
to run to completion. Thus, we have to sacrifice one at the cost of other. In other words, there is
Space-Time trade-off between algorithms.
If we need an algorithm that requires less memory space, then we choose the first algorithm at
the cost of more execution time. On the other hand if we need an algorithm that requires less
time for execution, then we choose the second algorithm at the cost of more memory space.
Arrays
Array definition:-Array is linear, homogeneous data structures whose elements are stored in
contiguous memory locations.
Arrays are subscripted variables stored in contiguous memory locations.
Accessing Array elements: Elements of arrays are accessed by using index or subscripts.
Types of Arrays:
One-dimensional Array or linear array: requires only one index to access an element.
Stacks
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure
that allows adding and removing elements in a particular order. Every time an element is added,
it goes on the top of the stack, the only element that can be removed is the element that was at
the top of the stack, just like a pile of objects.
Basic features of Stack
1. Stack is an ordered list of similar data type.
2. Stack is a LIFO structure. (Last in First out).
3. push() function is used to insert new elements into the Stack and pop() is used to delete
an element from the stack. Both insertion and deletion are allowed at only one end of
Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be in
Underflow state if it is completely empty.
Applications of Stack
The simplest application of a stack is to reverse a word. You push a given word to stack - letter
by letter - and then pop letters from the stack.
There are other uses also like : Parsing, Expression Conversion(Infix to Postfix, Postfix to Prefix
etc) and many more.
Implementation of Stack
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are
limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is
not limited in size. Here we will implement Stack using array.
Applications of stack
We are accustomed to write arithmetic expressions with the operation between the two operands:
a+b or c/d. If we write a+b*c, however, we have to apply precedence rules to avoid the
ambiguous evaluation (add first or multiply first?).
There's no real reason to put the operation between the variables or values. They can just as well
precede or follow the operands. You should note the advantage of prefix and postfix: the need
for precedence rules and parentheses are eliminated.
Infix Prefix Postfix
a+b +ab ab+
a+b*c +a*bc abc*+
(a + b) * (c - d) *+ab-cd ab+cd-*
b*b-4*a*c
40 - 3 * 5 + 1
https://www.slideshare.net/archijamwal5/ds-ppt
The above link provides the detailed stack ppt along with algorithms for push and pop.
Recursion
Some computer programming languages allow a module or function to call itself. This technique
is known as recursion. In recursion, a function either calls itself directly or calls a function
that in turn calls the original function . The function is called recursive function.
Properties
A recursive function can go infinite like a loop. To avoid infinite running of recursive function,
there are two properties that a recursive function must have
Base criteria There must be at least one base criteria or condition, such that, when this
condition is met the function stops calling itself recursively.
Progressive approach The recursive calls should progress in such a way that each time
a recursive call is made it comes closer to the base criteria.
Implementation
This implies, the caller function has to suspend its execution temporarily and resume later when
the execution control returns from the callee function. Here, the caller function needs to start
exactly from the point of execution where it puts itself on hold. It also needs the exact same data
values it was working on. For this purpose, an activation record (or stack frame) is created for
the caller function.
Analysis of Recursion
One may argue why to use recursion, as the same task can be done with iteration. The first
reason is, recursion makes a program more readable and because of latest enhanced CPU
systems, recursion is more efficient than iterations.
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than
one rings is as depicted
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits
over the larger one. There are other variations of the puzzle where the number of disks increase,
but the tower count remains the same.
Rules
The mission is to move all the disks to some another tower without violating the sequence of
arrangement. A few rules to be followed for Tower of Hanoi are
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Following is an animated representation of solving a Tower of Hanoi puzzle with three disks.
Tower of Hanoi puzzle with n disks can be solved in minimum 2n1 steps. This presentation
shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with
lesser amount of disks, say 1 or 2. We mark three towers with name, source, destination and
aux (only to help moving the disks). If we have only one disk, then it can easily be moved from
source to destination peg.
If we have 2 disks
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two
disks. We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all
other (n-1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks
onto it. We can imagine to apply the same in a recursive way for all given set of disks.
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 0, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP