SlideShare a Scribd company logo
1 of 33
Chapter 9
Binary Search Trees
Dr. Muhammad Hanif Durad
Department of Computer and Information Sciences
Pakistan Institute Engineering and Applied Sciences
hanif@pieas.edu.pk
Some slides have bee adapted with thanks from some other lectures
available on Internet. It made my life easier, as life is always
miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
Dr. Hanif Durad 2
Lecture Outline
 Binary Search Trees
 BST – Representation
 Various Operation in Binary Search Trees
 Inorder Traversal
 Tree Search
 Finding Min & Max
 Predecessor and Successor
 Insertion and Delete
 BST Problems
 Better Search Trees
IA, Chapter 12 P-251
Binary Search Trees-
Introduction
 View today as data structures that can support
dynamic set operations.
 Search, Minimum, Maximum, Predecessor, Successor,
Insert, and Delete.
 Can be used to build
 Dictionaries.
 Priority Queues.
 Basic operations take time proportional to the height
of the tree – O(h).
D:DSALCOMP 550-00115-btrees.ppt
Why BST
DeletionInsertionRetrievalData Structure
O(log n)
FAST
O(log n)
FAST
O(log n)
FAST
BST
O(n)
SLOW
O(n)
SLOW
O(log n)
FAST*
Sorted Array
O(n)
SLOW
O(n)
SLOW
O(n)
SLOW
Sorted Linked List
 BSTs provide good logarithmic time performance in the best and
average cases.
 Average case complexities of using linear data structures compared
to BSTs:
*using binary search
D:Data StructuresICS202Lecture18.ppt
9.2 Binary Search Tree Property
(1/2)
 Stored keys must satisfy
the binary search tree
property.
  y in left subtree of x,
then key[y]  key[x].
  y in right subtree of x,
then key[y]  key[x].
56
26 200
18 28 190 213
12 24 27
666
Binary Search Tree Property (2/2)
6
D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
Binary Search Trees
A binary search tree
Not a binary search tree
D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
BST – Representation
 Represented by a linked data structure of
nodes.
 root(T) points to the root of tree T.
 Each node contains fields:
 key
 left – pointer to left child: root of left subtree.
 right – pointer to right child : root of right subtree.
 p – pointer to parent. p[root[T]] = NIL (optional).
Rightleft
key
Parent
D:DSALCOMP 550-00115-btrees.ppt
9.3 Various Operation in Binary
Search Trees
Inorder Traversal (1/2)
Inorder-Tree-Walk (x)
1. if x  NIL
2. then Inorder-Tree-Walk(left[p])
3. print key[x]
4. Inorder-Tree-Walk(right[p])
The binary-search-tree property allows the keys of a binary search
tree to be printed, in (monotonically increasing) order, recursively.
D:DSALCOMP 550-00115-btrees.ppt
Inorder Traversal (2/2)
50
70
60
30
4020
Inorder traversal yields: 20, 30, 40, 50, 60, 70
D:Data StructuresHanif_SearchTreesTreesPart1.ppt
Recurrence equation:
T(0) = Θ(1)
T(n)=T(k) + T(n – k –1) + Θ(1)
Querying a Binary Search Tree
 All dynamic-set search operations can be
supported in O(h) time.
 h = (lg n) for a balanced binary tree (and for
an average tree built by adding nodes in
random order.)
 h = (n) for an unbalanced tree that resembles
a linear chain of n nodes in the worst case.
Tree Search
Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
5. else return Tree-Search(right[x], k)
Running time: O(h)
56
26 200
18 28 190 213
12 24 27
IA, P-256
Example: search in a binary tree
Dr. Hanif Durad 14
IA, P-257
Search for 13 in the tree
Iterative Tree Search
Iterative-Tree-Search(x, k)
1. while x  NIL and k  key[x]
2. do if k < key[x]
3. then x  left[x]
4. else x  right[x]
5. return x
The iterative tree search is more efficient on most computers.
The recursive tree search is more straightforward.
56
26 200
18 28 190 213
12 24 27
Finding Min & Max
Tree-Minimum(x) Tree-Maximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x
Q: How long do they take?
 The binary-search-tree property guarantees that:
 The minimum is located at the left-most node.
 The maximum is located at the right-most node.
