Download - Data Structures
Data Structures
Haim Kaplan and Uri ZwickNovember 2013
Lecture 3Dynamic Sets / Dictionaries
Binary Search Trees
2
Dictionaries/Dynamic sets
• Maintain a set of items.• Each item has key and info fields.• Keys belong to a totally ordered universe,
and can be compared with each other• Support the following operations:
Insert, Delete, Search, Min, Max, …
Extremely useful data structure!
Abstract Data Type: DictionariesDic-Item(k,i) – Create a dictionary item containing key k and info i,
Key(I), Info(I) – The key and info contained in Dic-Item I.
Dictionary() – Create an empty dictionary
Insert(D,I) – Insert I into D
Delete(D,I) – Delete I from D (assuming I is in D)
Search(D,k) – Find a dic-item with key k in D, if any.
Min(D) – Return the dic-item with the minimum key in D.
Max(D) – Return the dic-item with the maximum key in D.
Successor(D,I) – Return the successor of I in D.
Predecessor(D,I) – Return the predecessor of I in D.
Assume that dic-items have distinct keys
5
Implementing dictionaries using doubly linked lists (ver. 1)
• Store the dic-items in a list, in no particular order.
• Insert a new dic-item to an arbitrary position of the list, e.g., the first or last position.
• Delete a dic-item using a supplied pointer to it.
• Search, and other operations, are implemented by scanning the list.
Insert, Delete – O(1) timeOther operations – O(n) time
6
Implementing dictionaries using doubly linked lists (ver. 2)
• Store the dic-items in a list, in increasing order of keys.
• Insert a new dic-item to the appropriate position
• Delete a dic-item by using a supplied pointer to it.
• Search is implemented by scanning the list, stopping when the key of the current item is larger than the key sought.
Insert,Search – O(n) time (or O(n/2) “on average”)
Delete – O(1) timeMin, Max, Successor, Predecessor – O(1) time
7
Implementing dictionaries using (circular) arrays
• Store the dic-items in an array, in increasing order of keys.
• Insert a new dic-item to the appropriate position
• Delete a dic-item by using a supplied pointer to it.
• Search implemented using binary search.
Insert, Delete – O(n) time (or O(n/2) )Min, Max, Successor, Predecessor – O(1) time
Search – O(log n)
8
Binary search
10 25 38 47 56 67 73 84 95
Successful search:Search(38)
0 1 2 3 4 5 6 7 8
high
mid
low
9
Binary search
Unsuccessful search:Search(39)
10 25 38 47 56 67 73 84 95
0 1 2 3 4 5 6 7 8
high
mid
low
10
Binary search
Key k was foundin position mid
Key k should be insertedin position mid or mid+1
11
Can we implement alloperations in O(log n) time?
Yes! Using Binary Search Trees
12
Binary search trees
A binary tree in which each node contains a dic-item.
Satisfies the binary-search-tree property:If y is in the left subtree of x, then y.key < x.key.
If y is in the right subtree of x, then y.key > x.key.
2 8
7
5 101right
info
left
xparent key
13
Binary search trees
2 8
7
5 101right
info
left
xparent key
Dic-Item ≡ Tree-Node
left, right, parent are initially null
D.root
14
A set can be represented using several different trees
1
8
2
7 10
5
2 8
7
5 101
Height – length of a longest path to a leaf
height=2 height=4
0 0
01
0
2
1 1
0 2
4
3
15
Tree-Search(x,k) – Look for k in the subtree of x
2 8
7
5 101
x
Tree-Search(x,5)
x
x
Search(D,k) Tree-Search(D.root,k)
We usually start the search at the root of the tree:
16
Tree-Position(x,k) – Look for k in the subtree of xReturn the last node encountered
2 8
7
5 101
x
Tree-Position(x,6)
x
x
y
y
y
Returns the node containing 5
Tree-Position(x,k) is used to find insertion points
17
Printing the elements of a BST in sorted order(In-order walk)
2 8
7
5 1010
1
2
3
4
5
Printing, of course, is just an example…
18
Finding the minimum“keep going left”
2 8
7
5 101
19
Successor(x)
If x has a right child, the successor of x is the
minimal element in x.right.x
What if x.right=null ?
“Go right once, and then left all the way”
20
x
Successor(x)
If x.right=null, the successor of x is the lowest ancestor y of x such
that x is in its left subtree
y
“Go up from x untilthe first turn right’’
21
x
Successor(x)
y
If x has the largest key, then Successor(x)=null.
Predecessor is symmetric
22
Insertions and deletions
23
2 8
7
5 101
Insertions
Insert(6)
Insert(9)
6 9
25
2 8
7
5 101
Deletion: easy cases first
Delete(6) – 6 is a leaf; simply remove it.Delete(8) – 8 has only one child; bypass it.
6 9
Delete(2) – more complicated…
Delete(10) – 10 has only one child; bypass it.
26
Deletion of a binary node
z
y
If z has two children,let y be the successor of z
y has no left child
Remove y from the tree
Replace z by y
Binary-search-tree property preserved!
Is it enough to let z.keyy.key?And maybe also z.infoy.info?
27
Analysis
Each operation takes O(h+1) time, where h is the height of the tree
In general h may be as large as n
Want to keep the tree with small h
28
Balanced trees
h = log2(n+1)−1
How do we keep the tree more or less balanced?
A full tree of height h contains n=2h+1 − 1 nodes
29
Randomly built BSTs
Maybe balancing will take care of itself?
Not if we insert the elements in sorted order.We get a path of length n
Things are usually ok if weinsert the elements in random order
Theorem: If n distinct keys are inserted into a BST in random order, then the expected height of the tree is O(log n).
We want worst-case results…
30
Rotations
x
y
B
C
A
y
x
B
A
C
Right rotate
Left rotate