SlideShare a Scribd company logo
1 
Deletion 
… 
Delete node xfrom a tree T 
… 
We can distinguish three cases 
… 
x has no children 
… 
xhas one child 
… 
xhas two children
2
3 
Deletion Case 1 
… 
If xhas no children –just remove x
4 
Deletion Case 2 
… 
If xhas exactly one child, then to delete x, simply make p[x] point to that child
5 
Deletion Case 3 
… 
If xhas two children, then to delete it we have to 
… 
find its successor (or predecessor) y 
… 
remove y (note that y has at most one child –why?) 
… 
replace x with y
6 
Delete Pseudocode 
TreeDelete(T,z) 
01ifleft[z]=NILorright[z]=NIL 
02theny←z 
03elsey←TreeSuccessor(z) 
04ifleft[y]≠NIL 
05thenx←left[y] 
06elsex←right[y] 
07ifx≠NIL 
08thenp[x]←p[y] 
09ifp[y]=NIL 
10thenroot[T]←x 
11elseify=left[p[y]] 
12thenleft[p[y]]←x 
13elseright[p[y]]←x 
14ify≠z 
15thenkey[z]←key[y]//copyallfiledsofy 
16returny
7 
In order traversal of a BST 
… 
ITW can be thought of as a projection of the BST nodes onto a one dimensional interval
8 
BST Sorting 
… 
Use TreeInsert and InorderTreeWalk to sort a list of nelements, A 
TreeSort(A) 
01root[T]←NIL 
02fori←1ton 
03TreeInsert(T,A[i]) 
04InorderTreeWalk(root[T])
9 
… 
Sort the following numbers5 10 7 1 3 1 8 
… 
Build a binary search tree 
… 
Call InorderTreeWalk 
1 1 3 5 7 8 10 
BST Sorting (2)
10 
In what order should we insert? 
… 
We want to sort numbers {1,2,…,n} 
… 
Total time taken to insert these numbers equals the sum of the level numbers of the nodes. 
… 
Thus if numbers were inserted in ascending order we would get a tree of height n-1 in which there is one node at each level. 
… 
So total time for insertion in this case is 1+2+3+…+n-1 = O(n2).
11 
Inserting a random permutation 
… 
Suppose we take a random permutation of the keys and inserted them in this order. 
… 
The total time required for insertion is now a random variable. 
… 
We want to compute the expected value of this r.v. 
… 
Recall that the expected value of a r.v. is the average value it takes over a large number of trials.
12
13
14 
Expected insertion time for a random permutation 
… 
We will compute the average time taken to insert keys in the order specified by the n! permutations. 
… 
In other words, for each of the n! permutations we will compute the time taken to insert keys in that order and then compute the average. 
… 
Let T(n) denote this quantity.
15 
Inserting a random permutation (2) 
… 
Of the n! permutations, there are (n-1)! permutations in which the first element is i. 
… 
The tree formed in these instances has i as the root. The left subtree has keys 1..(i-1) and the right subtree has keys (i+1)..n 
… 
Consider the ordering of keys 1..(i-1) in the (n-1)! permutations. All (i-1)! permutations appear and each occurs (n-1)!/(i-1)! times.
16 
Inserting a random permuation(3) 
… 
Recall that if we only had keys 1..(i-1) then average time taken to insert them is T(i-1). 
… 
The average is taken over all permutations of 1..(i-1). 
… 
hence total time to insert all (i-1)! permutations is (i-1)!T(i-1).
17 
Inserting a random permutation(4) 
… 
When inserting keys 1..(i-1) into the left subtree, each key has to be compared with the root. 
… 
This leads to an additional unit cost for each key. 
… 
So total time to insert all (i-1)! permutations is (i-1)!(T(i-1)+(i-1)). 
… 
Since each permutation appears (n-1)!/(i-1)! times, total time to insert keys 1..(i-1) is (n-1)!(T(i-1)+(i-1))
18 
Inserting a random permutation(5) 
… 
Time to insert keys 1..(i-1) is (n-1)!(T(i-1)+(i-1)) 
… 
Similarly, time to insert keys (i+1)..n is (n-1)!(T(n-i)+(n-i)) 
… 
Total time to insert all n keys in permutations where the first key is i is (n-1)! (T(i-1)+T(n-i)+ n-1) 
… 
Total time to insert all n keys in all n! permutations is (n-1)! Σi=1n(T(i-1)+T(n-i)+ n-1).
19 
Building the recurrence 
… 
We are expressing the value of function T() at point n in terms of the value of T() at points 0..n- 1. This is called a recurrence relation.
20 
Solving the recurrence
21 
Solving the recurrence(2)
22 
Summing the harmonic series 
… 
What is the sum ½+ 1/3 + ¼+…1/n? 
1/2 
1/3 
1/4 
1/5 
1/6 
1 2 3 4 5 6 n 
x 
1/x 
It is at most the area under the curve f(x)=1/x between limits 1 and n 
1/n
23 
The expected time for insertion 
Thus the expected time for inserting a randomly chosen permutation of n keys is O(n log n)
24 
Minimum time to insert n keys 
… 
The time required to insert n keys is minimum when the resulting tree has the smallest possible height. 
… 
A binary tree on n nodes has height at least log2n 
… 
To insert the n/2 nodes at level log2n we require at least (n/2)log2n time. 
… 
Hence inserting n keys requires Ω(nlog2n) time.
25 
Summary of Running times 
… 
To insert n keys into a binary search tree which is initially empty requires 
… 
O(n2) time in the worst case. 
… 
O(nlog n) time in the best case. 
… 
O(nlog n) time in the average case; the average is taken over the n! different orders in which the n keys could be inserted.