Predecessor and Successor
 Successor of node x is the node y such that key[y] is
the smallest key greater than key[x].
 The successor of the largest key is NIL.
 Search consists of two cases.
 If node x has a non-empty right subtree, then x’s successor is
the minimum in the right subtree of x.
 If node x has an empty right subtree, then:
 As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
 x’s successor y is the node that x is the predecessor of (x is the
maximum in y’s left subtree).
 In other words, x’s successor y, is the lowest ancestor of x whose left
child is also an ancestor of x.
Pseudo-code for Successor
Code for predecessor is symmetric.
Running time: O(h)
56
26 200
18 28 190 213
12 24 27
Tree-Successor(x)
 if right[x]  NIL
2. then return Tree-Minimum(right[x])
3. y  p[x]
4. while y  NIL and x = right[y]
5. do x  y
6. y  p[y]
7. return y
Example: finding a successor
Find the successors of 15, 13
IA, P-257 Solved in notes
E:DSALData Structures 67109lect08.ppt
BST Insertion – Pseudocode
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
 Change the dynamic set
represented by a BST.
 Ensure the binary-search-
tree property holds after
change.
 Insertion is easier than
deletion.
56
26 200
18 28 190 213
12 24 27
Analysis of Insertion
 Initialization: O(1)
 While loop in lines 3-7
searches for place to
insert z, maintaining
parent y.
This takes O(h) time.
 Lines 8-13 insert the
value: O(1)
 TOTAL: O(h) time to
insert a node.
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13. else right[y]  z
Example: insertion
12
5
9
18
1915
17
2
13Insert 13 in the tree z
IA, P-262 Solved in notes
E:DSALData Structures 67109lect08.ppt
Exercise: Sorting Using BSTs
Sort (A)
for i  1 to n
do tree-insert(A[i])
inorder-tree-walk(root)
 What are the worst case and best case running times?
 In practice, how would this compare to other sorting
algorithms?
Answers to Exercise: Sorting
Using BSTs
 Analysis
 Each item’s insertion takes O(logN)
 N times- O(N*logN) or O(N2)
 Traversal O(N)
Printed as 5A
Tree-Delete (T, x)
if x has no children  case 0
then remove x
if x has one child  case 1
then make p[x] point to child
if x has two children (subtrees)  case 2
then swap x with its successor
perform case 0 or case 1 to delete it
 TOTAL: O(h) time to delete a node
Deletion – Pseudocode (1/2)
Tree-Delete(T, z)
/* Determine which node to splice out: either z or z’s successor. */
 if left[z] = NIL or right[z] = NIL
 then y  z
 else y  Tree-Successor[z]
/* Set x to a non-NIL child of x, or to NIL if y has no children. */
4. if left[y]  NIL
5. then x  left[y]
6. else x  right[y]
/* y is removed from the tree by manipulating pointers of p[y] and x */
7. if x  NIL
8. then p[x]  p[y]
/* Continued on next slide */
Deletion – Pseudocode (2/2)
Tree-Delete(T, z) (Contd. from previous slide)
9. if p[y] = NIL
10. then root[T]  x
11. else if y  left[p[i]]
12. then left[p[y]]  x
13. else right[p[y]]  x
/* If z’s successor was spliced out, copy its data into z */
14. if y  z
15. then key[z]  key[y]
16. copy y’s satellite data into z.
17. return y
Delete case 1: no children!
IA, P-263Solved in notes
E:DSALData Structures 67109lect08.ppt
Delete case 2: one child
IA, P-263 Solved in notes
E:DSALData Structures 67109lect08.ppt
Delete: case 3
successor
delete
IA, P-263 Solved in notes
E:DSALData Structures 67109lect08.ppt
Inefficiency of general BSTs
 All operations in BST are performed in time
