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

All you need to know about “Off-page SEO”

Blog banner

Virtual memory in windows

Blog banner

It's all about our Brain.- The Brain Metaphor

Blog banner

Explaining Buffer Overflow with Example

Blog banner

How secured are you online ?

Blog banner

Cloud Forensic Tools And Storage :A Review Paper

Blog banner

IP ADDRESS

Blog banner

security controls

Blog banner

Technical SEO : Total Guide

Blog banner

Network Security Risks

Blog banner

Cyber Security in Quantum and Space Communication

Blog banner

IT Service as as Value Creation

Blog banner

OPERATING SYSTEM OBJECTIVES AND FAULT TOLERENCE.

Blog banner

Process Description

Blog banner

Cyber Attacks -- Trends Patterns and Security Countermeasures

Blog banner

Types of threads

Blog banner

Vikrant’s first blog

Blog banner

How to kiss

Blog banner

What is Anxiety? How to manage Anxiety?

Blog banner

Direct Memory Access

Blog banner

Device driver

Blog banner

Raid and levels of raid.

Blog banner

INDIAN CHEAPEST COSMETICS BRAND

Blog banner

Improving the Accuracy of GPS and GNSS

Blog banner

Full Disk Encryption

Blog banner

IoT Evolution

Blog banner

Cloud Computing

Blog banner

JUSTICE FOR EVERY “BEZUBAAN ANIMAL”

Blog banner

Deadlock in Operating systems

Blog banner

World end

Blog banner

Efficiency of SQL Injection Method in Preventing E-Mail Hacking

Blog banner

Navigation With Indian Constellation(NavIC) by ISRO in Geographic Information Systems

Blog banner

Interrupts in operating system.

Blog banner

Virtual Memory

Blog banner

Deadlock and Starvation

Blog banner

Memory Management of Operating System(OS)

Blog banner

Tiranga - Abbas Haveliwala

Blog banner

E-Governance

Blog banner

How India made the GIS its Own, and its Use in Infrastructural Developments

Blog banner

PERSONAL STORIES

Blog banner

Real Time Scheduling

Blog banner

GraphX:- A graph processing tool

Blog banner