SlideShare a Scribd company logo
1 of 21
Download to read offline
Binary Search Trees
Binary Search Tree
• Each internal node has at most two children
• Ordered Binary Tree or Binary Search Tree
• Children of node are ordered pair known as left, right child
• Left sub tree of node contains nodes with keys < node's key
• Right sub tree of node has nodes with keys ≥ node's key
• Both left and right subtrees must be binary search trees
• Recursive definition: Either a tree of single node, or whose
root has an ordered pair of children, each is binary search tree
Binary Search Trees
• BST – A binary tree where a parent’s value is greater than
its left child and less than or equal to it’s right child
)
(
)
( i
right
i
i
left 

Operations
• Search(T,k) – Does value k exist in tree T
• Insert(T,k) – Insert value k into tree T
• Delete(T,x) – Delete node x from tree T
• Minimum(T) – What is the smallest value in the tree?
• Maximum(T) – What is the largest value in the tree?
• Successor(T,x) – What is the next element in sorted order
after x
• Predecessor(T,x) – What is the previous element in
sorted order of x
• Median(T) – return the median of the values in tree T
BST
Searching in a tree (Recursion)
TreeSearch(x, item)
If x=null or item =x->data
return x
if item < x->data
return Search(x->left,item)
else
return Search(x->right,item)
Searching in a tree (Iterative)
TreeSearch(x, item)
{
While x ≠ null and item ≠ x->data
{
if item < x->data
x= x->left
else
x= x->right
}
return x
}
if item = x->data
{
Item found
}
else
Not found
Complexity of search
• It will depend upon the height of the tree
because with every loop iteration we are
progressing to next level of the loop.
• Worst case: when we have a twig O(n)
• Average case : When tree is nearly complete
O(logn)
• Best Case: O(1)
Tree-Minimum (x)
1. while x->left != NULL
2. x= x->left
3. return x
Tree-Maximum (x):
1. while x-> right != NULL
2. x= x-> right
3. return x
Finding Successor
• Given x, find the node with the smallest key
greater then key[x]
• Two cases depend upon right sub tree of x
1: Right sub tree of x is nonempty, then
successor is leftmost node in right sub tree
2: Right sub tree of x is empty, then keep
going up until we are no longer a right
child. If there is no such ancestor then
successor is undefined.
• We are going to the next node in inorder
Tree-Successor(x):
1. if x-> right != NULL
2. return Tree-Minimum(x->right)
3. y = x-> parent
4. while y != NULL and x = = y->right
{
5. x = y
6. y = y->parent
}
7. return y
Insertion In a binary Search Tree
• Take an element z whose left and right
children are null and insert it into T
• Find place in T where z belongs (as if searching
for z) and add z
• Runtime on a tree of height h is O(h)
Special Case
• What if we need to insert a duplicate element
• Does the order of insertion matters : Yes, It
will change the topology of the tree
Tree-Insert (z)
1. y = NULL
2. x= root
3. while x != NULL
{
4. y = x
5. if z->data < x ->data
6. x = x->left
7. else x = x->right
}
8. z-> parent = y
9. if y = = NULL
10. root = z // Tree T was empty
11. else if (z->data < y->data)
12. y->left = z
13. else y->right = z
Deletion
Case-IV
• To delete root and successor is undefined,
then need to take care of the start pointer
Time complexity
Running time for delete, insertion and search
worst case O(n)
Average Case O(logn)
Creating a BST (Inserting n elements one by one)
worst case O(n2 )
Average case O(nlogn)
Inorder traversal of a BST gives a sorted list and
takes O(n) time.
BST.pdf
BST.pdf
BST.pdf

More Related Content

Similar to BST.pdf

Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.pptItsStranger1
 
BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).pptplagcheck
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.pptplagcheck
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptDharmannaRathod1
 
lecture 13
lecture 13lecture 13
lecture 13sajinsc
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.pptplagcheck
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
 

Similar to BST.pdf (20)

