0% found this document useful (0 votes)
6 views

02 Balanced Trees

Uploaded by

Benjamin Bravo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

02 Balanced Trees

Uploaded by

Benjamin Bravo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CS 1501

Balanced Trees
The Searching Problem
(still)

Given a collection of keys C, determine whether or not C


contains a specific key k

2
We couldn't get a O(lg n) runtime for BST

● In the worst case


○ (Unless otherwise specified, I'm always talking worst case)
● Because the BST rules couldn't guarantee the tree would be
balanced
● Could we design a different tree with other rules that would
guarantee a balanced result no matter the insertion order?

3
2-3 trees

● In addition to BST Nodes like we saw last time


○ 2-nodes
● Let's build a tree that has nodes with 3 children and 2 keys!
○ 3-nodes

10 50

Keys < 10 10 < Keys < 50 Keys > 50

4
How does this help us?

● We can now build trees from the leaves up instead of from


the root down
○ Consider inserting 10, then 50, then 25 into a 2-3 tree:

10 10 25 50 50

5
How does this help us?

● We can now build trees from the leaves up instead of from


the root down
○ Consider inserting 10, then 50, then 25 into a 2-3 tree:

25

10 50

6
Searching a 2-3 tree

E J R X

A C H L P Q S Z

● How would contains('H') proceed?


7
Inserting into a 2-3 tree

E J R X

A C H L P Q S Z

● How would put('K') proceed?


8
After put('K')

Want C here, need to split again, E is parent, C and J as children

Push E up M

E J R X
Push C up

A C H K L P Q S Z

Want D here, have to split the temporary 4-node into 2-nodes


C is parent with A and D as children
● How would put('D') proceed?
9
put('D')

E M

C E J R X

A C D H K L P Q S Z

10
After put('D')

E M

C J R X

A D H K L P Q S Z

● How would put('O') proceed?


11
After put('O')

E R

C J P X

A D H K L O Q S Z

12
How to split in a 2-3 tree

● In general, there 6 possible cases for splitting a temporary


4-node in a 2-3 tree:

13
Runtimes?

● Search will be O(h)


○ Where h is the height of the tree
○ So how tall can the tree grow?
■ When can the height grow?
● When we split the root
● Increases the length all paths from root to leaf by 1
● No other transformations increase the height of the tree
● All paths from root to leaf are the same length!
○ Meaning a 2-3 tree will always be perfectly balanced
regardless of insertion order!
■ h is O(lg n)
● Search is O(lg n)

14
What about insert runtime?

● May need to search all the way down the tree to find the
appropriate leaf to add to
○ O(lg n)
● If inserting into a 3-node, need to split it
○ Runtime for a split?
● If we split and the parent is also a 3-node, need to split that
○ How many possible splits?
● See Proposition F in Section 3.3 of the text

15
So what's the catch?

● Implementing this tree will be tricky


○ Consider the node object:
■ Can have a variable number of keys and children
■ Do we implement 2 different node classes?
■ When do we need to check what type of node we're
working with?
■ Much more complicated than a BST node
● … but does it have to be?

16
Implementing 3-nodes with binary tree nodes

E J

A C H L

J
E

L
C H
A
17
Red-Black BSTs

● Specifically, we'll be looking at


Sedgewick's left-leaning red-black BST
approach

● Red link bind together two 2-nodes to


represent 3-nodes
● Black links bind together the 2-3 tree
● Can store the color as a node attribute
○ Red links point to red nodes
○ Black links point to black nodes

18
Implementing 3-nodes with binary tree nodes

E J

A C H L

J
E
E

LL
C HH
A
A
19
Left-leaning red-black BST rules

● Have red and black links and satisfying the following three
restrictions:
○ Red links lean left.
○ No node has two red links connected to it.
○ The tree has perfect black balance
■ Every path from the root to a leaf link has the same
number of black links
● Black links are the links of a 2-3 tree, which we already
determined to be perfectly balanced
○ Hence, red-black BSTs will be perfectly balanced
according to their black links

20
Searching a red-black BST

Root

J
E

C H L
A

21
Inserting into a red-black BST

● First key added goes in a black node

10

● If the next key is less, just make the left child red

10
5

● What if the next key would be greater?

22
Make the right child red then rotate the parent left

10
15

Rotate left

15
10

● Rotating left is one of the 3 main operations we'll be using to


balance inserts into a red-black BST

23
Implementing rotate left

● Assuming we have the following node class:


class Node:
def __init__(self, key, is_red):
self.key = key
self.left = None
self.right = None
self.is_red = is_red # storing color as a boolean

● We can implement rotate_left() as:

def rotate_left(cur):
x = cur.right
cur.right = x.left
x.left = cur
x.is_red = cur.is_red
cur.is_red = True
return x 24
What about the next insert?

15
10

● Regardless of the next key (key<10, 10<key<15, key>15) the


next insert will be tricky
○ The easiest of these tricky cases will be a key > 15

25
Inserting to the right of the root black node

● Consider the equivalent 2-3 tree:

15
10 10 15

● What should the 2-3 tree to look like after inserting 25?
○ Convert that to a red-black BST to see what our goal is

15 15

10 25 10 25

26
So how do we get there?

● Just like with inserting into a single black node, we're going
to descend the tree like BST insert, then add a new red link

15 15
10 10 25

● Now, to get this looking like our target, we'll use another
operation: the color flip

27
Color flip

● Use color flipping on nodes with 2 red children


○ Set both child nodes to black
○ Set node, itself to red

15
15
10 25
10 25

● But now the root of our tree is red!


○ Even though in this case, the root is representing a 2-node,
and hence, should be black
○ After every insert, set the root to black
■ So why bother setting the current node to red as part of a
color flip??
28
Back to inserting into single 3-node

● What if the new key is less than the first two?


○ E.g., insert 5 into:

15
15 10
10 5

● Here, we use our third operation: rotate right


○ Apply to the root, color flip Don't forget
to set root
10 to black!
10
5 15
5 15
29
Now the middle case

● Insert 12 into:

15
15 10
10
12

● Rotating the node containing 10 left will get us in a similar


situation to where we started in the previous example!

15
12
10

30
And now…

● After 6 slides, you might be able to follow this graphic from


the textbook:

31
What about larger trees?

● Start off the same:


○ Search down the tree, insert below the appropriate leaf node
■ Will either be a stand-in for a 2-node or a 3-node
● We've covered all cases for inserting into either
■ May end up passing a red link up the tree
● From color flips
■ Need to apply rotations and color flips back up the tree
● The results of several inserts can be found on page 440 of
the text
○ Trace through the inserts on your own to make sure you
understand how this process is applied to larger trees!
32
Removing a key from the tree

33
Performance

● Has a 1-to-1 mapping with 2-3 trees, so guaranteed to have


logarithmic height
● This means that our operations will be O(lg n)
○ Worst case!
○ Specifically:
■ search, insertion, finding the minimum, finding the
maximum, floor, ceiling, rank, select, delete the minimum,
delete the maximum, delete, and range count
○ Refer to Proposition I from Section 3.3 of the text

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy