This document discusses ordered dictionaries and binary search trees (BSTs). It describes how BSTs can support dictionary operations like search, minimum, maximum, and successor in O(h) time, where h is the height of the tree. The height of a BST after n insertions can be O(n) in the worst case, such as when keys are inserted in ascending or descending order.
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]]
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