0% found this document useful (0 votes)
39 views

DS Notes

This document provides notes on data structures and algorithms. It introduces abstract data types and describes their benefits. Common abstract data types include lists, stacks, queues, and linked lists. It also describes operations on data structures like traversing, searching, inserting, and deleting. Specific data structures are then described in more detail like stacks, queues and linked lists. Searching and sorting algorithms are introduced, with selection sort and insertion sort explained as examples.

Uploaded by

zack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

DS Notes

This document provides notes on data structures and algorithms. It introduces abstract data types and describes their benefits. Common abstract data types include lists, stacks, queues, and linked lists. It also describes operations on data structures like traversing, searching, inserting, and deleting. Specific data structures are then described in more detail like stacks, queues and linked lists. Searching and sorting algorithms are introduced, with selection sort and insertion sort explained as examples.

Uploaded by

zack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Structure Notes By Rana

ABSTRACT DATA TYPES:


Introduction to Abstract Data Types (ADT): Class of objects whose logical behavior is defined
by a set of values and a set of operations. Normally the data types in programming language is called Abstract
Data Types for example: integer, long integer, float, double, etc.

The benefits of using ADTs include:

 Code is easier to understand (e.g., it is easier to see "high-level" steps being


performed, not obscured by low-level code).
 Implementations of ADTs can be changed (e.g., for efficiency) without requiring
changes to the program that uses the ADTs.
 ADTs can be reused in future programs.

Abstract Data Types

There are two parts to each ADT:

1. The public or external part, which consists of:


o the conceptual picture (the user's view of what the object looks like, how the
structure is organized)
o the conceptual operations (what the user can do to the ADT)
2. The private or internal part, which consists of:
o the representation (how the structure is actually stored)
o the implementation of the operations (the actual code)

In general, there are many possible operations that could be defined for each ADT;
however, they often fall into these categories:

1. initialize
2. add data
3. access data
4. remove data

In this class, we will study a number of different abstract data types, different ways to
implement them, and different ways to use them. Our first ADT (coming up in the next set
of notes) is the List.

Introduction to Data Structure: Before introducing data structures we should


understand that computers do store, retrieve, and process a large amount of data. If the
1
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

data is stored in well-organized way on storage media and in computer's memory then it
can be accessed quickly for processing.
Data structure introduction refers to a scheme for organizing data, or in other words
a data structure is an arrangement of data in computer's memory in such a way that it
could make the data quickly available to the processor for required calculations.
The functional definition of a data structure is known as ADT (Abstract Data Type)
which is independent of implementation. The implementation part is left on developers
who decide which technology better suits to their project needs.

Advantage of Data Structure:


1. Data Structure allows information to be securely store on a computer systems
2. Data Structure makes all processes involving them very quickly
3. Data Structure provides you with capacity to use and process you data on computer
system
4.
Disadvantage of Data Structure:
1. To alter data structure you be very advance IT technician.
2. It is almost impossible to change a data structure
3.
Data Structure Operations:
The following four operation play a major role in this text:
Traversing: Accessing each record exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called “Visiting” the
record.)
Searching: Finding the location of the record with a given key value, or finding the
locations of all records which satisfy one or more conditions.
Inserting: Adding a new record from the structure.
Deleting: Removing a record from the structure.

Following two operations, which are used in special situations, will also be considered
Sorting: Arranging the records in some logical order (e.g., alphabetically according to
some number or account number)
Merging: Combining the records in two different sorted file into a single sorted file
other operation, e.g., copying and concatenation, will be discussed later in the text.

2
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

Classification Data Structures: The data structure can be classified as follow.


 Array
 Stack
 Queue
 Linked List
 Trees
 Heap
 Dictionary
 Hash Table
 Graph
But in our Syllabus We have only Stack, Queue & Linked List.

Stack (LIFO): Stack is a last-in-first-out strategy data structure; this means that the
element stored in last will be removed first. Stack has specific but very useful applications;
some of them are as follows:
 Solving Recursion - recursive calls are placed onto a stack, and removed from there
once they are processed.
 Evaluating post-fix expressions
 Solving Towers of Hanoi
 Backtracking
 Depth-first search
 Converting a decimal number into a binary number
Queue (FIFO): Queue is a first-in-first-out data structure. The element that is added to the
queue data structure first, will be removed from the queue first. Dequeue, priority queue,
and circular queue are the variants of queue data structure. Queue has the following
application uses:
 Access to shared resources (e.g., printer)
 Multiprogramming
 Message queue
