Binary Search Trees (§ 9.1)Binary search tree (BST) is a binary tree storing key-value pairs (entries): Lower keys are in
left subtrees Higher keys are in
right subtrees
Inorder traversal visits entries in order of their keys
6
92
41 8
44
22
Search (§ 9.1.1)Searches start at root
If key we are searching for is lower, we go to left child
If key is higher, go to right child
If a match, return entry
If we reach the end, throw exception
Example: TreeSearch(4, root)
Algorithm TreeSearch(k, v)if v.isExternal()
/* throw exception */if k key(v)
return TreeSearch(k, v.left())else if k key(v)
return velse /* k key(v) */
return TreeSearch(k, v.right())
6
9
1 8
Search (§ 9.1.1)TreeSearch(5, root)
6
92
41 8
Algorithm TreeSearch(k, v)if v.isExternal()
/* throw exception */if k key(v)
return TreeSearch(k, v.left())else if k key(v)
return velse /* k key(v) */
return TreeSearch(k, v.right())
InsertionFirst, search for k
If k is not in tree, add new node where search endsElse, replace value in entryInsert(5)
6
92
41 8
6
92
41 8
5
DeletionFor remove(k), again start with search for key k If k is not in tree,
throw exception
Example: remove(4) Node v has only
one child; replace v with its child node w
6
92
41 8
5
v
w
6
92
51 8
55
8
6
Deletion (cont.)Example: remove(3)
Node v has two childrenFind node w that follows v in an inorder traversal
Go to right child of v and then keep taking left children
Copy w’s entry into v Replace w with z, w’s
right child
3
1
8
6 9
v
w
2
5
1
8
6 9
v
2
z
z
PerformanceNeeds one node per entry: Space is O(n) Find, insert, remove take O(h) time, where h is height of tree Worst case: O(n) Best case: O(log n)
AVL Tree Definition (§ 9.2)
AVL trees are balanced trees AVL Tree is a BST
such that the heights of a node’s children differ by at most 1.
Balanced AVL tree where heights are shown in red
6
92
41 81
1
1
5
2
3 2
4
Insertion in an AVL TreeFirst step of insertion is identical to BST insertionExample: insert(54)
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
before insertion after insertion
Trinode RestructuringSuppose insertion causes tree to become unbalancedPerform rotations so b becomes topmost node
Case 1: Single rotation (e.g., left rotation about a
b
a
c
T0
T1
T2 T3
b
a c
T0 T1 T2 T3
Trinode RestructuringSuppose insertion causes tree to become unbalancedPerform rotations so b becomes topmost node
Case 2: Double rotation (right rotation about c, then left rotation about a)
c
b
a
T0
T1 T2
T3
b
ca
T0 T1 T2 T3
Removal in an AVL TreeRemoval begins just like in a BST, but this may cause an imbalanceRemove(32):
44
17
7832 50
8848
62
54
44
17
7850
8848
62
54
before deletion of 32 after deletion
Rebalancing after a Removal
After removal, need to travel up the tree to see if it is balancedMust restructure nodes where balance has been lostRestructuring may unbalance higher nodes, so must continue checking until root is reached44
17
7850
8848
62
54
c
b
a
44
17
78
50 88
48
62
54
Running Times for AVL Trees
Single rebalance/restructure is O(1) Single application does constant amount of work
Tree remains balanced – its height stays O(log n)Find takes time O(log n)Insert takes time _____________
Initial find is O(log n) Could do at most _______________________ rebalances
Remove takes time _______________ Initial find is O(log n) Could do at most _______________________ restructures
Splay Trees are BSTs (§ 9.3)Splay tree follows rules of a BST:
Nodes in the left subtree have smaller keys
Nodes in right subtree have larger keys
Nodes with equal keys must all be in left or all be in right subtree
Inorder traversal returns entries in order of their keys
20
3721147
3510
1
2
5
6
8 36
9
40
Searching in a Splay TreeFind starts like any BST searchExample: find(11) Not in tree,
eventually must throw an exception
20
3721147
3510
1
2
5
6
8 36
9
40
Searching in a Splay TreeExample: find(8) Return entry once we
are done
20
3721147
3510
1
2
5
6
8 36
9
40
Splay Trees Always Restructure
Splay trees splay themselves after every find, insert and remove Use rotations to move a node up the
tree Stop splaying only when the node
becomes tree’s root
Splaying keeps the most recently used items near the top of the tree
Left rotation about the rootRight rotation about the root
Splay Trees Always Rebalance
New operation: splay Splaying uses rotations to move node up to root
3 types of rotations used in splaying Single rotation is used ONLY when parent node
is the root node
root
x
T1 T2
T3
root
x
T1
T2T3
x
root
T1
T2T3
x
root
T1 T2
T3
Splay Trees Always Rebalance
Double rotations normally used Rotation for when node & parent are
both left children (similar for both right children)
parent
x
T
1
T
2
T3
grandparent
T4
parent
grandparent
T4T3
T2
x
T1
Splay Trees Always Rebalance
Double rotations normally used Rotation for when node & parent are
not similar type children
parent
x
T2 T3
T4
grandparent
T1
parent
x
T2 T3 T4
grandparent
T1
Which Node to splay?
The node to be splayed depends on the operation we just completed:metho
dnode to splay
find If key found, splay that node If not found, splay last node reached in the
search
insert Splay the new node which holds the inserted entry
remove
If key found, splay parent of the node removed
If not found, splay last node reached in the search
Performance of Splay Trees
Amortized cost of splaying is O(log n) Find frequently-requested entries in
O(1) time! Where would you use a splay tree?
Where would splay trees be a BAD IDEA™?
Your Turn
Insert 61, 98, 95, 48, 89, 63, 24, and 62 into a BST, an AVL tree, & splay treeSearch for 61, 24, and 0 within your BST, AVL tree, and splay tree from above. Show the search path and any changes to your tree after each search.