



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Fall 2008;
Typology: Study notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Overview
Balance Rotation
Search Insert
Tree Balance
Can we keep tree (mostly) balanced?
AVL trees Red-black trees
Select invariant (that keeps tree balanced) Fix tree after each insertion / deletion Maintain invariant using rotations Provides operations with O( log(n) ) worst case
AVL Trees
Binary search tree Heights of children for node differ by at most 1
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
Heights of children shown in red
Tree Rotations
Changes shape of tree
Rotation moves one node up in the tree and one node down Height is decresed by moving larger subtrees up and smaller subtrees down
Types
Single rotation Left Right Double rotation Left-right Right-left
Tree Rotation Example
1
2
3
1
2
3
Example – Single Rotations
T 0 T 1 T 3 T 0 T 1 T 2
T 3
single left rotation
T 2
T 3 T 2 T 1
T 0 T 1 T 2 T 3
T 0
single right rotation
Example – Double Rotations
right-left double rotation
T 0 T 2 T 1
T 3 T 0
T 2 T 1 T 3
T 3 T 1 T 2
T 0 T 3
T 1 T 0 T 2
left-right double rotation
Red-black Trees
Red-black Trees
Discovered in 1972 by Rudolf Bayer
Insert / delete may require complicated bookkeeping & rotations
TreeMap, TreeSet use red-black trees
Types of Multi-way Search Trees
Internal nodes have 2 or 3 children
Internal nodes have up to 26 children (for strings)
T = minimum degree Non-root internal nodes have T-1 to 2T-1 children All leaves have same depth
Multi-way Search Trees
Search(17)
Multi-way Search Trees
Insert( 4 )
Multi-way Search Trees
Insert( 1 )
Split node Split parent