SlideShare une entreprise Scribd logo

Contenu connexe

Tendances (20)

Lec20
Lec20Lec20
Lec20
 
Lec2
Lec2Lec2
Lec2
 
Lec3
Lec3Lec3
Lec3
 
Lec11
Lec11Lec11
Lec11
 
Lec3
Lec3Lec3
Lec3
 
Lec1
Lec1Lec1
Lec1
 
Lec17
Lec17Lec17
Lec17
 
Hashing
HashingHashing
Hashing
 
Heap tree
Heap treeHeap tree
Heap tree
 
Greedy method
Greedy method Greedy method
Greedy method
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lec2
Lec2Lec2
Lec2
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Heap
HeapHeap
Heap
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
Topological sort
Topological sortTopological sort
Topological sort
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
kdtrees.pdf
kdtrees.pdfkdtrees.pdf
kdtrees.pdf
 
07. disjoint set
07. disjoint set07. disjoint set
07. disjoint set
 

En vedette

K state candidacy presentation
K state candidacy presentationK state candidacy presentation
K state candidacy presentationTeague Allen
 
Anna kostenko paintings_not_photographs1
Anna kostenko paintings_not_photographs1Anna kostenko paintings_not_photographs1
Anna kostenko paintings_not_photographs1filipj2000
 
οι κήποι του ζAhrady
οι κήποι του ζAhradyοι κήποι του ζAhrady
οι κήποι του ζAhradyfilipj2000
 
Internet messesage
Internet messesageInternet messesage
Internet messesagebearobo
 
Noches griegas
Noches griegasNoches griegas
Noches griegasfilipj2000
 
The beautyofnight
The beautyofnightThe beautyofnight
The beautyofnightfilipj2000
 
Ki cuba-2007 dg
Ki cuba-2007 dgKi cuba-2007 dg
Ki cuba-2007 dgfilipj2000
 
Proceeding aciar beefcattle_ias team_jan_2014
Proceeding aciar beefcattle_ias team_jan_2014Proceeding aciar beefcattle_ias team_jan_2014
Proceeding aciar beefcattle_ias team_jan_2014Ho Cao Viet
 
Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine Durgin
 
Natureza&decoracao
Natureza&decoracaoNatureza&decoracao
Natureza&decoracaofilipj2000
 
Zero Visibility: Critcality of Centralized Log Management - v1
Zero Visibility: Critcality of Centralized Log Management - v1Zero Visibility: Critcality of Centralized Log Management - v1
Zero Visibility: Critcality of Centralized Log Management - v1asherad
 
Fascinating....... mother russia
Fascinating....... mother russiaFascinating....... mother russia
Fascinating....... mother russiafilipj2000
 
Mobile Analytics
Mobile AnalyticsMobile Analytics
Mobile AnalyticsSamuel Chow
 

En vedette (20)

DISMATH_Part2
DISMATH_Part2DISMATH_Part2
DISMATH_Part2
 
K state candidacy presentation
K state candidacy presentationK state candidacy presentation
K state candidacy presentation
 
Anna kostenko paintings_not_photographs1
Anna kostenko paintings_not_photographs1Anna kostenko paintings_not_photographs1
Anna kostenko paintings_not_photographs1
 
οι κήποι του ζAhrady
οι κήποι του ζAhradyοι κήποι του ζAhrady
οι κήποι του ζAhrady
 
Internet messesage
Internet messesageInternet messesage
Internet messesage
 
Noches griegas
Noches griegasNoches griegas
Noches griegas
 
Death
DeathDeath
Death
 
Evropi nyxta
Evropi nyxtaEvropi nyxta
Evropi nyxta
 
The beautyofnight
The beautyofnightThe beautyofnight
The beautyofnight
 
People scenes
People scenesPeople scenes
People scenes
 
Project Management Fundamentals Course
Project Management Fundamentals CourseProject Management Fundamentals Course
Project Management Fundamentals Course
 
Ki cuba-2007 dg
Ki cuba-2007 dgKi cuba-2007 dg
Ki cuba-2007 dg
 
Proceeding aciar beefcattle_ias team_jan_2014
Proceeding aciar beefcattle_ias team_jan_2014Proceeding aciar beefcattle_ias team_jan_2014
Proceeding aciar beefcattle_ias team_jan_2014
 
Catherine durgin mdsk 6162
Catherine durgin mdsk 6162Catherine durgin mdsk 6162
Catherine durgin mdsk 6162
 
Natureza&decoracao
Natureza&decoracaoNatureza&decoracao
Natureza&decoracao
 
