Introduction To Data Structures
Introduction To Data Structures
Data Structure:
A data structure is a specialized format for organizing and
storing data. General data structure types include the array, the file,
the record, the table, the tree, and so on. Any data structure is
designed to organize data to suit a specific purpose so that it can be
accessed and worked with in appropriate ways. In computer
programming, a data structure may be selected or designed to store
data for the purpose of working on it with various algorithms.
A data structure is a way of organizing data that considers not only
the items stored, but also their relationship to each other. Advance
knowledge about the relationship between data items allows
designing of efficient algorithms for the manipulation of data.
A data structure is a particular way of organizing data in a
computer so that it can be used efficiently.
A data structure is a collection of data items stored in memory; in
addition a number of operations are provided by the software to
manipulate that data structure.
A data structure means there is a relationship of some kind
between the data items. Exactly what the relationships are
determine what type of data structure is being used.
A data structure is a specialized format for organizing and storing
data.
1|Page
What is Algorithm?
An algorithm is an effective method expressed as a finite list of well-defined
instructions for calculating a function. Starting from an initial state and initial input
(perhaps empty),the instructions describe a computation that, whenexecuted,
proceeds through a finite number of well-defined successive states, eventually
producing "output" and terminating at a final ending state. The transition from one
state to the next is not necessarily deterministic; some algorithms, known
as randomized algorithms, incorporate random input.
A process or set of rules to be followed in calculations or other problemsolving operations, especially by a computer.
Data Structure
Data structure is the way of organizing the data along with the
relationship among the data. The study of data structure includes
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 new records to the structure.
Deleting:
Removing a record from the structure.
Sometimes two or more data structure of operations may be used in a
given situation; e.g., we may want to delete the record with a given key,
which may mean we first need to search for the location of the record.
Difference between Primitive and Non-Primitive Data
Type.
Primitive Data Type:
A primitive data type is one that fits the base architecture of the
underlying computer such as int, float, and pointer, and all of the
variations, thereof such as char short long unsigned float double
and etc, are primitive data type.
Primitive data are only single values, they have not special
capabilities.
The examples of Primitive data type s are given byte, short, int,
long, float, double, char etc.
The integer reals, logic data character data pointer and reference
are primitive data structures data structure that normally are
directly operated upon by machine level instructions are known as
primitive structure and data type.
3|Page
Data Structures:
A data structure is an arrangement of data in a computer's memory or even disk storage.
Data structures can be classified into two types
Linear data structures are those data structures in which data elements are accessed (read and
written) in sequential fashion ( one by one)
Eg: Stacks , Queues, Lists, Arrays
Non Linear Data Structures:
Non Linear Data Structures are those in which data elements are not accessed in sequential
fashion.
Eg: trees, graphs
Algorithm:
Step by Step process of representing solution to a problem in words is called an Algorithm.
Characteristics of an Algorithm:
5|Page
Disadvantages
Array
Slow search
Slow deletes
Fixed size
Quick inserts
Fast access if index known
Slow inserts
Slow deletes
Fixed size
Stack
Queue
Linked List
Quick inserts
Quick deletes
Slow search
Binary Tree
Quick search
Quick inserts
Quick deletes
(If the tree remains balanced)
Complex to implement
2-3-4 Tree
Quick search
Complex to implement
Quick inserts
Quick deletes
(Tree always remains balanced)
(Similar trees good for disk storage)
Hash Table
Slow deletes
Access slow if key is not known
Inefficient memory usage
Heap
Quick inserts
Quick deletes
Access to largest item
Graph
6|Page
Stack :
Stack is a Linear Data Structure which follows Last in First Out mechanism.
It means: the first element inserted is the last one to be removed
Stack uses a variable called top which points topmost element in the stack. top is incremented
while pushing (inserting) an element in to the stack and decremented while poping (deleting) an
element from the stack
top
Push(A)
B
A
Push(B)
C
B
A
top
top
Push(C)
D
C
B
A
Push(D)
top
top
C
BA
Pop()
Note:
While pushing an element into the stack, stack is full condition should be checked
While deleting an element from the stack, stack is empty condition should be checked
Applications of Stack:
7|Page
Queue:
Queue is a Linear Data Structure which follows First in First out mechanism.
It means: the first element inserted is the first one to be removed
Queue uses two variables rear and front. Rear is incremented while inserting an element into the
queue and front is incremented while deleting element from the queue
rear
front
A
Insert(A)
B
A
Insert(B)
rear
front
C
B
A
Insert(C)
rear
front
D
C
B
A
Insert(D)
rear
D
C
B
front
Delete()
Note:
While inserting an element into the queue, queue is full condition should be checked
While deleting an element from the queue, queue is empty condition should be checked
Applications of Queues:
Real life examples
Waiting in line
Waiting on hold for tech support
Applications related to Computer Science
Threads
Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
8|Page
rear
front
Linked List:
To overcome the disadvantage of fixed size arrays linked list were introduced.
A linked list consists of nodes of data which are connected with each other. Every node consist of
two parts data and the link to other nodes. The nodes are created dynamically.
NODE
bat
Data
link
bat
cat
9|Page
sat
vat
NULL
Trees :
A tree is a Non-Linear Data Structure which consists of set of nodes called vertices and set of
edges which links vertices
Terminology:
Root Node: The starting node of a tree is called Root node of that tree
Terminal Nodes: The node which has no children is said to be terminal node or leaf .
Non-Terminal Node: The nodes which have children is said to be Non-Terminal Nodes
Depth: The length of largest path from root to terminals is said to be depth or height of
the tree
Ancestors: The ancestors of a node are all the nodes along the path from the root to the
node
Property
G
H
10 | P a g e
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
Value
:
:
:
:
:
:
:
:
:
9
4
A
ED, H, I, F, C
D, E, G
5
I
D,E, F
D, F
Binary Trees:
Binary trees are special class of trees in which max degree for each node is 2
Recursive definition:
A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint
binary trees called the left subtree and the right subtree.
Any tree can be transformed into binary tree. By left child-right sibling representation.
A
B
C
E
K
11 | P a g e
The keys in a nonempty left subtree are smaller than the key in the root of subtree.
The keys in a nonempty right subtree are grater than the key in the root of subtree.
The left and right subtrees are also binary search trees.
63
89
41
34
56
Inserting an element
Deleting an element
Traversing
12 | P a g e
72
95
Avl Tree:
If in a binary search tree, the elements are inserted in sorted order then the height will be n,
where n is number of elements. To overcome this disadvantage balanced trees were introduced.
An AVL Tree is a binary search tree such that for every internal node v of T, the
heights of the children of v can differ by at most 1.
44
2
17
78
1
32
88
50
1
48
Inserting an element
Deleting an element
Traversing
Height balancing
13 | P a g e
62
Graphs
A graph is a Non-Linear Data Structure which consists of set of nodes called vertices V and set
of edges E which links vertices
Note: A tree is a graph with out loops
0
1
3
Graph
2
5
Tree
Graph Traversal:
Problem: Search for a certain node or traverse all nodes in the graph
Depth First Search
Once a possible path is found, continue the search until the end of the path
Breadth First Search
Start several paths at a time, and advance in each one step at a time
14 | P a g e
What is all the fuss about objects and object-oriented technology? Is it real? Or is it hype? Well,
the truth is--it's a little bit of both. Object-oriented technology does, in fact, provide many
benefits to software developers and their products. However, historically a lot of hype has
surrounded this technology, causing confusion in both managers and programmers alike. Many
companies fell victim to this hardship (or took advantage of it) and claimed that their software
products were object-oriented when, in fact, they weren't. These false claims confused
consumers, causing widespread misinformation and mistrust of object-oriented technology.
Object:
As the name object-oriented implies, objects are key to understanding object-oriented
technology. You can look around you now and see many examples of real-world objects: your
dog, your desk, your television set, your bicycle.
Definition: An object is a software bundle of variables and related methods
15 | P a g e
Class:
In the real world, you often have many objects of the same kind. For example, your bicycle is
just one of many bicycles in the world. Using object-oriented terminology, we say that your
bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state
(current gear, current cadence, two wheels) and behavior (change gears, brake) in common.
However, each bicycle's state is independent of and can be different from other bicycles.
Definition: A class is a blueprint or prototype that defines the variables and methods common to
all objects of a certain kind.
Inheritance:
Acquiring the properties of one class in another class is called inheritance
Subclasses provide specialized behaviors from the basis of common elements provided
by the super class. Through the use of inheritance, programmers can reuse the code in the
superclass many times.
Programmers can implement superclasses called abstract classes that define "generic"
behaviors. The abstract superclass defines and may partially implement the behavior but
much of the class is undefined and unimplemented. Other programmers fill in the details
with specialized subclasses.
Data Abstraction:
The essential element of object oriented programming in abstraction. The complexity of
programming in object oriented programming is maintained through abstraction.
For example, the program consist of data and code which work over data. While executing a
program we dont thing in which location that data is being stored how the input device is
transferring the input to the memory etc. this abstraction allows us to execute the program
without thinking deeply about the complexity of execution of program.
16 | P a g e
Encapsulation:
Encapsulation is the mechanism that binds together code and the data and keeps them safe from
outside world. In the sense it is a protective wrapper that prevents the code and data from being
accessed by other code defied outside the wrapper. Access is controlled through a well defined
interface.
Polymorphism:
Existing in more that one form is called polymorphism.
Polymorphism means the ability to take more that one form. For example an operation may
exhibit different behavior in different behavior in different instances.
For example consider operation of addition. For two numbers the operation will generate a sum.
If the operands are string the operation would produces a third string by concatenation.
C++ supports polymorphism through method overloading and operator overloading
Method overloading:
if the same method name used for different procedures that the method is said to be overloaded.
Dynamic Binding:
Binding refer to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding means that the code associated with a given procedure call is not know until
the time of the call at runtime. It is associated with a polymorphism reference depends on the
dynamic type of that reference.
Message communication:
An object oriented program consists of objects that communicate with each other. The process
of programming in an object oriented language therefore involves the following basic steps:
1. creating classes that define objects and their behaviors.
2. creating objects from class definitions.
3. establishing communication among objects.
17 | P a g e
18 | P a g e
details of how the methods carry out their tasks. An end user (or class user), you should be told
what methods to call, how to call them, and the results that should be expected, but not HOW
they work.
We can further extend the meaning of the ADT when applying it to data structures such as a stack
and queue. In Java, as with any class, it means the data and the operations that can be performed
on it. In this context, although, even the fundamentals of how the data is stored should be
invisible to the user. Users not only should not know how the methods work, they should also not
know what structures are being used to store the data.
Consider for example the stack class. The end user knows that push() and pop() (amoung other
similar methods) exist and how they work. The user doesn't and shouldn't have to know how
push() and pop() work, or whether data is stored in an array, a linked list, or some other data
structure like a tree.
20 | P a g e