SlideShare a Scribd company logo
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.
Binary Search Tree (BST)
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

PPT
Binary Search Tree and AVL
PPT
Binary search tree(bst)
PPT
1.5 binary search tree
PPTX
Binary search tree
PPTX
Binary trees1
PDF
Binary search tree operations
PPTX
Binary Search Tree
PPTX
Binary Search Tree and AVL
Binary search tree(bst)
1.5 binary search tree
Binary search tree
Binary trees1
Binary search tree operations
Binary Search Tree

What's hot (20)

PDF
Binary tree
PPT
Binary search trees
PPTX
Trees (data structure)
PPTX
Binary Search Tree
PPT
Lec 17 heap data structure
PPT
Binary search in ds
PPT
Tree and Binary Search tree
PPT
BINARY SEARCH TREE
PPTX
Graph traversals in Data Structures
PPT
Bst(Binary Search Tree)
PPTX
trees in data structure
PPTX
Search tree,Tree and binary tree and heap tree
PPT
Binary search tree in data structures
PPTX
Binary tree and Binary search tree
PPT
Binary Search Tree Traversal.ppt
PPTX
Binary Heap Tree, Data Structure
PPSX
Data Structure (Tree)
PPTX
Queue Implementation Using Array & Linked List
Binary tree
Binary search trees
Trees (data structure)
Binary Search Tree
Lec 17 heap data structure
Binary search in ds
Tree and Binary Search tree
BINARY SEARCH TREE
Graph traversals in Data Structures
Bst(Binary Search Tree)
trees in data structure
Search tree,Tree and binary tree and heap tree
Binary search tree in data structures
Binary tree and Binary search tree
Binary Search Tree Traversal.ppt
Binary Heap Tree, Data Structure
Data Structure (Tree)
Queue Implementation Using Array & Linked List
Ad

Similar to Binary Search Tree (BST) (20)

PPT
A Binary Search Tree (BST) is a binary tree where each node stores a key or v...
PPTX
Binary Search Tree
PDF
Trees second part in data structures with examples
PPTX
DAA PPT.pptx
PPTX
Binary Search Tree
PDF
B Tree, Introduction ,example,Splay tree
PPT
Binary trees
PPTX
VCE Unit 05.pptx
PPTX
Trees in data structure
PPTX
9e28d06f-bae9-487e-8360-0e2c0a8c2972.pptx
DOCX
Biary search Tree.docx
PPT
1.2 operations of tree representations
PPTX
Lec 10_Binary Search Tree in data structure and algorithm.pptx
PPT
Lecture 7-BinarySearchTrees.ppt
PPSX
Unit-5 Advanced tree zxcppt
PDF
Binary Tree - Algorithms
PPTX
BINARY SEARCH TREE.pptx all about trees how hey grow
PPTX
learn tree, linked list, queue, stack, and other algo
PPTX
Binary tree
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
A Binary Search Tree (BST) is a binary tree where each node stores a key or v...
Binary Search Tree
Trees second part in data structures with examples
DAA PPT.pptx
Binary Search Tree
B Tree, Introduction ,example,Splay tree
Binary trees
VCE Unit 05.pptx
Trees in data structure
9e28d06f-bae9-487e-8360-0e2c0a8c2972.pptx
Biary search Tree.docx
1.2 operations of tree representations
Lec 10_Binary Search Tree in data structure and algorithm.pptx
Lecture 7-BinarySearchTrees.ppt
Unit-5 Advanced tree zxcppt
Binary Tree - Algorithms
BINARY SEARCH TREE.pptx all about trees how hey grow
learn tree, linked list, queue, stack, and other algo
Binary tree
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Ad

More from M Sajid R (6)

PPTX
Transport layer
PPTX
Novartis
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
PPTX
Query o
PPTX
Network And Topology
PPTX
Toyota
Transport layer
Novartis
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Query o
Network And Topology
Toyota

Recently uploaded (20)

PDF
Updated Idioms and Phrasal Verbs in English subject
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Complications of Minimal Access Surgery at WLH
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PDF
Trump Administration's workforce development strategy
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Updated Idioms and Phrasal Verbs in English subject
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Complications of Minimal Access Surgery at WLH
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
Weekly quiz Compilation Jan -July 25.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Trump Administration's workforce development strategy
A systematic review of self-coping strategies used by university students to ...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

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.
  • 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.