my name is Diego and I am a CS Masters student, my hobbies include:
you may at one point in time have thought to yourself what does a masters degree look like?
it mostly includes the following things:
what is research??? you may be asking
it basically boils down to solving problems that other people haven't solved yet
what's so awesome about western is that we have a super awesome undergraduate research program!
all that you have to do to get involved is talk to a professor / assistant professor, and see if their interests align with yours!
all this to say that if you are interested in:
if you are interested, hop on over to the faculty page, and look for anyone with the title "Associate/Assistant/* Professor".
right, i'm here to teach!
last week we learned about how BSTs work!
we found out that we could find and insert an element into a BST in usually $lg(n)$ time!
but alas! its not always $lg(n)$ for these
if we insert 4, 7, 16, 20, 37, 38, and 43 in that order...
we get a linked list!!! 🤦
oh no! what ever will we do???
if only we could figure out a way to "balance" this tree!
lets check it out
well, we can visually see when a tree is super unbalanced, so a "balance factor" just allows us to formalize that feeling!
what are some ways that we could measure this?
what are some ways that we could measure this?
we just need a tool to do this for us
but we're going to break it down into some simple steps
The main three steps of a left rotation are going to be:
The main three steps of a left rotation are going to be:
The main three steps of a left rotation are going to be:
The main three steps of a left rotation are going to be:
def left_rotate(tree, x):
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
if y.left is not None:
y.left.parent = x
y.parent = x.parent # link x's parent to y
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
if y.left is not None:
y.left.parent = x
y.parent = x.parent # link x's parent to y
if x.parent is None:
tree.root = y
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
if y.left is not None:
y.left.parent = x
y.parent = x.parent # link x's parent to y
if x.parent is None:
tree.root = y
elif x == x.parent.left:
x.parent.left = y
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
if y.left is not None:
y.left.parent = x
y.parent = x.parent # link x's parent to y
if x.parent is None:
tree.root = y
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
def left_rotate(tree, x):
y = x.right # set y
x.right = y.left
if y.left is not None:
y.left.parent = x
y.parent = x.parent # link x's parent to y
if x.parent is None:
tree.root = y
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
y.left = x
x.parent = y
Now that you understand the formal algorithm, lets work it out together!
what I hope you get out of this lecture is the following:
what even is an AVL tree to begin with
an AVL tree is a data-structure that allows us to guarantee $O(lg(N))$ search, insertion, and deletion
| search | insertion | deletion | |
|---|---|---|---|
| Linked List | $O(N)$ | $O(N)$ | $O(N)$ |
| BST | $O(N)$ | $O(N)$ | $O(N)$ |
| AVL Tree | $O(lg(N))$ | $O(lg(N))$ | $O(lg(N))$ |
what if I told you that you have all of the tools that you need to implement AVL trees?
but I promise, you do! we just need to tie it all together!
what was the reason that we had "bad" ($O(N)$) worst case times before?
what was the reason that we had "bad" ($O(N)$) worst case times before?
An AVL tree is simply a BST where all nodes in the tree always have a balance factor of 1, -1 or 0
$BF(R)=Height(R_l) - Height(R_r)$
as we know, the three main operations for a tree are:
so lets check out how these are going to work for an AVL tree!
We can actually inference these data-structures just like a BST because they have the same format!
lets hope they're all this simple!
unfortunately for us, insertion isn't gonna be so easy 🙁
but the main idea is simple
ok if I planned everything correctly there is an example on the whiteboard
hopefully we just worked through an example on the whiteboard of what an insertion will look like
it might have been intuitively easy to decide which side needed to get rebalanced there
but it is going to be useful if we have some "rules" to follow.
before we start an insertion, we assume that the tree is roughly balanced
this means that for every node, the following holds: $abs(BF(node)) \le 1$
this is super useful, because there are really only 4 cases that we have to worry about!
the four cases are as follows that could unbalance our tree are
note: $BF(node) = Height(node_r) - Height(node_l)$ in this example
let's focus on case one for now (unbalanced left)
which of the following could fix our tree?
A) left_rotate(N)B) right_rotate(N)C) left_rotate(C)D) right_rotate(C)let's focus on case one for now (unbalanced left)
which of the following could fix our tree?
A) left_rotate(N)B) right_rotate(N)C) left_rotate(C)D) right_rotate(C)let's focus on case one for now (unbalanced left)
which of the following could fix our tree?
A) left_rotate(N)B) right_rotate(N)C) left_rotate(C)D) right_rotate(C)great work, now let's focus on case two (unbalanced right)
it's going to take two different rotations two do this one
left_rotate(C)great work, now let's focus on case two (unbalanced right)
it's going to take two different rotations two do this one
left_rotate(C)great work, now let's focus on case two (unbalanced right)
it's going to take two different rotations two do this one
left_rotate(C)great work, now let's focus on case two (unbalanced right)
it's going to take two different rotations two do this one
left_rotate(C)right_rotate(N)great work, now let's focus on case two (unbalanced right)
it's going to take two different rotations two do this one
left_rotate(C)right_rotate(N)what's nice about what we just learned is that we actually just solved all four cases!
let's re-examine our cases and see if we can notice any similarities!
alright, it's cool visualization time!
if you google "AVL tree visualizer", it should be the first result.
but if it's not, you can navigate to these slides and click here
I don't really know how much time that took up, but if there are any remaining seconds, let's work on some worksheets!
if you have enjoyed these lectures, please make sure to make note to Dr. Nilles. it would genuinely mean a lot!