DS - BST
DS - BSTDS - BST
DS - BST
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
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)
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
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)
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Trees
TreesTrees
Trees
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
BinarySearchTrees (1).ppt
BinarySearchTrees (1).pptBinarySearchTrees (1).ppt
BinarySearchTrees (1).ppt
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
BinarySearchTrees.ppt
BinarySearchTrees.pptBinarySearchTrees.ppt
BinarySearchTrees.ppt
 
data structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.pptdata structure very BinarySearchTrees.ppt
data structure very BinarySearchTrees.ppt
 
lecture 13
lecture 13lecture 13
lecture 13
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

BST.pdf

  • 2. Binary Search Tree • Each internal node has at most two children • Ordered Binary Tree or Binary Search Tree • Children of node are ordered pair known as left, right child • Left sub tree of node contains nodes with keys < node's key • Right sub tree of node has nodes with keys ≥ node's key • Both left and right subtrees must be binary search trees • Recursive definition: Either a tree of single node, or whose root has an ordered pair of children, each is binary search tree
  • 3. Binary Search Trees • BST – A binary tree where a parent’s value is greater than its left child and less than or equal to it’s right child ) ( ) ( i right i i left  
  • 4. Operations • Search(T,k) – Does value k exist in tree T • Insert(T,k) – Insert value k into tree T • Delete(T,x) – Delete node x from tree T • Minimum(T) – What is the smallest value in the tree? • Maximum(T) – What is the largest value in the tree? • Successor(T,x) – What is the next element in sorted order after x • Predecessor(T,x) – What is the previous element in sorted order of x • Median(T) – return the median of the values in tree T
  • 5. BST
  • 6. Searching in a tree (Recursion) TreeSearch(x, item) If x=null or item =x->data return x if item < x->data return Search(x->left,item) else return Search(x->right,item)
  • 7. Searching in a tree (Iterative) TreeSearch(x, item) { While x ≠ null and item ≠ x->data { if item < x->data x= x->left else x= x->right } return x } if item = x->data { Item found } else Not found
  • 8. Complexity of search • It will depend upon the height of the tree because with every loop iteration we are progressing to next level of the loop. • Worst case: when we have a twig O(n) • Average case : When tree is nearly complete O(logn) • Best Case: O(1)
  • 9. Tree-Minimum (x) 1. while x->left != NULL 2. x= x->left 3. return x Tree-Maximum (x): 1. while x-> right != NULL 2. x= x-> right 3. return x
  • 10. Finding Successor • Given x, find the node with the smallest key greater then key[x] • Two cases depend upon right sub tree of x 1: Right sub tree of x is nonempty, then successor is leftmost node in right sub tree 2: Right sub tree of x is empty, then keep going up until we are no longer a right child. If there is no such ancestor then successor is undefined. • We are going to the next node in inorder
  • 11. Tree-Successor(x): 1. if x-> right != NULL 2. return Tree-Minimum(x->right) 3. y = x-> parent 4. while y != NULL and x = = y->right { 5. x = y 6. y = y->parent } 7. return y
  • 12. Insertion In a binary Search Tree • Take an element z whose left and right children are null and insert it into T • Find place in T where z belongs (as if searching for z) and add z • Runtime on a tree of height h is O(h)
  • 13. Special Case • What if we need to insert a duplicate element • Does the order of insertion matters : Yes, It will change the topology of the tree
  • 14. Tree-Insert (z) 1. y = NULL 2. x= root 3. while x != NULL { 4. y = x 5. if z->data < x ->data 6. x = x->left 7. else x = x->right } 8. z-> parent = y 9. if y = = NULL 10. root = z // Tree T was empty 11. else if (z->data < y->data) 12. y->left = z 13. else y->right = z
  • 16.
  • 17. Case-IV • To delete root and successor is undefined, then need to take care of the start pointer
  • 18. Time complexity Running time for delete, insertion and search worst case O(n) Average Case O(logn) Creating a BST (Inserting n elements one by one) worst case O(n2 ) Average case O(nlogn) Inorder traversal of a BST gives a sorted list and takes O(n) time.