O(h), where h is the height of BST
 Unfortunately h might be as large as n, e.g.,
after n consecutive insertions of elements with
keys in increasing order
 The advantages of the binary search (O(log n)
time update) might be lost if BST is not
balanced
E:DSALcomp202 ULPweek3[1].ppt
BST Problems
Same entries, different insertion sequence:
 Not good! Would like to keep tree balanced.
D:Data StructuresHanif_SearchTrees2-3Trees.ppt
 Prevent the degeneration of the BST :
 A BST can be set up to maintain balance during
updating operations (insertions and removals)
 Types of ST which maintain the optimal performance:
 splay trees
 AVL trees
 2-4 Trees
 Red-Black trees
 B-trees
Better Search Trees
E:Data StructuresCSCE3110BinarySearchTrees.ppt

More Related Content

What's hot

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
Array Data Structures
Array Data StructuresArray Data Structures
Array Data StructuresSoni Gupta
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningBig_Data_Ukraine
 
Principal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationPrincipal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationMarjan Sterjev
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Namgee Lee
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersAmrinder Arora
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 

What's hot (20)

Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Array
ArrayArray
Array
 
Array Data Structures
Array Data StructuresArray Data Structures
Array Data Structures
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Principal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and VisualizationPrincipal Components Analysis, Calculation and Visualization
Principal Components Analysis, Calculation and Visualization
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Chapter2
Chapter2Chapter2
Chapter2
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom Filters
 
Tree
TreeTree
Tree
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Array
ArrayArray
Array
 

Similar to Chapter 9 ds

5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docxfredharris32
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.pptplagcheck
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
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
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree Krish_ver2
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.pptSuneel61
 

Similar to Chapter 9 ds (20)

5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
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
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
09 binary trees
09 binary trees09 binary trees
09 binary trees
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Trees
TreesTrees
Trees
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Lec8
Lec8Lec8
Lec8
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 

More from Hanif Durad

More from Hanif Durad (20)

Chapter 26 aoa
Chapter 26 aoaChapter 26 aoa
Chapter 26 aoa
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Chapter 2 ds
Chapter 2 dsChapter 2 ds
Chapter 2 ds
 
Chapter 5 pc
Chapter 5 pcChapter 5 pc
Chapter 5 pc
 
Chapter 4 pc
Chapter 4 pcChapter 4 pc
Chapter 4 pc
 
Chapter 3 pc
Chapter 3 pcChapter 3 pc
Chapter 3 pc
 
Chapter 2 pc
Chapter 2 pcChapter 2 pc
Chapter 2 pc
 
Chapter 1 pc
Chapter 1 pcChapter 1 pc
Chapter 1 pc
 
Chapter 6 pc
Chapter 6 pcChapter 6 pc
Chapter 6 pc
 
Collective Communications in MPI
 Collective Communications in MPI Collective Communications in MPI
Collective Communications in MPI
 
Point-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPIPoint-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPI
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 