Linked List: Linked list data structure provides better memory management than
arrays. Because linked list is allocated memory at run time, so, there is no waste of
memory. Performance wise linked list is slower than array because there is no direct
access to linked list elements.
Linked list is proved to be a useful data structure when the number of elements to be
stored is not known ahead of time.
There are many flavors of linked list you will see: linear, circular, doubly, and doubly
circular.

3
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

Double Linked List: A doubly linked list is a list that contains links to next and
previous nodes. Unlike singly linked lists where traversal is only one way, doubly linked
lists allow traversals in both ways.

Circular linked list: Circular list is a list in which the link field of the last node is made
to point to the start/first node of the list.

Static Lists: A fixed data structure has a fixed size.


 Arrays: once you define the number of elements it can hold, this number can’t be
changed anymore
Dynamic List: A dynamic data structure grows and shrinks as required by the information
it contains.

Algorithms: complexity, Time-space Trade-off


Algorithms: An algorithm is a well-defined list of steps for solving a particular problem. The
time and space it uses are two major measures of the efficiency of an algorithm.
Complexity: The complexity of an algorithm is the function which give the running time
and/or space in term of input size.
Time-Space Trade-off: By increasing the amount of space for storing the data, one may be
able to reduce the time needed for processing the data or vice versa.
OR
The Time-space tradeoff refers to a choice between algorithmic solution of a data
processing problem that allows one to decrease the running time of an algorithmic solution
by increasing the space to store the data and vice versa.

4
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

SEARCHING: Finding the location of the record with a given key value, or finding the
locations of all records which satisfy one or more conditions.

Linear Search: Search each record of the file, one at a time, until finding the given Name
and hence the corresponding telephone number.

Binary Search: Search a sorted array by repeatedly dividing the search interval in half.
Begin with an interval covering the whole array. If the value of the search key is less than
the item in the middle of the interval, narrow the interval to the lower half. Otherwise
narrow it to the upper half. Repeatedly check until the value is found or the interval is
empty.

Searching Trade-offs:
Order of Growth of Binary Searching:

Sorting: Arranging the records in some logical order (e.g., alphabetically according to
some number or account number)

Study and Implementation of Sorting Algorithm:


Selection Sort: Algorithm works by scanning the list for the smallest value and moving it
to its final position. More specifically:
 find the smallest value in the list
 switch it with the value in the first position
 find the next smallest value in the list
 switch it with the value in the second position
 repeat until all values are in their proper places
 Code from Lewis/Chase works on array of Comparable objects:
 Could be easily modified to:
 use a Comparator
 sort in descending order

5
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

 work on a linked representation

OR
A sort algorithm that repeatedly searches remaining items to find the least one and moves
it to its final location. The run time is Θ(n²), where n is the number of elements. The
number of swaps is O(n).

Insertion Sort: Insertion sort orders a list of values by repetitively inserting a particular
value into a sorted subset of the list

 More specifically:
 Consider the first item to be a sorted sublist of length 1
 Insert the second item into the sorted sublist, shifting the first item if needed
 Insert the third item into the sorted sublist, shifting the other items as needed
 Repeat until all values have been inserted into their proper positions

6
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

OR
Sort by repeatedly taking the next item and inserting it into the final data structure in its
proper order with respect to items already inserted. Run time is O(n2) because of moves.

Merge Sort:
 Merge sort orders a list of values by recursively dividing the list in half until each sub-
list has one element, then recombining
 More specifically:
 divide the list into two roughly equal parts
 recursively divide each part in half, continuing until a part contains only one
element
 merge the two parts into one sorted list
 continue to merge parts as the recursion unfolds

OR
A sort algorithm that splits the items to be sorted into two groups, recursively sorts each
group, and merges them into a final, sorted sequence. Run time is Θ(n log n).

Quick Sort:
 Sequential sorting algorithms are too inefficient
 Quicksort uses a recursive procedure to sort the list. The algorithm is simple:
 Find an element that partitions the list in two, and organize the list
 (elements < partition) [partition] (elements > partition)
 Then sort each of the two partitions (using quicksort) recursively

OR
Pick an element from the array (the pivot), partition the remaining elements into those
greater than and less than this pivot, and recursively sort the partitions. There are many

7
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

variants of the basic scheme above: to select the pivot, to partition the array, to stop the
recursion on small partitions, etc.

Heap Sort:
Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-
case scenarios. Heap sort algorithm is divided into two basic parts:
 Creating a Heap of the unsorted list.
 Then a sorted array is created by repeatedly removing the largest/smallest element
from the heap, and inserting it into the array. The heap is reconstructed after each
removal.
OR
A sort algorithm that builds a heap, then repeatedly extracts the maximum item. Run time
is O(n log n).

