Repository for the Fall 2021 course web page
View the Project on GitHub IUDataStructuresCourse/course-web-page-Fall-2021
Ran out of time last week to discuss removing a node from a BST. So let’s go over that first.
Case 1: (no left child)
| |
z A
\ ==>
A
Case 2: (no right child)
| |
z A
/ ==>
A
Case 3: Two children
|
z
/ \
A B
The main idea is to replace z with the node after z, which is the first node y in subtree B.
Two cases to consider:
Case a) B is y
| |
z ==> y
/ \ / \
A y A C
\
C
Case b) B is not y (y is properly inside B)
| |
z ==> y
/ \ / \
A B A B
... ...
| |
y C
\
C
What is the time complexity? answer: O(h) where h is the height
Student group work
Given the following AVL Tree, delete the node with key 8 and restore the AVL property using tree rotations:
y x
/ \ right_rotate(y) / \
x C ---------------> A y
/ \ <------------- / \
A B left_rotate(x) B C
(This example has two nodes that end up violating the AVL property.)
8
/ \
5 10
/ \ / \
2 6 9 11
/ \ \ \
1 3 7 12
\
4 Solution: * Step 1: replace node 8 with node 9
9
/ \
5 10
/ \ \
2 6 11
/ \ \ \
1 3 7 12
\
4
Step 3: rotate 10 left
9
/ \
5 11
/ \ / \
2 6 10 12
/ \ \
1 3 7
\
4
Step 5: rotate 9 right
5
/ \
/ \
2 9
/ \ / \
1 3 6 11
\ \ / \
4 7 10 12
Given a set of n segments, is their a pair of segments that intersect?
Suppose we have a routine for testing whether 2 segments intersect.
Simplifications: * No vertical segments * No three-way (or more) intersections
Brute force: test all combinations O(n²)
A better algorithm: Line Sweep
We’ll need fast membership testing, insert, remove, and next/previous.
We want the height to be O(log(n)) where n is the number of nodes.
What’s the worst case? Let N(h) be the minimum number of nodes in an AVL tree of height h. We want to force N(h) to be something like 2^h nodes.
For each node, right has one more in height.
Recurrence formula for N(h):
N(h) = N(h-1) + N(h-2) + 1
Let’s figure out a simple lower bound:
N(h-1) + N(h-2) + 1 > 2 N(h-2)
Solving the recurrence
F(h) = 2 F(h-2)
yields
F(h) = 2^(h/2)
so
N(h) > 2^(h/2)
Now let’s solve for h in terms of N(h). We take the log of both sides
log( N(h) ) > log( 2^(h/2) ) = h/2 log(2) = h/2
so
2 log(N(h)) > h
N(h) is the minimum number of nodes for a AVL tree of height h, so n > N(h) and we have:
2 log(n) > 2 log(N(h)) > h
so h in O(log(n)).
ordered set: insert, delete, next, previous
priority queue: insert, delete, min