SlideShare a Scribd company logo
1 of 18
Download to read offline
1 
Ordered Dictionaries 
… 
In addition 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.
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.
3 
Binary Search 
… 
Narrow down the search range in stages 
… 
findElement(22)
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?
5 
Binary Search Trees 
… 
A binary search tree is a binary tree T such that 
… 
each internal node stores an item (k,e) of adictionary 
… 
keys stored at nodes in the left subtreeof vare lessthan or equal to k 
… 
keys stored at nodes in the right subtreeof varegreater than or equal to k 
… 
Example sequence 2,3,5,5,7,8
6 
Searching a BST 
… 
To find an element with key kin a tree T 
… 
compare kwith key[root[T]] 
… 
if k < key[root[T]], search for kin left[root[T]] 
… 
otherwise, search for kin right[root[T]]
7 
Search Examples 
… 
Search(T, 11)
8 
Search Examples (2) 
… 
Search(T, 6)
9 
… 
Recursive version 
Pseudocode for BST Search 
Search(T,k) 
01x←root[T] 
02ifx=NILthenreturnNIL 
03ifk=key[x]thenreturnx 
04ifk<key[x] 
05thenreturnSearch(left[x],k) 
06elsereturnSearch(right[x],k) 
Search(T,k) 
01x←root[T] 
02whilex≠NILandk≠key[x]do 
03ifk<key[x] 
04thenx←left[x] 
05elsex←right[x] 
06returnx 
… 
Iterative version
10 
Analysis of Search 
… 
Running time on tree of height his O(h) 
… 
After the insertion of nkeys, the worst- case running time of searching is O(n)
11 
BST Minimum (Maximum) 
… 
Find the minimum key in a tree rooted at x 
… 
Running time O(h), i.e., it is proportional to the height of the tree 
TreeMinimum(x) 
01whileleft[x]≠NIL 
02dox←left[x] 
03returnx
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])
13 
Successor (2) 
… 
Case 2 
… 
the right subtree of xis empty 
… 
successor is the lowest ancestor of xwhose left child is also an ancestor of x (Why?)
14 
… 
For a tree of height h, the running time is O(h) 
Successor Pseudocode 
TreeSuccessor(x) 
01ifright[x]≠NIL 
02thenreturnTreeMinimum(right[x]) 
03y←p[x] 
04whiley≠NILandx=right[y] 
05x←y 
06y←p[y] 
03returny
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 Twhere zbelongs (as if searching for z), 
… 
and add zthere 
… 
The running on a tree of height his O(h), i.e., it is proportional to the height of the tree
16 
BST Insertion Example 
… 
Insert 8
17 
BST Insertion Pseudo Code 
TreeInsert(T,z) 
01y←NIL 
02x←root[T] 
03whilex≠NIL 
04y←x 
05ifkey[z]<key[x] 
06thenx←left[x] 
07elsex←right[x] 
08p[z]←y 
09ify=NIL 
10thenroot[T]←z 
11elseifkey[z]<key[y] 
12thenleft[y]←z 
13elseright[y]←z
18 
BST Insertion: Worst Case 
… 
In what sequence should insertions be made to produce a BST of height n?

More Related Content

What's hot

Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2SHAKOOR AB
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingSangeethaSasi1
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and PolynomialAroosa Rajput
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2jomerson remorosa
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables Nifras Ismail
 
linked list
linked listlinked list
linked listAbbott
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingSam Light
 
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
 

What's hot (20)

Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashing
 
Sorting
SortingSorting
Sorting
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Hashing
HashingHashing
Hashing
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
linked list
linked listlinked list
linked list
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Hash table
Hash tableHash table
Hash table
 
Hashing
HashingHashing
Hashing
 
lecture 4
lecture 4lecture 4
lecture 4
 
Hashing
HashingHashing
Hashing
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
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
 
Hashing
HashingHashing
Hashing
 

Similar to Lec8

lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
lecture 12
lecture 12lecture 12
lecture 12sajinsc
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
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
 
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
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
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
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 

Similar to Lec8 (20)

lecture 11
lecture 11lecture 11
lecture 11
 
lecture 12
lecture 12lecture 12
lecture 12
 
Trees
TreesTrees
Trees
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
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
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
 
Lec4
Lec4Lec4
Lec4
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
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
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
C12 bst
C12 bstC12 bst
C12 bst
 
C12 bst
C12 bstC12 bst
C12 bst
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

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
 

Lec8

  • 1. 1 Ordered Dictionaries … In addition 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.
  • 2. 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.
  • 3. 3 Binary Search … Narrow down the search range in stages … findElement(22)
  • 4. 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?
  • 5. 5 Binary Search Trees … A binary search tree is a binary tree T such that … each internal node stores an item (k,e) of adictionary … keys stored at nodes in the left subtreeof vare lessthan or equal to k … keys stored at nodes in the right subtreeof varegreater than or equal to k … Example sequence 2,3,5,5,7,8
  • 6. 6 Searching a BST … To find an element with key kin a tree T … compare kwith key[root[T]] … if k < key[root[T]], search for kin left[root[T]] … otherwise, search for kin right[root[T]]
  • 7. 7 Search Examples … Search(T, 11)
  • 8. 8 Search Examples (2) … Search(T, 6)
  • 9. 9 … Recursive version Pseudocode for BST Search Search(T,k) 01x←root[T] 02ifx=NILthenreturnNIL 03ifk=key[x]thenreturnx 04ifk<key[x] 05thenreturnSearch(left[x],k) 06elsereturnSearch(right[x],k) Search(T,k) 01x←root[T] 02whilex≠NILandk≠key[x]do 03ifk<key[x] 04thenx←left[x] 05elsex←right[x] 06returnx … Iterative version
  • 10. 10 Analysis of Search … Running time on tree of height his O(h) … After the insertion of nkeys, the worst- case running time of searching is O(n)
  • 11. 11 BST Minimum (Maximum) … Find the minimum key in a tree rooted at x … Running time O(h), i.e., it is proportional to the height of the tree TreeMinimum(x) 01whileleft[x]≠NIL 02dox←left[x] 03returnx
  • 12. 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])
  • 13. 13 Successor (2) … Case 2 … the right subtree of xis empty … successor is the lowest ancestor of xwhose left child is also an ancestor of x (Why?)
  • 14. 14 … For a tree of height h, the running time is O(h) Successor Pseudocode TreeSuccessor(x) 01ifright[x]≠NIL 02thenreturnTreeMinimum(right[x]) 03y←p[x] 04whiley≠NILandx=right[y] 05x←y 06y←p[y] 03returny
  • 15. 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 Twhere zbelongs (as if searching for z), … and add zthere … The running on a tree of height his O(h), i.e., it is proportional to the height of the tree
  • 16. 16 BST Insertion Example … Insert 8
  • 17. 17 BST Insertion Pseudo Code TreeInsert(T,z) 01y←NIL 02x←root[T] 03whilex≠NIL 04y←x 05ifkey[z]<key[x] 06thenx←left[x] 07elsex←right[x] 08p[z]←y 09ify=NIL 10thenroot[T]←z 11elseifkey[z]<key[y] 12thenleft[y]←z 13elseright[y]←z
  • 18. 18 BST Insertion: Worst Case … In what sequence should insertions be made to produce a BST of height n?