External Sort: Any sort algorithm that uses external memory, such as tape or disk, during
the sort. Since most common sort algorithms assume high-speed random access to all
intermediate memory, they are unsuitable if the values to be sorted don't fit in main
memory.

OR
External sorting is a term for a class of sorting algorithms that can handle massive amounts
of data. External sorting is required when the data being sorted do not fit into the main
memory of a computing device (usually RAM) and instead they must reside in the
slower external memory (usually a hard drive).

Hashing: Suppose we have a very large data set stored in an array. The amount of time
required to look up an element in the array is either O(log n) or O( n) based on whether the
array is sorted or not. If the array is sorted then a technique such as binary search can be
used to search the array. Otherwise, the array must be searched linearly. Either case may
not be desirable if we need to process a very large data set. Therefore we discuss a new
technique called hashing that allows us to update and retrieve any entry in constant time
O(1). The constant time or O(1) performance means, the amount of time to perform the
operation does not depend on data size n.

8
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

Application of Hashing:
Hashing Function: There are three ways of calculating hash function
1. Modulus /Division Method.
2. Midsquare Method.
3. Folding Method.
All are defined deeply in DS Schaum’s series book.

Collision Detection Techniques:

Collisions: One problem with hashing is that it is possible that two strings can hash into the
same location. This is called a collision. We can deal with collisions using many strategies,
such as linear probing (looking for the next available location i+1, i+2, etc. from the hashed
value i), quadratic probing (same as linear probing, except we look for available positions
i+1 , i + 4, i + 9, etc from the hashed value i and separate chaining, the process of creating a
linked list of values if they hashed into the same location.

Trees:
A tree is a kind of graph, composed of nodes and links, such that:
 A link is a directed pointer from one node to another.
 There is one node, called the root, that has no incoming links.
 Each node, other than the root, has exactly one incoming link from its parent.
 Every node is reachable from the root.
A node can have any number of children. A node with no children is called a leaf ; a node
with children is an interior node.

Trees occur in many places in computer systems and in nature.


Trees ADT: Trees are as common and important as linked lists. Like lists there are many
variations: binary search trees, balanced trees, and heaps.
Beadth-first and Depth-first Traversal:

9
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

Binary Trees: A binary tree is either empty or it is a root node together with two subtrees,
the left subtree and the right subtree, each of which is a binary tree. The binary tree is full
if each level, except the last, contains as many nodes as possible. Therefore a binary tree is
complete if each level contains as many nodes as possible. Binary trees have the following
three distinctive characteristics:
1. Each node has at most two children (subtrees).
2. Each subtree is identified as either the left-subtree or the
right-subtree of the parent.
3. A binary tree may be empty.
Binary Search Trees:
A binary search tree is a binary tree that has two additional properties associated with it:
1. All key values in the left subtree of any node are less than the key value of that node.
2. All key values in the right subtree of any node are greater than the key value of that
node.
The figure below gives a diagrammatic representation of a binary search tree:

Tree Traversal: to traverse a tree data structure is to process, however you like, every
node in the data structure exactly once.
Note: You may 'pass through' a node as many times as you like but you can only process
the node once. We have previously talked about 'traversing a list', which means going
through the list and processing every node once.
1. During a pre-order traversal each node is processed before any nodes in its subtrees
2. During an in-order traversal each node is processed after all nodes in its left subtree
but before any nodes in its right subtree.
3. During a post-order traversal each node is processed after all nodes in both its
subtrees

10
College: Stadium Road SRE Majeed
Data Structure Notes By Rana

Post-Order: Process the node the first time it is observed.


Do (N) after everything else.
The figure shown illustrates this method. Again we number the
nodes in the order in which they will be processed:

In-Order or Infix Order Traversal: Process the node the second


time it is observed, Do (N) in between the two subtrees.
The following figure illustrates this method. Again we number the
nodes in the order in which they will be processed:

Pre-Order Traversal: Process the node the third time it is observed.


Do (N) before anything else.
The following figure illustrates this method. The nodes are numbered
in the order in which they will be processed:

Height-Balanced Binary Search Trees:


Graphs:
Graph ADT:
Representation of Graph:
Strongly Connected Graphs:
Spanning Trees:
Minimal Spanning Trees:
Shortest Path Algorithm:
Traveling Salesman Problem (TSM):
Book:
Data Structure by Schaum’s Series
http://www.sqa.org.uk/e-learning/LinkedDS04CD/page_13.htm#TreeTraversal

11
College: Stadium Road SRE Majeed

You might also like