cse373: data structures & algorithms lecture 4: dictionaries; … · 2015-10-09 · – union,...
TRANSCRIPT
![Page 1: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/1.jpg)
CSE373: Data Structures & Algorithms
Lecture 4: Dictionaries; Binary Search Trees
Kevin Quinn Fall 2015
![Page 2: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/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:
1. Stack: push, pop, isEmpty, … 2. Queue: enqueue, dequeue, isEmpty, …
Next:
3. Dictionary (also known as a Map): associate keys with values – Extremely common
Fall 2015 2 CSE373: Data Structures & Algorithms
![Page 3: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/3.jpg)
The Dictionary (a.k.a. Map) ADT
• Data: – set of (key, value) pairs – keys must be comparable
• Operations:
– insert(key,value) – find(key) – delete(key) – …
• cs373 à Data Structures
• dog à Labrador
• kanye à Kanye West
insert(cs373, ….)
find(kanye) Kanye West
Will tend to emphasize the keys; don’t forget about the stored values
Fall 2015 3 CSE373: Data Structures & Algorithms
![Page 4: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/4.jpg)
Common Uses of Dictionaries
Counting frequency of words in a book: Storing a contact list: Making a Facebook-esque graph of friends:
What happens when the keys aren’t all the same type? What about the values?
Fall 2015 4 CSE373: Data Structures & Algorithms
Map<String, Integer> Map<String, String>
Map<Person, Set<Person>>
![Page 5: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/5.jpg)
Comparison: The Set ADT
The Set ADT is like a Dictionary without any values – A key is present or not (no duplicates)
For find, insert, delete, there is little difference
– In dictionary, values are “just along for the ride” – So same data-structure ideas work for dictionaries and sets
But if your Set ADT has other important operations this may not hold – union, intersection, is_subset – Notice these are binary operators on sets
Fall 2015 5 CSE373: Data Structures & Algorithms
binary operation: a rule for combining two objects of a given type, to obtain another object of that type
![Page 6: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/6.jpg)
Dictionary data structures
There are many good data structures for (large) dictionaries
1. AVL trees (Friday’s class) – Binary search trees with guaranteed balancing
2. B-Trees – Also always balanced, but different and shallower – B ≠ Binary; B-Trees generally have large branching factor
3. Hashtables – Not tree-like at all
Skipping: Other balanced trees (e.g., red-black, splay)
But first some applications and less efficient implementations…
Fall 2015 6 CSE373: Data Structures & Algorithms
![Page 7: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/7.jpg)
A Modest Few Uses
• Search: inverted indexes, phone directories, … • Networks: router tables • Operating systems: page tables • Compilers: symbol tables • Databases: dictionaries with other nice properties • Biology: genome maps • …
Fall 2015 7 CSE373: Data Structures & Algorithms
Any time you want to store information according to some key and be able to retrieve it efficiently. Lots of programs do that!
![Page 8: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/8.jpg)
Simple implementations For dictionary with n key/value pairs
insert find delete Unsorted linked-list
Unsorted array
Sorted linked list
Sorted array * Unless we need to check for duplicates We’ll see a Binary Search Tree (BST) probably does better, but
not in the worst case unless we keep it balanced
Fall 2015 8 CSE373: Data Structures & Algorithms
O(1)* O(n) O(n)
O(1)* O(n) O(n)
O(n) O(n) O(n)
O(n) O(logn) O(n)
![Page 9: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/9.jpg)
Lazy Deletion
A general technique for making delete as fast as find: – Instead of actually removing the item just mark it deleted
Plusses: – Simpler – Can do removals later in batches – If re-added soon thereafter, just unmark the deletion
Minuses: – Extra space for the “is-it-deleted” flag – Data structure full of deleted nodes wastes space – find O(log m) time where m is data-structure size (okay) – May complicate other operations
Fall 2015 9 CSE373: Data Structures & Algorithms
10 12 24 30 41 42 44 45 50 ü û ü ü ü ü û ü ü
![Page 10: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/10.jpg)
Tree Terminology • node: an object containing a data value and le1/right children • root: topmost node of a tree • leaf: a node that has no children • branch: any internal node (non-‐root) • parent: a node that refers to this one • child: a node that this node refers to • sibling: a node with a common
• subtree: the smaller tree of nodes on the le1 or right of the current node
• height: length of the longest path from the root to any node (count edges)
• level or depth: length of the path from a root to a given node 7 6
3 2
1
5 4
root height = 2
Level 0
Level 1
Level 2
![Page 11: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/11.jpg)
Some tree terms (mostly review)
• There are many kinds of trees – Every binary tree is a tree – Every list is kind of a tree (think of “next” as the one child)
• There are many kinds of binary trees – Every binary search tree is a binary tree – Later: A binary heap is a different kind of binary tree
• A tree can be balanced or not – A balanced tree with n nodes has a height of O(log n) – Different tree data structures have different “balance
conditions” to achieve this
Fall 2015 11 CSE373: Data Structures & Algorithms
![Page 12: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/12.jpg)
Kinds of trees
Certain terms define trees with specific structure
• Binary tree: Each node has at most 2 children (branching factor 2) • n-ary tree: Each node has at most n children (branching factor n) • Perfect tree: Each row completely full • Complete tree: Each row completely full except maybe the bottom
row, which is filled from left to right
Fall 2015 12 CSE373: Data Structures & Algorithms
What is the height of a perfect binary tree with n nodes? A complete binary tree?
![Page 13: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/13.jpg)
Tree terms (review?)
Fall 2015 13 CSE373: Data Structures & Algorithms
A
E
B
D F
C
G
I H
L J M K N
Tree T
root(tree) leaves(tree) children(node) parent(node) siblings(node) ancestors(node) descendents(node) subtree(node)
depth(node) height(tree) degree(node) branching factor(tree)
![Page 14: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/14.jpg)
Binary Trees
• Binary tree is empty or – A root (with data) – A left subtree (may be empty) – A right subtree (may be
empty)
• Representation:
A
B
D E
C
F
H G
J I
Data right
pointer left
pointer
• For a dictionary, data will include a key and a value
Fall 2015 14 CSE373: Data Structures & Algorithms
![Page 15: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/15.jpg)
15
Binary Trees: Some Numbers Recall: height of a tree = longest path from root to leaf (count edges) For binary tree of height h:
– max # of leaves:
– max # of nodes:
– min # of leaves:
– min # of nodes:
Fall 2015 CSE373: Data Structures & Algorithms
![Page 16: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/16.jpg)
Binary Trees: Some Numbers Recall: height of a tree = longest path from root to leaf (count edges) For binary tree of height h:
– max # of leaves:
– max # of nodes:
– min # of leaves:
– min # of nodes:
2h
2(h + 1) - 1
1
h + 1
For n nodes, we cannot do better than O(log n) height, and we want to avoid O(n) height
Fall 2015 16 CSE373: Data Structures & Algorithms
![Page 17: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/17.jpg)
Calculating height
What is the height of a tree with root root?
Fall 2015 17 CSE373: Data Structures & Algorithms
int treeHeight(Node root) { ???
}
![Page 18: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/18.jpg)
Calculating height What is the height of a tree with root root?
Fall 2015 18 CSE373: Data Structures & Algorithms
int treeHeight(Node root) { if(root == null) return -1; return 1 + max(treeHeight(root.left), treeHeight(root.right)); }
Running time for tree with n nodes: O(n) – single pass over tree
Note: non-recursive is painful – need your own stack of pending nodes; much easier to use recursion’s call stack
![Page 19: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/19.jpg)
Tree Traversals
A traversal is an order for visiting all the nodes of a tree • Pre-order: root, left subtree, right subtree
• In-order: left subtree, root, right subtree
• Post-order: left subtree, right subtree, root
+
*
2 4
5
(an expression tree)
Fall 2015 19 CSE373: Data Structures & Algorithms
![Page 20: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/20.jpg)
Tree Traversals
A traversal is an order for visiting all the nodes of a tree • 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
(an expression tree)
Fall 2015 20 CSE373: Data Structures & Algorithms
![Page 21: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/21.jpg)
More on traversals
void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } }
Sometimes order doesn’t matter • Example: sum all elements
Sometimes order matters • Example: print tree with parent above
indented children (pre-order) • Example: evaluate an expression tree
(post-order)
A B D E C
F G
A
B
D E
C
F G
Fall 2015 21 CSE373: Data Structures & Algorithms
![Page 22: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/22.jpg)
Binary Search Tree
4
12 10 6 2
11 5
8
14
13
7 9
• Structure property (“binary”) – Each node has ≤ 2 children – Result: keeps operations simple
• Order property – All keys in left subtree smaller
than node’s key – All keys in right subtree larger
than node’s key – Result: easy to find any given key
Fall 2015 22 CSE373: Data Structures & Algorithms
![Page 23: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/23.jpg)
Are these BSTs?
3
11 7 1
8 4
5
4
18 10 6 2
11 5
8
20
21
7
15
Fall 2015 23 CSE373: Data Structures & Algorithms
![Page 24: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/24.jpg)
Are these BSTs?
3
11 7 1
8 4
5
4
18 10 6 2
11 5
8
20
21
7
15
Fall 2015 24 CSE373: Data Structures & Algorithms
![Page 25: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/25.jpg)
Find in BST, Recursive
20 9 2
15 5
12
30 7 17 10
int 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; }
Fall 2015 25 CSE373: Data Structures & Algorithms
![Page 26: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/26.jpg)
Find in BST, Iterative
20 9 2
15 5
12
30 7 17 10
int 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; }
Fall 2015 26 CSE373: Data Structures & Algorithms
![Page 27: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/27.jpg)
Other “Finding” Operations
• Find minimum node – “the Ralph Nader algorithm”
• Find maximum node – “the Zoolander algorithm”
• Find predecessor of a non-leaf • Find successor of a non-leaf • Find predecessor of a leaf • Find successor of a leaf
20 9 2
15 5
12
30 7 17 10
Fall 2015 27 CSE373: Data Structures & Algorithms
![Page 28: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/28.jpg)
Insert in BST
20 9 2
15 5
12
30 7 17
insert(13) insert(8) insert(31)
(New) insertions happen only at leaves – easy! 10
8 31
13
Fall 2015 28 CSE373: Data Structures & Algorithms
![Page 29: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/29.jpg)
Deletion in BST
20 9 2
15 5
12
30 7 17
Why might deletion be harder than insertion?
10
Fall 2015 29 CSE373: Data Structures & Algorithms
![Page 30: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/30.jpg)
Deletion • Removing an item disrupts the tree structure
• Basic idea: find the node to be removed, then “fix” the tree so that it is still a binary search tree
• Three cases: – Node has no children (leaf) – Node has one child – Node has two children
Fall 2015 30 CSE373: Data Structures & Algorithms
![Page 31: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/31.jpg)
Deletion – The Leaf Case
20 9 2
15 5
12
30 7 17
delete(17)
10
Fall 2015 31 CSE373: Data Structures & Algorithms
![Page 32: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/32.jpg)
Deletion – The One Child Case
20 9 2
15 5
12
30 7 10
Fall 2015 32 CSE373: Data Structures & Algorithms
delete(15)
![Page 33: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/33.jpg)
Deletion – The Two Child Case
30 9 2
20 5
12
7
What can we replace 5 with?
10
Fall 2015 33 CSE373: Data Structures & Algorithms
delete(5)
![Page 34: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/34.jpg)
Deletion – The Two Child Case
Idea: Replace the deleted node with a value guaranteed to be between the two child subtrees
Options: • successor from right subtree: findMin(node.right) • predecessor from left subtree: findMax(node.left)
– These are the easy cases of predecessor/successor Now delete the original node containing successor or predecessor • Leaf or one child case – easy cases of delete!
Fall 2015 34 CSE373: Data Structures & Algorithms
![Page 35: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/35.jpg)
Lazy Deletion
• Lazy deletion can work well for a BST – Simpler – Can do “real deletions” later as a batch – Some inserts can just “undelete” a tree node
• But – Can waste space and slow down find operations – Make some operations more complicated:
• How would you change findMin and findMax?
Fall 2015 35 CSE373: Data Structures & Algorithms
![Page 36: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/36.jpg)
BuildTree for BST • Let’s consider buildTree
– Insert all, starting from an empty tree
• Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
– 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?
1
2
3
O(n2) Not a happy place
Fall 2015 36 CSE373: Data Structures & Algorithms
![Page 37: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/37.jpg)
BuildTree for BST • Insert keys 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
• What we if could somehow re-arrange them – 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?
8 4 2
7 3
5
9
6
1
O(n log n), definitely better
Fall 2015 37 CSE373: Data Structures & Algorithms
![Page 38: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/38.jpg)
Unbalanced BST
• Balancing a tree at build time is insufficient, as sequences of operations can eventually transform that carefully balanced tree into the dreaded list
• At that point, everything is O(n) and nobody is happy – find – insert – delete
1
2
3
Fall 2015 38 CSE373: Data Structures & Algorithms
![Page 39: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/39.jpg)
Balanced BST
Observation • BST: the shallower the better! • For a BST with n nodes inserted in arbitrary order
– Average height is O(log n) – see text for proof – Worst case height is O(n)
• Simple cases, such as inserting in key order, lead to the worst-case scenario
Solution: Require a Balance Condition that 1. Ensures depth is always O(log n) – strong enough! 2. Is efficient to maintain – not too strong!
Fall 2015 39 CSE373: Data Structures & Algorithms
![Page 40: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/40.jpg)
Potential Balance Conditions 1. Left and right subtrees of the root
have equal number of nodes
2. Left and right subtrees of the root have equal height
Too weak! Height mismatch example:
Too weak! Double chain example:
Fall 2015 40 CSE373: Data Structures & Algorithms
![Page 41: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/41.jpg)
Potential Balance Conditions 3. Left and right subtrees of every node
have equal number of nodes
4. Left and right subtrees of every node have equal height
Too strong! Only perfect trees (2n – 1 nodes)
Too strong! Only perfect trees (2n – 1 nodes)
Fall 2015 41 CSE373: Data Structures & Algorithms
![Page 42: CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; … · 2015-10-09 · – union, intersection, is_subset – Notice these are binary operators on sets Fall 2015 CSE373:](https://reader030.vdocuments.net/reader030/viewer/2022040921/5e9a401a12e1a540cf70cc3b/html5/thumbnails/42.jpg)
42
The AVL Balance Condition Left and right subtrees of every node have heights differing by at most 1 Definition: balance(node) = height(node.left) – height(node.right) AVL property: for every node x, –1 ≤ balance(x) ≤ 1
• Ensures small depth – Will prove this by showing that an AVL tree of height
h must have a number of nodes exponential in h
• Efficient to maintain – Using single and double rotations
Fall 2015 CSE373: Data Structures & Algorithms