More Related Content

What's hot

lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
Matlab Assignment Experts
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
Sam Light
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
Computer Network Assignment Help
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
shin
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
skip list
skip listskip list
skip list
iammutex
 
Sorting
SortingSorting
Sorting
Zaid Shabbir
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
Hash presentation
Hash presentationHash presentation
Hash presentation
omercode
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functionsswartzje
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Merge sort
Merge sortMerge sort
Merge sort
Rojin Khadka
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
LAKSHMITHARUN PONNAM
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 

What's hot (20)

lecture 4
lecture 4lecture 4
lecture 4
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Linked list
Linked listLinked list
Linked list
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
skip list
skip listskip list
skip list
 
Sorting
SortingSorting
Sorting
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Hash presentation
Hash presentationHash presentation
Hash presentation
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functions
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Merge sort
Merge sortMerge sort
Merge sort
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Similar to Lec9

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
Pradeep Bisht
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
2.pptx
2.pptx2.pptx
2.pptx
MohAlyasin1
 
Fft presentation
Fft presentationFft presentation
Fft presentationilker Şin
 
Show that the worst-case time complexity for Binary Search is given by.docx
Show that the worst-case time complexity for Binary Search is given by.docxShow that the worst-case time complexity for Binary Search is given by.docx
Show that the worst-case time complexity for Binary Search is given by.docx
mmary455
 
Stirling theorem
Stirling theoremStirling theorem
Stirling theorem
Arshad Ibrahimi
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptx
workwithiec
 
Creating a Taylor Polynomial
Creating a Taylor PolynomialCreating a Taylor Polynomial
Creating a Taylor Polynomial
dmidgette
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
 
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdfFind the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
arihantelectronics
 
3. convolution fourier
3. convolution fourier3. convolution fourier
3. convolution fourier
skysunilyadav
 
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Kasun Ranga Wijeweera
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved ex
Maths Tutoring
 

Similar to Lec9 (20)

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
2.pptx
2.pptx2.pptx
2.pptx
 
Fft presentation
Fft presentationFft presentation
Fft presentation
 
Show that the worst-case time complexity for Binary Search is given by.docx
Show that the worst-case time complexity for Binary Search is given by.docxShow that the worst-case time complexity for Binary Search is given by.docx
Show that the worst-case time complexity for Binary Search is given by.docx
 
Stirling theorem
Stirling theoremStirling theorem
Stirling theorem
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Sol38
Sol38Sol38
Sol38
 
Sol38
Sol38Sol38
Sol38
 
Lecture 4 (1).pptx
Lecture 4 (1).pptxLecture 4 (1).pptx
Lecture 4 (1).pptx
 
Creating a Taylor Polynomial
Creating a Taylor PolynomialCreating a Taylor Polynomial
Creating a Taylor Polynomial
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdfFind the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
 
3. convolution fourier
3. convolution fourier3. convolution fourier
3. convolution fourier
 
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
 
Dynamical systems solved ex
Dynamical systems solved exDynamical systems solved ex
Dynamical systems solved ex
 

More from Nikhil Chilwant

The joyless economy
The joyless economyThe joyless economy
The joyless economy
Nikhil Chilwant
 
I 7
I 7I 7
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
Nikhil Chilwant
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 

