SlideShare a Scribd company logo
Binary Search Tree In Python
SlideMake.com
1
Introduction to Binary Search Tree
• A binary search tree is a data structure that organizes data in a
hierarchical manner.
• It is a type of binary tree where each node has at most two children.
• The left child of a node contains a value smaller than the parent,
while the right child contains a value greater than the parent.
2
Operations on Binary Search Tree
• Insertion: To add a new node to the tree, we compare the value with
the current node and follow the left or right child accordingly until we
find an empty spot.
• Deletion: When deleting a node, we need to consider three cases:
• - If the node has no children, we can simply remove it.
• - If the node has one child, we replace it with its child.
• - If the node has two children, we find the next smallest value (or
next largest) in the tree and replace it with the node to be deleted.
• Your third bullet
3
Traversing a Binary Search Tree
• Inorder Traversal: In this traversal, we visit the left subtree, then the
root, and finally the right subtree. This results in a sorted list of
values.
• Preorder Traversal: In this traversal, we visit the root, then the left
subtree, and finally the right subtree.
• Postorder Traversal: In this traversal, we visit the left subtree, then
the right subtree, and finally the root.
4
Searching in a Binary Search Tree
• To search for a value in a binary search tree, we compare the value
with the current node.
• If the value matches, we have found the node.
• If the value is smaller, we move to the left child.
5
Advantages of Binary Search Tree
• Efficient searching: Due to the hierarchical structure of the tree,
search operations can be performed quickly by eliminating half of
the remaining nodes at each step.
• Sorted storage: Inorder traversal of a binary search tree gives the
elements in sorted order, which can be useful in certain scenarios.
• Insertion and deletion flexibility: Binary search trees allow dynamic
insertion and deletion of nodes, maintaining the tree's balance
through rotation operations.
6
Disadvantages of Binary Search Tree
• Imbalanced trees: If the elements are inserted in a specific order, the
tree can become unbalanced, resulting in poor performance for
search operations.
• Lack of uniqueness: Binary search trees do not inherently enforce
uniqueness of values, which means duplicate values can be
inserted.
• Memory overhead: Binary search trees require additional memory to
store the tree structure, including pointers to left and right children.
7
Conclusion
• Binary search trees are a versatile data structure for organizing and
accessing data efficiently.
• They offer advantages such as efficient searching, sorted storage,
and flexibility in insertion and deletion.
• However, they can suffer from imbalanced trees, lack of uniqueness,
and memory overhead. Careful consideration of the data and
appropriate balancing techniques can help mitigate these issues.

More Related Content

Similar to Binary Search Tree In Python.pptx

Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
SURBHI SAROHA
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
ssuser039bf6
 
Binary search
Binary search Binary search
Binary search
Ramkrishna bhagat
 
Advanced Trees
Advanced TreesAdvanced Trees
Advanced Trees
Selvaraj Seerangan
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
Rj Juhith
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
KeshavBandil2
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Sana Yameen
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
INAM352782
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
INAM352782
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
SurajSharma266169
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
Dr.Shweta
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan
 
Binary tree
Binary tree Binary tree
Binary tree
Rajendran
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Main MeMory Data Base
Main MeMory Data BaseMain MeMory Data Base
Main MeMory Data Base
Siva Rushi
 

Similar to Binary Search Tree In Python.pptx (20)

Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
 
Binary search
Binary search Binary search
Binary search
 
Advanced Trees
Advanced TreesAdvanced Trees
Advanced Trees
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Main MeMory Data Base
Main MeMory Data BaseMain MeMory Data Base
Main MeMory Data Base
 

More from RohanThota3

Aluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminiumAluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminium
RohanThota3
 
readerwriter-190410082551.pdf
readerwriter-190410082551.pdfreaderwriter-190410082551.pdf
readerwriter-190410082551.pdf
RohanThota3
 
Arithmetic operators
Arithmetic operatorsArithmetic operators
Arithmetic operators
RohanThota3
 
22R01A66C1.pptx
22R01A66C1.pptx22R01A66C1.pptx
22R01A66C1.pptx
RohanThota3
 
22R01A67C1.pptx
22R01A67C1.pptx22R01A67C1.pptx
22R01A67C1.pptx
RohanThota3
 
M.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptxM.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptx
RohanThota3
 

More from RohanThota3 (6)

Aluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminiumAluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminium
 
readerwriter-190410082551.pdf
readerwriter-190410082551.pdfreaderwriter-190410082551.pdf
readerwriter-190410082551.pdf
 
Arithmetic operators
Arithmetic operatorsArithmetic operators
Arithmetic operators
 
22R01A66C1.pptx
22R01A66C1.pptx22R01A66C1.pptx
22R01A66C1.pptx
 
22R01A67C1.pptx
22R01A67C1.pptx22R01A67C1.pptx
22R01A67C1.pptx
 
M.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptxM.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptx
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 

Binary Search Tree In Python.pptx

  • 1. Binary Search Tree In Python SlideMake.com
  • 2. 1 Introduction to Binary Search Tree • A binary search tree is a data structure that organizes data in a hierarchical manner. • It is a type of binary tree where each node has at most two children. • The left child of a node contains a value smaller than the parent, while the right child contains a value greater than the parent.
  • 3. 2 Operations on Binary Search Tree • Insertion: To add a new node to the tree, we compare the value with the current node and follow the left or right child accordingly until we find an empty spot. • Deletion: When deleting a node, we need to consider three cases: • - If the node has no children, we can simply remove it. • - If the node has one child, we replace it with its child. • - If the node has two children, we find the next smallest value (or next largest) in the tree and replace it with the node to be deleted. • Your third bullet
  • 4. 3 Traversing a Binary Search Tree • Inorder Traversal: In this traversal, we visit the left subtree, then the root, and finally the right subtree. This results in a sorted list of values. • Preorder Traversal: In this traversal, we visit the root, then the left subtree, and finally the right subtree. • Postorder Traversal: In this traversal, we visit the left subtree, then the right subtree, and finally the root.
  • 5. 4 Searching in a Binary Search Tree • To search for a value in a binary search tree, we compare the value with the current node. • If the value matches, we have found the node. • If the value is smaller, we move to the left child.
  • 6. 5 Advantages of Binary Search Tree • Efficient searching: Due to the hierarchical structure of the tree, search operations can be performed quickly by eliminating half of the remaining nodes at each step. • Sorted storage: Inorder traversal of a binary search tree gives the elements in sorted order, which can be useful in certain scenarios. • Insertion and deletion flexibility: Binary search trees allow dynamic insertion and deletion of nodes, maintaining the tree's balance through rotation operations.
  • 7. 6 Disadvantages of Binary Search Tree • Imbalanced trees: If the elements are inserted in a specific order, the tree can become unbalanced, resulting in poor performance for search operations. • Lack of uniqueness: Binary search trees do not inherently enforce uniqueness of values, which means duplicate values can be inserted. • Memory overhead: Binary search trees require additional memory to store the tree structure, including pointers to left and right children.
  • 8. 7 Conclusion • Binary search trees are a versatile data structure for organizing and accessing data efficiently. • They offer advantages such as efficient searching, sorted storage, and flexibility in insertion and deletion. • However, they can suffer from imbalanced trees, lack of uniqueness, and memory overhead. Careful consideration of the data and appropriate balancing techniques can help mitigate these issues.