wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Binary Search Tree (BST) in Data Structure

profile
08_Shruti Gupta
Oct 14, 2024
0 Likes
0 Discussions
119 Reads

Introduction - Binary Search Tree ek special type ka binary tree hai jisme har node ke left side wale subtree ke sabhi nodes chhote hote hain root node se, aur right side wale subtree ke nodes bade hote hain. Yeh structure aise banaya jata hai taaki searching, insertion, aur deletion ka process efficient ho sake.


Basic Operations in BST:

1. Searching: BST mein searching kaafi fast hoti hai kyunki hamesha ek path follow hota hai. Jab hume koi value search karni hoti hai, toh pehle root node ko check karte hain. Agar root node se chhoti value ho toh left subtree mein jaate hain, aur agar badi ho toh right subtree mein jaate hain. Is tarah se hume ek specific element ka search time log(n) hota hai, jaha n total number of nodes hai. Ek example se samajhte hai agar tree kuch aisa hai:

50

/ \

30 70

/ \ / \

20 40 60 80

Ab agar hume 40 search karna hai, toh pehle root (50) check hoga, fir 40 chhota hai toh left mein jaayenge, fir 30 check hoga, 40 bada hai toh right mein jaayenge, aur hume 40 mil jayega.


2. Insertion: BST mein new element insert karte waqt hume same rule follow karna padta hai. Agar new value root node se chhoti hai toh left subtree mein insert karenge, aur badi hai toh right subtree mein.

Example: Agar hume 65 insert karna ho, toh 50 se bada hai toh right mein jaayenge, fir 70 se chhota hai toh 60 ke right mein insert karenge.


3. Deletion: BST se koi node delete karna thoda tricky hota hai. Teen cases hote hain:

  • Leaf node (no children) - Simply delete kar dete hain.
  • One child - Parent node ko directly child se connect kar dete hain.
  • Two children - Inorder successor ya predecessor find karte hain aur usko delete wali node se replace karte hain.



Balanced vs Unbalanced BST :- Ek balanced BST mein left aur right subtree ke height mein zyada fark nahi hota. Agar tree balanced ho, toh saari operations ki time complexity log(n) hoti hai. Lekin agar tree unbalanced ho jaaye, jaise ek skewed tree ban jaye, toh worst case mein searching, insertion, aur deletion ki time complexity O(n) ho jaati hai.


1. Skewed Tree Example (Unbalanced):

10

\

20

\

30

Yahan sab nodes ek side mein hain, isliye yeh ek unbalanced tree hai. Isme searching slow ho jaati hai.

2. Balanced Tree Examples:

AVL Tree: Yeh ek self-balancing BST hota hai jisme har node ka height difference (balance factor) -1, 0, ya 1 hota hai. Agar kabhi insertion ya deletion se tree imbalance ho jaaye, toh rotations ke through balance kiya jaata hai.

Red-Black Tree: Isme har node ko red ya black color assign hota hai aur rules follow kiye jaate hain taaki tree balanced rahe.


BST ka Time Complexity:-

1.Best Case (Balanced Tree) - O(log n)

2.Worst Case (Unbalanced Tree) - O(n)

Iska matlab agar tree balanced ho, toh hume searching, insertion, aur deletion efficiently karne mein time lagta hai, lekin agar tree unbalanced ho jaaye toh performance degrade ho jaati hai.


Real-World Applications of BST:

1.Databases: Searching aur indexing ke liye BST ka use hota hai, jisme hume records ko efficiently access karna hota hai.

2.Compiler Design: Symbol tables ko manage karne ke liye BST ka use hota hai.

3.File Systems: File hierarchies ko organize karne ke liye BST kaafi useful hota hai.

4.Memory Allocation: Dynamic memory allocation algorithms BST ka use karke free memory blocks ko track karte hain.


BST vs Other Data Structures: BST ko Hash Table aur Heap ke saath compare karein toh BST sorted data ko maintain kar sakta hai, jo hash tables mein possible nahi hota. Lekin hash tables searching mein BST se fast hote hain. Heap ka use jab min ya max element ko fast retrieve karna ho tab hota hai, lekin heap mein sorted order maintain nahi hoti.


Toh finally Binary Search Tree ek powerful data structure hai jo searching, sorting, aur dynamic data ke efficient management ke liye kaafi useful hota hai. Agar tree balanced rahe toh performance excellent hoti hai, lekin unbalanced tree se performance degrade ho sakti hai.


 


Comments ()


Sign in

Read Next

Outlook mail

Blog banner

Best password managers: Reviews of the top products

Blog banner

Segmentation and paging concept

Blog banner

Are Social Media Paid Campaigns Worth It?

Blog banner

undefined

Blog banner

Strengthening Active Directory Security

Blog banner

MULTITHREADING:ENHANCEING PERFORMANCE AND EFFICIENCY IN COMPUTING

Blog banner

How to tie a Tie

Blog banner

Message Passing in OS

Blog banner

MODERN OPERATING SYSTEM

Blog banner

Stephen Hawking : A Remarkable Physicist

Blog banner

Metasploit

Blog banner

"Games and the future"

Blog banner

Define Instagram.

Blog banner

BrainGate Technology

Blog banner

Analysis of Digital Evidence In Identity Theft Investigations

Blog banner

Chicken Dum Biryani

Blog banner

Quality check in IT services

Blog banner

Memory Management Techniques

Blog banner

File management -disha parekh

Blog banner

Computer Security

Blog banner

Data carving - using hex editor

Blog banner

SECURITY RISKS OF REMOTE WORKING

Blog banner

SQL Injection Techniques

Blog banner

ACHIEVEMENTS IN OPERATING SYSTEMS

Blog banner

Stories Woven in Silk: The Meaning Behind Patola Motifs

Blog banner

Guidelines for a Low sodium Diet.

Blog banner

Some web vulnerabilities

Blog banner

operating system

Blog banner

Women Empowerment

Blog banner

Process State

Blog banner

E-learning in today's world

Blog banner

Exploring Florida Beyond the Obvious Tourist Trails

Blog banner

Kernel in Operating System

Blog banner

Security Issues and Countermeasures

Blog banner

MySQL

Blog banner

Confidence Building Activities for the New School Year

Blog banner

Booting Process In Operating System

Blog banner

A small world of Sockets

Blog banner

Deadlock

Blog banner

Importance Of Yoga.

Blog banner

COMPUTER FORENSICS AND GRAPHICS

Blog banner