course-web-page-Fall-2021

Repository for the Fall 2021 course web page

View the Project on GitHub IUDataStructuresCourse/course-web-page-Fall-2021

More about Binary Search Trees and AVL Trees

Ran out of time last week to discuss removing a node from a BST. So let’s go over that first.

Remove node z from a BST

Introduce the Segment Intersection Project. Demo the solution.

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.

Is the AVL invariant enough to keep a BST balanced?

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)).

Using AVL Trees for sorting

ADT’s that can be implemented by AVL Trees