Binary and AVL Trees in C

CSC2100B Tutorial 4 Binary and AVL Trees in C Jianye Hao 1 Overview  Binary tree  Degree of tree is 2 struct node_s { Datatype element; struct n...
30 downloads 0 Views 704KB Size
CSC2100B Tutorial 4

Binary and AVL Trees in C Jianye Hao

1

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;

2

Trees – traversal (Recursion Method) Preorder void preorder(node *t) { if (t != NULL) { printf(“%d ”, t->element); preorder(t->leftChild); preorder(t->rightChild); } }

/* V */ /* L */ /* R */

3

Trees – traversal (Recursion Method)

Inorder void inorder(node *t) { if (t != NULL) { inorder(t->leftChild); /* L */ printf(“%d ”, t->element); /* V */ inorder(t->rightChild); /* R */ } }

4

Trees – traversal (Recursion Method)

Postorder void postorder(node *t) { if (t != NULL) { postorder(t->leftChild); postorder(t->rightChild); printf(“%d ”, t->element); } }

/* L */ /* R */ /* V */

5

Trees - traversal A  Preorder ABDGHECFI

B

 Inorder GDHBEAFIC

D

C

E

F

 Postorder GHDEBIFCA G

H

I 6

AVL Tree 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.

7

AVL Tree

8

Non-AVL Tree

9

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)

10

AVL tree Height(right subtree) – Height(left subtree)

11

Non-AVL tree Height(right subtree) – Height(left subtree)

12

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;

13

Four Models There are four models about the operation of AVL Tree: LL RR LR RL

14

15

Left-Rotation

20

20 Add 22

21

21

22

Left-Rotation

20

Left-Rotation

Left-Rotation

20 21

21 21

22

20

22

22

16

Left-Rotation 25 25 20

30 20

Add 23 16

22

29

31 16

21

30 22

29

31

24 21

24

23 25 20 Left-Rotation 16

22

25

25

29

30

20 Left-Rotation 31

16

22

29

22 Left-Rotation

30 20

31

16 21

24

21

24

30

29

31

21 23

24 17

23

23

Right-Rotation

22

22 Add 20 21

21 20

22 21 20

22

Right-Rotation 21

21 Right-Rotation

Right-Rotation 20

22

20

18

Right-Rotation 25

25

20

30

20

30

Add 10 16

12

22

29

31

16

18

12

22

29

31

18

10 25 25 Right-Rotation 20

25 Right-Rotation 20

30

Right-Rotation 16 12

16

22 29

31

16

22 29

18

12

20 29

31

31

10 12

30

30

18

22

18 19

10

10

Left-Right Rotation 17 9

17 23

9

Add 8

7

17 23

7

8

17

17 23

9 23 Right-Rotation

8 Left-Rotation 7

23

Left-Rotation

7 8

9

9

8

17 Right-Rotation 8

7

23

9

7 20

Left-Right Rotation 30

30

14 20

12

11

35

18

33

30

14 37

22

Add 21

11

35 20

12

18

Left14 Rotation 35

33

37

22

11

21

LeftRotation 20 35 14

12

22

18 21

33

14

12

37

22

RightRotation

30

20 37

18

33

21

RightRotation

30

20

12

35 22

18 21

33

20

14 37

12 11

30

18

22 21

35 33

37 21

11

11

Right-Left Rotation 17 9

17 Add 27

25

9

17 25

28

9

25

28

28

RightRotation 27 17

17

9

27

9

25

17

25 LeftRotation

9 LeftRotation 25

27

27 RightRotation 28

27

28

28

22

Right-Left Rotation 30 14

40 20

12

30

35

33

Add 38 45

37

14

40 20

12

30

48

35

33

Right- 40 Rotation

14 45

37

20

12

35

48

33

45

37

38 30 Right- 35 Rotation

14 12

30

20

33

14 40

37

12 45

38

38

LeftRotation 35

33

40 37

48

LeftRotation

35

30 20

48

14 45

38

12 48

40 33 37

20

45

38

48 23

How to identify rotations? 2 17

17 Add 28 25

Right Child

Left Rotation

25 28

Right Subtree

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)

24

How to identify rotations? 2 17

17 Add 23

Right Child

Right-Left Rotation

25

25 23

Left Subtree -2 17

17 Left Child

Add 12

15

Right Rotation

15 12

Left Subtree

-2 17 17 Add 16

Left Child 15

Left-Right Rotation

15 16 Right Subtree

25

30

30 2

14

35 20

12

18

33

14

Add 21 37

12

22

35 Right Child 20 33 37

18

Left Rotation

22 Right Subtree 21

30

30 2

14

35

20

12

18

33

14

Add 17

37

20

12

22

35 Right Child

33

Right-Left Rotation

37

18 22 Left Subtree 17 -2

30

30 Left Child

14 20

12

11

35

18

33

22

14

Add 21 37

20

12

11

35

18

33

Left-Right Rotation 37

22 Right Subtree 21

26

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. 27

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

28

AVL tree CreateNewNode t = malloc(sizeof(struct AVLnode); t->element = x; t->left = NULL; t->right = NULL;

29

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

30

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

31

Demo

http://www.site.uottawa.ca/~stan/csi2514/a pplets/avl/BT.html

32

You can insert, delete and locate nodes in the tree using control buttons. The data can be entered manually or randomly generated. By pressing button only, you can quickly build a large tree. 33

The End Any Questions?

34