0% found this document useful (0 votes)
2 views21 pages

Data Structures Tree Stack Queue

This document is a guide for beginners on Data Structures, specifically focusing on Trees, Stacks, and Queues, explained in simple Urdu-English. It includes detailed explanations of each data structure along with five complete C++ programs for each topic, demonstrating their functionality. The document covers key operations and provides real-life examples to illustrate the concepts effectively.

Uploaded by

tabishali0849
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Data Structures Tree Stack Queue

This document is a guide for beginners on Data Structures, specifically focusing on Trees, Stacks, and Queues, explained in simple Urdu-English. It includes detailed explanations of each data structure along with five complete C++ programs for each topic, demonstrating their functionality. The document covers key operations and provides real-life examples to illustrate the concepts effectively.

Uploaded by

tabishali0849
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structures

Trees • Stacks • Queues

Yeh guide specially unke liye banai gayi hai jo pehli baar Data Structures parh rahe hain. Is mein
Trees, Stacks aur Queues ko bilkul simple aur easy Urdu-English mein samjhaya gaya hai. Har topic
ke saath 5 complete C++ programs diye gaye hain jo line-by-line explain kiye gaye hain.

Topics Covered Programs

Stack (Dher / Woh cheez jo upar se aati jaati hai) 5 Complete Programs

Queue (Antaar / Line) 5 Complete Programs

Tree (Ped / Hierarchy) 5 Complete Programs


CHAPTER 1: STACK

Stack Kya Hota Hai?


Stack ek aisi data structure hai jisme items ek dusre ke upar rakhte jaate hain — bilkul waisay jaisay
aap kitabein ek ke upar ek rakhte hain. Sab se upar wali cheez pehle nikali jaati hai. Isko LIFO kehte
hain — Last In, First Out (jo sabse aakhir mein aya, woh pehle bahar jaata hai).

REAL LIFE EXAMPLE: Jab aap plates dho ke ek ke upar ek rakhte hain, toh jo plate sab se aakhir
mein rakhi, woh sab se pehle use hogi. Yahi STACK hai!

Stack ki 3 Main Operations:


• PUSH: Koi cheez stack mein daalna (upar rakhna)
• POP: Stack se cheez nikaalna (sab se upar se)
• PEEK / TOP: Sab se upar wali cheez dekhna bina nikaale

Program 1: Basic Stack using Array (Array se bana hua Stack)


#include<iostream>
using namespace std;

int stack[100]; // Stack store karne ki jagah


int top = -1; // top = -1 matlab stack khali hai

// PUSH function - cheez daalne ke liye


void push(int value) {
top++; // top ko ek aage karo
stack[top] = value; // wahan value rakh do
cout << value << " push hua!" << endl;
}

// POP function - cheez nikaalne ke liye


void pop() {
if(top == -1) {
cout << "Stack khali hai!" << endl;
} else {
cout << stack[top] << " pop hua!" << endl;
top--; // top ko peeche karo
}
}

// PEEK - sirf dekhna


void peek() {
if(top == -1) cout << "Stack khali hai!" << endl;
else cout << "Top element: " << stack[top] << endl;
}

