????????(AVL Trees) - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

????????(AVL Trees)

Description:

BF(T) for any node T in an AVL tree is 1, 0, or 1. Balance Factors for an AVL Tree ... int bf; AvlNode *leftChild, *rightChild; class AVL { public: AVL() : root(0) ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 28
Provided by: tcu3
Category:
Tags: avl | class | trees

less

Transcript and Presenter's Notes

Title: ????????(AVL Trees)


1
2008/01/06
  • ????????(AVL Trees)

2
Height-Balanced
  • Definition
  • An empty tree is height-balanced.
  • If T is nonempty binary tree with TL and TR as
    its left and right subtrees respectively.
  • T is height-balanced iff
  • TL and TR are height-balanced, and
  • hL-hR?1 where hL and hR are heights of TL and
    TR, respectively.

3
Examples
Not height-balanced
Height-balanced
4
Balance Factor
  • Definition
  • For every node T, define its balance factor,
    BF(T), as
  • BF(T) hL - hR
  • where hL and hR are the heights of the left
    and right subtrees of T.
  • BF(T) for any node T in an AVL tree is 1, 0, or
    1.

5
Balance Factors for an AVL Tree
6
Construction of an AVL Tree
  • Consider to insert the following numbers
  • 8, 9, 10, 2, 1, 5, 3, 6, 4, 7, 11, 12

0
-1
8
8
0
9
Insert 8
Insert 9
7
Consider the nearest parent A with bf 2
-2
8
8
-1
RR
9
9
0
insert in the right subtree of the right subtree
of A
10
Insert 10
8
1
9
1
0
10
8
0
2
Insert 2
9
Consider the nearest parent A with bf 2
2
9
2
0
10
8
8
1
LL
2
2
0
1
insert in the left subtree of the left subtree of
A
Insert 1
10
Consider the nearest parent A with bf 2
2
9
9
-1
0
LR
10
2
0
1
1
insert in the right subtree of the left subtree
of A
8
8
0
5
Insert 5
11
1
1
8
8
-1
-1
-1
-1
9
2
9
2
0
0
0
0
1
0
1
1
10
5
10
5
0
0
0
3
3
6
Insert 3
Insert 6
12
Consider the nearest parent A with bf 2
2
8
-2
-1
9
2
2
0
0
RL
1
1
10
5
-1
0
insert in the left subtree of the right subtree
of A
3
6
3
0
4
Insert 4
13
2
8
8
-1
-1
3
9
LR
0
1
-1
2
10
5
5
0
0
-1
1
6
4
0
7
Insert 7
14
-1
5
-1
1
3
8
-2
1
-1
0
RR
9
9
2
6
4
0
-1
0
1
10
7
10
0
11
Insert 11
15
-1
5
-1
1
8
3
1
-1
-1
0
10
2
6
4
0
0
-1
0
1
9
7
11
0
12
Insert 12
16
Rotation Types (1)
  • Suppose Y is the new node.
  • LL Y is inserted in the left subtree of the left
    subtree of A.

A
B
B
LL
A
TBR
TA
TBL
TBR
TA
TBL
Height (TA) 2 Height(TBR) 1 gt Height (TA)
1 Height(TBR) Height (TBL) 1
Height(TBR) gtHeight (TBR) Height(TA)
17
Rotation Types (2)
  • LR Y is inserted in the right subtree of the
    left subtree of A

A
C
LR
TA
B
B
A
C
TA
TCL
TCR
TCL
TCR
18
Rotation Types (3)
  • RR Y is inserted in the right subtree of the
    right subtree of A.

A
B
RR
B
TA
A
C
C
TB
TA
TB
19
Rotation Types (4)
  • RL Y is inserted in the left subtree of the
    right subtree of A

C
A
RL
A
B
B
TA
C
TA
TCL
TCR
TCL
TCR
20
The Class Definition of AVL Tree
class AvlNode public AvlNode(int k)
data k bf 0 leftChild NULL rightChild
NULL private int data int
bf AvlNode leftChild, rightChild
class AVL public AVL() root(0)
bool Search(int key) bool
Insert(int key) bool Delete(int key)
private AvlNode root
Store the value of balance factor of the node
21
Phase 1
bool AVLInsert(int key) if (!root)
root new AvlNode(key)
return true //Phase 1 locate
insertion point for key AvlNode a 0,
//most recent node with bf 1
pa 0, //parent of a
p root, //p moves through the tree
pp 0 //parent of p
22
while (p) if (p-gtbf !
0) a p
pa pp if (k gt p-gtkey)
pp p
p p-gtrightChild else if
(k lt p-gtkey) pp p
p p-gtleftChild
else return false
23
Phase 2
//Phase 2 Insert and rebalance //k is not in
the tree and may be inserted as the appropriate
child of pp. AvlNode y new AvlNode(k) if (k
lt pp-gtkey) pp-gtleftChild y
//insert as left Child else
pp-gtrightChild y //insert as
right Child
24
  • Adjust balance factors of nodes on path from a to
    pp.

int d AvlNode b, //child of a
c //child of b if (a NULL) p
root d (k gt p-gtkey)? -1 1 else if (k gt
a-gtkey) b p a-gtrightChild d
-1 else b p a-gtleftChild
d 1 while (p ! y) if (k gt p-gtkey)
p-gtbf -1 p p-gtrightChild
else p-gtbf 1 p
p-gtleftChild
1
pa
8
d-1
-1
-1
a
9
2
b
0
0
0
p
1
10
5
1
0
0
3
6
-1
p
y
4
p
25
Rotation - LL
if (a NULL) return true else if
(a-gtbf0 a-gtbfd 0) a-gtbf d
return true if (d 1) //left
imbalance if (b-gtbf 1) //rotation
type LL a-gtleftChild
b-gtrightChild b-gtrightChild a
a-gtbf 0 b-gtbf 0
a
b
A
B
b
a
B
C
A
TA
C
TB
TA
TB
26
Rotation - LR
else //rotation type LR c
b-gtrightChild b-gtrightChild
c-gtleftChild a-gtleftChild
c-gtrightChild c-gtleftChild b
c-gtrightChild a switch (c-gtbf)
case 1 a-gtbf -1
b-gtbf 0 break case -1
b-gtbf 1 a-gtbf 0 break
case 0 b-gtbf 0 b-gtbf
0 break c-gtbf 0 b
c //b is the new root
a
b
c
c
b
a
27
else //right imbalance. This is
symmetric to left imbalance if (pa
NULL) root b else if (a
pa-gtleftChild) pa-gtleftChild b
else pa-gtrightChild b return
true
pa
pa
a
b
b
LL
a
Write a Comment
User Comments (0)
About PowerShow.com