Chapter 9 ds

  • 1. Chapter 9 Binary Search Trees Dr. Muhammad Hanif Durad Department of Computer and Information Sciences Pakistan Institute Engineering and Applied Sciences hanif@pieas.edu.pk Some slides have bee adapted with thanks from some other lectures available on Internet. It made my life easier, as life is always miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
  • 2. Dr. Hanif Durad 2 Lecture Outline  Binary Search Trees  BST – Representation  Various Operation in Binary Search Trees  Inorder Traversal  Tree Search  Finding Min & Max  Predecessor and Successor  Insertion and Delete  BST Problems  Better Search Trees IA, Chapter 12 P-251
  • 3. Binary Search Trees- Introduction  View today as data structures that can support dynamic set operations.  Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete.  Can be used to build  Dictionaries.  Priority Queues.  Basic operations take time proportional to the height of the tree – O(h). D:DSALCOMP 550-00115-btrees.ppt
  • 4. Why BST DeletionInsertionRetrievalData Structure O(log n) FAST O(log n) FAST O(log n) FAST BST O(n) SLOW O(n) SLOW O(log n) FAST* Sorted Array O(n) SLOW O(n) SLOW O(n) SLOW Sorted Linked List  BSTs provide good logarithmic time performance in the best and average cases.  Average case complexities of using linear data structures compared to BSTs: *using binary search D:Data StructuresICS202Lecture18.ppt
  • 5. 9.2 Binary Search Tree Property (1/2)  Stored keys must satisfy the binary search tree property.   y in left subtree of x, then key[y]  key[x].   y in right subtree of x, then key[y]  key[x]. 56 26 200 18 28 190 213 12 24 27
  • 6. 666 Binary Search Tree Property (2/2) 6 D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
  • 7. Binary Search Trees A binary search tree Not a binary search tree D:Data StructuresCOMP171 Data Structures and Algorithmbst.ppt
  • 8. BST – Representation  Represented by a linked data structure of nodes.  root(T) points to the root of tree T.  Each node contains fields:  key  left – pointer to left child: root of left subtree.  right – pointer to right child : root of right subtree.  p – pointer to parent. p[root[T]] = NIL (optional). Rightleft key Parent D:DSALCOMP 550-00115-btrees.ppt
  • 9. 9.3 Various Operation in Binary Search Trees
  • 10. Inorder Traversal (1/2) Inorder-Tree-Walk (x) 1. if x  NIL 2. then Inorder-Tree-Walk(left[p]) 3. print key[x] 4. Inorder-Tree-Walk(right[p]) The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively. D:DSALCOMP 550-00115-btrees.ppt
  • 11. Inorder Traversal (2/2) 50 70 60 30 4020 Inorder traversal yields: 20, 30, 40, 50, 60, 70 D:Data StructuresHanif_SearchTreesTreesPart1.ppt Recurrence equation: T(0) = Θ(1) T(n)=T(k) + T(n – k –1) + Θ(1)
  • 12. Querying a Binary Search Tree  All dynamic-set search operations can be supported in O(h) time.  h = (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.)  h = (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.
  • 13. Tree Search Tree-Search(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) Running time: O(h) 56 26 200 18 28 190 213 12 24 27 IA, P-256
  • 14. Example: search in a binary tree Dr. Hanif Durad 14 IA, P-257 Search for 13 in the tree
  • 15. Iterative Tree Search Iterative-Tree-Search(x, k) 1. while x  NIL and k  key[x] 2. do if k < key[x] 3. then x  left[x] 4. else x  right[x] 5. return x The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward. 56 26 200 18 28 190 213 12 24 27
  • 16. Finding Min & Max Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x 3. return x Q: How long do they take?  The binary-search-tree property guarantees that:  The minimum is located at the left-most node.  The maximum is located at the right-most node.
  • 17. Predecessor and Successor  Successor of node x is the node y such that key[y] is the smallest key greater than key[x].  The successor of the largest key is NIL.  Search consists of two cases.  If node x has a non-empty right subtree, then x’s successor is the minimum in the right subtree of x.  If node x has an empty right subtree, then:  As long as we move to the left up the tree (move up through right children), we are visiting smaller keys.  x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree).  In other words, x’s successor y, is the lowest ancestor of x whose left child is also an ancestor of x.
  • 18. Pseudo-code for Successor Code for predecessor is symmetric. Running time: O(h) 56 26 200 18 28 190 213 12 24 27 Tree-Successor(x)  if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y
  • 19. Example: finding a successor Find the successors of 15, 13 IA, P-257 Solved in notes E:DSALData Structures 67109lect08.ppt
  • 20. BST Insertion – Pseudocode Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z  Change the dynamic set represented by a BST.  Ensure the binary-search- tree property holds after change.  Insertion is easier than deletion. 56 26 200 18 28 190 213 12 24 27
  • 21. Analysis of Insertion  Initialization: O(1)  While loop in lines 3-7 searches for place to insert z, maintaining parent y. This takes O(h) time.  Lines 8-13 insert the value: O(1)  TOTAL: O(h) time to insert a node. Tree-Insert(T, z) 1. y  NIL 2. x  root[T] 3. while x  NIL 4. do y  x 5. if key[z] < key[x] 6. then x  left[x] 7. else x  right[x] 8. p[z]  y 9. if y = NIL 10. then root[t]  z 11. else if key[z] < key[y] 12. then left[y]  z 13. else right[y]  z
  • 22. Example: insertion 12 5 9 18 1915 17 2 13Insert 13 in the tree z IA, P-262 Solved in notes E:DSALData Structures 67109lect08.ppt
  • 23. Exercise: Sorting Using BSTs Sort (A) for i  1 to n do tree-insert(A[i]) inorder-tree-walk(root)  What are the worst case and best case running times?  In practice, how would this compare to other sorting algorithms?
  • 24. Answers to Exercise: Sorting Using BSTs  Analysis  Each item’s insertion takes O(logN)  N times- O(N*logN) or O(N2)  Traversal O(N) Printed as 5A
  • 25. Tree-Delete (T, x) if x has no children  case 0 then remove x if x has one child  case 1 then make p[x] point to child if x has two children (subtrees)  case 2 then swap x with its successor perform case 0 or case 1 to delete it  TOTAL: O(h) time to delete a node
  • 26. Deletion – Pseudocode (1/2) Tree-Delete(T, z) /* Determine which node to splice out: either z or z’s successor. */  if left[z] = NIL or right[z] = NIL  then y  z  else y  Tree-Successor[z] /* Set x to a non-NIL child of x, or to NIL if y has no children. */ 4. if left[y]  NIL 5. then x  left[y] 6. else x  right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ 7. if x  NIL 8. then p[x]  p[y] /* Continued on next slide */
  • 27. Deletion – Pseudocode (2/2) Tree-Delete(T, z) (Contd. from previous slide) 9. if p[y] = NIL 10. then root[T]  x 11. else if y  left[p[i]] 12. then left[p[y]]  x 13. else right[p[y]]  x /* If z’s successor was spliced out, copy its data into z */ 14. if y  z 15. then key[z]  key[y] 16. copy y’s satellite data into z. 17. return y
  • 28. Delete case 1: no children! IA, P-263Solved in notes E:DSALData Structures 67109lect08.ppt
  • 29. Delete case 2: one child IA, P-263 Solved in notes E:DSALData Structures 67109lect08.ppt
  • 30. Delete: case 3 successor delete IA, P-263 Solved in notes E:DSALData Structures 67109lect08.ppt
  • 31. Inefficiency of general BSTs  All operations in BST are performed in time O(h), where h is the height of BST  Unfortunately h might be as large as n, e.g., after n consecutive insertions of elements with keys in increasing order  The advantages of the binary search (O(log n) time update) might be lost if BST is not balanced E:DSALcomp202 ULPweek3[1].ppt
  • 32. BST Problems Same entries, different insertion sequence:  Not good! Would like to keep tree balanced. D:Data StructuresHanif_SearchTrees2-3Trees.ppt
  • 33.  Prevent the degeneration of the BST :  A BST can be set up to maintain balance during updating operations (insertions and removals)  Types of ST which maintain the optimal performance:  splay trees  AVL trees  2-4 Trees  Red-Black trees  B-trees Better Search Trees E:Data StructuresCSCE3110BinarySearchTrees.ppt

Editor's Notes

  1. Washington State University