SlideShare ist ein Scribd-Unternehmen logo
1 von 10
BINARY SEARCH
TREE
Group leader:
Jethanand
Class: BSCS – 3rd A
BINARY SEARCH TREE:
• Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers. It is called a binary tree because each tree node has
a maximum of two children. It is called a search tree because it can be used
to search for the presence of a number in O(log(n)) time.
STRUCTURE:
• Each node in a BST contains a key/value pair and has
at most two children: a left child and a right child.
• The left child contains a key/value pair smaller than
its parent, while the right child contains a key/value
pair greater than its parent.
• This property holds true for every node in the tree,
making it easy to search for elements.
BASIC OPERATIONS
In-order Traversal − Traverses a tree in an in-order manner.
Pre-order Traversal − Traverses a tree in a pre-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
Search − Searches an element in a tree.
Insert − Inserts an element in a Tree.
Deletion- Delete an element in tree.
TEVERSAL IN BINARY SEARCH TREE:
• 1. In order Teversal:
Visit the left subtree, then the current node, and
finally the right subtree. It prints the nodes in ascending order.(L,N,R)
• Example: 3,4,5,7,9,14,15,16,17,18,20
• 2. Preorder Traversal:
Visit the current node, then the left subtree, and
finally the right subtree. (N,L,R)
• Example: 14,4,3,9,7,5,15,18,16,17,20
• 3. Post order Traversal:
Visit the left subtree, then the right subtree, and
finally the current node. (L,R,N)
• Example: 3,5,7,9,4,17,16,20,18,15,14
SEARCH OPERATION:
• The key property of a BST enables efficient searching. Starting from the root, we
compare the target value with the current node.
• If the target is smaller, we move to the left child; if it's larger, we move to the right
child.
• We repeat this process until we find a match or reach a null node, indicating that
the value is not present in the tree.
• The search operation has an average and worst-case time complexity of O(log n) in
balanced BSTs, where n is the number of elements.
EXAMPLE:
• Consider the graph shown below and the key = 6.
• Step 1: Initially compare the key with the root i.e., 8. As 6
is less than 8, search in the left subtree of 8.
• Step 2: Now compare the key with 3. As key is greater
than 3, search next in the right subtree of 3.
• Step 3:Now compare the key with 6. The value of the key
is 6. So we have found the key.
INSERTION OPERATION:
 The insertion logic into BST is similar to its searching operation. A new value is always inserted
at the leaf node of the BST.
 Compare the value with the root of the BST.
 If the value to be inserted is less than the root, move to the left subtree.
 Otherwise, if the value is greater than the root, move to the right subtree.
 Continue this process, until we hit a leaf node.
 If the value is less than the leaf, create a left child of the leaf and insert the value.
 Otherwise, if the value is greater than the leaf, create a right child of the leaf and insert the value
in the right child.
EXAMPLE:
• Consider the following BST and the value
= 40 to be added.
• Initially, 40 is less than 100. So move to
the left subtree.
• Now, 40 is greater than 20. So move to
the right subtree.
• Now we reach the leaf node 30. As 40 is
greater than 30, create right child of 30
and insert the value 40.
After insertion
Before insertion
DELETION OPERATION
 Deleting a node from a BST involves three possible cases:
 Case 1: The node has no children: We can simply remove the node.
 Case 2: The node has one child: We replace the node with its child.
 Case 3: The node has two children: We find the minimum value in the right subtree (or the maximum value
in the left subtree) and replace the node with it, then delete the duplicate value from the subtree.
 The time complexity of the deletion operation is also O(log n) in balanced BSTs.

Weitere ähnliche Inhalte

Ähnlich wie Binary search tree.pptx

Ähnlich wie Binary search tree.pptx (20)

VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 
Heap sort
Heap sortHeap sort
Heap sort
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Sorting
SortingSorting
Sorting
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Kürzlich hochgeladen (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Binary search tree.pptx

  • 2. BINARY SEARCH TREE: • Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers. It is called a binary tree because each tree node has a maximum of two children. It is called a search tree because it can be used to search for the presence of a number in O(log(n)) time.
  • 3. STRUCTURE: • Each node in a BST contains a key/value pair and has at most two children: a left child and a right child. • The left child contains a key/value pair smaller than its parent, while the right child contains a key/value pair greater than its parent. • This property holds true for every node in the tree, making it easy to search for elements.
  • 4. BASIC OPERATIONS In-order Traversal − Traverses a tree in an in-order manner. Pre-order Traversal − Traverses a tree in a pre-order manner. Post-order Traversal − Traverses a tree in a post-order manner. Search − Searches an element in a tree. Insert − Inserts an element in a Tree. Deletion- Delete an element in tree.
  • 5. TEVERSAL IN BINARY SEARCH TREE: • 1. In order Teversal: Visit the left subtree, then the current node, and finally the right subtree. It prints the nodes in ascending order.(L,N,R) • Example: 3,4,5,7,9,14,15,16,17,18,20 • 2. Preorder Traversal: Visit the current node, then the left subtree, and finally the right subtree. (N,L,R) • Example: 14,4,3,9,7,5,15,18,16,17,20 • 3. Post order Traversal: Visit the left subtree, then the right subtree, and finally the current node. (L,R,N) • Example: 3,5,7,9,4,17,16,20,18,15,14
  • 6. SEARCH OPERATION: • The key property of a BST enables efficient searching. Starting from the root, we compare the target value with the current node. • If the target is smaller, we move to the left child; if it's larger, we move to the right child. • We repeat this process until we find a match or reach a null node, indicating that the value is not present in the tree. • The search operation has an average and worst-case time complexity of O(log n) in balanced BSTs, where n is the number of elements.
  • 7. EXAMPLE: • Consider the graph shown below and the key = 6. • Step 1: Initially compare the key with the root i.e., 8. As 6 is less than 8, search in the left subtree of 8. • Step 2: Now compare the key with 3. As key is greater than 3, search next in the right subtree of 3. • Step 3:Now compare the key with 6. The value of the key is 6. So we have found the key.
  • 8. INSERTION OPERATION:  The insertion logic into BST is similar to its searching operation. A new value is always inserted at the leaf node of the BST.  Compare the value with the root of the BST.  If the value to be inserted is less than the root, move to the left subtree.  Otherwise, if the value is greater than the root, move to the right subtree.  Continue this process, until we hit a leaf node.  If the value is less than the leaf, create a left child of the leaf and insert the value.  Otherwise, if the value is greater than the leaf, create a right child of the leaf and insert the value in the right child.
  • 9. EXAMPLE: • Consider the following BST and the value = 40 to be added. • Initially, 40 is less than 100. So move to the left subtree. • Now, 40 is greater than 20. So move to the right subtree. • Now we reach the leaf node 30. As 40 is greater than 30, create right child of 30 and insert the value 40. After insertion Before insertion
  • 10. DELETION OPERATION  Deleting a node from a BST involves three possible cases:  Case 1: The node has no children: We can simply remove the node.  Case 2: The node has one child: We replace the node with its child.  Case 3: The node has two children: We find the minimum value in the right subtree (or the maximum value in the left subtree) and replace the node with it, then delete the duplicate value from the subtree.  The time complexity of the deletion operation is also O(log n) in balanced BSTs.