Download Balancing Trees

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

B-tree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CS122 Algorithms and Data Structures
What does it mean to “balance”
trees?
MW 11:00 am - 12:15 pm, MSEC 101
Instructor: Xiao Qin
Lecture 13: Balancing Trees
Rule #1
Rule #2
4
4
2
5
3
6
2
1
1
Rule #3
5
7
2
4
2
6
3
Rule #2: Require that every node have left and right subtrees
of the same height. Too restrictive.
Balancing Trees
Rule 3 violated
at node 4
4
0
3
7
Rule #1: Require that the left and right subtrees of the root
node have the same height. We can do better.
1
6
5
1
6
3
0
n What does it mean to “balance” trees?
The difference in height of right and left
subtrees of any node is either 0 or 1.
n When they are right-heavy or left-heavy,
the trees need to be balance.
Rule #3: Require that, for every node, the height of the left and
right subtrees can differ by most one.
1
Approaches
n
Reorder data and build a tree
Approach 1:
Reorder data and build a tree
Recursive implementation
Data must be sorted in an array before
the tree is created
n If the tree must be used while some
elements are still coming, this approach
is inefficient.
n
n
n
Restructure the tree when elements
arrive and result in an unbalanced tree
Approach 2:
The DSW Algorithm
Sorting is avoided
n An array is NOT required
n The basic functions are:
1. Right and left rotations
2. Backbone
3. Transfer the backbone into a
balanced tree
Approaches 3: AVL Trees
n
n
Repair
n
Suppose the tree violates a balance
condition. How and when can it be
repaired?
– Repair is accomplished via “tree rotations”.
– Repair is done either during insertions, or
after access of a node
n
n
n
Rebalance trees globally or locally
If a portion of the tree is affected, tree
rebalancing can be done locally
If the insertion or deletion of a node disturbs
the balance of a tree, the balance can be
immediately repaired.
Balancing AVL does not guarantee that the
resulting tree is perfectly balanced
Balance factor
Difference between the heights of left
and right subtrees.
n Height of the right subtree minus the
height of the left subtree.
n
2
Insertion of a Node
Insertion of a Node (cont.)
P
P
+2
+1
Q
Q
h
-1
0
h
h
Q
Q
+1
0
h
P
P
+2
+1
h
h
h
h
h
h
h
h
QL
New item
Case 1: Insert a node in the right subtree of the right child
Rotate Q about P
Insertion of a Node (cont.)
In these two cases, the tree P is a
stand-alone tree.
n The tree P can be part of a larger AVL
tree
n The central problem: Find a node P for
which the balance factor is
unacceptable after the insertion of a
node.
n
Deletion of a Node
New item
Case 2: Insert a node in the left subtree of the right child
Insertion of a Node (cont.)
Find the node P:
Move up toward the root from the
position of the new node and update
the balance factors of the nodes
encountered.
2. The first node for which the balance
factor becomes +/-2 is the root P of a
subtree that needs to be rebalanced.
n
1.
Deletion of a Node (cont.)
P
P
Only consider the deletion of a node
with at most one descendant.
n Balance factors are updated from the
parent of the deleted node up to the
root.
n It may improve the balance factor of its
parent
n It may worsen the balance factor of its
grandparent
+2
+1
n
Q
Q
+1
+1
h-1
h
h-1
h
h-1
h
QL
Delete a node
Case 1: Rotate Q about P
3
Deletion of a Node (cont.)
Deletion of a Node (cont.)
P
P
P
P
+2
+1
+1
+1
Q
Q
Q
Q
+1
0
-1
-1
h-1
h
h-1
h
-1
h
h
h-1
R
h-1
Delete
h-2
h-1
h-2
h-1
RR
RL
Case 2: Rotate Q about P
Case 3: Rotate R about Q, then about P
Deletion of a Node (cont.)
P
Using C++
P
+1
+1
k2
Q
Q
-1
k2
h-1
Z
R
h-1
Delete
Y
h-1
RL
X
New Item
X
New item
h-2
h-1
h
R
-1
h-1
k1
k1
-1
h
h-2
-1
h
h
QL
Delete a node
+1
R
RR
Case 4: Rotate R about Q, then about P
Using C++
Y
Z
void SingleRotate1 (AvlNode *&k2) const {
AvlNode *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2 = k1;
}
Conclusions
k2
k3
AVL trees maintain balance of binary
search trees while they are being
created via insertions of data.
n An alternative approach is to have trees
that readjust themselves when data is
accessed, making often accessed data
items move to the top of the tree. We
won’t be covering these (splay trees).
n
k1
k1
D
k3
h
k2
A
A
B
B
C
D
C
void DoubleRotate1 (AvlNode *&k3) const {
SingleRotate1( k3->left );
SingleRotate1( k3 );
}
4