int main() {
push(10); // 10 daalo
push(20); // 20 daalo
push(30); // 30 daalo
peek(); // Upar kya hai?
pop(); // Ek nikalo
peek(); // Ab upar kya hai?
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. int stack[100] — Humne 100 jagah ka array banaya jahan cheezein rakhi jayengi.
2. int top = -1 — top variable batata hai sab se upar kahan hain. -1 matlab stack bilkul khali hai.
3. push() mein: pehle top ko ek badha do, phir us jagah value rakh do.
4. pop() mein: pehle check karo stack khali toh nahi, phir top ko ek kam karo.
5. peek() mein: sirf stack[top] print karo bina kuch nikale.
6. Output: 10 push, 20 push, 30 push → peek shows 30 → 30 pop → peek shows 20

Program 2: Stack using Class (OOP style - Professional tarika)


#include<iostream>
using namespace std;

class Stack {
int arr[100];
int top;
public:
Stack() { top = -1; } // Constructor: shuru mein khali

void push(int x) {
if(top >= 99) cout << "Stack Full!" << endl;
else arr[++top] = x;
}

int pop() {
if(top < 0) { cout << "Khali!" << endl; return -1; }
return arr[top--];
}

bool isEmpty() { return top == -1; }

void display() {
cout << "Stack (Upar se): ";
for(int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}
};

int main() {
Stack s;
[Link](5); [Link](10); [Link](15);
[Link]();
cout << "Pop: " << [Link]() << endl;
[Link]();
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. class Stack — Hum ne Stack ko ek class ki tarah define kiya. Yeh professional/OOP way hai.
2. Constructor Stack() — Jab object banta hai, top automatically -1 ho jaata hai.
3. arr[++top] = x — pehle top badho, phir value rakh do (shortcut).
4. arr[top--] — pehle value wapas karo, phir top kam karo (shortcut).
5. display() — Loop upar se neeche chalta hai taake stack ki sahi position dikhe.
6. Output: Stack: 15 10 5 → Pop: 15 → Stack: 10 5

Program 3: Stack se Parentheses Check Karna (Brackets Balance)


#include<iostream>
#include<stack> // C++ ki built-in stack
#include<string>
using namespace std;

bool checkBrackets(string str) {


stack<char> s;
for(int i = 0; i < [Link](); i++) {
char c = str[i];
if(c == '(' || c == '{' || c == '[') {
[Link](c); // Opening bracket: push karo
}
else if(c == ')' || c == '}' || c == ']') {
if([Link]()) return false; // Koi opener nahi
[Link](); // Matching opener nikaalo
}
}
return [Link](); // Agar sab match hua toh true
}

int main() {
cout << checkBrackets("(a+b)") << endl; // 1 = sahi
cout << checkBrackets("{[()]}") << endl; // 1 = sahi
cout << checkBrackets("(a+b") << endl; // 0 = galat
cout << checkBrackets(")(") << endl; // 0 = galat
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. Yeh program check karta hai ke kisi string mein brackets (parentheses) balanced hain ya nahi.
2. Jab bhi opening bracket ( ya { ya [ mile, use push karo.
3. Jab closing bracket mile, stack se ek pop karo (matlab ek opening wapas li).
4. Akhir mein agar stack khali hai, sab balanced hai. Warna galat.
5. Real use: Compilers (like g++) isi tarah code check karte hain!
6. Output: 1 1 0 0 (1 = correct, 0 = incorrect)

Program 4: Stack se Number Reverse Karna


#include<iostream>
#include<stack>
using namespace std;

void reverseNumber(int n) {
stack<int> s;
cout << "Original: " << n << endl;

// Digits ek ek karke push karo


while(n > 0) {
[Link](n % 10); // Last digit lo
n = n / 10; // Number chota karo
}

// Stack se pop karke reversed number banao


cout << "Reversed: ";
while(![Link]()) {
cout << [Link]();
[Link]();
}
cout << endl;
}

int main() {
reverseNumber(12345);
reverseNumber(9876);
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. Yeh program kisi number ko ulta (reverse) kar deta hai using stack.
2. n % 10 se number ka last digit milta hai. Jaise 12345 % 10 = 5.
3. n / 10 se last digit hata di jaati hai. Jaise 12345 / 10 = 1234.
4. Saare digits push hote hain: 5, 4, 3, 2, 1 (is order mein stack mein).
5. Pop karte waqt LIFO ki wajah se 1, 2, 3, 4, 5 nikalta hai — yani reversed!
6. Output: Original: 12345 → Reversed: 54321

Program 5: Stack se Undo Feature (Jaise MS Word mein Ctrl+Z)


#include<iostream>
#include<stack>
#include<string>
using namespace std;

stack<string> history; // Actions ka record

void doAction(string action) {


[Link](action);
cout << "Kiya: " << action << endl;
}

void undo() {
if([Link]()) {
cout << "Kuch nahi undo karne ko!" << endl;
} else {
cout << "Undo: " << [Link]() << endl;
[Link]();
}
}

int main() {
doAction("Type A");
doAction("Type B");
doAction("Bold kiya");
doAction("Delete kiya");
cout << "--- Undo start ---" << endl;
undo(); // Delete undo
undo(); // Bold undo
undo(); // Type B undo
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. Yeh ek simple Undo system hai — bilkul jaise MS Word ya Notepad mein Ctrl+Z kaam karta hai.
2. Har kaam karne par us kaam ka naam stack mein push ho jaata hai.
3. Jab undo karo, sab se aakhri kaam (top) wapas le lo — LIFO principle!
4. Isi liye Stack real software mein bhi undo/redo ke liye use hota hai.
5. Output: Kiya: Type A, Type B, Bold kiya, Delete kiya → Undo: Delete, Bold, Type B
CHAPTER 2: QUEUE

Queue Kya Hoti Hai?


Queue ek aisi data structure hai jo bilkul waisay kaam karti hai jaisay kisi dukan ya bus stop par line hoti
hai. Jo pehle aaya, woh pehle bahar jaata hai. Isko FIFO kehte hain — First In, First Out (jo pehle aya
woh pehle jaayega).

REAL LIFE EXAMPLE: Printer ki queue — jab aap 3 documents print karte hain, pehla document
pehle print hoga, doosra baad mein, teesra sabse aakhir mein. Yahi QUEUE hai!

Queue ki 3 Main Operations:


• ENQUEUE: Queue mein koi cheez daalna (line ke peeche se)
• DEQUEUE: Queue se cheez nikaalna (line ke aage se)
• FRONT/PEEK: Sab se aage wali cheez dekhna bina nikaale

Stack vs Queue farq:


Feature Stack Queue

Order LIFO (Last In First Out) FIFO (First In First Out)

Insertion Top par (push) Rear/Back se (enqueue)

Removal Top se (pop) Front se (dequeue)

Example Plates ki dher Line mein logon ka intezaar

Program 1: Basic Queue using Array


#include<iostream>
using namespace std;

int queue[100];
int front = 0, rear = 0; // front=nikaalo, rear=daalo

void enqueue(int value) {


if(rear >= 100) { cout << "Queue Full!" << endl; return; }
queue[rear] = value;
rear++;
cout << value << " line mein aaya" << endl;
}

void dequeue() {
if(front == rear) { cout << "Queue khali!" << endl; return; }
cout << queue[front] << " bahar gaya" << endl;
front++;
}

void showFront() {
if(front == rear) cout << "Queue khali!" << endl;
else cout << "Aage wala: " << queue[front] << endl;
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
showFront(); // 10 dikhega
dequeue(); // 10 nikala
showFront(); // 20 dikhega
dequeue(); // 20 nikala
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. front — yeh batata hai queue ka aagla end kahan hai (yahan se cheezein nikalti hain).
2. rear — yeh batata hai queue ka peechla end kahan hai (yahan se cheezein daali jaati hain).
3. enqueue() mein: queue[rear] mein value rakho, phir rear ek badha do.
4. dequeue() mein: queue[front] se nikalo, phir front ek badha do.
5. front == rear ka matlab hai queue bilkul khali hai.
6. Output: 10, 20, 30 line mein → front: 10 → 10 bahar → front: 20 → 20 bahar

Program 2: Queue using Class (OOP Style)


#include<iostream>
using namespace std;

class Queue {
int arr[100];
int front, rear, count;
public:
Queue() { front=0; rear=0; count=0; }

void enqueue(int x) {
if(count == 100) { cout << "Full!" << endl; return; }
arr[rear] = x;
rear = (rear + 1) % 100; // Circular movement
count++;
}

int dequeue() {
if(count == 0) { cout << "Khali!" << endl; return -1; }
int val = arr[front];
front = (front + 1) % 100;
count--;
return val;
}

bool isEmpty() { return count == 0; }


int size() { return count; }

void display() {
cout << "Queue (Front se): ";
int i = front;
for(int j = 0; j < count; j++) {
cout << arr[i] << " ";
i = (i+1) % 100;
}
cout << endl;
}
};

int main() {
Queue q;
[Link](1); [Link](2); [Link](3);
[Link]();
cout << "Dequeue: " << [Link]() << endl;
[Link]();
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. count — kitni cheezein queue mein hain yeh track karta hai.
2. (rear + 1) % 100 — Circular Queue technique: array ka akhri cell use hone ke baad phir pehle se shuru.
3. Yeh class-based approach zyada organized aur safe hai.
4. display() mein front se lekar saari items print hoti hain.
5. Output: Queue: 1 2 3 → Dequeue: 1 → Queue: 2 3

Program 3: Queue se Printer Simulation


#include<iostream>
#include<queue> // C++ built-in queue
#include<string>
using namespace std;

void printDocuments() {
queue<string> printer;

// Documents print queue mein daalo


[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");

cout << "Total documents: " << [Link]() << endl;


cout << "Printing shuru..." << endl << endl;

int num = 1;
while(![Link]()) {
cout << "Printing #" << num << ": " << [Link]() << endl;
[Link](); // Print ho gaya, nikaalo
num++;
}
cout << "Sab print ho gaya!" << endl;
}

int main() {
printDocuments();
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. queue — C++ ki built-in queue use ki jisme strings store hoti hain.
2. [Link]() — document queue mein daalna (enqueue).
3. [Link]() — sabse pehle wala document dekhna.
4. [Link]() — document print ho jaane ke baad queue se hatana (dequeue).
5. [Link]() — check karna ke queue khali toh nahi — loop yahan rukta hai.
6. Output: [Link] pehle, phir [Link], phir [Link], phir [Link]

Program 4: Queue se Bank Counter Simulation


#include<iostream>
#include<queue>
#include<string>
using namespace std;

int main() {
queue<string> bankLine;

// Log aate gaye


[Link]("Ahmed");
[Link]("Sara");
[Link]("Ali");
[Link]("Fatima");
[Link]("Hassan");

cout << "Bank Counter Simulation" << endl;


cout << "Total log: " << [Link]() << endl << endl;

int token = 1;
while(![Link]()) {
cout << "Token #" << token << " - ";
cout << [Link]() << " counter par aao" << endl;
[Link]();
token++;
}

cout << endl << "Sab ka kaam ho gaya!" << endl;


return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. Yeh program real-world bank queue ko simulate karta hai.
2. Har customer ka naam queue mein push hota hai jab woh aata hai.
3. Counter par kaam hone ke baad front() se naam dekhte hain aur pop() se hatate hain.
4. FIFO principle: Ahmed pehle aaya, Ahmed pehle counter par jaayega.
5. Output: Ahmed ko token 1, Sara ko token 2... — bilkul real bank ki tarah!

Program 5: Double Ended Queue - DEQUE (Dono Siron se Insert/Delete)


#include<iostream>
#include<deque> // Double Ended Queue
using namespace std;

int main() {
deque<int> dq;

// Aage se bhi daalo, peeche se bhi


dq.push_back(10); // Peeche daalo: [10]
dq.push_back(20); // Peeche daalo: [10,20]
dq.push_front(5); // Aage daalo: [5,10,20]
dq.push_front(1); // Aage daalo: [1,5,10,20]

cout << "Deque contents: ";


for(int x : dq) cout << x << " ";
cout << endl;

dq.pop_front(); // Aage se nikalo: [5,10,20]


dq.pop_back(); // Peeche se nikalo: [5,10]

cout << "After removals: ";


for(int x : dq) cout << x << " ";
cout << endl;

cout << "Front: " << [Link]() << endl;


cout << "Back: " << [Link]() << endl;
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. DEQUE (Double Ended Queue) — Yeh special queue hai jisme dono ends se insert/delete ho sakta
hai.
2. push_back() — peeche se daalna (normal queue jaisay).
3. push_front() — aage se daalna (yeh normal queue mein nahi hota!).
4. pop_front() / pop_back() — aage ya peeche se nikaalna.
5. Real use: Undo-redo systems, sliding window algorithms.
6. Output: 1 5 10 20 → After removals: 5 10 → Front: 5, Back: 10
CHAPTER 3: TREE

Tree Kya Hota Hai?


Tree ek aisi data structure hai jo ek ped (tree) ki tarah dikhti hai — lekin ulti (roots upar, branches
neeche). Iska ek main node hota hai jise Root kehte hain. Har node ke neeche aur nodes ho sakti hain
jinhe Children kehte hain. Tree hierarchical data represent karta hai — jaise family tree ya folder
structure.

REAL LIFE EXAMPLE: Aapke computer mein C:/Documents/My Folder/[Link] — yeh folder structure
ek tree hai! C: root hai, Documents child hai, My Folder uska child hai.

Tree ki Important Terms:


• Root: Tree ka sab se upar wala node (sirf ek hota hai)
• Node: Tree ka har ek element
• Parent: Jis node ke neeche doosre nodes hon
• Child: Jo node kisi doosre ke neeche ho
• Leaf: Woh node jiske koi children na hon (end node)
• Height: Root se sab se neeche tak kitne levels hain
• Left / Right Child: Binary tree mein har node ke sirf 2 bachche ho sakte hain

Binary Search Tree (BST) — Sabse Important Tree:


BST mein: Chhoti values BAAYE (left) jaati hain, Badi values DAYEN (right) jaati hain. Isi tarah
searching bahut fast ho jaati hai!

Tree Traversal (Tree mein Ghoomna) — 3 Tarike:


• Inorder (Left → Root → Right): Sorted order mein data milta hai
• Preorder (Root → Left → Right): Root pehle, phir children
• Postorder (Left → Right → Root): Children pehle, phir root

Program 1: Binary Tree Banana aur Inorder Traversal


#include<iostream>
using namespace std;

// Tree ka ek node
struct Node {
int data;
Node* left; // Left child
Node* right; // Right child
Node(int val) {
data = val;
left = NULL; // Shuru mein koi child nahi
right = NULL;
}
};

// Inorder: Left, Root, Right


void inorder(Node* root) {
if(root == NULL) return; // Base case
inorder(root->left); // Pehle left jao
cout << root->data << " "; // Root print karo
inorder(root->right); // Phir right jao
}

int main() {
// Tree manually banao
Node* root = new Node(10); // Root
root->left = new Node(5); // Left child
root->right = new Node(15); // Right child
root->left->left = new Node(3);
root->left->right = new Node(7);

cout << "Inorder: ";


inorder(root); // Output: 3 5 7 10 15
cout << endl;
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. struct Node — Tree ka ek daana (node). Isme data, left pointer aur right pointer hain.
2. Node* left / right — Yeh pointers hain jo agley nodes ki taraf ishara karte hain.
3. new Node(val) — Naya node memory mein banata hai.
4. inorder() recursively pehle left jata hai, phir root print karta hai, phir right.
5. Recursion ka matlab: function khud apne aap ko call karta hai chhote input ke saath.
6. Tree structure: 10 root, 5 left, 15 right, 3 aur 7 left ke children. Inorder: 3 5 7 10 15

Program 2: Binary Search Tree (BST) mein Insert aur Search


#include<iostream>
using namespace std;

struct Node {
int data;
Node *left, *right;
Node(int v): data(v), left(NULL), right(NULL){}
};

Node* insert(Node* root, int val) {


if(root == NULL) return new Node(val); // Khali jagah mili
if(val < root->data) // Chhota = left
root->left = insert(root->left, val);
else // Bara = right
root->right = insert(root->right, val);
return root;
}

bool search(Node* root, int val) {


if(root == NULL) return false; // Nahi mila
if(root->data == val) return true; // Mila!
if(val < root->data) // Chhota: left mein dhundho
return search(root->left, val);
return search(root->right, val); // Bara: right mein
}

int main() {
Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);

cout << search(root, 40) << endl; // 1 = mila


cout << search(root, 99) << endl; // 0 = nahi mila
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. BST Rule: Chhoti value left mein jaati hai, badi value right mein jaati hai.
2. insert() recursively sahi jagah dhundta hai aur wahan node rakhta hai.
3. search() bhi recursively kaam karta hai — agar value chhoti toh left jao, badi toh right.
4. BST mein searching O(log n) hoti hai — bahut fast! Normal array se zyada.
5. Tree: 50 root → 30 left, 70 right → 20,40 left ke children.
6. Output: 1 (40 mila), 0 (99 nahi mila)

Program 3: Preorder aur Postorder Traversal


#include<iostream>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node(int v): data(v), left(NULL), right(NULL){}
};

// Preorder: Root, Left, Right


void preorder(Node* root) {
if(root == NULL) return;
cout << root->data << " "; // Pehle root
preorder(root->left); // Phir left
preorder(root->right); // Phir right
}

// Postorder: Left, Right, Root


void postorder(Node* root) {
if(root == NULL) return;
postorder(root->left); // Pehle left
postorder(root->right); // Phir right
cout << root->data << " "; // Aakhir mein root
}

// Inorder: Left, Root, Right


void inorder(Node* root) {
if(root == NULL) return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

int main() {
Node* r = new Node(1);
r->left = new Node(2);
r->right = new Node(3);
r->left->left = new Node(4);
r->left->right = new Node(5);

cout << "Preorder: "; preorder(r); cout << endl;


cout << "Inorder: "; inorder(r); cout << endl;
cout << "Postorder: "; postorder(r); cout << endl;
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. Preorder (Root-Left-Right): Pehle root khud print hota hai, phir bachche.
2. Inorder (Left-Root-Right): Left bachcha, phir root, phir right bachcha.
3. Postorder (Left-Right-Root): Pehle dono bachche, phir root — delete karne mein useful.
4. Same tree, alag order mein ghoomne se alag output milta hai.
5. Tree: 1 root, 2 left, 3 right, 4 aur 5 (2 ke children).
6. Output → Preorder: 1 2 4 5 3 | Inorder: 4 2 5 1 3 | Postorder: 4 5 2 3 1

Program 4: Tree ki Height/Depth Nikalna


#include<iostream>
using namespace std;

struct Node {
int data;
Node *left, *right;
Node(int v): data(v), left(NULL), right(NULL){}
};

// Height = Root se sab se deep leaf tak kitne levels


int height(Node* root) {
if(root == NULL) return 0; // Khali tree ki height 0

int leftH = height(root->left); // Left ki height


int rightH = height(root->right); // Right ki height

// Jo side zyada uski height + 1 (khud ke liye)


return max(leftH, rightH) + 1;
}

int countNodes(Node* root) {


if(root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}

int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->left->left = new Node(5);

cout << "Height: " << height(root) << endl;


cout << "Total Nodes: " << countNodes(root) << endl;
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. height() function recursively har node ki left aur right subtree ki height nikalti hai.
2. Jo side zyada deep ho, woh + 1 (khud ke liye) return hoti hai.
3. max(leftH, rightH) — dono mein se bara choose karo.
4. countNodes() isi tarah kaam karta hai: 1 (khud) + left tree nodes + right tree nodes.
5. Is tree ki height 4 hai (1 → 2 → 4 → 5) aur total 5 nodes hain.
6. Output: Height: 4 | Total Nodes: 5

Program 5: BST se Minimum aur Maximum Value Nikalna


#include<iostream>
using namespace std;

struct Node {
int data;
Node *left, *right;
Node(int v): data(v), left(NULL), right(NULL){}
};

Node* insert(Node* root, int val) {


if(!root) return new Node(val);
if(val < root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}

// Minimum = BST mein sab se left wala


int findMin(Node* root) {
if(root->left == NULL) return root->data;
return findMin(root->left); // Aur left jao
}

// Maximum = BST mein sab se right wala


int findMax(Node* root) {
if(root->right == NULL) return root->data;
return findMax(root->right); // Aur right jao
}

int main() {
Node* root = NULL;
int values[] = {50, 30, 70, 10, 40, 60, 90};
for(int v : values)
root = insert(root, v);

cout << "Minimum: " << findMin(root) << endl; // 10


cout << "Maximum: " << findMax(root) << endl; // 90
return 0;
}

Explanation (Yeh code kaise kaam karta hai):


1. BST ki khaas baat: Minimum value hamesha sab se left (baaye) node mein hoti hai.
2. Maximum value hamesha sab se right (dayen) node mein hoti hai.
3. findMin() har baar left jaata rehta hai jab tak left NULL na ho jaye.
4. findMax() har baar right jaata rehta hai jab tak right NULL na ho jaye.
5. Values: 50 root → 30,70 children → 10,40,60,90 grandchildren.
6. Output: Minimum: 10 | Maximum: 90
QUICK REVISION SUMMARY

Kal ke paper ke liye yaad rakhne wali cheezein:

Topic Key Concept Order Real Example

LIFO
STACK Ek dher Plates, Ctrl+Z
(Last In First Out)

FIFO
QUEUE Line / Antaar Printer, Bank line
(First In First Out)

TREE Ped (hierarchy) Root → Children File system, Family tree

Important Functions Yaad Rakhein:


• Stack: push() — daalna | pop() — nikaalna | top/peek() — dekhna
• Queue: enqueue/push() — daalna | dequeue/pop() — nikaalna | front() — dekhna
• Tree: insert() — node daalna | search() — dhundna | inorder/preorder/postorder() — traversal
• BST Min: Hamesha sab se left node mein — findMin() left jaata rehta hai
• BST Max: Hamesha sab se right node mein — findMax() right jaata rehta hai
• Tree Height: max(leftHeight, rightHeight) + 1 — recursion se

BEST OF LUCK PAPER MEIN! Yaad rakho: Stack = LIFO, Queue = FIFO, Tree = Hierarchy. BST
mein chhota left, bara right. Traversal: In=Left-Root-Right, Pre=Root-Left-Right,
Post=Left-Right-Root.

You might also like