Data Structure – डाटा स्ट्र क्चर क्या है ?
Data structure किसी िंप्यूटर कसस्टम में डे टा िो स्टोर तथा व्यवस्थित (organize) िरने िा एि तरीिा
होता है । किससे कि हम डे टा िा आसानी से इस्तेमाल िर सिें।
अथाा त डे टा िो इस प्रिार स्टोर तथा organize किया िाता है कि उसिो बाद में किसी भी समय आसानी से
access किया िा सिें।
डाटा स्टर क्चर C, C++, Java िी तरह िोई programming भाषा नहीं है बस्थि यह algorithms िा एि
set है किसिा प्रयोग हम programming languages में data िो structure िरने िे कलए िरते हैं .
Data structure बहुत सारें computer science algorithms िा एि मु ख्य भाग है किसिे द्वारा
programmers डाटा िो अच्छे ढं ग से handle िर सिते हैं . यह program या software िी
performance िो बेहतर िरने में बहुत ही अहम भूकमिा कनभाता है .
Types of Data Structure
data structure दो प्रिार िे होते है :-
1. Primitive डे टा स्टर क्चर
2. Non-primitive डे टा स्टर क्चर
1:-Primitive डे टा स्ट्र क्चर:- primitive डे टा स्टर क्चर वह डे टा स्टर क्चर होता है किसे direct ही मशीन
instructions से operate किया िा सिता है ।
अथाा त यह कसस्टम तथा compiler िे द्वारा कडफाइन होता है ।
Non-primitive डे टा स्ट्र क्चर:- primitive डे टा स्टर क्चर वह डे टा स्टर क्चर होता है किसे direct मशीन
instructions से operate नही किया िा सिता है । ये डे टा स्टर क्चर primitive डे टा स्टर क्चर से derived होते
है ।
Non-primitive डे टा स्टर क्चर दो प्रिार िा होता है :-
1:-Linear डे टा स्टर क्चर
2:-Non-linear स्टर क्चर
1:- Linear data structure
linear एि ऐसा डे टा स्टर क्चर है किसमें डे टा items िो linear (रे खीय) रूप में संग्रकहत तथा व्यवस्थित किया
िाता है , किसमें एि डे टा item दू सरे से एि रे खा िे रूप में िुड़ा होता है ।
ex:- array, linked list, queue, stack.
2:- Non-linear data structure
Non-linear एि ऐसा डे टा स्टर क्चर है , किसमें डे टा items िो क्रमबद्ध (sequential) तरीिे से व्यवस्थित
नही किया िाता है ।
किसमें एि डे टा item किसी भी अन्य डे टा items िे साथ िुड़ा हुआ हो सिता है ।
ex:-tree, graph.
Data structure operations
इसिे मुख्य operations (िाया ) नीचे कदए गये हैं :-
1. Searching – किसी element िो find िरने िी प्रकक्रया searching िहलाती है . searching िो
पूरा िरने िी दो algorithms होती हैं पहला binary search और दू सरा linear search.
2. Sorting – डाटा structure िो किसी कवशेष क्रम में arrange िरने िी प्रोसेस sorting िहलाती
है . sorting िो perform िरने िी बहुत सारीं algorithms है िैसे कि – insertion sort,
selection sort, bubble sort, radix sort आकद.
इसे पढ़ें :- Sorting क्या है और इसिे प्रिार
3. Insertion – किसी location में elements िो add िरने िी प्रकक्रया insertion िहलाती है . यकद
किसी डाटा स्टर क्चर िा size n है तो हम उसमें िेवल n-1 elements ही insert िर सिते हैं .
4. Deletion – किसी element िो remove िरने िी प्रोसेस deletion िहलाती है . हम किसी भी
location से data िो delete िर सिते है .
5. Traversing – Traversing िा मतलब है कि किसी कवशे ष िाया िो िरने िे कलए data structure
िे प्रत्येि element िो traverse िरना.
6. Merging – दो ऎसी list किनमें समान प्रिार िे data elements हों, िो एि साथ िोड़ दे ना
merging िहलाती है . किससे हमें एि तीसरी list प्राप्त होती है .
Different approaches to designing an algorithm
1. Bottom Up Approach
इस approach में कडिाईन lowest level components और subsystems िे साथ शुरू होता है . इन
components िा प्रयोग िरिे उसिे आगे िे high level components िा कनमाा ण किया िाता है . यह
प्रकक्रया तब ति चलती रहती है िब ति कि सारें components तथा subsystems िो एि अिेले
component में कनकमात नहीं िर कलया िाता. किसे हम एि पूरी तरह कनकमात system िहते है .
इसमें abstraction कितना ज्यादा बढे गा, उतना ही ज्यादा design िा level high होगा.
इसके लाभ:-
• इसमें components िो दु बारा से use किया िा सिता है .
• इसमें risk िो identify िरना आसान होता है .
• इसिा प्रयोग implementation िी low-level details िो छु पाने िे कलए िर सिते है . और top-
down तिनीि िे साथ merge िर सिते है .
इसकी हानियााँ:-
• यह problem िे structure से ज्यादा सम्बस्थित नही होता है .
• उच्च क्वाकलटी िे bottom up solution िो कनकमात िरना बहुत िकिन होता है .
2. Top Down Approach
इसमें प्रत्येि कसस्टम बहुत सारें subsystems तथा components में कवभाकित हो िाता है . और आगे प्रत्येि
subsystems भी subsystems और components िे समूह में कवभाकित हो िाता है .
कवभाकित होने िी इस प्रकक्रया से कसस्टम िा एि hierarchy structure बनने में मदद कमलती है .
सरल शब्ों में िहें तो, “इसमें कसस्टम बहुत सारें subsystem और component में कवभाकित हो िाता है
और ऐसा प्रत्येि subsystem िे साथ भी होता है . यह प्रकक्रया तब चलती रहती है िब ति कि सबसे कनम्नतम
level िा कसस्टम नही आ िाता.”
इसमे कडिाईन िे शुरुआत में एि पूरे कसस्टम िो define किया िाता है . और उसिे बाद subsystem और
components िी definitions िो लगातार डाला िाता है . और िब सारी definitions एि साथ कमल िाती
है . तो यह एि पूरा कसस्टम बन िाता है .
इसके फायदे :-
• इसिा िो मुख्य फायदा है वह यह है कि इसिा पू रा ध्यान requirements पर होता है किससे कि
िरूरतों िे कहसाब से एि responsive design कनकमात हो सिे.
• interface में आने वाली errors िो आसानी से handle किया िा सिता है ,
इसके िुकसाि:-
• इसमें components िो कफर से use नही किया िा सिता है .
• इसिा architecture अच्छा और simple नही होता है .
Complexity
Time Complexity
एि algorithm िी time complexity, अल्गोररथम िे द्वारा अपनी process िो पूरा िरने में लगने वाले
िुल समय िी मात्रा है .
ज्यादातर अल्गोररथम िी टाइम िॉम्प्लेस्थिटी िो Big O notation िा उपयोग िरिे व्यक्त किया िाता है ।
यह एि asymptotic notation है . इसिो व्यक्त िरने िे सभी notations कनम्नकलस्थखत है .
• Big O – O(n),
• Bi.g Theta – Θ(n)
• Big Omega – Ω(n)
execution िो समाप्त िरने िे कलए किसी भी एल्गोररथम द्वारा perform किये गए steps िी संख्या िी
कगनती (counting) िे द्वारा time complexity िो estimate किया िाता है .
space complexity
एि algorithm िी space complexity अल्गोररथम िे द्वारा ली गयी space िी मात्रा है . स्पेस िॉम्प्लेस्थिटी
िे अंदर auxiliary space तथा input िे द्वारा use कलया गया space दोनों आते हैं .
auxiliary space िो है वह algorithm िे द्वारा execution िे दौरान प्रयोग किया गया temporary space
या extra space होता है .
एि अल्गोररथम िी space complexity िो Big O (O(n)) notation िे द्वारा व्यक्त किया िाता है .
बहुत सारीं algorithms िे पास inputs होते हैं िो size में कभन्न कभन्न होते हैं . ऐसी स्थिकत में space
complexity िो है वह input िे size पर कनभार रहती है .
Stack in Data Structure– स्ट्ै क क्या है?
stack एि कवशे ष प्रिार िा linear डे टा स्टर क्चर होता है िो कि LIFO (last in first out) िे कसद्धान्त पर
िाया िरता है अथाा त वह item िो कि सबसे अंत में add किया िाता है उसे सबसे पहले remove िर कदया
िाता है तथा िो item सबसे पहले add किया िाता है उसे सबसे अंत में remove किया िाता है ।
Data Structure Stack in Hindi
स्टै ि में किसी item िो डालने (add) तथा remove (हटाने) िे कलए कसफा एि end होता है किसे हम top
end िहते है । इसमें िेवल top end से ही items िो add और remove किया िा सिता है .
दू सरे शब्ों में िहें तो, “Data Structure में, Stack एि abstract data type (ADT) है और इसमें एि
limited size िे elements या items िो ही स्टोर किया िा सिता है ”
स्टै ि िा प्रयोग ज्यादातर सभी programming languages में किया िाता है . और इसे stack इसकलए िहा
िाता है क्योंकि यह real world िे stack िी तरह व्यवहार िरता है . इसका real world example –
किताबों िा ढे र, चूकड़यााँ पहनना और कनिालना, तथा plates िा ढे र आकद.
Operations of Stack – स्ट्ै क के ऑपरे शि
Stack में कनम्नकलस्थखत operations िो perform किया िाता है :-
1. push() – िब स्टै ि में item िो insert किया िाता है तो वह push ऑपरे शन िहलाता है और यकद
स्टै ि full होता है तो तब overflow condition होती है .
2. pop() – िब स्टै ि में से item िो delete किया िाता है तो इस operation िो pop operation िहते
हैं . यकद स्टै ि खाली होता है तो तब underflow condtion होती है .
स्टै ि िो हम तब overflow िह सिते है िब वह पूरी तरह भरा हुआ होता है तथा तब underflow िह
सिते है िब वह पूरी तरह खाली होता है ।
3. isEmpty() – यह ऑपरे शन बताता है कि stack खाली है या नहीं.
4. isFull() – यह ऑपरे शन बताता है कि stack भरा हुआ है या नहीं.
5. peek() – यह दी गयी position पर element िो return िरता है ,
6. count() – यह stack में मौिूद elements िी िुल संख्या िो return िरता है .
7. change() – यह दी गयी position पर element िो बदल दे ता है .
8. display() – यह स्टै ि में मौिूद सभी elements (items) िो print िरता है .
Application of Stack – स्ट्ै क के अिुप्रयोग
इसिा प्रयोग कनम्नकलस्थखत िानों पर किया िाता हैं :-
1. स्टै ि िा प्रयोग expression evaluation िे कलए किया िाता है .
2. इसिा प्रयोग एि expression में parenthesis matching िो check िरने िे कलए किया िाता है .
3. इसिा प्रयोग infix expression िो postfix expression या prefix expression में convert िरने िे
कलए किया िाता है .
4. बहुत सारीं िगहों िैसे कि – photoshop, editors आकद में undo और redo िे िाया िे कलए स्टै ि िा
प्रयोग किया िाता है .
5. web browsers में backward और forward िे feature िे कलए इसिा use किया िाता है .
6. इसिा प्रयोग बहुत सारीं algorithms में किया िाता है िैसे कि – tree traversals, Stock span
problem, histogram problem आकद.
7. बहुत सारें compilers स्टै ि िा प्रयोग expressions िे syntax िो parsing िरने िे कलए िरते है .
8. इसिा प्रयोग backtracking िी problem िो solve िरने िे कलए किया िाता है . backtracking िा
अथा है वापस पीछे िाना.
9. मैमोरी मैनेिमेंट में इसिा उपयोग किया िाता है .
10. इसिो graph traversal algorithms में use किया िाता है .
11. इसिा प्रयोग CPU scheduling और disk scheduling में किया िाता है .
Introduction to Polish Notation in Data Structure
Polish Notation in the data structure is a method of expressing mathematical, logical, and algebraic
equations universally.
This type of notation was introduced by the Polish mathematician Lukasiewicz. Polish Notation in
data structure tells us about different ways to write an arithmetic expression. An arithmetic
expression contains 2 things, i.e., operands and operators. Operands are either numbers or
variables that can be replaced by numbers to evaluate the expressions. Operators are symbols
symbolizing the operation to be performed between operands present in the expression. Like the
expression (1+2)∗(3+4)(1+2)∗(3+4) standard becomes ∗+12+34∗+12+34 in Polish Notation. Polish
notation is also called prefix notation. It means that operations are written before the operands. The
operators are placed left for every pair of operands. Let’s say for the expression a+b, the prefix
notation would be +ab.
Types of Notations
Three types of polish notations exist in the data structure. Let's have look at them one-by-one.
• Infix Notation :
This polish notation in data structure states that the operator is written in between the
operands. It is the most common type of notation we generally use to represent expressions.
It's the fully parenthesized notation. We find it much easier to write mathematical
expressions in Infix Notation, but it is difficult to parse expressions on computers in the form
of infix Polish Notation.
• (3+7)
• (1*(2+3))
• Prefix Notation :
This polish notation in data structure states that the operator should be present as a prefix or
before the operands. This notation is also known as "Polish Notation". For example, if we
have an expression like x+y, then here x and y are operands, and ‘+’ is the operator. The
prefix notation or polish notation of this expression will be "+xy".
• 3+7 will convert into +37
• 1*(2+3) will convert into *1(+23)
• Postfix Notation :
This notation states that the operator should be present as a suffix, postfix, or after the
operands. It is also known as Suffix notation or Reverse Polish Notation. For example, if we
have an expression like x+y, then here x and y are operands, and ‘+’ is the operator. The
prefix notation or polish notation of this expression will be "xy+".
In general, a computer can easily understand postfix expressions. This notation is universally
accepted and is preferred for designing and programming the arithmetic and logical units of
a CPU (Central Processing Unit). All the expressions that are entered into a computer are
converted into Postfix or Reverse Polish Notation, stored in a stack, and then computed.
Therefore, postfix expression plays a vital role in the tech industry.
3+7 will convert into 37+
1*(2+3) will convert into (23+)1*
What is array & types of arrays
Array :-
array एि non-primitive तथा linear डे टा स्टर क्चर है िो कि एिसमान(similar) डे टा items िा समूह
होता है , अथाा त यह कसफा एि ही प्रिार िे डे टा िो ही स्टोर िरे गा ( या तो यह कसफा सभी integer डे टा िो
स्टोर िरे गा या कफर सभी floating point िो )।
यह contiguous memory locations में समान प्रिार िे data elements िा archive stored िरता है ।
Array डे टा स्टर क्चर िा प्रयोग डे टा objects िे समूह िो संग्रकहत िरने िे कलये किया िाता है ।
“Arrays एि static डे टा स्टर क्चर है अथाा त् हम िेवल compile time में ही मेमोरी िो allocate िर सिते
है और इसे run-time में बदल नही सिते है ।”
Types of array in hindi:-
Arrays कनम्नकलस्थखत तीन प्रिार िा होता है :-
1:- one dimensional arrays.
2:- two dimensional arrays.
3:- Multi dimensional arrays.
1:- one dimensional(1-D) arrays:-
वह arrays किसमे कसफा एि subscript होती है उसे one dimensional arrays िहते है । इसिा प्रयोग
linear रूप में डे टा िो स्टोर िरने िे कलए किया िाता है ।
1-डी ऐरे या वैक्टर िे रूप में भी िाना िाता है , सी भाषा में one-dimensional array िा िेवल एि
Dimensions होता है । इन्हें एि row or column द्वारा दशाा या िाता है ।
2:- two dimensional(2-D) arrays:-
वह arrays किसमें दो subscript होती है उसे two dimensional array िहते है । two dimensional
arrays िो matrix तथा table भी िहते है ।
Two dimensional array में data items िो row तथा column matrix form में arrange
िरते हैं । Two dimensional array multidimensional array िी simple form होती हैं
3:- Multi dimensional(M-D) arrays:–
वह arrays किसमें दो से ज्यादा subscript होती है वह Muti-dimensional arrays िहलाता है ।
Two dimensional िे बाद यकद हमे 3 dimensional ,4 dimensional array िो बनाने िी
िरूरत पड़ती हैं तब उसे multi dimensional array िहते हैं । Two dimensional िो भी एि
तरह से multi-dimensional िहा िा सिता हैं क्यूिी इसमे एि से अकिि dimension होती हैं ।
3 dimensional िे बाद multi dimensional िो समझना िाफी confusing हो िाता हैं ।
इसकलए हद से हद 3d या 4d ति बनाना सही रहता हैं ।
Advantage and disadvantage of array
array advantage:-
array advantage कनम्नकलस्थखत होते है :-
1:- Array िो आसानी से implement किया िा सिता है ।
2:- एि ही प्रिार िे कवकभन्न डे टा items िो िेवल एि नाम िे द्वारा प्रदकशात किया िा सिता है ।
3:- array एि ही समय में अनेि डे टा items िो स्टोर िर सिता है ।
4:- 2D arrays िा प्रयोग matrices िो प्रदकशात िरने िे कलए किया िाता है ।
Disadvantage of array:-
arrays िे कनम्नकलस्थखत advantage होते है :-
1:- Array िे द्वारा मैमोरी िा waste होता है ।
2:- Array एि static डे टा स्टर क्चर है किसिे िारण इसिा size पहले से ही define होता है ।
3:- हमें array में एि element िो delete तथा insert िरने िे कलए पूरे array िो traverse िरना पड़ता
है ।
Difference between Stack and Array
Basis of
Comparison Stacks Array
Stack is a linear data structure An array is a collection of related
represented by a sequential data values called elements
Definition
collection of elements in a each identified by an indexed
fixed an order array
In the array the elements belong
Stacks are based on the LIFO to indexes, i.e., if you want to get
principle, i.e., the element into the fourth element you have
Principle
inserted at the last, is the first to write the variable name with
element to come out of the list. its index or location within the
square bracket eg arr[4]
Insertion and deletion in stacks Insertion and deletion in the
Operations take place only from one end array can be done at any index in
of the list called the top. the array.
Storage The stack has a dynamic size. The array has a fixed size.
The stack can contain
The array contains elements of
Data Types elements of different data
the same data type.
types.
We can do both linear and
Methods We can do only a linear search
Binary search
Random access to elements is Random access to elements is
Data Access
not allowed in stacks allowed in arrays
We can implement a stack We cannot implement an array
Implementation
using the array using stack
There are limited number of It is rich in methods or
operations can be performed operations that can be perform
Methods
on a stack: push, pop, peek, on it like sorting, traversing,
etc. reverse, push, pop, etc.
Basis of
Comparison Stacks Array
It has only one pointer- the
In arrays, memory can be
top. This pointer indicates the
allocated in compile-time and is
Pointers address of the topmost
also known as static memory
element or the last inserted
allocation.
one of the stack.
Difference between Linear and Non-linear Data Structures:
[Link] Linear Data Structure Non-linear Data Structure
In a linear data structure, data elements are
arranged in a linear order where each and In a non-linear data structure, data elements
1.
every element is attached to its previous and are attached in hierarchically manner.
next adjacent.
In linear data structure, single level is Whereas in non-linear data structure,
2.
involved. multiple levels are involved.
Its implementation is easy in comparison to While its implementation is complex in
3.
non-linear data structure. comparison to linear data structure.
While in non-linear data structure, data
In linear data structure, data elements can be
4. elements can’t be traversed in a single run
traversed in a single run only.
only.
While in a non-linear data structure, memory
In a linear data structure, memory is not
5. is utilized in an efficient way.
utilized in an efficient way.
Its examples are: array, stack, queue, linked
6. While its examples are: trees and graphs.
list, etc.
Applications of non-linear data structures
Applications of linear data structures are
7. are in Artificial Intelligence and image
mainly in application software development.
processing.
Non-linear data structures are useful for
Linear data structures are useful for simple representing complex relationships and data
8.
data storage and manipulation. hierarchies, such as in social networks, file
systems, or computer networks.
[Link] Linear Data Structure Non-linear Data Structure
Performance is usually good for simple
Performance can vary depending on the
operations like adding or removing at the
9. structure and the operation, but can be
ends, but slower for operations like searching
optimized for specific operations.
or removing elements in the middle.
What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called a recursive function. Using a recursive algorithm, certain
problems can be solved quite easily. Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. A recursive function
solves a particular problem by calling a copy of itself and solving smaller subproblems of
the original problems. Many more recursive calls can be generated as and when required.
C language में, Recursion एि प्रकक्रया है किसमें एि function अपने आप िो बार-बार call िरता है . इस
function िो recursive function िहते हैं.
इस प्रिार िी function call िो recursive calls िहा िाता है . Recursion में बहुत सारीं recursive calls होती
है इसकलए इसिो terminate िरने िे कलए terminate condition िा प्रयोग िरना आवश्यि होता है .
Recursion िो हम सभी problems पर apply नहीं िर सिते परन्तु इसिा प्रयोग िरिे हम िुछ problems
िो आसानी से solve िर सिते हैं . उदाहरण िे कलए – हम इसिा प्रयोग sorting, searching, traversal आकद
problems िो solve िरने िे कलए िर िर सिते हैं .
C प्रोग्राकमंग लैंग्वेि recursion िो सपोटा िरती है . परन्तु इसिा प्रयोग िरने िे दौरान programmer
िो function में exit condition िो define िरना आवश्यि होता है अन्यथा यह infinite loop ति चलता
रहे गा.
Recursive functions बहुत सारीं mathematical problems िो solve िरने में बहुत उपयोगी होती है िैसे
कि – एि number िे factorial िो calculate िरना, fibonacci series िो िनरे ट िरना आकद.
Advantage of Recursion
1. यह अनावश्यि function call िो िम िरता है.
2. इसिे द्वारा हम problems िो आसानी से solve िर सिते है क्योंकि iterative solution बहुत बड़ा
और complex (िकिन) होता है .
3. यह हमारे code िो elegant (सुंदर) बना दे ता है.
Disadvantage of Recursion
1. Recursion िा प्रयोग िरिे बनाये गये program िो समझना और debug िरना मुस्थिल होता है.
2. इसिे कलए हमें ज्यादा memory space िी आवश्यिता होती है .
3. इसमें program िो execute िरने में ज्यादा समय लगता है .
Data Structure Queue
Queue एि non-primitive तथा linear डे टा स्टर क्चर है और यह FIFO (first in first out) िे कसद्धान्त पर
िाया िरता है अथाा त वह item िो कि सबसे पहले add किया िाता है वही item सबसे पहले remove
किया िायेगा और वह item िो कि सबसे अंत में add किया िाता है उसे अंत में ही remove किया िायेगा।
दू सरे शब्ों में िहें तो, “Queue एि abstract data structure है . यह Stack िी तरह ही होता है परन्तु
queue में दोनों ends खुले रहते हैं िबकि स्टै ि में िेवल top end ही खुला रहता है .”
Queue िो हम अपनी वास्तकवि दु कनया में अिर ही प्रयोग िरते हुए दे खते है , चनलए इसका उदाहरण
दे खते है:-
“रे लवे िा उदाहरण ले ते है , एि व्यस्थक्त िो रे लवे में कटिट ररिवे शन िी लाइन में सबसे पहले लगा होता है
और सबसे पहले कटिट लेिर चले िाता है , वह व्यस्थक्त िो last में लगा हुआ रहता है वह अंत में ही बाहर
िायेगा।”
Queue में दो end होते है एि front end होता है तथा दू सरा rear end होता है । Rear end में item िो
add किया िाता है तथा front end से item िो remove किया िाता है ।
Operations of Queue in Hindi
Queue में perform किये िाने वाले operations कनम्नकलस्थखत हैं :-
1. Enqueue – िब हम queue में किसी item या element िो add िरते हैं तो उस ऑपरे शन िो
enqueue िहते है .
2. Dequeue – िब हम queue में से किसी item िो delete िर दे ते हैं तो उसे ऑपरे शन िो dequeue
िहते हैं .
3. Peek – इस ऑपरे शन िा प्रयोग queue िे front element िो प्राप्त िरने िे कलए किया िाता है और
इसमें element िो delete नहीं किया िाता.
4. isEmpty – इसिा प्रयोग यह check िरने िे कलए किया िाता है कि queue खाली है या नहीं. िब
queue खाली होता है तो वह underflow condtion िो throw िरता है .
5. isFull – इसिा प्रयोग यह check िरने िे कलए किया िाता है कि queue पूरी तरह से full है या नहीं.
िब queue पूरी तरह से full होता है तो यह overflow condtion िो throw िरता है .
Types of Queue – इसके प्रकार
ये चार प्रिार िा होता है , िो कि नीचे कदए गये हैं :-
1 – Linear Queue – इसमें, insertion िो एि end से किया िाता है और दू सरे end से deletion किया
िाता है . वह end िहााँ से insertion किया िाता है उसे rear end िहते है और वह end िहााँ से deletion
िरते है उसे front end िहते है .
2 – Circular Queue – इसमें , सभी nodes िो circle िे रूप में प्रस्तुत किया िाता है . इसमें last
element पहले element िे साथ िुडा रहता है . इसे ring buffer भी िहते है . Circular Queue में item
िो rear end से add किया िाता है तथा item िो front end से remove किया िाता है ।
3 – Priority Queue – यह एि कवशेष प्रिार िा queue होता है किसमें प्रत्येि element िे साथ एि
priority (प्राथकमिता) िुडी रहती है और यह उसी priority िे आिार पर िाया िरता है . इसमें किस
element िी priority सबसे िम होती है उसे सबसे पहले remove किया िाता है और यकद elements िी
priority समान होती है तो तब FIFO कसद्धां त िे आिार पर elements िो arrange किया िाता है .
4 – Dequeue – Dequeue िा पूरा नाम double-ended queue है । Dequeue एि ऐसा डे टा स्टर क्चर है
किसमें हम items िो front तथा rear end दोनों से add भी िर सिते है और remove भी िर सिते है ।
Implementation of queue
queue िो हम array, stack और linked list िे द्वारा implement िर सिते है . इसे implement िरने
िा सबसे आसान array है . array िा प्रयोग िरिे हम इसे आसानी से implement िर सिते है .
array िे द्वारा queue िो implement िरने िे कलए, n size िा एि array create िरते है , और FRONT
और REAR िो 0 पर initialized िरते है . इसिा मतलब यह है कि अभी array खाली (empty) है .
इसमें front िो है वह array िे first element िा index होता है . और rear, array िे last element िा
index होता है .
िैसे िैसे हम array में elements िो add िरते िाते है वैसे वैसे rear िा index बढ़ता िाता है परन्तु front
वैसे िा वैसे ही रहता है .
नीचे queue operations िे implementation िो कदखाया गया है :-
1:- enqueue :-
enqueue मतलब queue में नए elements िो add िरना.
elements िो add िरने से पहले यह check किया िाता है कि queue भरा हुआ (full) है या नही. अगर
queue full है तो overflow error िो print किया िाता है और program िो exit िर कदया िाता है .
अगर queue full नही है तो, rear में increment किया िाता है और element िो add किया िाता है .
2:- dequeue :-
dequeue िा मतलब है queue से element िो remove िरना.
elements िो remove िरने से पहले यह check किया िाता है कि queue खाली (empty) है या नही.
अगर queue empty है तो underflow error िो print किया िाता है . और प्रोग्राम िो exit िर कदया िाता
है .
और यकद queue खाली नहीं है तो front में element िो print किया िाता है और head िो increment
िर कदया िाता है .
3:- display :-
इसिा प्रयोग queue िे सभी elements िो print िरने िे कलए किया िाता है . यकद queue खाली नही है
तो, सभी elements िो front से rear ति traverse और print किया िाता है .
what is deque
deque िा पूरा नाम double ended queue है . यह queue िा एि प्रिार है . इसमें हम दोनों ends
(front और rear) में insertion और deletion िे operations िो परफॉमा िर सिते है . इसिा मतलब
यह है कि हम front end में insert भी िर सिते है और delete भी. और इसी प्रिार rear end में insert
भी िर सिते है और delete भी.
इसिो दो प्रिार से प्रस्तुत किया िा सिता है िो कि कनम्नकलस्थखत है :-
1. Input restricted double ended queue
2. Output restricted double ended queue
Input restricted double ended queue
Input restricted double ended queue में, insertion ऑपरे शन िो िेवल एि end से परफॉमा किया
िा सिता है और deletion ऑपरे शन िो दोनों ends से परफॉमा किया िा सिता है .
output restricted double ended queue
output restricted double ended queue में, deletion ऑपरे शन िो िेवल एि end से perform
किया िाता है िबकि insertion operation िो दोनों ends से परफॉमा किया िाता है .
operations of deque
इसमें perform किये िाने वाले basic operation कनम्नकलस्थखत है :-
• insertFront() – deque िे front में item िो add िरना.
• insertLast() – deque िे rear में एि item िो add िरना
• deleteFront() – deque िे front से item िो delete िरना.
• deleteLast() – item िे rear से item िो delete िरना.
इन operations िे साथ साथ यह कनम्नकलस्थखत operations िो भी support िरता है :-
getFront():- queue से front item िो प्राप्त िरना
getRear():- queue से last item िो प्राप्त िरना.
isEmpty():- यह check िरता है कि deque खाली है या नही.
isFull():- यह check िरता है कि deque full है या नही.
What is Priority queue
Priority queue िो है वह queue िा एि extension है . यह एि प्रिार िा कवशे ष data structure होता
है .
सािारण queue िी तरह ही, priority queue िी भी समान method होती है परन्तु इसमें एि मुख्य अंतर
होता है . priority queue में, items िो key value िे द्वारा व्यवस्थित किया िाता है . किस item िी value
सबसे िम होगी वह front में हो गा और किस item िी वै ल्यू सबसे ज्यादा होगी वह rear में होगा. इस प्रिार
हम items िो उनिी key value िे आिार पर priority दे ते है .
किसिी कितनी िम value होगी उसिी उतनी ज्यादा priority होगी. और किसिी कितनी ज्यादा value
होगी उसिी उतनी िम priority होगी
basic operations
इसिे basic operations कनम्नकलस्थखत है :-
• insert / enqueue – queue िे rear में item िो add िरना.
• remove / dequeue – queue िे front से item िो remove िरना.
• peek – queue िे front में element िो प्राप्त िरना.
• isfull – यह check िरता है यकद queue full हो गया है .
• isEmpty – यह check िरता है यकद queue खाली हो गया है .
Difference between Queue and Deque:
[Link] Queue Deque
A queue is a linear data structure that
stores a collection of elements, with A deque (double-ended queue) is a linear data
operations to enqueue (add) elements structure that stores a collection of elements, with
at the back of the queue, and dequeue operations to add and remove elements from both
(remove) elements from the front of ends of the deque.
1. the queue.
Elements can only be inserted at the Elements can be inserted from both ends of the data
2. end of the data structure. structure.
Elements can only be removed from Elements can be removed from both ends of the data
3. the front of the data structure. structure.
Queues are a specialized data Deque can be used to implement the functionalities
structure that uses the FIFO approach of both Stack (LIFO approach i.e., Last In First Out)
4. i.e., First In First Out. and Queue (FIFO approach i.e., First In First Out).
A queue can be implemented using Deque can be implemented using Circular Array or
5. Array or Linked List. Doubly Linked List.
[Link] Queue Deque
Queues can be used as a building
Deques can be used as a building block for
block for implementing more
implementing more complex data structures, such as
complex data structures, such as
double-ended priority queues or circular buffers.
6. priority queues or stacks.
Common deque operations include addFirst (add an
element to the front of the deque), addLast (add an
Common queue operations include
element to the back of the deque), removeFirst
enqueue, dequeue, peek (return the
(remove the first element from the deque),
front element without removing it),
removeLast (remove the last element from the
and size (return the number of
deque), peekFirst (return the first element without
elements in the queue).
removing it), and peekLast (return the last element
7. without removing it).
Queue elements cannot be accessed
Deque can be traversed using iterator.
8. with the help of an iterator.
Examples of queue-based algorithms
Examples of deque-based algorithms include sliding
include Breadth-First Search (BFS)
window problems and maintaining a maximum or
and printing a binary tree level-by-
minimum value in a sliding window.
9. level.
Difference between Stack and Queue Data Structures
Stacks Queues
A stack is a data structure that stores a A queue is a data structure that stores a collection of
collection of elements, with operations to elements, with operations to enqueue (add) elements
push (add) and pop (remove) elements at the back of the queue, and dequeue (remove)
from the top of the stack. elements from the front of the queue.
Stacks are based on the LIFO principle, Queues are based on the FIFO principle, i.e., the
i.e., the element inserted at the last, is element inserted at the first, is the first element to
the first element to come out of the list. come out of the list.
Stacks are often used for tasks that
Queues are often used for tasks that involve processing
require backtracking, such as parsing
elements in a specific order, such as handling requests
expressions or implementing undo
or scheduling tasks.
functionality.
Stacks Queues
Insertion and deletion in queues takes place from the
Insertion and deletion in stacks takes
opposite ends of the list. The insertion takes place at
place only from one end of the list called
the rear of the list and the deletion takes place from
the top.
the front of the list.
Insert operation is called push operation. Insert operation is called enqueue operation.
Stacks are implemented using an array or Queues are implemented using an array or linked list
linked list data structure. data structure.
Delete operation is called pop operation. Delete operation is called dequeue operation.
In stacks we maintain only one pointer to In queues we maintain two pointers to access the list.
access the list, called the top, which The front pointer always points to the first element
always points to the last element present inserted in the list and is still present, and the rear
in the list. pointer always points to the last inserted element.
Stack is used in solving problems works Queue is used in solving problems having sequential
on recursion. processing.
Stacks are often used for recursive Queues are often used in multithreaded applications,
algorithms or for maintaining a history of where tasks are added to a queue and executed by a
function calls. pool of worker threads.
Queue is of three types – 1. Circular Queue 2. Priority
Stack does not have any types.
queue 3. double-ended queue.
Can be considered as a vertical collection
Can be considered as a horizontal collection visual.
visual.
Examples of queue-based algorithms include Breadth-
Examples of stack-based languages
First Search (BFS) and printing a binary tree level-by-
include PostScript and Forth.
level.
Linked List
linked list एि एसी data structure है किसमे data structure िे item िे अलावा lost मे आने वाले next item िे
address िो contain किया िाता है |इस data structure िो linked list इसकलए िहते है क्योकि इसमे list िे
सभी item आने वाले item से link होते है |
linked कलस्ट िे प्रत्येि block िो नोड िहते है |किसिे दो part होते है :-
1.पहले भाग linked कलस्ट िे item िो रखता है |
2.दु सरे भाग मे ,next item िे address िो रखता है |
इस data structure मे element , memory address से link नहीं होते है बस्थि किसी लॉकििल कलंि से कलंि होते
है िो िी सभी नोड िे दु सरे भाग मे store होता है |कलंि एि pointer variable होता है किसिा type दु सरे नोड िे
data type िा होता है
Linked list एि non-primitive, linear डे टा स्टर क्चर है।
linked list एि ऐसा डे टा स्टर क्चर होता है किसिी length िो run-time में बढ़ाया या घटाया िा सिता है । अथाात
यह dynamic होता है ।
linked list िा प्रयोग tree तथा graph िो बनाने िे कलए किया िाता है ।
Advantage of linked
इसिे लाभ कनम्नकलस्थखत है :-
1:- linked list एि dynamic data structure है.
2:- linked list िो run time में हम घटा भी सिते है और बढ़ा भी सिते हैं . अथाा त memory िो run-
time में ही allocate और deallocate िर सिते है .
3:- इसमें हम आसानी से insertion और deletion िायों िो िर सिते है . अथाा त आसानी से हम node
िो insert तथा delete िर सिते है .
4:- कलं क्ड कलस्ट में memory िो अच्छी तरह utilize किया िाता है . क्योंकि हमें इसमें पहले से मैमोरी
allocate नहीं िरनी पड़ती है .
5:- इसिा access time बहुत ही fast होता है . और कबना memory overhead िे एि कनयत समय में
access िर सिते है .
6:- linked list िा प्रयोग िरिे हम linear data structures िैसे:- Stack, queue िो आसानी से
implement िर सिते है .
disadvantage of linked list
इसिी िुछ हाकनयााँ कनम्नकलस्थखत है :-
1. कलंक्ड कलस्ट में array िी तुलना में elements िो स्टोर िरने िे कलए अकिि memory िी िरूरत
पड़ती है . क्योंकि कलं क्ड कलस्ट िी प्रत्ये ि node एि pointer िो contain िरती है किसिे िारण इसे
अकिि memory िी आवश्यिता होती है .
2. कलंक्ड कलस्ट में nodes िो traverse िरना बहुत िकिन होता है . इसमें हम किसी एि element िो
randomaly एिेस नही िर सिते है . (िैसा कि हम array में index िे द्वारा िरते है .)
उदाहरण के नलए:- अगर हम किसी n position में स्थित node िो traverse िरना चाहें तो हमें n से
पहले आने वाले सभी nodes िो traverse िरना पड़े गा. किससे हमारा बहुत सारा समय नष्ट हो
िायेगा.
3. linked list में reverse traversing िरना बहुत ही difficult होता है . doubly linked में हम आसानी
से िर सिते है परन्तु उसमें pointer िे कलए ज्यादा memory िी िरूरत होती है .
Types Of Linked List:
1. Singly Linked List
It is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type.
The node contains a pointer to the next node means that the node stores the address of
the next node in the sequence. A single linked list allows the traversal of data only in
one way. Below is the image for the same:
2. Doubly Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list that
contains a pointer to the next as well as the previous node in sequence.
Therefore, it contains three parts of data, a pointer to the next node, and a pointer to the
previous node. This would enable us to traverse the list in the backward direction as
well. Below is the image for the same:
3. Circular Linked List
A circular linked list is that in which the last node contains the pointer to the first node
of the list.
While traversing a circular linked list, we can begin at any node and traverse the list in
any direction forward and backward until we reach the same node we started. Thus, a
circular linked list has no beginning and no end. Below is the image for the same:
Difference Between Sequential and Linked list:
S.N Linked list Sequential list
1. Data is stored in nodes that are linked together Data is stored in a linear sequence
Each node contains data and a pointer to the next
2. Each element is stored one after the other
node
3. Allows for fast insertions and deletions Allows for fast traversal and access
4. More complex to implement Simpler to implement
Can be used for implementing data structures Can be used for implementing data
5.
like linked lists and trees structures like arrays and stacks
Less memory is required as no pointers
6. Requires more memory for pointers
needed
7. Can be used for dynamic data structures Suitable for static data structures
8. Flexible in terms of size and structure Fixed-size and structure
9. Random access is not possible Random access is possible
Pointers may be pointing to null or non-existent All elements are stored in contiguous
10.
memory in case of broken links memory
Can have multiple pointers in case of Only one pointer is required to traverse
11.
bidirectional links the list
S.N Linked list Sequential list
12. Can have a circular linked list Only a linear sequential list is possible
13. Can have multiple head and tail pointers Only one head and tail pointer is required
14. Can have variable length of data in each node Fixed length of data in each element
Can be used for implementing more complex Can be used for simple data structures like
15.
data structures like graphs. queues and stacks.
difference between array and linked list
array linked list
linked list समान प्रिार िे elements िा एि
1:- array समान प्रिार िे data type िा एि
ordered collection होता है िो कि एि दू सरे से
ordered collection होता है .
pointers िे द्वारा connect रहते है .
2:- यह random access िो support िरता है
यह sequential access िो सपोटा िरता है ,
किसिा मतलब यह है कि हम इसे direct इसिे
किसिा मतलब है कि linked list में किसी
index िा प्रयोग िरिे access िर सिते है िैसे-
elements/nodes िो एिेस िरने िे कलए हमें
1st element िे कलए arr[0], 8वें element िे कलए
पूरी list िो sequentially traverse िरना पड़े गा.
arr[7] आकद.
3:- इसमें elements िो contiguous memory इसमें नये elements िो memory में िहीं भी स्टोर
location में स्टोर किया िाता है . किया िा सिता है .
4:- इसमें elements िो बहुत तेिी से access िर
सिते है तथा इसिी कनयत time complexity O(1) इसिी time complexity O(n) है .
है .
5:- इसमें, insertion तथा deletion ऑपरे शन में
इसमें insertion तथा deletion ऑपरे शन fast होते
अकिि time लगता है क्योंकि मैमोरी लोिेशन
है .
continuous तथा fix होते है .
6:- इसमें memory िो compile time में allocate इसमें memory िो run time में allocate किया
किया िाता है . इसे static memory allocation भी िाता है . इसे dynamic memory allocation भी
िहते है . िहते है .
7:- array में, प्रत्येि element स्वत्रंत होता है तथा इसे linked list में, प्रत्ये ि node अगले और कपछले
इसिे index value िे द्वारा access किया िाता है . nodes से point होते है .
8:- यह single dimensional, two dimensional यह linear (singly), doubly या circular हो सिते
या multi-dimensional हो सिते है . है .
9:- array िा size इसिे declaration िे समय linked list िा साइज़ run-time में बढ़ता है िैसे
specify किया िाता है . िैसे इसमें नए नोड add किये िाते हैं .
Tree in Data Structure
• Tree एि hierarchical data structure होता है िो कि information या data िो hierarchical (श्रेणीबद्ध)
तरीिे से स्टोर िरता है.
• “टर ी(tree), nodes िा एि समूह होता है किनमें सामान्यतया hierarchical relationship होती है।”
• Tree एि non-linear डे टा स्टर क्चर होता है।
• Tree में parent-child relationship होती है।
• Tree िे प्रत्येि data item िो हम node िहते है।
• Tree में िो सबसे ऊपर वाला node होता है उसे हम root node िहते है ।
• एि node िा अकिितम एि ही parent हो सिता है। लेकिन िेवल root node िा िोई parent नही होता
है।
• एि tree में प्रत्येि node िा शून्य या ज्यादा child nodes हो सिते है ।
• ऐसे nodes किनिे एि भी child nodes नही होते है उन्हें leaf node या terminal node िहते है।
• वैसे तो tree हमेशा ऊपर िी ओर बढ़ता है लेकिन data structure िा tree हमेशा नीचे िी ओर बढ़ता है ।
Tree Terminologies
Terms Description
एि Tree में root node सबसे ऊपर िा node होता है . दू सरे शब्ों में िहें तो, root node िा िोई भी
Root parent नहीं होता है.
ऊपर कदए गये कचत्र में A एि root node है .
यकद िोई node किसी sub-node िो contain िरता है तो इस node िो sub-node िा parent िहा
Parent िाता है .
node दू सरे शब्ों में िहें तो, “एि node िा िीि कपछला वाला node, parent node होता है .”
कचत्र में, B िो है वह D, E, F िा parent है .
यकद एि नोड किसी नोड िा वंशि (descendant) है , तो इस नोड िो child node िे रूप में िाना िाता
Child node है।
ऊपर कदए गये कचत्र में, D, E, F िो हैं वे B िे child हैं .
Sibling वे nodes किनिा एि ही parent होता है उन्हें siblings िहते हैं . कचत्र में, B और C siblings हैं .
वह नोड किसिा िोई भी child नहीं होता है उसे leaf node िहते हैं . कचत्र में, D, H, F, G leaf nodes हैं.
Leaf node Tree में, एि leaf node सबसे नीचे वाला नोड होता है . एि tree में कितने भी leaf nodes हो सिते है .
Leaf nodes िो external nodes भी िहते हैं .
एि edge दो nodes िे मध्य िा connection होता है . यह दो nodes िे बीच िी line होती है .
Edge
कचत्र में, A और B िे बीच िी line एि edge है .
Internal
वह नोड किसिा िम से िम एि child node होता है उसे internal node िहते हैं.
node
• Ancestor node:- An ancestor of a node is any predecessor node on a path from
the root to that node. The root node doesn’t have any ancestors. In the tree
shown in the above image, nodes 1, 2, and 5 are the ancestors of node 10.
• Descendant: The immediate successor of the given node is known as a
descendant of a node. In the above figure, 10 is the descendant of node 5.
Binary Tree Data Structure
A Binary Tree Data Structure is a hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child. It is commonly used in computer science for
efficient storage and retrieval of data, with various operations such as insertion, deletion, and traversal.
Types of Binary Tree based on the number of children:
Following are the types of Binary Tree based on the number of children:
1. Full Binary Tree
2. Degenerate Binary Tree
3. Skewed Binary Trees
1. Full Binary Tree
A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are
examples of a full binary tree. We can also say a full binary tree is a binary tree in which
all nodes except leaf nodes have two children.
A full Binary tree is a special type of binary tree in which every parent node/internal
node has either two or no children. It is also known as a proper binary tree.
Full Binary Tree
2. Degenerate (or pathological) tree
A Tree where every internal node has one child. Such trees are performance-wise same
as linked list. A degenerate or pathological tree is a tree having a single child either left
or right.
Degenerate (or pathological) tree
3. Skewed Binary Tree
A skewed binary tree is a pathological/degenerate tree in which the tree is either
dominated by the left nodes or the right nodes. Thus, there are two types of skewed
binary tree: left-skewed binary tree and right-skewed binary tree.
Skewed Binary Tree
Traversing
Traversing in the data structure is a very important operation that can be performed on any data
structure. Traversing in Data Structure means systematically visiting every element of it. Traversing
is a process in which each element of a data structure is accessed. Accessing an element of data
structure means visiting every element at least once. Traversing is performed to display every
element of data structure or to perform any operation on its element. Traversing is also known as
iterating over the data structure.
Binary tree traversal
Binary tree िे traversal में एि node िो कसफा एि बार ही visit किया िाता है ।
Binary tree िे traversal िा अथा है कि “tree िे प्रत्येि node िो किसी order में visit िरना”
1:-In-order traversal
2:-Pre-order traversal
3:-post-order traversal
1:-Inorder traversal:-Inorder traversal िे कलए कनम्नकलस्थखत कबंदु है :-
(1):-Left children या left subtree िो traverse किया िाता है ।
(2):-Root या parent िो visit किया िाता है ।
(3):-Right subtree या right children िो traverse किया िाता है ।
अब हम इसिो उदहारण िे द्वारा समझ सिते है :-
इस कचत्र िा output कनम्न है :-
9,5,1,7,2,11,8,4,3,6
2:-Preorder traversal:- Pre-order traversal िे कलए कनम्नकलस्थखत कबं दु है :-
(1):-सबसे पहले root या parent िो visit िरते है ।
(2):-उसिे बाद left subtree या left children िो visit िरते है ।
(3):-उसिे बाद right subtree या right children िो visit िरते है ।
उदहारण ऊपर वाले कचत्र िा output:-
8,5,9,7,1,11,2,4,6,3
3:-Post order traversal:- Post-order traversal िे कलए कनम्नकलस्थखत कबंदु है :-
(1):-सबसे पहले left subtree या left child िो visit िरते है ।
(2):-उसिे बाद right subtree या right child िो visit िरते है ।
(3):-उसिे बाद root(parent) िो visit िरते है ।
उदहारण िे कलए ऊपर वाले कचत्र िा output:-
9,1,2,11,7,5,3,6,4,8
Binary Search Tree
• Binary search tree एि कवशेष प्रिार िा binary tree है िो Class िे रूप में पररभाकषत किया िा
सिता है . किसमें Nodes िो एि कवकशष्ट क्रम में व्यवस्थित किया िाता है .
• left sub tree िी सारी information root से छोटी है और right sub tree िी सारी information
root से बड़ी है .
• right sub – tree में सभी नोड् स िी value, root िी value से अकिि या बराबर है ।
• left और right दोनों ही sub binary search tree है .
What is graph
data structure graph िो हम कनम्न कबंदुओं िे आिार पर आसानी से समझ सिते है :-
1:- ग्राफ एि non-primitive, नॉन-लीकनयर डे टा स्टर क्चर होता है ।
2:- ग्राफ एि vertex(node) िा समूह होता है । एि vertex दू सरे vertex िे साथ िुड़ा रहता है और दो
vertex िे मध्य connection िो हम edge िहते है । Edge दो nodes िे मध्य एि िम्युकनिेशन कलंि िी
तरह िाया िरता है ।
3:- ग्राफ (V,E) िा समूह होता है िहााँ V, vertex िा समूह होता है और E, Edge िा समूह होता है ।
Types of Representation of Graph
Graph representation िरने िे कलए कनम्नकलस्थखत तरीिे है
(i) Adjacency Matrix
(iii) Adjacency List Representation
Adjacency Matrix:-
इस प्रिार िे Representation मे vertex िा दू सरी से संबंि (relation) िो एि matrix िे द्वारा प्रस्तुत
िरते है
कनम्नां कित कचत्र मे दशाा ये गए graph िा adjacency matrix representation कचत्र िे नीचे दशाा या गया है ।
यहां हमने उन vertices िे नीचे 1 कलखा है िो सामने कलखी vertex िी adjacent है । शेष िे नीचे 0 कलखा
है ।
िैसे V1 ,V2 और V4 िी adjacent है हमने V2 और V4 िे नीचे 1 कलखा है और V1 और V3 िे कलए 0
कलखा है |
Adjacency list representation
कनम्न कचत्र मे दशाा ये गये graph िे कलए हम adjacency list प्रस्तुतीिरण िरें गे । इसिे कलए सवाप्रथम एि
table बनाएाँ गे ,
किसमे प्रत्येि node िी adjacent nodes, उसिे सामने कलखी हो .
अब इस table िो कनम्न रूप मे list मे पररवकतात िरें गे ।
यहां पर किस node िी adjacent nodes बताई गई है उसमे दो pointer प्रयुक्त किए है
पहला pointer तो अगली node िो दशाा रहा है और दू सरा pointer adjacent node िो.
What is Graph Traversal
Graph Traversal िा अथा है – graph िी प्रत्येि node िो visit िरना ।
एि graph िी vertices िो visit िरने िे अनेि तरीिे हो सिते है
िो दो तरीिे हम आपिो बताने िा रहे है
वो भूत ही प्रचुर मात्रा मे प्रयोग किए िाते है
और ये तरीिे traversal िे अत्यंत सरल तरीिे कसद्ध हुये है
ये कनम्नकलस्थखत है ।
types of Graph Traversal
(1) Breadth First Search (BFS)
(2) Depth First Search (DFS)
BFS(Breadth First Search)-
िैसा िी नाम से प्रतीत होता है िी इस तरीिे मे nodes िो चौड़ाई से visit िरना है ।
BFS मे पहले हम start vertex िी समस्त vertices िो visit िरते है
और कफर इनिी सभी unvisited vertex िो visit िरते है ।
इसी प्रिार आगे ति समस्त vertices िो visit किया िाता है ।
BFS िे कलए कनम्नकलस्थखत algorithm िो ध्यान मे रखते है –
status 1 = ready
status 2 = waiting
status 3 = process
Step1- graph िी सभी node िो visit िे कलए तैयार रखे । (Status 1)
Step2- start vertex A िो Queue मे डालिर इसिा status waiting िरे । (Status 2)
Step3- step 4 से step5 ति दोहराये िब ति queue खाली न हो िाये ।
Step4- queue िी पहली node n िो queue से remove िरिे n िो process िर दें । (Status 3)
Step5- queue िे अंत मे n िे सभी adjacent vertices िो िी ready – state मे है िो डाल दें
। (Status 1)
Step 6- step3 िा loop खत्म हुआ।
Step7- Exit
इस पूरे process िो समझने िे कलए कन्नचे कदये गए उदाहरण िा अध्ययन िरे ।
इस कचत्र िे कलए पहले adjacent table बना लेते है –
अब हम किसी भी vertex िो start vertex बनािर िाया शुरू िर सिते है
इस उदाहरण मे हमने B िो start vertex माना है । अब हमे इसे queue मे डालना है ।
इस queue िे दो भाग है – पहला भाग तो सािारण queue कहय किसमे हम vertex िी adjacent vertices
डालें गे ,
िबकि दू सरे भाग मे यह entry िी गई है िी वे किसिी adjacent vertices है िो िो vertices िो queue
मे डालना है ।
अब इन vertices िी unvisited adjacent vertices िो visit िरना है
िैसे िी हम C िी बची adjacent verticesिो queue मे डालेंगे ।
अब हमे D िी बची adjacent vertices िो भी queue मे डालना है ।
अब िैसे िी हम दे ख सिते है िी graph िी समस्त nodes visit हो चुिी है ।
अतः graph िा traversal सम्पन्न हो चु िा है
और queue िे नीचे कलखी list ही कदये गए graph िा BFS है ।
एि graph BFS कभन्न हो सिता है । चूंकि यह start vertex पर कनभार िरता है
DFS(Depth First Search) –
इसिे नाम से ही प्रतीत हो रहा है िी यह तरीिा graph िो गहराई से visit िरता है ।
DFS मे हम सबसे पहले start vertex िो stack िे द्वारा visit िरते है
कफर इसिी समस्त adjacent vertices िो stack मे डालिर stack िे top पर स्थित कक्रया तब ति
दोहराते है
िब ति िी stack खाली न हो िाए ।
DFS िरने िे कलए अग्रकलस्थखत algorithm िो ध्यान मे रखते है
status 1 = ready
status 2 = waiting
status 3 = process
step1 graph िी सभी node िो visit िे कलए तैयार रखे । (Status 1)
step2 start vertex िो stack मे डालिर इसिा status waiting िर दे । (Status 2)
step3 step 3 से 5 तब ति दोहराए िब ति िी stack खाली न हो िाए.
step4 steck िे top मे से node िो कनिालिर उसे process िरे . (Status 3)
step5 n िी adjacent vertices िो stack मे डालिर उनिा status 1 से 2 िरे .
step6 step3 िा loop खत्म हुआ ।
step7 exit.
इस पूरे process िो समझने िे कलए नीचे कदये गए उदाहरण िा अध्ययन िरे
अब start vertex िो stack मे डाल दे
अब सबसे पहले visited node िो stack िे नीचे कलख ले ।
visited node िी समस्त adjacent node िो stack मे डाल दे ।
अब पुनः top of the stack िो stack से बाहर कनिालिर इसिी adjacent िो stack मे डाल दें
और इसी प्रिार ति आगे िरते रकहए िब ति stack खाली न हो िाए
अब िैसे िी हम दे ख सिते है िी stack पूरा खाली हो चुिा है अतः DFS सम्पन्न हो चु िा है
खाली stack िे नीचे list ही graph िा DSF है
एि graph DSF भी कभन्न हो सिता है चूंकि यह भी start vertex पर कनभार िरता है
Applications of Graph Data Structure
Computer Science
Graphs are used to model many problems and solutions in computer science, such as
representing networks, web pages, and social media connections. Graph algorithms are used in
pathfinding, data compression, and scheduling.
Social Networks
Graphs represent and analyze social networks, such as the connections between individuals and
groups.
Transportation
Graphs can be used to model transportation systems, such as roads and flights, and to find the
shortest or quickest routes between locations.
Computer Vision
Graphs represent and analyze images and videos, such as tracking objects and detecting edges.
Natural Language Processing
Graphs can represent and analyze text, such as in syntactic and semantic dependency graphs.
Telecommunication
Graphs are used to model telecommunication networks, such as telephone and computer
networks, and to analyze traffic and routing.
Circuit Design
Graphs are used in the design of electronic circuits, such as logic circuits and circuit diagrams.
Bioinformatics
Graphs model and analyze biological data, such as protein-protein interaction and genetic
networks.
Operations research
Graphs are used to model and analyze complex systems in operations research, such as
transportation systems, logistics networks, and supply chain management.
Artificial Intelligence
Graphs are used to model and analyze data in many AI applications, such as machine learning,
Artificial Intelligence, and natural language processing.
Sorting – सॉनटिं ग क्या है?
डे टा स्टर क्चर में sorting वह प्रकक्रया है किसिे द्वारा हम डे टा िो एि logical order में arrange (क्रमबद्ध)
िरते है . यह लॉकििल ऑडा र ascending ऑडा र भी हो सिता है या descending ऑडा र भी हो सिता है .
ascending िा अथा होता है कि बढ़ते क्रम में और descending िा अथा होता है घटते क्रम में.
Sorting िा संबंि ढू ं ढने (searching) से है , हमारी अपनी किंदगी में बहुत सी चीिें होती है किन्हें हम ढू ं ढते है
िैसे:- google में िोई टॉकपि, किताब में िोई पेि, कडक्शनरी में िोई शब्, किसी परीक्षा में रोल नंबर तथा
हमारे मोबाइल िे contacts no. आकद.
तो ये सभी चीिें िो होती है वह sorted (arrange) होती है किससे हम आसानी से उन्हें ढू ं ढ ले ते है .
Types of sorting (सॉकटिं ग िे प्रिार)
यह दो प्रिार िी होती है :-
1:- internal सॉकटिं ग
2:- external सॉकटिं ग
1:- internal sorting:- इस सॉकटिं ग में sort किये िाने वाला सभी डे टा main memory में ही रहता है . internal
sorting िे प्रिार कनम्नकलस्थखत है :-
1:- bubble sort
2:- insertion sort
3:- quick sort
4:- heap sort
5:- selection sort
2:- external sorting:- इस सॉकटिं ग में sort किये िाने वाला डे टा secondary memory में रहता है . क्यूं कि डे टा
इतना ज्यादा होता है कि वह main memory में नहीं आ पाता. external सॉकटिं ग िा एि ही प्रिार होता है वह है
merge sort
what is selection sort
selection sort बहुत ही सरल तिनीि है . इस सॉकटिं ग algorithm में सबसे पहले array में से सबसे छोटे
element िो select किया िाता है तथा इस element िो array में िो पहले िान पर element होता है उसिे
साथ बदल कदया िाता है . इसिे बाद िो दू सरा अगला छोटा element होता है उसे select किया िाता है तथा
उसे array में दू सरे िान वाले element िे साथ बदल कदया िाता है और यह तब ति चलता रहता है िब ति
कि पूरी array sort नहीं हो िाती है .
selection sort िी case complexity:- O(n^2) है . िहााँ n, elements िी सं ख्या है .
इस sorting तिनीि में कलस्ट दो भागों में कवभाकित िी िाती है .
पहला भाग sorted भाग होता है किसिो बाएं तरफ कलखा िाता है .
दू सरा भाग unsorted भाग होता है किसे दायें तरफ कलखा िाता है .
selection sort algorithm
इस algorithm में कनम्नकलस्थखत steps होते है :-
step 1:- कलस्ट में सभी unsorted elements िो compare किया िाता है तथा सबसे छोटे element िो select
किया िाता है उसे कलस्ट िे पहले element िे साथ बदल कदया िाता है .
step 2:- दू सरे सबसे छोटे element िो select किया िाता है उसे दू सरे कलस्ट िे दू सरे element िे साथ बदल
कदया िाता है .
step 3:- तीसरे सबसे छोटे element िो select किया िाता है use तीसरे element िे साथ बदल कदया िाता है .
step 4:- इस प्रिार यह क्रम चलते रहता है िब ति कि पूरी array कलस्ट sort ना हो िाएाँ .
इस algorithm िो selection sort इसकलए िहा िाता है क्योंकि इसमें लगातार अगले छोटे element िो select
किया िाता है और उसे बदल (swap) कदया िाता है .
Insertion sort in hindi:-
insertion sort भी एि सरल sorting तिनीि है तथा यह छोटे डे टा सेट्स िे कलए सबसे उपयु क्त है . परन्तु
यह बड़े डे टा सेट्स िे कलए उपयुक्त नहीं है .
इस तिनीि में हम एि element िो pick िरते है और उसे उसिे appropriate िान पर insert िर
दे ते है .
इसिी औसत case complexity:- О(n2) होती है . िहााँ n, elements िी संख्या है .
insertion sort एि तेि सॉकटिं ग algorithm नहीं है क्योंकि यह nested loops िा प्रयोग elements िो
अपनी िगह में कशफ्ट िरने में िरती है . परन्तु यह bubble sort तथा selection sort से अच्छी सॉकटिं ग
तिनीि है क्योंकि insertion sort िी complexity इन दोनों से िम है .
अगर हमारे पास n elements है तो हमें उसे sort िरने िे कलए (n-1) pass िी आवश्यिता होगी.
किस प्रिार हम ताश िे पत्ों िो क्रम में arrange िरते है उसी प्रिार यह सॉकटिं ग भी िाया िरती है .
insertion sort algorithm
INSERTION_SORT (Algorithm)
1. FOR j ← 2 TO length[A]
2. DO key ← A[ j]
3. {Put A[ j] into the sorted sequence A[1 . . j − 1]}
4. i←j−1
5. WHILE i > 0 and A[i] > key
6. DO A[i +1] ← A[i]
7. i←i−1
8. A[i + 1] ← key
Bubble Sort
यह अत्यकिि िाम में आने वाली सबसे सािारण तिनीि है । इसमें किसी भी Array िे प्रथम मान िी तुलना
Array िे दू सरे मान से िरते हैं । यकद Array िा दू सरा मान प्रथम मान से बडा है , तो आरोही क्रम में िमाने
िे कलये दू सरे Data िो प्रथम िान पर रख कदया िाता है व प्रथम िान िे Data िो दू सरे िान पर।
कफर Array िे दू सरे मान िी तुलना तीसरे मान से िरते हैं और यकद तीसरा मान दू सरे मान से बडा है तो
तीसरे मान िी िगह दू सरा मान व दू सरे मान िी िगह तीसरा मान रख कदया िाता है । यकद दू सरा मान तीसरे
मान से बडा नहीं है तो Array िे दू सरे व तीसरे मानों िे िान में िोई पररवतान नहीं किया िाता है ।
मानों िे िान पररवतान िा ये क्रम तब ति चलाया िाता है , िब ति कि सारे मान आरोही क्रम में व्यवस्थित
ना हो िाए। ये क्रम N-1 बार चलाया िाता है , िहां N Array िे िुल मानों िी संख्या है ।
Bubble Sort िा Algorithm कनम्नानुसार है -
Here LArray[N] is an Array with N Elements. This Algorithm SORTS the Data Items of the
Array
START
REPEATE FOR I = 1 To N – 1 STEP I = I + 1 [ Outer Loop]
REPEATE FOR J = 1 To N – I STEP J = J + 1 [ Inner Loop ]
IF LArray[ J ] > LArray[ J + 1 ]
LArray[ J ] = LArray[ J + 1 ] [ Interchange Data Items ]
[ End of Inner Loop ]
[ End of Outer Loop ]
End
merge sort and example
merge sort िो है वह divide & conquer तिनीि िा प्रयोग िरता है . divide & conquer तिनीि िो
िॉन नयूमन्न ने 1945 में प्रस्ताकवत किया था.
divide & conquer एि ऐसी तिनीि है किसमें डे टा िी एि comlex (िकिन) list िो sub-list में
कवभाकित िर कलया िाता है और इस प्रिार कलस्ट िो तब ति कवभाकित किया िाता है िब ति कि कलस्ट में
िेवल एि element बचें.
इसिे बाद इन sub-lists िो sort िरिे combine िर कदया िाता है .
merge sort िो two way sort भी िहते है .
merge sort िी time complexity O( n log n) होती है किस िारण merge sort िो बहुत अच्छी
algorithm समझा िाता है .
merge sort example
इस sort िो समझने िे कलए हम कनम्नकलस्थखत उदाहरण लेते है :-
यह एि unsorted array है :-
िैसा कि हम िानते है कि merge sort में सबसे पहले पूरे array िो आिे भाग में कवभाकित किया िाता है .
हमारे पास इस array में 8 elements है तथा इस array िो दो भागों में कवभाकित किया िाता है किसमें कि 4
– 4 elements होंगे.
कफर इन दो arrays िो भी दो भागों में कवभाकित किया िाता है .
इसिे बाद हम इन arrays िो भी कवभाकित िर दे ते है . इसिे बाद कलस्ट में िेवल एि elements बचेगा.
किसे कवभाकित नहीं किया िा सिता है .
अब हम इन सब िो उसी प्रिार combine िरें गे किस प्रिार िी ये कवभाकित हुए थे .
सबसे पहले हम elements िो प्रत्येि कलस्ट िे कलए compare िरते है तथा उसिे बाद इन्हें sort िरिे
combine िर कदया िाता है .
सबसे पहले 15 और 34 िो compare िरते है परन्तु ये तो पहले से ही sorted है . 30 और 12 िो
compare किया िाता है क्योंकि 30, 12 से बड़ा है इसकलए 12 िो 30 से पहले कलखेंगे. 38 और 21 िो
compare िरें गे इसमें 21 िो 38 से पहले कलखें गे. 43 और 50 पहले से sorted है .
अब हम दो elements वाली कलस्ट िो compare िरें गे .
इसिे बाद अंत में इन दो कलस्ट िो compare किया िाता है :-
merge sort algorithm in hindi:-
इस sort िी algorithm कनम्नकलस्थखत है :-
MERGE-SORT (A, p, r)
1:- IF p<r
2:- THEN q = FLOOR [(p+q)/2] ///divide step
3:- MERGE (A, p, q) ///conquer step
4:- MERGE (A, q+1, r) ///conquer step
5:- MERGE (A, p, q, r) ///conquer step
Radix Sort
Radix sort एि integer sorting algorithm है िो अलग-अलग अंिों (digits) िी keys िी grouping
िरिे integer keys िे साथ डे टा िो sort िरता है । Radix sort, numbers िी array िो sort िरने िे
कलए counting sort िा प्रयोग subroutine िी तरह िरता है .
दू सरे शब्ों में िहें तो, “Radix sort एि sorting तिनीि है किसमें सबसे पहले समान place value िे
अलग-अलग digits िी grouping िरिे elements िो sort किया िाता है . कफर उसिे बाद elements
िो उनिे घटते या बढ़ते क्रम िे अनुसार sort किया िाता है .”
Radix sort algorithm
Algorithm: Radix-Sort (list, n)
shift = 1
for loop = 1 to keysize do
for entry = 1 to n do
bucketnumber = (list[entry].key / shift) mod 10
append (bucket[bucketnumber], list[entry])
list = combinebuckets()
shift = shift * 10
quick sort
quick sort भी merge sort िी तरह एि divide & conquer अल्गोररथम पर आिाररत सॉकटिं ग
तिनीि है .
इसे 1960 में Tony Hoare द्वारा कविकसत किया गया था.
इस सॉकटिं ग तिनीि में arrays िे elements िो दो छोटे arrays में कवभाकित किया िाता है .
quick sort िो है वह InPlace सॉकटिं ग िा एि प्रिार है .
इस सॉकटिं ग में, सबसे पहले कलस्ट में से किसी भी element िो select किया िाता है किसे हम pivot िहते
है .
pivot से छोटे elements इसिे बाएं तरफ रहें गें. िबकि pivot से बड़े elements इसिे दायीं तरफ रहें गे.
quick sort िी औसत complexity:- O (n log n) है .
तथा इसिी worst case complexity:- O (n^2) है िहााँ n, elements िी संख्या है .
क्योंकि worst case में भी quick sort िी complexity िम होती है इसकलए यह बहुत तेि तथा efficient
है .
quick sort algorithm
इस सॉकटिं ग िी algorithm कनम्नकलस्थखत है .
step1:- array कलस्ट में एि element िो select िरते है किसे हम pivot वैल्यू िहते है .
step2:- elements िो इस प्रिार दू बारा arrange िरते है कि वे सभी elements िो pivot वैल्यू से छोटी
है वे arrays िे बायीं तरफ रहती है और वे सभी elements िो pivot वैल्यू से बड़ी होती है उन्हें array िे
दायीं तरफ रखा िाता है . और वह element िो pivot िे सामान होते है उन्हें array में किसी भी तरफ रखा
िा सिता है .
step3:- array िे दोनों भागों िो सॉटा किया िाता है . दोनों भागों िो दु बारा quick sort algorithm िा
प्रयोग िरिे सॉटा किया िाता है .
Quick sort example:-
Data structure searching
Searching (सनचिंग):- िैसा कि आपिो पता ही होगा सकचिंग िा अथा है “ढू ं ढना” या
“खोिना” .
डे टा स्टर क्चर में ‘searching’ वह प्रकक्रया है किसमें किसी element िो कलस्ट में खोिा
िाता है िो कि एि या एि से अकिि condition िो संतुष्ट िरता हो.
types of searching
डे टा स्टर क्चर में searching िे कलए हम दो तिनीिों िा प्रयोग िरते हैं िो कि कनम्नकलस्थखत
हैं :-
1:– linear search (लीकनयर सचा)
2:- binary search (बाइनरी सचा)
1:- Linear search
इसिो sequential search भी िहते है .
इस searching तिनीि में कदए गये डे टा element िो तब ति एि एि िरिे कलस्ट िे
प्रत्येि element िे साथ compare किया िाता है िब ति कि element कमल नहीं िाता.
इसमें सबसे पहले कदए गये element िो कलस्ट िे प्रथम element िे साथ compare किया
िाता है यकद दोनों element एि समान है तो वह index value ररटना िरता है नहीं तो -
1 ररटना िरता है .
कफर इसिे बाद कदए गये element िो कलस्ट िे दु सरे element िे साथ compare किया
िाता है . यकद दोनों element समान है तो वह index value ररटना िरता है नहीं तो -1
ररटना िरता है .
इसी प्रिार पूरी कलस्ट िो compare किया िाता है िब ति कि element कमल नहीं िाता
है . अगर पूरी कलस्ट compare िरने िे बाद भी element नहीं कमलता है तो सचा
unsuccessful हो िाएगा.
यह सबसे सरल searching तिनीि है परन्तु इसमें समय बहुत लगता है . क्योंकि linear
search िी औसत case complexity O(n) है .
linear search algorithm:-
step1 i=1 {i=0}
step2 if i>n, go to step 7
step3 if A[i]=x, go to step 6
step4 i=i+1
step5 go to step 2
step6 return i
step7 return -1
step8 exit
उदाहरण िे द्वारा हम इसे आसानी से समझ सिते है .
माना कि हमारे पास कनम्नकलस्थखत array कलस्ट है .
21 70 15 30 56 78 80
और हमें इसमें 30 िो खोिना है .
step1:- कदए गये element (30) िो कलस्ट िे प्रथम element (21) िे साथ
compare (तुलना) किया िाता है .
21 70 15 30 56 78 80
दोनों एिसमान नहीं है तो हम दु सरे element में िायेंगे.
step2:- 30 िी कलस्ट िे दु सरे element (70) िे साथ तु लना िरें गे
21 70 15 30 56 78 80
दोनों एिसमान नहीं है तो हम अगले element में िायेंगे.
step3:- 30 िी तु लना 15 िे साथ िरें गे.
21 70 15 30 56 78 80
दोनों एि समान नहीं है तो हम अगले element में िायेंगे.
step4:- अब हम कदए गये element (30) िी तुलना अगले element 30 िे साथ
िरें गे .
21 70 15 30 56 78 80
दोनों एि समान है तो हम तुलना िरना बंद िर दें गे और index 3 ररटना िरें गे.
Binary search
िब िोई बड़ा डाटा स्टर क्चर होता है तो linear search में बहुत अकिि समय लग िाता
है . इसकलए linear search िी िमी िो दू र िरने िे कलए binary search िो
कविकसत किया गया.
binary search बहुत ही तेि searching अल्गोररथम है किसिी time complexity
O(log n) है . यह divide & conquer कसद्धां त पर आिाररत है .
binary search िेवल उसी कलस्ट में िी िा सिती है िो कि sorted (क्रमानुसार) हों.
इसिा प्रयोग ऐसी कलस्ट में नहीं िर सिते िो कि sorted order में नहीं है .
इस सकचिंग तिनीि में कदए गये element िी कलस्ट िे middle element िे साथ तुलना
िी िाती है . यकद दोनों एिसमान है तो वह index value ररटना िरता है .
यकद एि समान नहीं है तो हम check िरते है कि कदया गया element िो है वह middle
element से बड़ा है या छोटा.
यकद वह छोटा है तो हम कलस्ट िे छोटे भाग में यही प्रकक्रया दोहराएं गे .
और यकद वह बड़ा है तो हम कलस्ट िे बड़े भाग में यही प्रकक्रया दोहराएं गे . और यह तब ति
िरें गे िब ति कि element कमल नहीं िाता.
Binary search algorithm
उदाहरण:- इसिो हम भलीभां कत उदाहरण िे द्वारा समझ सिते है .
माना हमारे पास कनम्नकलस्थखत array कलस्ट है .
3 5 11 17 25 30 32
हमें कदया गया element 5 है किसे हमने कलस्ट में ढू ं ढना है .
step1:– सबसे पहले हम कदए गये element 5 िी तुलना middle element 17 से
िरते है .
3 5 11 17 25 30 32
दोनों एिसमान नहीं है और 5 िो है वह 17 से छोटा है .
तो हम कलस्ट िे बाएं वाले भाग (छोटे वाले भाग) में ही search िरें गे.
3 5 11
step2:– कदए गये element 5 िो middle element 5 िे साथ compare िरें गे.
3 5 11
दोनों एिसमान है तो हम तुलना िरना बंद िर दें गे. और index 1 ररटना िरें गे.
IMPORTANT TOPICS
• polish notation-conversion
• Tower of Hanoi problem
• implementation of queue and circular queue
• operations of singly linked list (Only algorithm)-searching, traversing, inserting and deleting.
• Difference between push and pop
• Types of binary tree
• Terminologies of graph
• C program for selection sort, Insertion sort, Bubble sort.
• C program for linear search and binary search