SlideShare a Scribd company logo
1 of 14
Group Member:
Mohammad Sajid
Hamza Khan Abbasi
Topic
Binary Search Tree
Binary Search Tree:
• In a binary tree, every node can have maximum of
two children.
•All children to the left of a node have smaller values
than parent.
•All children to the right of a node have larger values.
Operations On Binary Search Tree:
•Search
•Insertion
•Deletion
Search Operation Algorithms in BST:
Step 1: Read the search element
Step 2: Compare, search element with the value of root node in the tree.
Step 3: If both are matching, then display "Given node found!!!" and
terminate the function
Step 4: If both are not matching, then check whether search element is
smaller or larger than that node value.
Step 5: If search element is smaller, then continue the search process in
left subtree.
Step 6: If search element is larger, then continue the search process in
right subtree.
Step 7: Repeat the same until we found exact element or we completed with a
leaf node
Step 8: If we reach to the node with search value, then display "Element is found"
and terminate the function.
Step 9: If we reach to a leaf node and it is also not matching, then display
"Element not found" and terminate the function.
Diagram:
Time Complextiy
O(log n)
Insertion Operation in BST:
Step 1: Create a newNode with given value and set
its left and right to NULL.
Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of
newNode is smaller or larger than the node (here it is root node).
Step 5: If newNode is smaller than or equal to the node, then
move to its left child. If newNode is larger than the node, then
move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (e.i.,
reach to NULL).
Step 7: After reaching a leaf node, then isert the newNode as left
child if newNode is smaller or equal to that leaf else insert it as right
child.
Diagram:
Time complexity
O(log n)
Deletion Operation in BST:
Deleting a node from Binary search tree has
follwing three cases is performed with O(log
n) time complexity.
Case 1: Deleting a Leaf node (A node with no children)
Case 2: Deleting a node with one child
Case 3: Deleting a node with two children
Case 1: Deleting a leaf node
Step 1: Find the node to be deleted using search
operation
Step 2: Delete the node using free function (If it is a leaf)
and terminate the function.
Delete (7)
Case 2: Deleting a node with one child
We use the following steps to delete a node with one child from BST...
Step 1: Find the node to be deleted using search operation
Step 2: If it has only one child, then create a link between its parent and
child nodes.
Step 3: Delete the node using free function and terminate the function.
Delete (9)
Case 3: Deleting a node with two children
We use the following steps to delete a node with two children from BST...
Step 1: Find the node to be deleted using search operation
Step 2: If it has two children, then find the largest node in its left
subtree (OR) the smallest node in its right subtree.
Step 3: Swap both deleting node and node which found in above step.
Step 4: Then, check whether deleting node came to case 1 or case 2 else
goto steps 2
Step 5: If it comes to case 1, then delete using case 1 logic.
Step 6: If it comes to case 2, then delete using case 2 logic.
Step 7: Repeat the same process until node is deleted from the tree.
Binary Search Tree (BST)
Binary Search Tree (BST)

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 

What's hot (20)

trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
2-3 Tree
2-3 Tree2-3 Tree
2-3 Tree
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
B tree
B treeB tree
B tree
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 

Similar to Binary Search Tree (BST)

Binary trees
Binary treesBinary trees
Binary trees
Amit Vats
 
Part 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docxPart 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docx
herbertwilson5999
 

Similar to Binary Search Tree (BST) (20)

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
 
Binary trees
Binary treesBinary trees
Binary trees
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
1.2 operations of tree representations
1.2 operations of tree representations 1.2 operations of tree representations
1.2 operations of tree representations
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcppt
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Binary tree
Binary treeBinary tree
Binary tree
 
Part 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docxPart 1 Your tasks for this assignment are the following1. Disc.docx
Part 1 Your tasks for this assignment are the following1. Disc.docx
 
Binary search tree.pptx
Binary search tree.pptxBinary search tree.pptx
Binary search tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Chapter Two.pdf
Chapter Two.pdfChapter Two.pdf
Chapter Two.pdf
 
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 

More from M Sajid R (6)

Transport layer
Transport layerTransport layer
Transport layer
 
Novartis
NovartisNovartis
Novartis
 
JavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentationJavaScript / Web Engineering / Web Development / html + css + js/presentation
JavaScript / Web Engineering / Web Development / html + css + js/presentation
 
Query o
Query oQuery o
Query o
 
Network And Topology
Network And Topology Network And Topology
Network And Topology
 
Toyota
ToyotaToyota
Toyota
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

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
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

Binary Search Tree (BST)

  • 1. Group Member: Mohammad Sajid Hamza Khan Abbasi Topic Binary Search Tree
  • 2. Binary Search Tree: • In a binary tree, every node can have maximum of two children. •All children to the left of a node have smaller values than parent. •All children to the right of a node have larger values.
  • 3.
  • 4. Operations On Binary Search Tree: •Search •Insertion •Deletion
  • 5. Search Operation Algorithms in BST: Step 1: Read the search element Step 2: Compare, search element with the value of root node in the tree. Step 3: If both are matching, then display "Given node found!!!" and terminate the function Step 4: If both are not matching, then check whether search element is smaller or larger than that node value. Step 5: If search element is smaller, then continue the search process in left subtree. Step 6: If search element is larger, then continue the search process in right subtree.
  • 6. Step 7: Repeat the same until we found exact element or we completed with a leaf node Step 8: If we reach to the node with search value, then display "Element is found" and terminate the function. Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found" and terminate the function. Diagram: Time Complextiy O(log n)
  • 7. Insertion Operation in BST: Step 1: Create a newNode with given value and set its left and right to NULL. Step 2: Check whether tree is Empty. Step 3: If the tree is Empty, then set set root to newNode. Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the node (here it is root node). Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is larger than the node, then move to its right child.
  • 8. Step 6: Repeat the above step until we reach to a leaf node (e.i., reach to NULL). Step 7: After reaching a leaf node, then isert the newNode as left child if newNode is smaller or equal to that leaf else insert it as right child. Diagram: Time complexity O(log n)
  • 9. Deletion Operation in BST: Deleting a node from Binary search tree has follwing three cases is performed with O(log n) time complexity. Case 1: Deleting a Leaf node (A node with no children) Case 2: Deleting a node with one child Case 3: Deleting a node with two children
  • 10. Case 1: Deleting a leaf node Step 1: Find the node to be deleted using search operation Step 2: Delete the node using free function (If it is a leaf) and terminate the function. Delete (7)
  • 11. Case 2: Deleting a node with one child We use the following steps to delete a node with one child from BST... Step 1: Find the node to be deleted using search operation Step 2: If it has only one child, then create a link between its parent and child nodes. Step 3: Delete the node using free function and terminate the function. Delete (9)
  • 12. Case 3: Deleting a node with two children We use the following steps to delete a node with two children from BST... Step 1: Find the node to be deleted using search operation Step 2: If it has two children, then find the largest node in its left subtree (OR) the smallest node in its right subtree. Step 3: Swap both deleting node and node which found in above step. Step 4: Then, check whether deleting node came to case 1 or case 2 else goto steps 2 Step 5: If it comes to case 1, then delete using case 1 logic. Step 6: If it comes to case 2, then delete using case 2 logic. Step 7: Repeat the same process until node is deleted from the tree.