Computer Fundamental
Introduction to Data Structure
Data Structure is a technique or method of study how the data are interrelated to each other logically or mathematically
Data Structure হলো এমন একটি পদ্ধতি বা কৌশল, যার মাধ্যমে logical বা mathematical ভাবে পরস্পর সম্পর্কযুক্ত ডাটা গুলো নিয়ে অধ্যয়ন করা হয়।

1. Linear Data Structure
Linear data structures arrange data elements sequentially, where each element is connected to its previous and next adjacent elements.
Example: Array, Stack, Queue, Linked List, etc.
2. Static Data Structure
In static data structures, the memory size is fixed at compile time, and elements can be accessed directly using indices.
Example: Array.
3. Dynamic Data Structure
In dynamic data structures, the memory size flexibly expands or shrinks at runtime. Memory is used efficiently, but resizing may cause some overhead.
Example: Queue, Stack, etc.
4. Non-Linear Data Structure
Non-linear data structures do not arrange elements sequentially. Elements are organized hierarchically or in networks, allowing multiple connections. Traversing all elements in a non-linear data structure is not possible in a single pass.
Example: Trees, Graphs.
১. লিনিয়ার ডাটা স্ট্রাকচার
এখানে ডাটা উপাদানগুলো ধারাবাহিকভাবে সাজানো থাকে, যেখানে প্রতিটি উপাদান তার আগের এবং পরের উপাদানের সাথে সংযুক্ত থাকে।
উদাহরণ: অ্যারে, স্ট্যাক, কিউ, লিংকড লিস্ট ইত্যাদি।
২. স্ট্যাটিক ডাটা স্ট্রাকচার
স্ট্যাটিক ডাটা স্ট্রাকচারে মেমোরির আকার কম্পাইল টাইমে নির্ধারিত হয়ে যায় এবং ইন্ডেক্স ব্যবহার করে সরাসরি উপাদানগুলো অ্যাক্সেস করা যায়।
উদাহরণ: অ্যারে।
৩. ডায়নামিক ডাটা স্ট্রাকচার
ডায়নামিক ডাটা স্ট্রাকচারে মেমোরির আকার রানটাইমে বাড়ানো বা কমানো যায়। এতে মেমোরি কার্যকরভাবে(efficiently) ব্যবহার করা হয়, তবে মেমোরি রিসাইজ করার সময় কিছু ওভারহেড হতে পারে।
উদাহরণ: কিউ, স্ট্যাক ইত্যাদি।
৪. নন-লিনিয়ার ডাটা স্ট্রাকচার
নন-লিনিয়ার ডাটা স্ট্রাকচারে উপাদানগুলো ধারাবাহিকভাবে সাজানো থাকে না। এখানে উপাদানগুলো স্তরভিত্তিক(hierarchical) বা নেটওয়ার্ক আকারে সংযুক্ত থাকে, যেখানে একাধিক কানেকশন সম্ভব। নন-লিনিয়ার ডাটা স্ট্রাকচারের সব উপাদান একবারে ট্রাভার্স করা সম্ভব নয়।
উদাহরণ: ট্রি, গ্রাফ।
Data Structure Operation
1. Insertion:Adding a new element or node into the data structure.
2. Deletion:Removing an element or node from the data structure.
3. Search: Finding an element or node within the data structure.
4. Update: Modifying an existing element or node in the data structure.
5. Traversal: Visiting every element or node in the data structure in a specific order.
6. Merge: Combining two or more data structures into one.
7. Access: Retrieving an element or node from the data structure by its position or key.
১. Insertion: Data Structure এ একটি নতুন উপাদান বা node যোগ করা।
২. Deletion: Data Structure থেকে একটি উপাদান বা node মুছে ফেলা।
৩. Search: Data Structure এ একটি উপাদান বা node খুঁজে বের করা।
৪. Update: Data Structure এ একটি বিদ্যমান উপাদান বা node সংশোধন করা।
৫. Traversal: Data Structure এ প্রতিটি উপাদান বা node একটি নির্দিষ্ট অর্ডারে পরিদর্শন করা।
৬. Merge:দুটি বা ততোধিক Data Structure একত্রিত করা।
৭. Access: Data Structure এর একটি উপাদান বা node তার অবস্থান বা key দ্বারা পুনরুদ্ধার করা।
Learning Data Structures and Algorithms (DSA) is crucial for efficient problem-solving, job interviews at top tech companies, improving coding skills, solving real-world problems, gaining a competitive edge in programming contests, adapting to new technologies, and enhancing decision-making skills. Mastering DSA allows you to write efficient, cleaner code, solve complex problems, and stay competitive in the fast-evolving tech industry.
Data Structures এবং Algorithms শেখার গুরুত্ব কী?
ডাটা স্ট্রাকচার ও অ্যালগরিদম (DSA) শেখা প্রতিটি প্রোগ্রামারের জন্য অপরিহার্য, কারণ এটি জটিল সমস্যা সমাধানের দক্ষতা বাড়ায়, গুগল-মাইক্রোসফটের মতো টপ টেক কোম্পানির ইন্টারভিউতে সাফল্য এনে দেয়, কোডিং দক্ষতা উন্নত করে, বাস্তব জীবনের চ্যালেঞ্জিং সমস্যা সমাধানে সাহায্য করে, ACM ICPC বা Google Code Jam-এর মতো প্রোগ্রামিং প্রতিযোগিতায় এগিয়ে রাখে, নতুন প্রযুক্তি শিখতে গতি দেয় এবং সিদ্ধান্ত গ্রহণের দক্ষতা বাড়িয়ে যৌক্তিক চিন্তার দক্ষতা উন্নত করে।
- Operating Systems: For managing hardware resources and running applications smoothly.
- Database Systems: To store, retrieve, and manage data efficiently.
- Web Applications: For handling user requests and data processing.
- Machine Learning: To process large datasets and train models effectively.
- Video Games: For game logic, graphics rendering, and real-time user interaction.
- Cryptography: To secure data through complex encryption algorithms.
- Data Analysis: For sorting and interpreting large amounts of information.
- Search Engines: To crawl websites and deliver relevant search results quickly.
- Social Networks: Platforms like Facebook use data structures to model connections between friends.
- Navigation Systems: GPS apps use algorithms to find the shortest path from one location to another.
- E-Commerce: Online shopping sites use algorithms for product recommendations based on user behavior and preferences.
Data Structure এর ব্যবহার
Data Structures এবং Algorithms (DSA) সফটওয়্যার ডেভেলপমেন্টের প্রায় প্রতিটি ক্ষেত্রে মৌলিক ভূমিকা পালন করে:
- Operating Systems: হার্ডওয়্যার রিসোর্স পরিচালনা এবং অ্যাপ্লিকেশন সঞ্চালন নির্বিঘ্নে করার জন্য।
- Database Systems: Data দক্ষতার সাথে সংরক্ষণ, পুনরুদ্ধার, এবং পরিচালনা করার জন্য।
- Web Applications: ব্যবহারকারীর অনুরোধ এবং Data প্রক্রিয়াকরণ করার জন্য।
- Machine Learning: বড় Data সেট প্রক্রিয়া এবং মডেল প্রশিক্ষণ কার্যকরভাবে করার জন্য।
- Video Games: গেমের Logic, গ্রাফিক্স Rendering এবং রিয়েল-টাইম ব্যবহারকারীর ইন্টারঅ্যাকশন জন্য।
- Cryptography: Data সুরক্ষিত করার জন্য জটিল Encryption অ্যালগরিদমের মাধ্যমে।
- Data Analysis: বিশাল পরিমাণের Data বিশ্লেষণ এবং ব্যাখ্যা করার জন্য।
- Search Engines: ওয়েবসাইট ক্রল করা এবং দ্রুত প্রাসঙ্গিক Search ফলাফল সরবরাহ করার জন্য।
- Social Networks: ফেসবুকের মতো প্ল্যাটফর্মগুলি Data Structures ব্যবহার করে বন্ধুদের মধ্যে সংযোগ মডেল করতে।
- Navigation Systems: GPS অ্যাপস ব্যবহৃত সেরা পথ খুঁজে বের করার জন্য।
- E-Commerce: অনলাইন শপিং সাইটগুলি ব্যবহারকারীর আচরণ এবং পছন্দের ভিত্তিতে পণ্য সুপারিশ করার জন্য অ্যালগরিদম ব্যবহার করে।
The characteristics of a data structure include:
Correctness: This means the data structure works properly and gives the right results. It handles all situations correctly.
Time Complexity: This shows how fast the data structure can do its tasks. It tells us how much longer it takes when the data gets bigger.
Space Complexity: This means how much memory the data structure uses. Good data structures use less memory, which is important when memory is limited or the data is very large.
When choosing a data structure, it’s important to balance these features. A good data structure should be fast, accurate, and able to work well in different situations.
একটি Data Structure-এর বৈশিষ্ট্যগুলো হলো —
Correctness: Data Structure সব ধরনের পরিস্থিতিতে সঠিকভাবে কাজ করবে এবং সঠিক ফলাফল প্রদান করবে।
Time Complexity: Data Structure কত দ্রুত তার কাজ সম্পন্ন করতে পারে তা Time Complexity দ্বারা বোঝা যায়। Data-এর পরিমাণ বাড়তে থাকলে কোড execute হতে কত সময় লাগবে, সেটিও এটি নির্দেশ করে।
Space Complexity: একটি প্রোগ্রামে কতটা memory ব্যবহার হয় তা Space Complexity দ্বারা বোঝা যায়। ভালো Data Structure সাধারণত কম memory ব্যবহার করে, যা limited memory সিস্টেমে অনেক গুরুত্বপূর্ণ।
সঠিক Data Structure নির্বাচন করার সময় এই বৈশিষ্ট্যগুলো — Correctness, Time Complexity, এবং Space Complexity — এর মধ্যে ভারসাম্য রাখা প্রয়োজন। একটি ভালো Data Structure হওয়া উচিত দ্রুত, নির্ভুল, এবং বিভিন্ন পরিস্থিতিতে দক্ষভাবে কাজ করতে সক্ষম।
What is an Algorithm?
An algorithm is a step-by-step procedure or a set of instructions that are executed in a specific order to achieve a desired output. Algorithms are generally independent of any programming language, meaning they can be implemented in more than one programming language.
Criteria of an Algorithm:
An algorithm should have the following characteristics:
Unambiguous: Each step of the algorithm must be clear and unambiguous. Every step and its inputs/outputs should have only one meaning.
Input: An algorithm should have zero or more well-defined inputs.
Output: An algorithm should produce one or more well-defined outputs, which match the desired result.
Finiteness: An algorithm must terminate after a finite number of steps.
Feasibility: The algorithm should be feasible with the available resources.
Independent: The algorithm should be independent of programming code, providing step-by-step instructions.
Algorithm কী?
একটি algorithm হলো একটি step-by-step প্রক্রিয়া বা নির্দেশাবলির সেট যা একটি নির্দিষ্ট order-এ execute করা হয় যাতে একটি desired output অর্জন করা যায়। Algorithm সাধারণত যেকোনো programming language-এ স্বাধীন, অর্থাৎ এটি একাধিক programming language-এ implement করা যেতে পারে।
Algorithm-এর Criteria:
একটি algorithm-এর নিম্নলিখিত characteristics থাকা উচিত:
Unambiguous: algorithm-এর প্রতিটি step স্পষ্ট এবং unambiguous হওয়া উচিত। প্রতিটি step এবং এর inputs/outputs-এর একটি একক meaning থাকা উচিত।
Input: একটি algorithm-এ শূন্য বা একাধিক well-defined inputs থাকা উচিত।
Output: একটি algorithm-এ এক বা একাধিক well-defined outputs থাকা উচিত, যা desired result অনুযায়ী মিলবে।
Finiteness: একটি algorithm-কে একটি finite number of steps-এর মধ্যে terminate হতে হবে।
Feasibility: algorithmটি available resources-এর সাথে feasible হওয়া উচিত।
Independent: algorithmটি programming code-এর স্বাধীন হওয়া উচিত, এবং এটি step-by-step instructions প্রদান করতে হবে।
Complexity of Algorithm
1
Previous Question on Complexity
| Exprsn | Stack | Postfix |
|---|---|---|
| ( | ||
| P | ( | P |
| = | ( | P = |
| 12 | ( | P = 12 |
| / | (/ | P = 12 |
| ( | (/ ( | P = 12 |
| 7 | (/ ( | P = 12 7 |
| – | (/ (- | P = 12 7 |
| 3 | (/ (- | P = 12 7 3 |
| ) | (/ | P = 12 7 3 – |
| + | (+ | P = 12 7 3 – / |
| 2 | (+ | P = 12 7 3 – / 2 |
| ) | P = 12 7 3 – / 2 + |
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
Array
1
Previous Question on Array
| Exprsn | Stack | Postfix |
|---|---|---|
| ( | ||
| P | ( | P |
| = | ( | P = |
| 12 | ( | P = 12 |
| / | (/ | P = 12 |
| ( | (/ ( | P = 12 |
| 7 | (/ ( | P = 12 7 |
| – | (/ (- | P = 12 7 |
| 3 | (/ (- | P = 12 7 3 |
| ) | (/ | P = 12 7 3 – |
| + | (+ | P = 12 7 3 – / |
| 2 | (+ | P = 12 7 3 – / 2 |
| ) | P = 12 7 3 – / 2 + |
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
Linked List
A linked list is a linear data structure where data are not stored sequentially in the computer memory. Instead, the elements are linked with each other using addresses (links). Each element, called a node, contains data and a reference to the next node in the list.
Types of Linked List:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
Linked list হলো একটি linear data structure যেখানে data computer memory-তে sequentialভাবে store করা হয় না। বরং প্রতিটি data element address (link)-এর মাধ্যমে একে অপরের সাথে connected থাকে। প্রতিটি element-কে node বলা হয়, যেখানে data এবং পরবর্তী node-এর address সংরক্ষিত থাকে।
Types of Linked List:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
A singly linked list is a linear collection of data items called nodes, where each node is divided into two parts.
- Data
- Link

- The first node has a special name called HEAD.
- The data part stores the data item.
- The link part stores the address of the next node.
- The last node is called the tail node.
- The linked list starts with a special pointer called the HEAD pointer and terminates with a NULL pointer.
Singly linked list হলো একটি linear collection of data items, যেগুলোকে node বলা হয়। প্রতিটি node দুইটি অংশে বিভক্ত থাকে।
- Data
- Link

- প্রথম node-টির একটি বিশেষ নাম রয়েছে, যাকে HEAD বলা হয়।
- Data part-এ data item store করা হয়।
- Link part-এ পরবর্তী node-এর address store করা হয়।
- শেষ node-টিকে tail node বলা হয়।
- Linked list একটি বিশেষ pointer দিয়ে শুরু হয় যাকে HEAD pointer বলা হয় এবং এটি NULL pointer-এ শেষ হয়।
struct node { int data; struct node *next; };
Doubly linked list is the linear collection of data item called node where each node has divided into three parts.
- previous
- data
- next

- Data part store data items.
- Next part store the address of next node
- previous part store the address of previous node.
- Doubly linked list start with special pointer called first pointer ending with last pointer.
- It allows us to perform traversing in both way “Forward & Backward”
Declaration of Doubly linked list:
struct node { int data; struct node *next; struct node *prev; }
Circular linked list is the variation of singly and doubly linked list where first node point to last node and last node point to first node
It is used when we want traversing of data no. of time without reinitialized the start pointer as well as we can visit all the nodes from any nodes.
Types of Circular linked list:
- Circular singly Linked list
- Circular doubly linked list
If the last node of singly linked list hold the address of start node then it’s called circular singly linked list.

If the last node of doubly linked list hold the address of first node and first node hold the address of last node then it’s called circular doubly linked list.

Previous Question on Linked List
| Exprsn | Stack | Postfix |
|---|---|---|
| ( | ||
| P | ( | P |
| = | ( | P = |
| 12 | ( | P = 12 |
| / | (/ | P = 12 |
| ( | (/ ( | P = 12 |
| 7 | (/ ( | P = 12 7 |
| – | (/ (- | P = 12 7 |
| 3 | (/ (- | P = 12 7 3 |
| ) | (/ | P = 12 7 3 – |
| + | (+ | P = 12 7 3 – / |
| 2 | (+ | P = 12 7 3 – / 2 |
| ) | P = 12 7 3 – / 2 + |
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
Stack
A stack is a linear data structure that operates on the LIFO (Last In, First Out) principle, meaning that the last element added to the stack is the first one to be removed. In a stack, all insertions and deletions occur at one end, called the top of the stack. The structure is named “stack” because it works like real-life stacks such as books or plates, where elements can only be added or removed from the top.
Application of Stack:
- Memory Management
- Function Call
- Expression Evaluation
- Backtracking
- Browser History
- Task Management
Stack হলো একটি linear data structure যা LIFO (Last In, First Out) principle অনুসরণ করে, অর্থাৎ stack-এ সর্বশেষ যে element যোগ করা হয়, সেটিই প্রথমে remove হয়। Stack-এ সব insertion এবং deletion একটি নির্দিষ্ট প্রান্তে ঘটে, যাকে stack-এর top বলা হয়। এই data structure-টির নাম stack রাখা হয়েছে কারণ এটি বাস্তব জীবনের stack যেমন বই বা প্লেটের stack-এর মতো কাজ করে, যেখানে শুধু top থেকেই element add বা remove করা যায়।
Application of Stack:
- Memory Management
- Function Call
- Expression Evaluation
- Backtracking
- Browser History
- Task Management
When we define a stack as an Abstract Data Type (ADT), our primary focus is on the operations that can be performed on the stack, rather than how the stack is implemented internally.
The primary operations of a stack are as follows:
push(data): Adds a new element, data, onto the top of the stack.
pop(): Removes and returns the last element that was added to the stack (the topmost element).
top(): Retrieves the last inserted element from the stack without removing it, allowing you to peek at the top element.
size(): Returns the total number of elements currently in the stack.
isEmpty(): Checks if the stack is empty. Returns TRUE if the stack contains no elements, otherwise returns FALSE.
isFull(): Checks if the stack has reached its maximum capacity. Returns TRUE if the stack is full, otherwise returns FALSE.
Algorithm Insertion to Stack : PUSH operation
1. Checks if the stack is full.
2. If the stack is full, return "overflow" and exit.
3. If the stack is not full, increments top to point next
empty space.
4. Adds data element to the stack location, where top
is pointing.
5. Returns success.pseudo-code of PUSH operation
PUSH(stack, data) if stack is full: return "Overflow" // Stack is full, cannot add more elements else: top= top + 1 // Move to the next available position in the stack stack[top] = data // Add the data element at the current top position return "Success" // Data added successfully
Algorithm Insertion to Stack : POP operation
1. Checks if the stack is empty. 2. If the stack is empty, return underflow and exit. 3. If the stack is not empty, accesses the data element at which top is pointing. 4. Decreases the value of top by 1. 5. Returns success.
pseudo-code of POP operation
POP(stack) if stack is empty: return "Underflow" // Stack is empty, cannot remove elements else: data = stack[top] // Access the data element at the current top position top = top - 1 // Move the top pointer to the previous element return "Success" // Data removed successfully
Infix expression: If operator is placed in between the operands then its called infix expression.
Infix Expression: <operand> <operator> <operand>
Example: A + B, A * B;
Prefix expression: If operator is placed before the operands then its called prefix expression.
Prefix Expression: <operator> <operand> <operand>
Example: +AB, *AB ;
Postfix expression: If operator is placed after the operands then its called postfix expression.
Postfix Expression: <operand> <operand> <operator>
Example: AB+, AB* ;
Classification of Stack Arithmetic Expression
On the basis of operand position, the stack arithmetic expression can be classified into three types:
- Infix
- Prefix
- Postfix
Precedence and Associativity
To understand how these notations work, it is essential to know two key concepts: Precedence and Associativity.
Precedence:
Precedence refers to the priority of operators, which determines which operator will operate on the operand first.
Example: a + b * c
Here, the “*” operator has higher precedence than the “+” operator.
Associativity:
Associativity defines the order in which operators of the same precedence are evaluated when they appear together in an expression.
Left-to-right associativity: The operator on the left is evaluated first.
Right-to-left associativity: The operator on the right is evaluated first.
Example: a + b – c
Here, “+” and “-” have the same precedence, so the associativity rule applies, and the operator that appears first is executed first.
Stack Arithmetic Expression-এর Classification
Operand position-এর ভিত্তিতে stack arithmetic expression-কে তিনটি ভাগে ভাগ করা যায়:
- Infix
- Prefix
- Postfix
Precedence এবং Associativity
এই notation গুলো কীভাবে কাজ করে তা বোঝার জন্য দুটি গুরুত্বপূর্ণ concept জানা প্রয়োজন: Precedence এবং Associativity।
Precedence:
Precedence বলতে operator-এর priority বোঝায়, অর্থাৎ কোন operator আগে operand-এর উপর কাজ করবে তা নির্ধারণ করে।
Example: a + b * c
এখানে “*” operator-এর precedence “+” operator-এর চেয়ে বেশি।
Associativity:
Associativity নির্ধারণ করে একই precedence-এর একাধিক operator একসাথে থাকলে কোন ক্রমে তারা evaluate হবে।
Left-to-right associativity: বাম পাশের operator আগে execute হয়।
Right-to-left associativity: ডান পাশের operator আগে execute হয়।
Example: a + b – c
এখানে “+” এবং “-” operator-এর precedence একই, তাই associativity rule অনুযায়ী যে operator আগে আসে, সেটিই আগে execute হয়।

(i) (A + B) * (B * D)
(ii) (a+b)*c
(iii)a/b+c/d
(iv)(a+b*d)*(b-c)
(i) (A + B) * (B * D)
= (AB+) * (BD*)
= AB+ BD* *
(ii) (a+b)*c
= (ab+) * c
= ab+c*
(iii) a/b+c/d
= (ab/) + (cd/)
= ab/cd/+
(iv) (a+b*d)*(b-c)
=(a+(bd*))*(bc-)
=(abd*+)*(bc-)
=abd*+bc-*
(i) (A + B) * (B * D)
(ii) (a+b)*c
(iii)a/b+c/d
(iv)(a+b*d)*(b-c)
(v) ((a+b)*c)-d
(i) (A + B) * (B * D)
= (+AB) * (*BD)
= *+AB *BD
(ii) (a+b)*c
= (+ab) * c
= *+abc
(iii) a/b+c/d
= (/ab) + (/cd)
= +/ab/cd
(iv) (a+b*d)*(b-c)
=(a+(*bd))*(-bc)
=(+a*bd)*(-bc)
=*+a*bd-bc
(v) ((a+b)*c)-d
=((+ab)*c)-d
=(*+abc)-d
= – * + abcd
(i) ABD*+BC-*
(ii) ab+cd+*
(i) ABD*+BC-*
= A (B*D)+BC-*
= (A+(B*D))BC-*
=(A+(B*D))(B-C)*
=(A+(B*D))*(B-C)
(ii) ab+cd+*
= (a+b)cd+*
= (a+b) (c+d)*
= (a+b) * (c+d)
(i) -*+abcd
(ii)/+A*BD-BC
(iii)+/ab/cd
(i) -*+abcd
= -*(a+b)cd
= -((a+b)*c)d
=((a+b)*c)-d
(ii) /+A*BD-BC
= /+A(B*D)-BC
= /(A+(B*D))-BC
= /(A+(B*D))(B-C)
= (A+(B*D))/(B-C)
(iii)+/ab/cd
=+(a/b)/cd
=+(a/b)(c/d)
=(a/b)+(c/d)
Below is the algorithm to convert an infix expression to postfix notation:
- Initialize:
- Push
"("onto the Stack. - Add
")"to the end of the expressionX.
- Push
- Scan Expression
Xfrom left to right:- Repeat Steps 3 to 6 for each element of
Xuntil the Stack is empty.
- Repeat Steps 3 to 6 for each element of
- If the character is an operand:
- Add the operand directly to
Y.
- Add the operand directly to
- If the character is a left parenthesis
"(":- Push
"("onto the Stack.
- Push
- If the character is an operator:
- Repeatedly pop operators from the Stack and add them to
Ywhile the operator at the top of the Stack has equal or higher precedence than the current operator. - Push the current operator onto the Stack.
- Repeatedly pop operators from the Stack and add them to
- If the character is a right parenthesis
")":- Repeatedly pop operators from the Stack and add them to
Yuntil a left parenthesis"("is encountered. - Remove the left parenthesis from the Stack.
- Repeatedly pop operators from the Stack and add them to
- End of Expression:
- Once the entire expression is processed, the final postfix expression
Ywill be available in the output.
- Once the entire expression is processed, the final postfix expression
Example
Infix to Postfix Conversion Stack Table
A+B/C*(D+E)-F
Adding “)” at the end of expression =A+B/C*(D+E)-F)
| Scan | Stack | Postfix Expression |
|---|---|---|
| ( | ||
| A | ( | A |
| + | (+ | A |
| B | (+ | A B |
| / | (+ / | A B |
| C | (+ / | A B C |
| * | (+ * | A B C / |
| ( | (+ * ( | A B C / |
| D | (+ * ( | A B C / D |
| + | (+ * ( + | A B C / D |
| E | (+ * ( + | A B C / D E |
| ) | (+ * | A B C / D E + |
| – | (- | A B C / D E + * |
| F | (- | A B C / D E + * + F |
| ) | A B C / D E + * + F – |
Postfix expression: A B C / D E + * + F –
Below is the algorithm to evaluate a postfix expression:
- Initialize:
- Create an empty stack.
- Scan the postfix expression from left to right:
- Repeat the following steps for each symbol in the expression.
- If the symbol is an operand (a number or a variable):
- Push the operand’s value onto the stack.
- If the symbol is an operator (+, -, *, /, ^, etc.):
- Pop the top two operands from the stack.
- Let the first popped operand be
operand1and the second popped operand beoperand2. - Perform the operation:
operand2 operator operand1. - Push the result of this operation back onto the stack.
- After scanning the entire expression:
- The final result of the evaluation will be the only value remaining on the stack.
- Pop this value and return it as the result of the expression.
Example: P = 12 7 3 – / 2 +
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
[(A+B)-{C+D}]

A + ( B * C − ( D / E $ F ) * G) * H

10 5 2 + * 12 3 / -

Previous Question on Stack
| Exprsn | Stack | Postfix |
|---|---|---|
| ( | ||
| P | ( | P |
| = | ( | P = |
| 12 | ( | P = 12 |
| / | (/ | P = 12 |
| ( | (/ ( | P = 12 |
| 7 | (/ ( | P = 12 7 |
| – | (/ (- | P = 12 7 |
| 3 | (/ (- | P = 12 7 3 |
| ) | (/ | P = 12 7 3 – |
| + | (+ | P = 12 7 3 – / |
| 2 | (+ | P = 12 7 3 – / 2 |
| ) | P = 12 7 3 – / 2 + |
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
Queue
A queue is a linear data structure that follows the FIFO (First In, First Out) principle, which means the element that is inserted first is removed first. It is an Abstract Data Type (ADT), similar to a stack, but it differs in the way elements are accessed. Unlike a stack, which is open at only one end, a queue is open at both ends: elements are inserted at one end called the rear and removed from the other end called the front.
Queue হলো একটি linear data structure যা FIFO (First In, First Out) principle অনুসরণ করে, অর্থাৎ queue-তে যে elementটি প্রথমে insert হয়, সেটিই প্রথমে remove হয়। এটি একটি Abstract Data Type (ADT), যা stack-এর মতো হলেও element access করার পদ্ধতিতে ভিন্ন। Stack যেখানে শুধু একটি প্রান্তে open থাকে, সেখানে queue দুইটি প্রান্তে open থাকে। Queue-তে element rear end থেকে insert করা হয় এবং front end থেকে remove করা হয়।
Queue operations include creating (initializing) the queue, using it to store and access data, and finally removing data permanently from memory. The basic operations of a queue are explained below:
Enqueue(): This operation is used to insert (add) an element into the queue at the rear end.
Dequeue(): This operation is used to remove (delete) an element from the queue from the front end.
Peek(): This operation returns the element present at the front of the queue without removing it.
isFull(): This operation checks whether the queue is full or not.
isNull(): This operation checks whether the queue is empty or not.
Queue operations-এর মধ্যে queue initialize করা, data store ও use করা এবং memory থেকে স্থায়ীভাবে data remove করা অন্তর্ভুক্ত। Queue-এর basic operations নিচে দেওয়া হলো:
Enqueue(): এই operation-টি queue-এর rear end-এ নতুন element insert (add) করতে ব্যবহৃত হয়।
Dequeue(): এই operation-টি queue-এর front end থেকে element remove (delete) করতে ব্যবহৃত হয়।
Peek(): এই operation-টি queue-এর front-এ থাকা element-টি delete না করে return করে।
isFull(): এই operation-টি queue পূর্ণ (full) কিনা তা check করে।
isNull(): এই operation-টি queue খালি (empty) কিনা তা check করে।
In a Queue, elements are added at the rear and removed from the front.
Front: The position of the first element in the queue. This element will be removed first (FIFO).
Rear: The position where new elements are inserted at the end of the queue.
Example:
Queue: [10, 20, 30, _ , _]
front = 0 ← element 10
rear = 2 ← element 30
Here, 10 is the front element (to be removed first) and 30 is the rear element (last inserted).
Queue Operations 1. Enqueue (Insert element):
Add an element at the rear of the queue.
Example:
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
Enqueue 10:
Queue: [ 10 , _ , _ , _ , _ ]
front = 0, rear = 0
Enqueue 20:
Queue: [ 10 , 20 , _ , _ , _ ]
front = 0, rear = 1
Enqueue 30:
Queue: [ 10 , 20 , 30 , _ , _ ]
front = 0, rear = 2
2. Dequeue (Remove element):
Remove an element from the front of the queue.
Example:
Dequeue:
Queue: [ _ , 20 , 30 , _ , _ ]
front = 1, rear = 2
3. Peek / Front:
Check the element at the front without removing it.
Front element: 20
4. isEmpty:
Check if the queue is empty (front = -1 or front > rear).
Queue empty? No
5. isFull:
Check if the queue is full (rear = size-1).
Queue full? No
Queue-তে element add করা হয় rear-এ এবং remove করা হয় front-থেকে।
Front: Queue-এর প্রথম element-এর অবস্থান। এই element প্রথমে remove হবে (FIFO).
Rear: Queue-এর শেষের position যেখানে নতুন element insert করা হয়।
উদাহরণ:
Queue: [10, 20, 30, _ , _]
front = 0 ← element 10
rear = 2 ← element 30
এখানে, 10 হলো front element (প্রথমে remove হবে) এবং 30 হলো rear element (শেষে insert হয়েছে)।
Queue-এর operations:1. Enqueue (Element insert করা):
Element insert করা হয় Queue-এর rear-এ।
উদাহরণ:
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
Enqueue 10:
Queue: [ 10 , _ , _ , _ , _ ]
front = 0, rear = 0
Enqueue 20:
Queue: [ 10 , 20 , _ , _ , _ ]
front = 0, rear = 1
Enqueue 30:
Queue: [ 10 , 20 , 30 , _ , _ ]
front = 0, rear = 2
2. Dequeue (Element remove করা):
Element remove করা হয় Queue-এর front-থেকে।
উদাহরণ:
Dequeue:
Queue: [ _ , 20 , 30 , _ , _ ]
front = 1, rear = 2
3. Peek / Front:
Front element check করা হয়, remove না করে।
Front element: 20
4. isEmpty:
Queue খালি কিনা check করা হয় (front = -1 বা front > rear)।
Queue খালি? না
5. isFull:
Queue full কিনা check করা হয় (rear = size-1)।
Queue full? না
- CPU Scheduling, Disk Scheduling
- When data is transferred asynchronously between two processes, the Queue is used for synchronization. Examples include IO Buffers, pipes, file IO, etc.
- Handling of interrupts in real-time systems.
- Call Center phone systems use Queues to hold callers in a specific order.
- CPU Scheduling, Disk Scheduling
- যখন ডেটা দুটি process-এর মধ্যে asynchronously transfer হয়, তখন synchronization-এর জন্য Queue ব্যবহার করা হয়। উদাহরণ: IO Buffers, pipes, file IO ইত্যাদি।
- Real-time systems-এ interrupts handling-এর ক্ষেত্রে Queue ব্যবহৃত হয়।
- Call Center phone systems Queue ব্যবহার করে callers-দের একটি নির্দিষ্ট ক্রমে অপেক্ষমাণ রাখে।
A Simple Queue is a linear data structure that follows the FIFO (First In First Out) principle.
The element that is inserted first is removed first.
Example:
Suppose we have an empty Queue with size 5.
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
Insert elements 10, 20, and 30 into the Queue.
Queue (array): [ 10 , 20 , 30 , _ , _ ]
front = 0, rear = 2
Now, remove one element from the Queue.
The element 10 is removed.
Queue (array): [ _ , 20 , 30 , _ , _ ]
front = 1, rear = 2
This shows that the element inserted first is removed first.
Array ব্যবহার করে Simple Queue:
Simple Queue একটি array ব্যবহার করে implement করা যায়।
এটি FIFO (First In First Out) rule অনুসরণ করে।
উদাহরণ:
ধরা যাক, আমাদের কাছে size 5-এর একটি empty Queue আছে।
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
এখন Queue-তে 10, 20 এবং 30 element insert করা হলো।
Queue (array): [ 10 , 20 , 30 , _ , _ ]
front = 0, rear = 2
এবার Queue থেকে একটি element remove করা হলো।
এখানে 10 remove হয়েছে।
Queue (array): [ _ , 20 , 30 , _ , _ ]
front = 1, rear = 2
এতে বোঝা যায় যে, যে element আগে insert হয়,
সেটিই আগে remove হয় — এটিই FIFO principle।
A Circular Queue is a type of Queue where the last position is connected back to the first position. It follows the FIFO (First In First Out) principle and efficiently uses memory.
Example:
Suppose we have a Circular Queue of size 5.
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
Insert elements 10, 20, 30, 40, and 50.
Queue (array): [ 10 , 20 , 30 , 40 , 50 ]
front = 0, rear = 4
Now remove two elements (10 and 20).
Queue (array): [ _ , _ , 30 , 40 , 50 ]
front = 2, rear = 4
Now insert two more elements: 60 and 70. Since it is a Circular Queue, insertion continues from the beginning.
Queue (array): [ 60 , 70 , 30 , 40 , 50 ]
front = 2, rear = 1
This shows how Circular Queue reuses empty spaces.Circular Queue হলো Queue-এর একটি ধরন যেখানে last position আবার first position-এর সাথে connect থাকে। এটি FIFO (First In First Out) principle অনুসরণ করে এবং memory efficientভাবে ব্যবহার করে।
উদাহরণ:
ধরা যাক, size 5-এর একটি Circular Queue আছে।
Queue (array): [ _ , _ , _ , _ , _ ]
front = -1, rear = -1
Queue-তে 10, 20, 30, 40 এবং 50 element insert করা হলো।
Queue (array): [ 10 , 20 , 30 , 40 , 50 ]
front = 0, rear = 4
এরপর Queue থেকে 10 এবং 20 element remove করা হলো।
Queue (array): [ _ , _ , 30 , 40 , 50 ]
front = 2, rear = 4
এখন আবার 60 এবং 70 element insert করা হলো। Circular Queue হওয়ায় insertion শুরু থেকে continue হয়।
Queue (array): [ 60 , 70 , 30 , 40 , 50 ]
front = 2, rear = 1
এতে বোঝা যায় যে Circular Queue empty space পুনরায় ব্যবহার করে, যা Simple Queue-এর একটি বড় limitation দূর করে।Simple Queue (Problem):
In a Simple Queue, once elements are removed, the empty spaces at the beginning of the array cannot be reused.
Queue (array): [ _ , _ , 30 , 40 , 50 ]
front = 2, rear = 4
Now if we want to add 60,70 , We cannot do it in simple queue.Even though there is free space at index 0 and 1, new elements cannot be inserted if rear reaches the end of the array. This causes wastage of memory.
Circular Queue (Solution):
Circular Queue overcomes this drawback by connecting the last position back to the first position. It reuses the empty spaces efficiently.
Queue (array): [ 60 , 70 , 30 , 40 , 50 ]
front = 2, rear = 1
Here, new elements are inserted at the beginning even though the rear had reached the end earlier.
Conclusion:
- Simple Queue wastes memory.
- Circular Queue uses memory efficiently.
Simple Queue (Drawback):
Simple Queue-তে element remove করার পর array-এর শুরুতে যে empty space তৈরি হয়, তা পুনরায় ব্যবহার করা যায় না।
Queue (array): [ _ , _ , 30 , 40 , 50 ]
front = 2, rear = 4
এখন আমরা যদি 60, 70 insert করতে চাই এখানে index 0 এবং 1 ফাঁকা থাকা সত্ত্বেও, rear যদি array-এর শেষ index-এ পৌঁছে যায়, তাহলে নতুন element insert করা যায় না। এর ফলে memory wastage হয়।
Circular Queue (Overcome Solution):
Circular Queue এই drawback overcome করে। এখানে array-এর last position আবার first position-এর সাথে connect থাকে, ফলে empty space পুনরায় ব্যবহার করা যায়।
Queue (array): [ 60 , 70 , 30 , 40 , 50 ]
front = 2, rear = 1
এখানে rear আবার শুরুতে ফিরে এসে নতুন element insert করতে পারে, যদিও আগে rear array-এর শেষ পর্যন্ত পৌঁছে গিয়েছিল।
সংক্ষেপে পার্থক্য:
- Simple Queue → memory waste করে
- Circular Queue → memory efficiently ব্যবহার করে
A Priority Queue is a type of Queue in which each element is assigned a priority.
Elements are removed based on priority, not just the order they arrive.
Higher priority elements are dequeued first.
Operations in Priority Queue:
1. Enqueue (Insert element with priority): Add an element along with its priority.
2. Dequeue: Remove the element with the highest priority from the queue.
3. Peek: View the element with the highest priority without removing it.
Example:
Suppose we have a priority queue with size 5.
Enqueue elements with priorities:
10(priority 2), 20(priority 1), 30(priority 3), 40(priority 2), 50(priority 1)
Queue (element, priority): [(10,2), (20,1), (30,3), (40,2), (50,1)]
Dequeue operation removes the element with highest priority first:
Dequeue → removes (30,3) Queue now: [(10,2), (20,1), (40,2), (50,1)]
Next Dequeue → removes next highest priority:
Dequeue → removes (10,2) Queue now: [(20,1), (40,2), (50,1)]
Key Points:
– Priority Queue can be implemented using arrays, linked lists, or heaps.
– Elements with the same priority are usually processed in FIFO order.
Priority Queue হলো এমন একটি Queue যেখানে প্রতিটি element-এর সাথে একটি priority assign করা থাকে।
Element remove করা হয় priority অনুযায়ী, arrival order অনুযায়ী নয়।
Higher priority element প্রথমে remove হয়।
Priority Queue-এর Operations:
1. Enqueue (Priority সহ element insert করা): Element এবং তার priority add করা।
2. Dequeue: Queue থেকে সর্বোচ্চ priority element remove করা।
3. Peek: সর্বোচ্চ priority element দেখার জন্য, remove না করে।
উদাহরণ:
ধরা যাক একটি Priority Queue size 5-এর।
Priority সহ element insert করা হলো:
10(priority 2), 20(priority 1), 30(priority 3), 40(priority 2), 50(priority 1)
Queue (element, priority): [(10,2), (20,1), (30,3), (40,2), (50,1)]
Dequeue করলে সর্বোচ্চ priority element remove হয়:
Dequeue → removes (30,3) Queue now: [(10,2), (20,1), (40,2), (50,1)]
পরবর্তী Dequeue → পরবর্তী সর্বোচ্চ priority remove:
Dequeue → removes (10,2) Queue now: [(20,1), (40,2), (50,1)]
মূল বিষয়:
– Priority Queue array, linked list বা heap ব্যবহার করে implement করা যায়।
– সমান priority-element সাধারণত FIFO order-এ process করা হয়।
A Double Ended Queue (Deque) is a special type of queue where elements can be inserted and removed from both ends: from the front and from the rear.
Operations in Deque:
1. Enqueue (Insert an element): Add an element either at the front or rear of the deque.
2. Dequeue (Remove an element): Remove an element either from the front or rear of the deque.
3. Peek Front: View the front element without removing it.
4. Peek Rear: View the rear element without removing it.
Example 1: Enqueue at Rear
Initially, the deque is empty with a size of 5.
Deque: [ _ , _ , _ , _ , _ ]
Front = -1, Rear = -1
Insert 10 at the rear.
Deque: [10, _ , _ , _ , _ ]
Front = 0, Rear = 0
Insert 20 at the rear.
Deque: [10, 20, _ , _ , _ ]
Front = 0, Rear = 1
Insert 30 at the rear.
Deque: [10, 20, 30, _ , _ ]
Front = 0, Rear = 2
Example 2: Enqueue at Front
Insert 5 at the front (front pointer moves backwards, circularly).
Deque: [10, 20, 30, _ , 5 ]
Front = 4, Rear = 2
Insert 2 at the front.
Deque: [10, 20, 30, 2 , 5 ]
Front = 3, Rear = 2
Example 3: Dequeue Operations
Remove from front → 2 is removed.
Deque: [10, 20, 30, _ , 5 ]
Front = 4, Rear = 2
Remove from rear → 30 is removed.
Deque: [10, 20, _ , _ , 5 ]
Front = 4, Rear = 1
Summary:
– Enqueue at Rear: Add element to rear, rear pointer moves forward.
– Enqueue at Front: Add element to front, front pointer moves backward (circularly).
– Dequeue at Front: Remove element from front, front pointer moves forward.
– Dequeue at Rear: Remove element from rear, rear pointer moves backward.
– Circular Array: Efficient use of space when front or rear pointer reaches the array limits.
Double Ended Queue বা Deque হলো এমন একটি Queue যেখানে element উভয় দিক থেকে insert এবং remove করা যায়: front এবং rear থেকে।
Deque-এর Operations:
1. Enqueue (Element insert করা): Element front বা rear-এ insert করা হয়।
2. Dequeue (Element remove করা): Element front বা rear থেকে remove করা হয়।
3. Peek Front: Front element দেখার জন্য, remove না করে।
4. Peek Rear: Rear element দেখার জন্য, remove না করে।
উদাহরণ ১: Rear-এ Enqueue
প্রথমে deque খালি থাকে এবং size 5।
Deque: [ _ , _ , _ , _ , _ ]
Front = -1, Rear = -1
10 rear-এ insert করা হলো।
Deque: [10, _ , _ , _ , _ ]
Front = 0, Rear = 0
20 rear-এ insert করা হলো।
Deque: [10, 20, _ , _ , _ ]
Front = 0, Rear = 1
30 rear-এ insert করা হলো।
Deque: [10, 20, 30, _ , _ ]
Front = 0, Rear = 2
উদাহরণ ২: Front-এ Enqueue
5 front-এ insert করা হলো (front pointer backward move করেছে, circularly)।
Deque: [10, 20, 30, _ , 5 ]
Front = 4, Rear = 2
2 front-এ insert করা হলো।
Deque: [10, 20, 30, 2 , 5 ]
Front = 3, Rear = 2
উদাহরণ ৩: Dequeue Operations
Front থেকে remove করা হলো → 2 remove হল।
Deque: [10, 20, 30, _ , 5 ]
Front = 4, Rear = 2
Rear থেকে remove করা হলো → 30 remove হল।
Deque: [10, 20, _ , _ , 5 ]
Front = 4, Rear = 1
সংক্ষেপে:
– Rear-এ Enqueue: Element rear-এ add হয়, rear pointer আগিয়ে চলে।
– Front-এ Enqueue: Element front-এ add হয়, front pointer পিছনে চলে (circularly)।
– Front থেকে Dequeue: Element front থেকে remove হয়, front pointer আগিয়ে চলে।
– Rear থেকে Dequeue: Element rear থেকে remove হয়, rear pointer পিছনে চলে।
– Circular Array: যখন front বা rear pointer array-এর limit পৌঁছে যায়, তখন circular array memory efficiently ব্যবহার করে।
Previous Question on Queue
| Exprsn | Stack | Postfix |
|---|---|---|
| ( | ||
| P | ( | P |
| = | ( | P = |
| 12 | ( | P = 12 |
| / | (/ | P = 12 |
| ( | (/ ( | P = 12 |
| 7 | (/ ( | P = 12 7 |
| – | (/ (- | P = 12 7 |
| 3 | (/ (- | P = 12 7 3 |
| ) | (/ | P = 12 7 3 – |
| + | (+ | P = 12 7 3 – / |
| 2 | (+ | P = 12 7 3 – / 2 |
| ) | P = 12 7 3 – / 2 + |
✔ 12 → Push onto stack → Stack: [12]
✔ 7 → Push onto stack → Stack: [12, 7]
✔ 3 → Push onto stack → Stack: [12, 7, 3]
✔ – → Pop 7 and 3, perform 7 – 3 = 4 → Push 4 → Stack: [12, 4]
✔ / → Pop 12 and 4, perform 12 / 4 = 3 → Push 3 → Stack: [3]
✔ 2 → Push onto stack → Stack: [3, 2]
✔ + → Pop 3 and 2, perform 3 + 2 = 5 → Push 5 → Stack: [5]
The evaluated value of the expression is: P = 5
Tree Data Structure
Tree
A tree is a hierarchical, non-linear data structure consisting of nodes connected by edges.
Root Node: The topmost node that has no parent.
Parent-Child Relationship: Each node (except root) has exactly one parent and may have one or more children.
Connected & Acyclic: All nodes are connected and there is no cycle; only one unique path exists between two nodes.
Leaf Nodes: Nodes that do not have any children.
Subtrees: Each child node with its descendants forms a subtree.
Tree
Tree হলো একটি hierarchical ও non-linear data structure, যেখানে nodes গুলো edge দ্বারা সংযুক্ত থাকে।
Root Node: সবচেয়ে উপরের node যার কোনো parent নেই।
Parent-Child সম্পর্ক: Root ছাড়া প্রতিটি node-এর একটি parent থাকে এবং এক বা একাধিক child থাকতে পারে।
Connected ও Acyclic: সব node পরস্পরের সাথে সংযুক্ত এবং কোনো cycle থাকে না; যেকোনো দুই node-এর মধ্যে একটি মাত্র path থাকে।
Leaf Nodes: যেসব node-এর কোনো child নেই।
Subtrees: কোনো child node ও তার অধীন সব node মিলে একটি subtree গঠন করে।
Leaf Node (External Node): The last nodes of each path that do not have any child pointers are called leaf or external nodes.
Internal Node: A node that has at least one child node is called an internal node.
Edge: An edge is the link or connection between any two nodes in a tree.
Root: The topmost node of a tree is called the root node.
Height of a Node: The height of a node is the number of edges on the longest path from that node to a leaf node.
Depth of a Node: The depth of a node is the number of edges from the root node to that node.
Height of a Tree: The height of a tree is the height of the root node or the depth of the deepest node.
Degree of a Node: The degree of a node is the total number of branches or children it has.
Forest: A collection of two or more disjoint trees is called a forest.
Leaf Node (External Node): যে node-এর কোনো child pointer নেই, অর্থাৎ প্রতিটি path-এর শেষ node, তাকে leaf বা external node বলা হয়।
Internal Node: যে node-এর অন্তত একটি child node আছে তাকে internal node বলা হয়।
Edge: Tree-তে যেকোনো দুইটি node-এর মধ্যে সংযোগকে edge বলা হয়।
Root: Tree-এর সবচেয়ে উপরের node-কে root node বলা হয়।
Height of a Node: কোনো node থেকে সবচেয়ে দূরের leaf node পর্যন্ত যতগুলো edge আছে, সেটাই ঐ node-এর height।
Depth of a Node: Root node থেকে কোনো নির্দিষ্ট node পর্যন্ত যতগুলো edge আছে, সেটাই ঐ node-এর depth।
Height of a Tree: Tree-এর height হলো root node-এর height বা সবচেয়ে গভীর node-এর depth।
Degree of a Node: কোনো node-এর মোট child সংখ্যা বা branch সংখ্যাকেই ঐ node-এর degree বলা হয়।
Forest: একাধিক আলাদা আলাদা (disjoint) tree-এর সমষ্টিকে forest বলা হয়।

1. General Tree
A General Tree is a tree where a node can have any number of children.
Example:
A
/ | \
B C D
\
E
2. Binary Tree
A Binary Tree is a tree where each node can have at most two children.
Example:
A
/ \
B C
Types of Binary Tree
i. Full Binary Tree
Every node has either 0 or 2 children.
Example:
A
/ \
B C
/ \
D E
ii. Complete Binary Tree
All levels are filled except possibly the last, filled left to right.
Example:
A
/ \
B C
/ \
D E
iii. Perfect Binary Tree
All internal nodes have two children and all leaves are at the same level.
Example:
A
/ \
B C
/ \ / \
D E F G
iv. Skewed Binary Tree
Every node has only one child.
Example:
A
\
B
\
C
\
D
১. General Tree
General Tree হলো এমন একটি tree যেখানে একটি node-এর একাধিক child থাকতে পারে।
উদাহরণ:
A
/ | \
B C D
\
E
২. Binary Tree
Binary Tree এমন একটি tree যেখানে প্রতিটি node-এর সর্বোচ্চ দুইটি child থাকতে পারে।
উদাহরণ:
A
/ \
B C
Binary Tree-এর প্রকারভেদ
i. Full Binary Tree
প্রতিটি node-এর হয় ০টি অথবা ২টি child থাকে।
উদাহরণ:
A
/ \
B C
/ \
D E
ii. Complete Binary Tree
শেষ level ছাড়া সব level পূর্ণ থাকে এবং শেষ level বাম দিক থেকে পূরণ হয়।
উদাহরণ:
A
/ \
B C
/ \
D E
iii. Perfect Binary Tree
সব internal node-এর দুইটি child থাকে এবং সব leaf node একই level-এ থাকে।
উদাহরণ:
A
/ \
B C
/ \ / \
D E F G
iv. Skewed Binary Tree
প্রতিটি node-এর মাত্র একটি child থাকে।
উদাহরণ:
A
\
B
\
C
\
D
Properties of a Binary Tree A Binary Tree is a hierarchical data structure in which each node has at most two children, called left child and right child.
1) Maximum Children Each node in a Binary Tree can have at most two children (left and right).
2) Maximum Nodes at a Level At level L (root at level 0), the maximum number of nodes is 2L.
3) Maximum Nodes in a Binary Tree For a Binary Tree of height h, the maximum number of nodes is 2h+1 − 1.
4) Minimum Nodes in a Binary Tree The minimum number of nodes in a Binary Tree of height h is h + 1.
5) Number of Edges If a Binary Tree has n nodes, then it has exactly n − 1 edges.
6) Leaf Nodes Nodes with no children are called leaf nodes. A Binary Tree always has at least one leaf node.
7) Height of a Binary Tree Height is the maximum number of edges from the root to the deepest leaf node.
Binary Tree এর Properties Binary Tree হলো একটি hierarchical data structure যেখানে প্রতিটি node এর সর্বোচ্চ দুইটি child থাকতে পারে, যেগুলোকে left child এবং right child বলা হয়। এর কিছু গুরুত্বপূর্ণ properties রয়েছে যা data structure এ ব্যবহৃত হয়।
1) Maximum Children Binary Tree এর প্রতিটি node এর সর্বোচ্চ দুইটি child থাকতে পারে (left এবং right)।
2) Maximum Nodes at a Level Level L এ (root ধরা হয় level 0), সর্বোচ্চ node সংখ্যা হলো 2L।
3) Maximum Nodes in a Binary Tree যদি Binary Tree এর height h হয়, তাহলে সর্বোচ্চ node সংখ্যা হবে 2h+1 − 1।
4) Minimum Nodes in a Binary Tree Height h হলে Binary Tree তে সর্বনিম্ন node সংখ্যা হবে h + 1।
5) Number of Edges যদি Binary Tree তে n টি node থাকে, তাহলে edge এর সংখ্যা হবে n − 1।
6) Leaf Nodes যেসব node এর কোনো child নেই তাদের leaf node বলা হয়। Binary Tree তে কমপক্ষে একটি leaf node থাকে।
7) Height of a Binary Tree Height হলো root node থেকে deepest leaf node পর্যন্ত সর্বোচ্চ edge সংখ্যা।
If the level number is L (root is at level 0), then maximum number of nodes at that level is: Maximum Nodes = 2L
Example
Let, L = 4
Maximum Nodes = 24 = 16 nodes
Relation (Nodes → Level) If the maximum number of nodes at a level is given as N, then the level can be found using: L = log2(N)
Example
Let, N = 17
L = log2(17) ≈ 4.08
So, the level will be 5 (because we take the next integer level when it is not a whole number)
This relation helps to understand the structure and growth of a Binary Tree.
Binary Tree তে প্রতিটি level এ নির্দিষ্ট সংখ্যক node থাকতে পারে। Level এবং node এর মধ্যে একটি নির্দিষ্ট mathematical সম্পর্ক রয়েছে।
Relation (Level → Node) যদি level সংখ্যা L হয় (root ধরা হয় level 0), তাহলে ঐ level এ সর্বোচ্চ node সংখ্যা হবে: Maximum Node = 2L
Example
ধরা যাক, L = 4
Maximum Node = 24 = 16 node
Relation (Node → Level) যদি কোনো level এ সর্বোচ্চ node সংখ্যা N দেওয়া থাকে, তাহলে level বের করা যায়: L = log2(N)
Example
ধরা যাক, N = 17
L = log2(17) ≈ 4.08
সুতরাং level হবে 5 (কারণ এটি পূর্ণ সংখ্যা না হলে পরবর্তী পূর্ণ level ধরা হয়)
এই সম্পর্ক ব্যবহার করে Binary Tree এর structure এবং growth সহজে বোঝা যায়।
Binary Search Tree (BST) A Binary Search Tree is a Binary Tree in which for every node, all values in the left subtree are smaller than the node value and all values in the right subtree are greater than the node value. This property makes searching fast and efficient.
Example
Insert values: 50, 30, 70, 20, 40, 60, 80
50
/ \
30 70
/ \ / \
20 40 60 80Key Property Inorder traversal of a BST always gives sorted order.
Binary Search Tree (BST) Binary Search Tree হলো একটি Binary Tree যেখানে প্রতিটি node এর জন্য left subtree এর সব value node এর চেয়ে ছোট এবং right subtree এর সব value node এর চেয়ে বড় হয়। এই নিয়মের কারণে searching দ্রুত হয়।
Example
Value insert করা হলো: 50, 30, 70, 20, 40, 60, 80
50
/ \
30 70
/ \ / \
20 40 60 80Key Property BST এর inorder traversal করলে সব value sorted order এ পাওয়া যায়।
Binary Search Tree Traversal Methods Traversal means visiting each node of a Binary Search Tree (BST) exactly once in a specific order.
Example BST
50
/ \
30 70
/ \ / \
20 40 60 801) Inorder Traversal (Left → Root → Right) Inorder traversal of a BST always gives values in sorted order.
Output: 20, 30, 40, 50, 60, 70, 80
2) Preorder Traversal (Root → Left → Right) Root node is visited first, then left and right subtrees.
Output: 50, 30, 20, 40, 70, 60, 80
3) Postorder Traversal (Left → Right → Root) Root node is visited last after both subtrees.
Output: 20, 40, 30, 60, 80, 70, 50
Binary Search Tree Traversal Method Traversal মানে Binary Search Tree (BST) এর প্রতিটি node নির্দিষ্ট একটি order এ একবার করে visit করা।
Example BST
50
/ \
30 70
/ \ / \
20 40 60 801) Inorder Traversal (Left → Root → Right) BST এর inorder traversal করলে সব value sorted order এ পাওয়া যায়।
Output: 20, 30, 40, 50, 60, 70, 80
2) Preorder Traversal (Root → Left → Right) প্রথমে root node visit করা হয়, তারপর left এবং right subtree।
Output: 50, 30, 20, 40, 70, 60, 80
3) Postorder Traversal (Left → Right → Root) সবশেষে root node visit করা হয়।
Output: 20, 40, 30, 60, 80, 70, 50
Inorder: D B E A F C
Preorder: A B D E C F
Given
Inorder: D B E A F C
Preorder: A B D E C F
Rule
1) In Preorder, the first element is always the Root.
2) In Inorder, elements left of Root belong to Left Subtree, and elements right of Root belong to Right Subtree.
Step 1: Find Root
Preorder = A B D E C F → Root = A
Now locate A in Inorder: D B E A F C
Left(Inorder) = D B E
Right(Inorder) = F C
Step 2: Build Left Subtree of A
Left(Inorder) = D B E (3 nodes) → so next 3 nodes in Preorder after A belong to left subtree.
Preorder after A = B D E … → Left(Preorder) = B D E
Now for Left subtree:
Preorder = B D E → Root = B
Find B in Inorder: D B E
Left of B (Inorder) = D
Right of B (Inorder) = E
So B’s left child = D, B’s right child = E
Step 3: Build Right Subtree of A
Right(Inorder) = F C (2 nodes) → remaining Preorder nodes belong to right subtree.
Remaining Preorder = C F → Right(Preorder) = C F
Now for Right subtree:
Preorder = C F → Root = C
Find C in Inorder: F C
Left of C (Inorder) = F
Right of C (Inorder) = (none)
So C’s left child = F, C has no right child.
Final Constructed Binary Tree
A
/ \
B C
/ \ /
D E FGiven
Postorder Traversal: 8, 9, 6, 7, 4, 5, 2, 3, 1
Inorder Traversal: 8, 3, 9, 4, 7, 2, 5, 1, 6
Rule Used
1) In Postorder, the last element is always the Root.
2) In Inorder, elements to the left of the root belong to the left subtree and elements to the right belong to the right subtree.
Step 1: Identify Root
Postorder last element = 1 → Root of the tree is 1.
Inorder split at 1:
Left(Inorder) = 8, 3, 9, 4, 7, 2, 5
Right(Inorder) = 6
Step 2: Construct Right Subtree
Right(Inorder) has only one node → Right child of 1 is 6.
Step 3: Construct Left Subtree
Remaining Postorder (before 1) = 8, 9, 6, 7, 4, 5, 2, 3
Last element here = 3 → Root of left subtree.
Inorder split at 3:
Left of 3 = 8
Right of 3 = 9, 4, 7, 2, 5
Continuing this process recursively, the left subtree is constructed.
Final Binary Tree Structure
1
/ \
3 6
/ \
8 2
/ \
4 5
/ \
9 7Step 4: Find Height of the Tree
Height of a binary tree is the number of edges on the longest path from root to a leaf.
Longest path here is:
1 → 3 → 2 → 4 → 9
This path has 4 edges.
Final Answer
The height of the given binary tree is 4.
1) Write the Inorder Traversal of the above binary tree.2) Write the Preorder Traversal of the above binary tree.
3) Write the Postorder Traversal of the above binary tree.
D B G E A C F
Preorder Traversal:
A B D E G C F
Postorder Traversal:
D G E B F C A
Previous Question on Tree
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
8 9 10 11 12 13 14 15
(a) Preorder Traversal (Root -> Left -> Right)
In preorder traversal, we visit the root (main) node first, then go to the left side and visit all its nodes, and after that, we go to the right side and visit its nodes.
Preorder Traversal: 1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15
(b) In-order Traversal (Left -> Root -> Right)
In in-order traversal, we first visit all the nodes on the left side, then visit the root node, and finally visit the nodes on the right side.
In-order Traversal: 8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15
(c) Post-order Traversal (Left -> Right -> Root)
In post-order traversal, we first visit all the nodes on the left side, then visit the nodes on the right side, and finally visit the root node.
Post-order Traversal: 8, 9, 4, 10, 11, 5, 2, 12, 13, 6, 14, 15, 7, 3, 1
Heap Data Structure
Heap Property
Max-Heap: In a Max-Heap, the value of each node is greater than or equal to the values of its children. The largest element is always stored at the root node.
Min-Heap: In a Min-Heap, the value of each node is less than or equal to the values of its children. The smallest element is always stored at the root node.
Heap Property
Max-Heap: Max-Heap এ প্রতিটি parent node এর মান তার child node গুলোর মানের চেয়ে বড় বা সমান হয়। সবচেয়ে বড় element সবসময় root node এ থাকে।
Min-Heap: Min-Heap এ প্রতিটি parent node এর মান তার child node গুলোর মানের চেয়ে ছোট বা সমান হয়। সবচেয়ে ছোট element সবসময় root node এ থাকে।
Heap Sort: Heaps can be used for sorting data. By repeatedly removing the root element (minimum or maximum) and rebuilding the heap, the elements can be sorted efficiently.
Graph Algorithms: Heaps are used in graph algorithms such as Dijkstra’s algorithm and Prim’s algorithm, where fast access to the minimum or maximum element is essential.
Heap Sort: Heaps ব্যবহার করে sorting করা যায়। Root element (minimum বা maximum) বারবার remove করে এবং heap rebuild করার মাধ্যমে data efficient ভাবে sort করা হয়।
Graph Algorithms: Heaps ব্যবহার করা হয় Dijkstra’s algorithm এবং Prim’s algorithm এর মতো graph algorithm এ, যেখানে দ্রুত minimum বা maximum element access করা খুব গুরুত্বপূর্ণ।
Previous Question on Heap Data Structure
Initial array as a binary tree:
56
/ \
33 48
/ \ / \
29 99 12 344
Swap 48 and 12:
56
/ \
33 12
/ \ / \
29 99 48 344
Swap 33 and 29:
56
/ \
29 12
/ \ / \
33 99 48 344
Swap 56 and 12:
12
/ \
29 56
/ \ / \
33 99 48 344
Swap 56 and 48:
12
/ \
29 48
/ \ / \
33 99 56 344
Final Result
The array now satisfies the min heap property: every parent node is smaller than or equal to its children.
12
/ \
29 48
/ \ / \
33 99 56 344
🎥 Video Solution: Heap & its properties, Covert min heap from given array.
| 0 | 1 | 2 | 3 | 4 | 5 |
| 7 | 2 | 9 | 4 | 5 | 3 |
Represent as a Complete Binary Tree:
7
/ \
2 9
/ \ /
4 5 3
Start Heapify from Bottom Non-Leaf Nodes:
1. Compare 9 and 3 (node 9 has children 3):
– Here, 9 > 3. So, swap the nodes 9 and 3.
Tree now:
7
/ \
2 3
/ \ /
4 5 9
2. Compare 2 with 4 and 5 (node 2 has children 4 and 5):
– Here, 2 < 4 and 2 < 5. So, no swap is needed.
Tree remains:
7
/ \
2 3
/ \ /
4 5 9
3. Compare 7 with 2 and 3 (root node 7 has children 2 and 3):
– Here, 7 > 2 (left child). So, swap nodes 7 and 2.
Tree after swap:
2
/ \
7 3
/ \ /
4 5 9
4. Compare 7 with 4 and 5 (node 7 has children 4 and 5):
– Here, 7 > 4 (left child). So, swap nodes 7 and 4.
Tree after swap:
2
/ \
4 3
/ \ /
7 5 9
Final Min-Heap:
The min-heap is now represented as a Complete Binary Tree, where each parent node is smaller than its children, satisfying the min-heap property.
Sorting Algorithm
What is Sorting Algorithm?
A Sorting Algorithm is a method used to arrange the elements of an array or list in a specific order, such as ascending or descending. Sorting helps in organizing data efficiently and makes searching and processing faster.
Example
Unsorted Array:
10 50 21 36 45 62 19
Sorted Array (Ascending Order):
10 19 21 36 45 50 62
What is Sorting Algorithm?
Sorting Algorithm হলো এমন একটি পদ্ধতি যা ব্যবহার করে কোনো Array অথবা List-এর element গুলোকে একটি নির্দিষ্ট ক্রমে সাজানো হয়, যেমন ascending order বা descending order। Sorting ডাটা কে সুশৃঙ্খল করে এবং searching ও processing সহজ করে তোলে।
Example
Unsorted Array:
10 50 21 36 45 62 19
Sorted Array (Ascending Order):
10 19 21 36 45 50 62
bubbleSort(array)
for i ← 1 to sizeOfArray − 1 do for j ← 1 to sizeOfArray − 1 − i do
if array[j] > array[j + 1] then
swap array[j] and array[j + 1]
end if end for end for
end bubbleSort
Bubble Sort (Ascending) — Step-by-step Iterations
Initial List: [30, 25, 62, 45, 7, 10]
Pass 1 (i = 1)
1) Compare 30 and 25 → swap → [25, 30, 62, 45, 7, 10]
2) Compare 30 and 62 → no swap → [25, 30, 62, 45, 7, 10]
3) Compare 62 and 45 → swap → [25, 30, 45, 62, 7, 10]
4) Compare 62 and 7 → swap → [25, 30, 45, 7, 62, 10]
5) Compare 62 and 10 → swap → [25, 30, 45, 7, 10, 62]
End of Pass 1: [25, 30, 45, 7, 10, 62]
Pass 2 (i = 2)
1) Compare 25 and 30 → no swap → [25, 30, 45, 7, 10, 62]
2) Compare 30 and 45 → no swap → [25, 30, 45, 7, 10, 62]
3) Compare 45 and 7 → swap → [25, 30, 7, 45, 10, 62]
4) Compare 45 and 10 → swap → [25, 30, 7, 10, 45, 62]
End of Pass 2: [25, 30, 7, 10, 45, 62]
Pass 3 (i = 3)
1) Compare 25 and 30 → no swap → [25, 30, 7, 10, 45, 62]
2) Compare 30 and 7 → swap → [25, 7, 30, 10, 45, 62]
3) Compare 30 and 10 → swap → [25, 7, 10, 30, 45, 62]
End of Pass 3: [25, 7, 10, 30, 45, 62]
Pass 4 (i = 4)
1) Compare 25 and 7 → swap → [7, 25, 10, 30, 45, 62]
2) Compare 25 and 10 → swap → [7, 10, 25, 30, 45, 62]
End of Pass 4: [7, 10, 25, 30, 45, 62]
Pass 5 (i = 5)
1) Compare 7 and 10 → no swap → [7, 10, 25, 30, 45, 62]
End of Pass 5: [7, 10, 25, 30, 45, 62]
Note: No swaps happened, so the list is already sorted (early stop).
Final Sorted List (Ascending): [7, 10, 25, 30, 45, 62]
Time and Space Complexity of Bubble Sort
Time Complexity
The time complexity of Bubble Sort depends on the initial order of the elements in the array.
• Best Case: O(n)
When the array is already sorted, Bubble Sort requires only one pass with no swaps.
• Average Case: O(n²)
When the elements are in random order, Bubble Sort performs multiple comparisons and swaps.
• Worst Case: O(n²)
When the array is sorted in reverse order, maximum comparisons and swaps are required.
Space Complexity
The space complexity of Bubble Sort is O(1) because it is an in-place sorting algorithm and does not require any additional memory apart from a few variables.
Bubble Sort এর Time এবং Space Complexity
Time Complexity
Bubble Sort এর Time Complexity array-এর initial order এর উপর নির্ভর করে।
• Best Case: O(n)
যখন array আগে থেকেই sorted থাকে, তখন Bubble Sort শুধুমাত্র একটি pass এ কোনো swap ছাড়াই কাজ শেষ করে।
• Average Case: O(n²)
যখন element গুলো random order এ থাকে, তখন Bubble Sort অনেক comparison এবং swap করে।
• Worst Case: O(n²)
যখন array reverse order এ sorted থাকে, তখন সর্বাধিক comparison এবং swap প্রয়োজন হয়।
Space Complexity
Bubble Sort এর Space Complexity হলো O(1), কারণ এটি একটি in-place sorting algorithm
এবং input data ছাড়া অতিরিক্ত কোনো memory ব্যবহার করে না।
Selection Sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting
the smallest element from the unsorted part of the array and placing it at the beginning.
The array is divided into a sorted part and an unsorted part.
Selection Sort হলো একটি সহজ comparison-based sorting algorithm। এই algorithm-এ array কে
একটি sorted part এবং একটি unsorted part এ ভাগ করা হয়। Unsroted part থেকে প্রতিবার
সবচেয়ে ছোট element নির্বাচন করে সেটিকে সামনে এনে বসানো হয়।
Algorithm (Steps)
1) Start from the first element of the array.
2) Assume the first element is the minimum element.
3) Compare this minimum element with the remaining elements of the array.
4) If a smaller element is found, update the minimum element.
5) Swap the minimum element with the first element of the unsorted part.
6) Move the boundary of the sorted part one position forward.
7) Repeat the above steps until the entire array is sorted.
selectionSort(array)
for i ← 1 to sizeOfArray − 1
minIndex ← i
for j ← i + 1 to sizeOfArray
if array[j] < array[minIndex]
minIndex ← j
end for
if minIndex ≠ i
swap array[i] and array[minIndex]
end for
end selectionSort
Insertion Sort — Step-by-step Execution
Initial Array: [30, 25, 62, 45, 7, 10]
Pass 1 (i = 2, key = 25)
Compare 25 with 30 → shift 30 to the right
Insert 25 at position 1
Array after Pass 1: [25, 30, 62, 45, 7, 10]
Pass 2 (i = 3, key = 62)
Compare 62 with 30 → no shift required
Array after Pass 2: [25, 30, 62, 45, 7, 10]
Pass 3 (i = 4, key = 45)
Compare 45 with 62 → shift 62 to the right
Compare 45 with 30 → stop shifting
Insert 45 in correct position
Array after Pass 3: [25, 30, 45, 62, 7, 10]
Pass 4 (i = 5, key = 7)
Compare 7 with 62 → shift 62 to the right
Compare 7 with 45 → shift 45 to the right
Compare 7 with 30 → shift 30 to the right
Compare 7 with 25 → shift 25 to the right
Insert 7 at the beginning
Array after Pass 4: [7, 25, 30, 45, 62, 10]
Pass 5 (i = 6, key = 10)
Compare 10 with 62 → shift 62 to the right
Compare 10 with 45 → shift 45 to the right
Compare 10 with 30 → shift 30 to the right
Compare 10 with 25 → shift 25 to the right
Compare 10 with 7 → stop shifting
Insert 10 in correct position
Array after Pass 5: [7, 10, 25, 30, 45, 62]
Final Sorted Array (Ascending Order): [7, 10, 25, 30, 45, 62]
Quick Sort Algorithm
Quick Sort is an efficient comparison-based sorting algorithm that follows the Divide and Conquer technique.
It works by selecting a pivot element, partitioning the array around the pivot, and recursively sorting the subarrays.
Quick Sort Algorithm
Quick Sort হলো একটি efficient comparison-based sorting algorithm যা Divide and Conquer technique ব্যবহার করে।
এখানে একটি pivot element নির্বাচন করা হয়, pivot-এর চারপাশে array partition করা হয় এবং তারপর subarray গুলো recursively sort করা হয়।
Algorithm (Steps)
1) Choose a pivot element from the array.
2) Rearrange the array so that elements smaller than the pivot are on the left and larger elements are on the right.
3) Place the pivot in its correct position.
4) Recursively apply Quick Sort on the left subarray.
5) Recursively apply Quick Sort on the right subarray.
6) Repeat the process until the entire array is sorted.
quickSort(array, low, high)
if low < high
pivotIndex ← partition(array, low, high)
quickSort(array, low, pivotIndex − 1)
quickSort(array, pivotIndex + 1, high)
end quickSort
partition(array, low, high)
pivot ← array[high]
i ← low − 1
for j ← low to high − 1
if array[j] < pivot
i ← i + 1
swap array[i] and array[j]
swap array[i + 1] and array[high]
return i + 1
Selection Sort — Step-by-step Example
Given Array: [30, 25, 62, 45, 7, 10]
Pass 1 (i = 1)
Assume minimum = 30
Compare 30 with 25 → new minimum = 25
Compare 25 with 62 → no change
Compare 25 with 45 → no change
Compare 25 with 7 → new minimum = 7
Compare 7 with 10 → no change
Swap 30 and 7
Array after Pass 1: [7, 25, 62, 45, 30, 10]
Pass 2 (i = 2)
Assume minimum = 25
Compare 25 with 62 → no change
Compare 25 with 45 → no change
Compare 25 with 30 → no change
Compare 25 with 10 → new minimum = 10
Swap 25 and 10
Array after Pass 2: [7, 10, 62, 45, 30, 25]
Pass 3 (i = 3)
Assume minimum = 62
Compare 62 with 45 → new minimum = 45
Compare 45 with 30 → new minimum = 30
Compare 30 with 25 → new minimum = 25
Swap 62 and 25
Array after Pass 3: [7, 10, 25, 45, 30, 62]
Pass 4 (i = 4)
Assume minimum = 45
Compare 45 with 30 → new minimum = 30
Compare 30 with 62 → no change
Swap 45 and 30
Array after Pass 4: [7, 10, 25, 30, 45, 62]
Pass 5 (i = 5)
Assume minimum = 45
Compare 45 with 62 → no change
No swap needed
Array after Pass 5: [7, 10, 25, 30, 45, 62]
Final Sorted Array (Ascending Order): [7, 10, 25, 30, 45, 62]
Merge Sort Algorithm
Merge Sort is an efficient, comparison-based sorting algorithm that follows the
Divide and Conquer strategy. It divides the array into smaller subarrays,
sorts them recursively, and then merges the sorted subarrays to produce the final sorted array.
Merge Sort Algorithm
Merge Sort হলো একটি efficient comparison-based sorting algorithm যা
Divide and Conquer technique অনুসরণ করে। এই algorithm-এ array কে
ছোট ছোট subarray তে ভাগ করা হয়, তারপর প্রতিটি subarray recursively sort করা হয়
এবং শেষে sorted subarray গুলো merge করে একটি sorted array তৈরি করা হয়।
Algorithm (Steps)
1) Divide the given array into two halves.
2) Recursively apply Merge Sort on the left half.
3) Recursively apply Merge Sort on the right half.
4) Merge the two sorted halves into a single sorted array.
5) Repeat the process until the entire array is sorted.
mergeSort(array, left, right)
if left < right
mid ← (left + right) / 2
mergeSort(array, left, mid)
mergeSort(array, mid + 1, right)
merge(array, left, mid, right)
end mergeSort
merge(array, left, mid, right)
n1 ← mid − left + 1
n2 ← right − mid
create array L[1..n1] and R[1..n2]
for i ← 1 to n1
L[i] ← array[left + i − 1]
for j ← 1 to n2
R[j] ← array[mid + j]
i ← 1, j ← 1, k ← left
while i ≤ n1 and j ≤ n2
if L[i] ≤ R[j]
array[k] ← L[i]
i ← i + 1
else
array[k] ← R[j]
j ← j + 1
k ← k + 1
while i ≤ n1
array[k] ← L[i]
i ← i + 1
k ← k + 1
while j ≤ n2
array[k] ← R[j]
j ← j + 1
k ← k + 1
end merge
Merge Sort — Step-by-step Example (Ascending)
Given Array: [30, 25, 62, 45, 7, 10]
Step 1: Divide (Split) the array
Split into two halves:
Left = [30, 25, 62]
Right = [45, 7, 10]
Step 2: Divide the Left half
Left = [30, 25, 62] → Split into:
L1 = [30, 25]
L2 = [62]
Step 3: Divide L1 further
L1 = [30, 25] → Split into:
[30] and [25]
Step 4: Merge (Sort) [30] and [25]
Merge → [25, 30]
Step 5: Merge [25, 30] and [62]
Merge → [25, 30, 62]
Sorted Left Half: [25, 30, 62]
Step 6: Divide the Right half
Right = [45, 7, 10] → Split into:
R1 = [45, 7]
R2 = [10]
Step 7: Divide R1 further
R1 = [45, 7] → Split into:
[45] and [7]
Step 8: Merge (Sort) [45] and [7]
Merge → [7, 45]
Step 9: Merge [7, 45] and [10]
Merge → [7, 10, 45]
Sorted Right Half: [7, 10, 45]
Step 10: Final Merge of two sorted halves
Merge [25, 30, 62] and [7, 10, 45]:
Result → [7, 10, 25, 30, 45, 62]
Final Sorted Array (Ascending Order): [7, 10, 25, 30, 45, 62]
<
Time Complexity of Merge Sort (Iteration-wise)
Merge Sort follows the Divide and Conquer approach. The array is repeatedly divided into smaller subarrays and then merged in a sorted manner.
Division Phase:
At each level, the array is divided into two halves until subarrays of size 1 are obtained.
Number of levels of division = log₂n
Merging Phase:
At each level, all elements are merged once.
Time taken at each level = O(n)
Total Time Complexity:
Since there are log n levels and each level takes O(n) time,
Time Complexity = O(n log n)
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Merge Sort এর Time Complexity (Iteration-wise)
Merge Sort একটি Divide and Conquer based sorting algorithm। এখানে array-কে বারবার ছোট ছোট subarray-তে ভাগ করা হয় এবং পরে সেগুলো merge করে sorted করা হয়।
Division Phase:
প্রতিটি ধাপে array-কে দুই ভাগে ভাগ করা হয় যতক্ষণ না subarray-এর size 1 হয়।
Division level এর সংখ্যা = log₂n
Merging Phase:
প্রতিটি level-এ সব element একবার করে merge হয়।
প্রতিটি level-এ সময় লাগে = O(n)
Total Time Complexity:
যেহেতু মোট log n টি level আছে এবং প্রতিটি level-এ O(n) সময় লাগে,
Time Complexity = O(n log n)
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)
Quick Sort Algorithm
Quick Sort is an efficient comparison-based sorting algorithm that follows the Divide and Conquer technique.
It works by selecting a pivot element, partitioning the array around the pivot, and recursively sorting the subarrays.
Quick Sort Algorithm
Quick Sort হলো একটি efficient comparison-based sorting algorithm যা Divide and Conquer technique ব্যবহার করে।
এখানে একটি pivot element নির্বাচন করা হয়, pivot-এর চারপাশে array partition করা হয় এবং তারপর subarray গুলো recursively sort করা হয়।
Algorithm (Steps)
1) Choose a pivot element from the array.
2) Rearrange the array so that elements smaller than the pivot are on the left and larger elements are on the right.
3) Place the pivot in its correct position.
4) Recursively apply Quick Sort on the left subarray.
5) Recursively apply Quick Sort on the right subarray.
6) Repeat the process until the entire array is sorted.
quickSort(array, low, high)
if low < high
pivotIndex ← partition(array, low, high)
quickSort(array, low, pivotIndex − 1)
quickSort(array, pivotIndex + 1, high)
end quickSort
partition(array, low, high)
pivot ← array[high]
i ← low − 1
for j ← low to high − 1
if array[j] < pivot
i ← i + 1
swap array[i] and array[j]
swap array[i + 1] and array[high]
return i + 1
Advantages of Sorting Algorithms
Bubble Sort: Simple to understand and implement. Suitable for small datasets and educational purposes.
Insertion Sort: Efficient for small or nearly sorted datasets. Uses less overhead and is stable.
Selection Sort: Performs minimum number of swaps. Useful when memory writes are costly.
Merge Sort: Highly efficient and consistent performance. Suitable for large datasets and external sorting.
Quick Sort: Very fast in practice with good average-case performance. Requires less extra memory.
Heap Sort: Guarantees O(n log n) time complexity and works in-place.
Comparison of Sorting Algorithms
Sorting Algorithm-এর Advantages
Bubble Sort: বোঝা ও implement করা খুব সহজ। ছোট dataset এবং learning purpose-এর জন্য উপযোগী।
Insertion Sort: Small বা nearly sorted dataset-এর জন্য efficient। এটি stable এবং কম overhead ব্যবহার করে।
Selection Sort: Minimum swap ব্যবহার করে। যেখানে memory write costly সেখানে কার্যকর।
Merge Sort: Large dataset-এর জন্য খুব efficient এবং consistent performance দেয়। External sorting-এর জন্য উপযোগী।
Quick Sort: Practical ক্ষেত্রে খুব দ্রুত কাজ করে এবং average case performance ভালো। Extra memory কম লাগে।
Heap Sort: Guaranteed O(n log n) time complexity দেয় এবং in-place কাজ করে।
Sorting Algorithms-এর Comparison

Previous Question on Sorting Algorithm
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
8 9 10 11 12 13 14 15
(a) Preorder Traversal (Root -> Left -> Right)
In preorder traversal, we visit the root (main) node first, then go to the left side and visit all its nodes, and after that, we go to the right side and visit its nodes.
Preorder Traversal: 1, 2, 4, 8, 9, 5, 10, 11, 3, 6, 12, 13, 7, 14, 15
(b) In-order Traversal (Left -> Root -> Right)
In in-order traversal, we first visit all the nodes on the left side, then visit the root node, and finally visit the nodes on the right side.
In-order Traversal: 8, 4, 9, 2, 10, 5, 11, 1, 12, 6, 13, 3, 14, 7, 15
(c) Post-order Traversal (Left -> Right -> Root)
In post-order traversal, we first visit all the nodes on the left side, then visit the nodes on the right side, and finally visit the root node.
Post-order Traversal: 8, 9, 4, 10, 11, 5, 2, 12, 13, 6, 14, 15, 7, 3, 1
Previous Job Qustion on Computer Fundamental
- List down the layers of OSI model in top-down manner. [Different Ministry, AP(CSE), 2023]
- Describe briefly the TCP/IP tunneling using appropriate diagram. [Different Ministry, AP(CSE), 2023]
- How do you define packet fragmentation? Explain briefly the transparent and non-transparent fragmentation with necessary diagram. [Different Ministry, AP(CSE), 2023]
- Explain the logic of a "Checksum". How is it used to verify data integrity during file transfer?. [Different Ministry, AP(CSE), 2023]
MCQ on Computer Fundamental

Time's up

