CSE 326 Lecture 7: More On Search Trees Today's Topics:: From Last Time: Remove (Delete) Operation
CSE 326 Lecture 7: More On Search Trees Today's Topics:: From Last Time: Remove (Delete) Operation
! Todays Topics: " Lazy Operations " Run Time Analysis of Binary Search Tree Operations " Balanced Search Trees # AVL Trees and Rotations ! Covered in Chapter 4 of the text
94
1. Find the node containing X 2. Replace it with: If it has no children, with NULL If it has 1 child, with that child If it has 2 children, with the node with the smallest value in its right subtree, (or largest value in left subtree) 3. Recursively remove node used in 2 and 3
! Worst case: Recursion propagates all
10
97
24
11
17
possible in the hope that a future operation will make the current operation unnecessary
Da t a Stru -ure ct s
Lazy Deletion
! Idea: Mark node as deleted; no need to reorganize tree
" Skip marked nodes during Find or Insert " Reorganize tree only when number of marked nodes exceeds a percentage of real nodes (e.g. 50%) " Constant time penalty only due to marked nodes depth increases only by a constant amount if 50% are marked undeleted nodes (N nodes max N/2 marked)
! Modify Insert to make use of marked nodes whenever
" What is the best case tree? What is the worst case tree? ! Best Case Running Time of Insert/Remove/etc. = ? ! Worst Case Running Time = ? ! Average Case Running Time = ?
log N ! d ! N-1
! So, best case running time of BST operations is O(log N) ! Worst case running time is O(N) ! Average case running time = O(average value of d) = O(log N)
" Can prove that average depth over all nodes = O(log N) if all insertion sequences equally likely. " See Chap. 4 in textbook for proof
Can we do better?
! Worst case running time of BST operations is O(N)
depths of left and right subtrees? " Goal: Get depth down from O(N) to O(log N)
6 4 1 9 5 8
Insert 2 & make tree complete
5 2 1
8 4 6 9
8
order to achieve faster running times for the BST operations " Adelson-Velskii and Landis (AVL) trees (1962) " Splay trees and other self-adjusting trees (1978) " B-trees and other multiway search trees (1972)
AVL Trees
! AVL trees are height-balanced binary
search trees
! Balance factor of a node = height(left
7 (-1)-0 = -1 8 0
10
6 0 0 4 0 1 9 1 5 8 0 0 6 0 1 4 7 -1 0 9
13
" Height h ! 1.44 log(N+2)-0.328 " Prove using recurrence relation for minimum number of nodes S(h) in an AVL tree of height h: S(h) = S(h-1) + S(h-2) + 1 " Use Fibonacci numbers to get bound on S(h) bound on height h Height = " See textbook for details
O(log N) 0 1
Insert 3 6 0 1 5 0 4 7 -1 0 9
14
6 0 1 5 0 4 7 -1 0 9 Insert 3
6 1
2
1 4 0 3
7 -1 0 9
for some node on the path from insertion point to root node
! Idea: After Inserting the new node,
1. Back up to root updating heights along the access path 2. If Balance Factor = 2 or 2, adjust tree by rotation around deepest such node. 6 1
2
1 4
R. Rao, CSE 326
7 -1 0 9
16
0 3
6 0 1 5 0 4 7 -1 0 9
Insert 3
6 1
Rotate 0 4 0 3
6 0 7 -1 5 0 AVL 9 0
2
1 4 0 3
7 -1 0 9
AVL
Not AVL
17
BF = 1 BF = 1 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0 BF = 0
18
Outside Case
BF = ? BF = ? BF = ? BF = ? BF = 0 BF = 0 BF = 0
19
Inside Case
BF = BF = BF = BF = BF = 0 BF = BF = BF = BF = BF =
20
j Z
k X
R. Rao, CSE 326
Y
22
j Z Y
k X
R. Rao, CSE 326 23
j k
Do a right rotation
Z Y X
R. Rao, CSE 326 24
j k
Do a right rotation
Z Y X
R. Rao, CSE 326 25
k j X
R. Rao, CSE 326
Z
26
j Z
k X
R. Rao, CSE 326
Y
27
j k
Z X
R. Rao, CSE 326
Y
28
k j X
Z Y
R. Rao, CSE 326 29
j Z
k X
R. Rao, CSE 326
Y
30
j i Z W
31
k X V
R. Rao, CSE 326
j k X V
R. Rao, CSE 326
i W
32
i k W X
R. Rao, CSE 326
Z V
33
i j W
k X
R. Rao, CSE 326
Z
34
4 3 6
35
37