Zero Visibility: Critcality of Centralized Log Management - v1
Zero Visibility: Critcality of Centralized Log Management - v1Zero Visibility: Critcality of Centralized Log Management - v1
Zero Visibility: Critcality of Centralized Log Management - v1
 
Fascinating....... mother russia
Fascinating....... mother russiaFascinating....... mother russia
Fascinating....... mother russia
 
Mobile Analytics
Mobile AnalyticsMobile Analytics
Mobile Analytics
 
Polarreise
PolarreisePolarreise
Polarreise
 
国内社交电视产品研究
国内社交电视产品研究国内社交电视产品研究
国内社交电视产品研究
 

Similaire à Lec8

Similaire à Lec8 (20)

Lec8
Lec8Lec8
Lec8
 
Trees
TreesTrees
Trees
 
C12 bst
C12 bstC12 bst
C12 bst
 
C12 bst
C12 bstC12 bst
C12 bst
 
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
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
Lec16
Lec16Lec16
Lec16
 
lecture 11
lecture 11lecture 11
lecture 11
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
lecture 12
lecture 12lecture 12
lecture 12
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
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
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx5220191CS146 Data Structures and AlgorithmsC.docx
5220191CS146 Data Structures and AlgorithmsC.docx
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
B trees
B treesB trees
B trees
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 

Lec8

  • 1. Ordered Dictionaries  Inaddition to dictionary functionality, we want to support following operations:  Min()  Max()  Predecessor(S, k)  Successor(S, k)  For this we require that there should be a total order on the keys. 1
  • 2. A List-Based Implementation  Unordered list  searching takes O(n) time  inserting takes O(1) time  Ordered list  searching takes O(n) time  inserting takes O(n) time  Using array would definitely improve search time. 2
  • 3. Binary Search  Narrow down the search range in stages  findElement(22) 3
  • 4. Running Time  The range of candidate items to be searched is halved after comparing the key with the middle element  Binary search runs in O(lg n) time (remember recurrence...)  What about insertion and deletion? 4
  • 5. Binary Search Trees  A binary search tree is a binary tree T such that  each internal node stores an item (k,e) of a dictionary  keys stored at nodes in the left subtree of v are less than or equal to k  keys stored at nodes in the right subtree of v are greater than or equal to k  Example sequence 2,3,5,5,7,8 5
  • 6. Searching a BST  To find an element with key k in a tree T  compare k with key[root[T]]  if k < key[root[T]], search for k in left[root[T]]  otherwise, search for k in right[root[T]] 6
  • 8. Search Examples (2)  Search(T, 6) 8
  • 9. Pseudocode for BST Search  Recursive version Search(T,k) 01 x root[T] 02 if x = NIL then return NIL 03 if k = key[x] then return x 04 if k < key[x] 05 then return Search(left[x],k) 06 else return Search(right[x],k)  Iterative version Search(T,k) 01 x root[T] 02 while x NIL and k key[x] do 03 if k < key[x] 04 then x left[x] 05 else x right[x] 06 return x 9
  • 10. Analysis of Search  Running time on tree of height h is O(h)  After the insertion of n keys, the worst- case running time of searching is O(n) 10
  • 11. BST Minimum (Maximum)  Find the minimum key in a tree rooted at x TreeMinimum(x) 01 while left[x] NIL 02 do x left[x] 03 return x  Running time O(h), i.e., it is proportional to the height of the tree 11
  • 12. Successor  Given x, find the node with the smallest key greater than key[x]  We can distinguish two cases, depending on the right subtree of x  Case 1  right subtree of x is nonempty  successor is leftmost node in the right subtree (Why?)  this can be done by returning TreeMinimum(right[x]) 12
  • 13. Successor (2)  Case 2  the right subtree of x is empty  successor is the lowest ancestor of x whose left child is also an ancestor of x (Why?) 13
  • 14. Successor Pseudocode TreeSuccessor(x) 01 if right[x] NIL 02 then return TreeMinimum(right[x]) 03 y p[x] 04 while y NIL and x = right[y] 05 x y 06 y p[y] 03 return y  For a tree of height h, the running time is O(h) 14
  • 15. BST Insertion  The basic idea is similar to searching  take an element z (whose left and right children are NIL) and insert it into T  find place in T where z belongs (as if searching for z),  and add z there  The running on a tree of height h is O(h), i.e., it is proportional to the height of the tree 15
  • 17. BST Insertion Pseudo Code TreeInsert(T,z) 01 y NIL 02 x root[T] 03 while x NIL 04 y x 05 if key[z] < key[x] 06 then x left[x] 07 else x right[x] 08 p[z] y 09 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 17
  • 18. BST Insertion: Worst Case  In what sequence should insertions be made to produce a BST of height n? 18