binary and avl trees in c - cse.cuhk.edu.hk · pdf filebinary and avl trees in c jianye hao. 2...
TRANSCRIPT
1
CSC2100B Tutorial 4
Binary and AVL Trees in C
Jianye Hao
2
Overview
Binary tree
Degree of tree is 2
struct node_s {
Datatype element;
struct node_s *leftChild;
struct node_s *rightChild;
};
typedef struct node_s node;
3
Trees – traversal (Recursion Method)
Preorder
void preorder(node *t) {
if (t != NULL) {
printf(“%d ”, t->element); /* V */
preorder(t->leftChild); /* L */
preorder(t->rightChild); /* R */
}
}
4
Inorder
void inorder(node *t) {
if (t != NULL) {
inorder(t->leftChild); /* L */
printf(“%d ”, t->element); /* V */
inorder(t->rightChild); /* R */
}
}
Trees – traversal (Recursion Method)
5
Trees – traversal (Recursion Method)
Postorder
void postorder(node *t) {
if (t != NULL) {
postorder(t->leftChild); /* L */
postorder(t->rightChild); /* R */
printf(“%d ”, t->element); /* V */
}
}
6
Trees - traversal
Preorder
A B D G H E C F I
Inorder
G D H B E A F I C
Postorder
G H D E B I F C A
A
B C
D E F
IG H
7
Definition
An AVL tree (or Height-Balanced tree) is a
binary search tree such that:
The height of the left and right subtrees of the
root differ by at most 1.
The left and right subtrees of the root are AVL
trees.
AVL Tree
8
AVL Tree
9
Non-AVL Tree
10
Balance Factor
To keep track of whether a binary search
tree is a AVL tree, we associate with each
node a balance factor, which is
Height(right subtree) – Height(left subtree)
11
AVL tree
Height(right subtree) – Height(left subtree)
12
Non-AVL tree
Height(right subtree) – Height(left subtree)
13
AVL tree structure in C
For each node, the difference of height
between left and right are no more than 1.
struct AVLnode_s {
Datatype element;
struct AVLnode *left;
struct AVLnode *right;
};
typedef struct AVLnode_s AVLnode;
14
Four Models
There are four models about the operation
of AVL Tree:
LL RR LR RL
15
16
Left-Rotation
20
21
Add 22
20
21
22
20
21
22
Left-Rotation
20
21
22
Left-Rotation
20
21
22
Left-Rotation
17
Left-Rotation
Left-Rotation
20
25
30
16 22 29 31
21 24
Add 2320
25
30
16 22 29 31
21 24
23
20
25
30
16 22 29 31
21 24
23
Left-Rotation20
25
30
16 22 29 31
21 24
23
Left-Rotation22
25
30
20
21
29 31
16
24
23
18
Right-Rotation
22
21
Add 20
22
21
20
Right-Rotation
20
21
22
22
21
20
22
21
20
Right-Rotation
Right-Rotation
19
Right-Rotation
20
25
30
16 22 29 31
12 18
Add 1020
25
30
16 22 29 31
12 18
10
20
25
30
16 22 29 31
12 18
10
Right-Rotation
20
25
30
16 22 29 31
12 18
10
Right-Rotation 16
25
30
12 20 29 31
10 2218
Right-Rotation
20
Left-Right
Rotation
9
17
23
7
Add 89
17
23
8
Left-Rotation
9
17
23
Left-Rotation
Right-Rotation8
17
23
97
Right-Rotation
7
9
17
23
8
7
7
8
9
17
23
7
8
21
Left-Right
Rotation
14
30
35
12 373320
18 2211
14
30
35
12 373320
21
18 2211
Add 21
14
30
35
12 373320
21
18 2211
Left-
Rotation
20
30
35
14 373322
11
211812
Left-
Rotation 20
30
35
14 373322
11
211812
Right-
Rotation
30
20
35
14
3733
22
11 21
1812
Right-
Rotation
22
Right-Left
Rotation
9
17
25
28
9
17
25
28
Add 27
27
9
17
25
28
27
Right-
Rotation
9
17
25
27
28
Right-
Rotation
9
17
25
27
28
Left-
Rotation
9
17
25
27
28
Left-
Rotation
23
Right-Left
Rotation
14
30
40
12 453520
33 37
38
14
30
40
12 453520
33 37
Add 38
48 48
38
14
30
40
12 453520
33 37 48
Right-
Rotation
38
14
30
4012
45
35
20 33
37
48
Right-
Rotation
38
14
30
4012
45
35
20 33
37
48
Left-
Rotation
38
14
30 40
12
45
35
20
33 37
48
Left-
Rotation
24
How to identify rotations?
17
25
28
Add 28
17
25
First find the node that cause the imbalance (balance factor)
Then find the corresponding child of the imbalanced node (left node or right node)
Finally find the corresponding subtree of that child (left or right)
2
Right Child
Right Subtree
Left Rotation
25
How to identify rotations?
17
25
23
Add 23
17
25
2
Right Child
Left Subtree
Right-Left Rotation
17
15
12
Add 12
17
15
-2
Left Child
Left Subtree
Right Rotation
17
15
16
Add 16
17
15
-2
Left Child
Right Subtree
Left-Right Rotation
26
Add 2114
30
35
12 373320
18 22
14
30
35
12 373320
18 22
21
Add 2114
30
35
12 373320
18 2211
14
30
35
12 373320
18 2211
21
2
Right Child
Right Subtree
Left Rotation
Add 1714
30
35
12 373320
18 22
14
30
35
12 373320
18 22
17
2
Right Child
Left Subtree
Right-Left Rotation
-2
Left Child
Right Subtree
Left-Right Rotation
27
Balancing an AVL tree after an insertion
Begin at the node containing the item which was just inserted and move back along the access path toward the root.{ For each node determine its height and check the balance
condition. { If the tree is AVL balanced and no further nodes need be
considered.
else If the node has become unbalanced, a rotation is needed to balance it.
}
} we proceed to the next node on the access path.
28
AVLnode *insert(Datatype x, AVLnode *t) {
if (t == NULL) {
/* CreateNewNode */
}
else if (x < t->element) {
t->left = insert(x, t->left);
/* DoLeft */
}
else if (x > t->element) {
t->right = insert(x, t->right);
/* DoRight */
}
}
29
AVL tree
CreateNewNode
t = malloc(sizeof(struct AVLnode);
t->element = x;
t->left = NULL;
t->right = NULL;
30
AVL tree
DoLeft
if (height(t->left) - height(t->right) == 2)
if (x < t->left->element)
t = singleRotateWithLeft(t); // LL
else
t = doubleRotateWithLeft(t); // LR
31
AVL tree
DoRight
if (height(t->right) - height(t->left) == 2)
if (x > t->right->element)
t = singleRotateWithRight(t); // RR
else
t = doubleRotateWithRight(t); // RL
32
Demo
http://www.site.uottawa.ca/~stan/csi2514/a
pplets/avl/BT.html
33
You can insert, delete and locate nodes in
the tree using control buttons.
The data can be entered manually or
randomly generated.
By pressing <Insert> button only, you can
quickly build a large tree.
34
The End
Any Questions?