Lec9

  • 1. 1 Deletion … Delete node xfrom a tree T … We can distinguish three cases … x has no children … xhas one child … xhas two children
  • 2. 2
  • 3. 3 Deletion Case 1 … If xhas no children –just remove x
  • 4. 4 Deletion Case 2 … If xhas exactly one child, then to delete x, simply make p[x] point to that child
  • 5. 5 Deletion Case 3 … If xhas two children, then to delete it we have to … find its successor (or predecessor) y … remove y (note that y has at most one child –why?) … replace x with y
  • 6. 6 Delete Pseudocode TreeDelete(T,z) 01ifleft[z]=NILorright[z]=NIL 02theny←z 03elsey←TreeSuccessor(z) 04ifleft[y]≠NIL 05thenx←left[y] 06elsex←right[y] 07ifx≠NIL 08thenp[x]←p[y] 09ifp[y]=NIL 10thenroot[T]←x 11elseify=left[p[y]] 12thenleft[p[y]]←x 13elseright[p[y]]←x 14ify≠z 15thenkey[z]←key[y]//copyallfiledsofy 16returny
  • 7. 7 In order traversal of a BST … ITW can be thought of as a projection of the BST nodes onto a one dimensional interval
  • 8. 8 BST Sorting … Use TreeInsert and InorderTreeWalk to sort a list of nelements, A TreeSort(A) 01root[T]←NIL 02fori←1ton 03TreeInsert(T,A[i]) 04InorderTreeWalk(root[T])
  • 9. 9 … Sort the following numbers5 10 7 1 3 1 8 … Build a binary search tree … Call InorderTreeWalk 1 1 3 5 7 8 10 BST Sorting (2)
  • 10. 10 In what order should we insert? … We want to sort numbers {1,2,…,n} … Total time taken to insert these numbers equals the sum of the level numbers of the nodes. … Thus if numbers were inserted in ascending order we would get a tree of height n-1 in which there is one node at each level. … So total time for insertion in this case is 1+2+3+…+n-1 = O(n2).
  • 11. 11 Inserting a random permutation … Suppose we take a random permutation of the keys and inserted them in this order. … The total time required for insertion is now a random variable. … We want to compute the expected value of this r.v. … Recall that the expected value of a r.v. is the average value it takes over a large number of trials.
  • 12. 12
  • 13. 13
  • 14. 14 Expected insertion time for a random permutation … We will compute the average time taken to insert keys in the order specified by the n! permutations. … In other words, for each of the n! permutations we will compute the time taken to insert keys in that order and then compute the average. … Let T(n) denote this quantity.
  • 15. 15 Inserting a random permutation (2) … Of the n! permutations, there are (n-1)! permutations in which the first element is i. … The tree formed in these instances has i as the root. The left subtree has keys 1..(i-1) and the right subtree has keys (i+1)..n … Consider the ordering of keys 1..(i-1) in the (n-1)! permutations. All (i-1)! permutations appear and each occurs (n-1)!/(i-1)! times.
  • 16. 16 Inserting a random permuation(3) … Recall that if we only had keys 1..(i-1) then average time taken to insert them is T(i-1). … The average is taken over all permutations of 1..(i-1). … hence total time to insert all (i-1)! permutations is (i-1)!T(i-1).
  • 17. 17 Inserting a random permutation(4) … When inserting keys 1..(i-1) into the left subtree, each key has to be compared with the root. … This leads to an additional unit cost for each key. … So total time to insert all (i-1)! permutations is (i-1)!(T(i-1)+(i-1)). … Since each permutation appears (n-1)!/(i-1)! times, total time to insert keys 1..(i-1) is (n-1)!(T(i-1)+(i-1))
  • 18. 18 Inserting a random permutation(5) … Time to insert keys 1..(i-1) is (n-1)!(T(i-1)+(i-1)) … Similarly, time to insert keys (i+1)..n is (n-1)!(T(n-i)+(n-i)) … Total time to insert all n keys in permutations where the first key is i is (n-1)! (T(i-1)+T(n-i)+ n-1) … Total time to insert all n keys in all n! permutations is (n-1)! Σi=1n(T(i-1)+T(n-i)+ n-1).
  • 19. 19 Building the recurrence … We are expressing the value of function T() at point n in terms of the value of T() at points 0..n- 1. This is called a recurrence relation.
  • 20. 20 Solving the recurrence
  • 21. 21 Solving the recurrence(2)
  • 22. 22 Summing the harmonic series … What is the sum ½+ 1/3 + ¼+…1/n? 1/2 1/3 1/4 1/5 1/6 1 2 3 4 5 6 n x 1/x It is at most the area under the curve f(x)=1/x between limits 1 and n 1/n
  • 23. 23 The expected time for insertion Thus the expected time for inserting a randomly chosen permutation of n keys is O(n log n)
  • 24. 24 Minimum time to insert n keys … The time required to insert n keys is minimum when the resulting tree has the smallest possible height. … A binary tree on n nodes has height at least log2n … To insert the n/2 nodes at level log2n we require at least (n/2)log2n time. … Hence inserting n keys requires Ω(nlog2n) time.
  • 25. 25 Summary of Running times … To insert n keys into a binary search tree which is initially empty requires … O(n2) time in the worst case. … O(nlog n) time in the best case. … O(nlog n) time in the average case; the average is taken over the n! different orders in which the n keys could be inserted.