Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2...

34
1 CSC2100B Tutorial 4 Binary and AVL Trees in C Jianye Hao

Transcript of Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2...

Page 1: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

1

CSC2100B Tutorial 4

Binary and AVL Trees in C

Jianye Hao

Page 2: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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;

Page 3: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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 */

}

}

Page 4: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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)

Page 5: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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 */

}

}

Page 6: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 7: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 8: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

8

AVL Tree

Page 9: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

9

Non-AVL Tree

Page 10: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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)

Page 11: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

11

AVL tree

Height(right subtree) – Height(left subtree)

Page 12: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

12

Non-AVL tree

Height(right subtree) – Height(left subtree)

Page 13: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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;

Page 14: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

14

Four Models

There are four models about the operation

of AVL Tree:

LL RR LR RL

Page 15: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

15

Page 16: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 17: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 18: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 19: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 20: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 21: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 22: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 23: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 24: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 25: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 26: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 27: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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.

Page 28: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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 */

}

}

Page 29: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

29

AVL tree

CreateNewNode

t = malloc(sizeof(struct AVLnode);

t->element = x;

t->left = NULL;

t->right = NULL;

Page 30: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 31: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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

Page 33: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

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.

Page 34: Binary and AVL Trees in C - cse.cuhk.edu.hk · PDF fileBinary and AVL Trees in C Jianye Hao. 2 Overview Binary tree Degree of tree is 2 struct node_s {Datatype element; struct node_s

34

The End

Any Questions?