cse 332 data abstractions: dictionary adt: arrays, lists and trees · 2012-06-27 · dictionaries...
TRANSCRIPT
![Page 1: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/1.jpg)
CSE 332 Data Abstractions:
Dictionary ADT: Arrays, Lists and Trees
Kate Deibel
Summer 2012
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 1
![Page 2: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/2.jpg)
Where We Are Studying the absolutely essential ADTs of computer science and classic data structures for implementing them
ADTs so far:
Stack: push, pop, isEmpty, …
Queue: enqueue, dequeue, isEmpty, …
Priority queue: insert, deleteMin, …
Next:
Dictionary/Map: key-value pairs
Set: just keys
Grabbag: random selection
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 2
![Page 3: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/3.jpg)
MEET THE DICTIONARY AND SET ADTS
Dictionary sometimes goes by Map. It's easier to spell.
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 3
![Page 4: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/4.jpg)
Dictionary and Set ADTs
The ADTs we have already discussed are mainly defined around actions:
Stack: LIFO ordering
Queue: FIFO ordering
Priority Queue: ordering by priority
The Dictionary and Set ADTs are the same except they focus on data storage/retrieval:
insert information into structure
find information in structure
remove information from structure
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 4
![Page 5: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/5.jpg)
A Key Idea
If you put marbles into a sack of marbles, how do you get back your original marbles?
You only can do that if all marbles are somehow unique.
The Dictionary and Set ADTs insist that everything put inside of them must be unique (i.e., no duplicates).
This is achieved through keys.
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 5
![Page 6: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/6.jpg)
The Dictionary (a.k.a. Map) ADT
Data:
Set of (key, value) pairs
keys are mapped to values
keys must be comparable
keys must be unique
Standard Operations:
insert(key, value)
find(key)
delete(key)
insert(deibel, ….)
find(swansond)
Swanson, David, …
Like with Priority Queues, we will tend to emphasize the keys, but you should not forget about the stored values
• jfogarty James
Fogarty
…
• trobison Tyler Robison
…
• swansond David Swanson,
…
• deibel
Katherine, Deibel …
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 6
![Page 7: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/7.jpg)
The Set ADT
Data:
keys must be comparable
keys must be unique
Standard Operations:
insert(key)
find(key)
delete(key)
insert(deibel)
find(swansond)
swansond
• jfogarty
• trobison
• swansond
• deibel
• djg
• tompa
• tanimoto
• rea
…
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 7
![Page 8: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/8.jpg)
Comparing Set and Dictionary
Set and Dictionary are essentially the same
Set has no values and only keys
Dictionary's values are "just along for the ride"
The same data structure ideas thus work for both dictionaries and sets
We will thus focus on implementing dictionaries
But this may not hold if your Set ADT has other important mathematical set operations
Examples: union, intersection, isSubset, etc.
These are binary operators on sets
There are better data structures for these
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 8
![Page 9: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/9.jpg)
A Modest Few Uses
Any time you want to store information according to some key and then be able to retrieve it efficiently, a dictionary helps:
Networks: router tables
Operating systems: page tables
Compilers: symbol tables
Databases: dictionaries with other nice properties
Search: inverted indexes, phone directories, …
And many more
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 9
![Page 10: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/10.jpg)
But wait…
No duplicate keys? Isn't this limiting? Duplicate data occurs all the time!?
Yes, but dictionaries can handle this:
Complete duplicates are rare. Use a different field(s) for a better key
Generate unique keys for each entry (this is how hashtables work)
Depends on why you want duplicates
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 10
![Page 11: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/11.jpg)
Example: Dictionary for Counting
One example where duplicates occur is calculating frequency of occurrences
To count the occurrences of words in a story:
Each dictionary entry is keyed by the word
The related value is the count
When entering words into dictionary
Check if word is already there
If no, enter it with a value of 1
If yes, increment its value
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 11
![Page 12: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/12.jpg)
IMPLEMENTING THE DICTIONARY
Calling Noah Webster…
or at least a Civil War veteran in a British sanatorium…
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 12
![Page 13: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/13.jpg)
Some Simple Implementations
Arrays and linked lists are viable options, just not great particular good ones.
For a dictionary with n key/value pairs, the worst-case performances are:
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 13
Insert Find Delete
Unsorted Array O(1) O(n) O(n)
Unsorted Linked List O(1) O(n) O(n)
Sorted Array O(n) O(log n) O(n)
Sorted Linked List O(n) O(n) O(n)
Again, the array shifting
is costly
![Page 14: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/14.jpg)
Lazy Deletion in Sorted Arrays
Instead of actually removing an item from the sorted array, just mark it as deleted using an extra array
Advantages:
Delete is now as fast as find: O(log n)
Can do removals later in batches
If re-added soon thereafter, just unmark the deletion
Disadvantages:
Extra space for the “is-it-deleted” flag
Data structure full of deleted nodes wastes space
find O(log m) time (m is data-structure size)
May complicate other operations
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 14
10 12 24 30 41 42 44 45 50
![Page 15: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/15.jpg)
Better Dictionary Data Structures
The next several lectures will dicuss implementing dictionaries with several different data structures
AVL trees
Binary search trees with guaranteed balancing
Splay Trees
BSTs that move recently accessed nodes to the root
B-Trees
Another balanced tree but different and shallower
Hashtables
Not tree-like at all
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 15
![Page 16: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/16.jpg)
See a Pattern?
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 16
TREES!!
![Page 17: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/17.jpg)
Why Trees?
Trees offer speed ups because of their branching factors
Binary Search Trees are structured forms of binary search
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 17
![Page 18: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/18.jpg)
Binary Search
3 4 5 7 8 9 10 1
find(4)
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 18
![Page 19: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/19.jpg)
Binary Search Tree
Our goal is the performance of binary search in a tree representation
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 19
3 4 5 7 8 9 10 1
![Page 20: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/20.jpg)
Why Trees?
Trees offer speed ups because of their branching factors
Binary Search Trees are structured forms of binary search
Even a basic BST is fairly good
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 20
Insert Find Delete
Worse-Case O(n) O(n) O(n)
Average-Case O(log n) O(log n) O(log n)
![Page 21: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/21.jpg)
BINARY SEARCH TREES: A REVIEW
Cats like to climb trees… my Susie prefers boxes…
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 21
![Page 22: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/22.jpg)
Binary Trees
A non-empty binary tree consists of a
a root (with data)
a left subtree (may be empty)
a right subtree (may be empty)
Representation:
For a dictionary, data will include a key and a value
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 22
Data
right pointer
left pointer
A
B
D E
C
F
H G
J I
![Page 23: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/23.jpg)
Tree Traversals
Pre-Order: root, left subtree, right subtree
+ * 2 4 5
In-Order: left subtree, root, right subtree
2 * 4 + 5
Post-Order:left subtree, right subtree, root
2 4 * 5 +
+
*
2 4
5
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 23
A traversal is a recursively defined order for visiting all the nodes of a binary tree
![Page 24: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/24.jpg)
Binary Search Trees
BSTs are binary trees with the following added criteria:
Each node has a key for comparing nodes
Keys in left subtree are smaller than node’s key
Keys in right subtree are larger than node’s key
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 24
A
B
D E
C
F
H G
J I
![Page 25: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/25.jpg)
Are these BSTs?
3
11 7 1
8 4
5
4
18 10 6 2
11 5
8
20
21
7
15
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 25
![Page 26: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/26.jpg)
Are these BSTs?
3
11 7 1
8 4
5
4
18 10 6 2
11 5
8
20
21
7
15
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 26
![Page 27: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/27.jpg)
Calculating Height
What is the height of a BST with root r?
Running time for tree with n nodes:
O(n) – single pass over tree
How would you do this without recursion?
Stack of pending nodes, or use two queues
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 27
int treeHeight(Node root) { if(root == null) return -1; return 1 + max(treeHeight(root.left), treeHeight(root.right));
}
![Page 28: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/28.jpg)
Find in BST, Recursive
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 28
20 9 2
15 5
12
30 7 17 10
Data find(Key key, Node root){
if(root == null)
return null;
if(key < root.key)
return find(key, root.left);
if(key > root.key)
return find(key, root.right);
return root.data;
}
![Page 29: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/29.jpg)
Find in BST, Iterative
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 29
Data find(Key key, Node root){
while(root != null && root.key != key) {
if(key < root.key)
root = root.left;
else(key > root.key)
root = root.right;
}
if(root == null)
return null;
return root.data;
} 20 9 2
15 5
12
30 7 17 10
![Page 30: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/30.jpg)
Performance of Find
We have already said it is worst-case O(n)
Average case is O(log n)
But if want to be exact, the time to find node x is actually Θ(depth of x in tree)
If we can bound the depth of nodes, we automatically bound the time for find()
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 30
![Page 31: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/31.jpg)
Other “Finding” Operations
Find minimum node
Find maximum node
Find predecessor of a non-leaf
Find successor of a non-leaf
Find predecessor of a leaf
Find successor of a leaf
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 31
20 9 2
15 5
12
30 7 17 10
![Page 32: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/32.jpg)
Insert in BST
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 32
insert(13)
insert(8)
insert(31)
20 9 2
15 5
12
30 7 17 10
![Page 33: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/33.jpg)
Insert in BST
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 33
insert(13)
insert(8)
insert(31)
9 2
15 5
12
7
20
30 17 10
13
![Page 34: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/34.jpg)
Insert in BST
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 34
insert(13)
insert(8)
insert(31)
9 2
15 5
12
7
20
30 17 10
13
8
![Page 35: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/35.jpg)
Insert in BST
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 35
insert(13)
insert(8)
insert(31)
9 2
15 5
12
7
20
30 17 10
13
8 31
![Page 36: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/36.jpg)
Insert in BST
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 36
The code for insert is the same as with find except you add a node when you fail to find it.
What makes it easy is that inserts only happen at the leaves.
9 2
15 5
12
7
20
30 17 10
13
8 31
![Page 37: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/37.jpg)
Deletion in BST
20 9 2
15 5
12
30 7 17
Why might deletion be harder than insertion?
10
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 37
![Page 38: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/38.jpg)
Deletion
Removing an item disrupts the tree structure
Basic idea:
find the node to be removed,
Remove it
Fix the tree so that it is still a BST
Three cases:
node has no children (leaf)
node has one child
node has two children
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 38
![Page 39: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/39.jpg)
Deletion – The Leaf Case
This is by far the easiest case… you just cut off the node and correct its parent
20 9 2
15 5
12
30 7 17
delete(17)
10
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 39
![Page 40: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/40.jpg)
Deletion – The One Child Case
If there is only one child, we just pull up the child to take its parents place
delete(15)
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 40
20 9 2
15 5
12
30 7 10
20
9 2
5
12
30
7 10
![Page 41: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/41.jpg)
Deletion – The Two Child Case
Deleting a node with two children is the most difficult case. We need to replace the deleted node with another node.
What node is the best to replace 5 with?
30 9 2
20 5
12
7 10 delete(5)
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 41
![Page 42: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/42.jpg)
Deletion – The Two Child Case
Idea: Replace the deleted node with a value guaranteed to be between the node's two child subtrees
Options are
successor from right subtree: findMin(node.right)
predecessor from left subtree: findMax(node.left)
These are the easy cases of predecessor/successor
Either option is fine as both are guaranteed to exist in this case
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 42
![Page 43: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/43.jpg)
Delete Using Successor
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 43
30 9 2
20 5
12
7 10
delete(5)
findMin(right sub tree) 7
30 9 2
20 7
12
10
![Page 44: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/44.jpg)
Delete Using Predecessor
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 44
30 9 2
20 5
12
7 10
delete(5)
findMax(left sub tree) 2
30 9
20 2
12
7 10
![Page 45: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/45.jpg)
BuildTree for BST
We had buildHeap, so let’s consider buildTree
Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty tree
If inserted in given order, what is the tree?
What big-O runtime for this kind of sorted input?
Is inserting in the reverse order any better?
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 45
1
2
3
O(n2) 9
8
7
![Page 46: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/46.jpg)
BuildTree for BST (take 2)
What if we rearrange the keys?
median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9
What tree does that give us?
What big-O runtime?
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 46
8 4 2
7 3
5
9
6
1
O(n log n)
![Page 47: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/47.jpg)
Give up on BuildTree
The median trick will guarantee a O(n log n) build time, but it is not worth the effort.
Why?
Subsequent inserts and deletes will eventually transform the carefully balanced tree into the dreaded list
Then everything will have the O(n) performance of a linked list
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 47
![Page 48: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/48.jpg)
Achieving a Balanced BST (part 1)
For a BST with n nodes inserted in arbitrary order
Average height is O(log n) – see text
Worst case height is O(n)
Simple cases, such as pre-sorted, lead to worst-case scenario
Inserts and removes can and will destroy the balance
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 48
![Page 49: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/49.jpg)
Achieving a Balanced BST (part 2)
Shallower trees give better performance
This happens when the tree's height is O(log n) like a perfect or complete tree
Solution: Require a Balance Condition that
1. ensures depth is always O(log n)
2. is easy to maintain
Doing so will take some careful data structure implementation… Monday's topic
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 49
![Page 50: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/50.jpg)
DATA STRUCTURE SCENARIOS
Time to put your learning into practice…
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 50
![Page 51: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/51.jpg)
About Scenarios
We will try to use lecture time to get some experience in manipulating data structures
We will do these in small groups then share them with the class
We will shake up the groups from time to time to get different experiences
For any data structure scenario problem:
Make any assumptions you need to
There are no “right” answers for any of these questions
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 51
![Page 52: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/52.jpg)
GrabBag A GrabBag is used use for choosing a random element from a collection. GrabBags are useful for simulating random draws without repetition, like drawing cards from a deck or numbers in a bingo game.
GrabBag Operations:
Insert(item e): e is inserted into the grabbag
Grab(): if not empty, return a random element
Size(): return how many items are in the grabbag
List(): return a list of all items in the grabbag
In groups:
Describe how you would implement a GrabBag.
Discuss the time complexities of each of the operations.
How complex are calls to random number generators?
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 52
![Page 53: CSE 332 Data Abstractions: Dictionary ADT: Arrays, Lists and Trees · 2012-06-27 · dictionaries with several different data structures AVL trees Binary search trees with guaranteed](https://reader030.vdocuments.net/reader030/viewer/2022040820/5e6840aca3c6cd20ab77ddde/html5/thumbnails/53.jpg)
Improving Linked Lists
For reasons beyond your control, you have to work with a very large linked list. You will be doing many finds, inserts, and deletes. Although you cannot stop using a linked list, you are allowed to modify the linked structure to improve performance.
What can you do?
June 27, 2012 CSE 332 Data Abstractions, Summer 2012 53