25
Chapter 8 (cont’)Part3 1 AVL TREE

AVL Tree

  • Upload
    fola

  • View
    30

  • Download
    2

Embed Size (px)

DESCRIPTION

AVL Tree. Chapter 8 ( cont ’)Part3. Search Trees. Two standard search trees : Binary Search Trees (non-balanced ) All items in left sub-tree are less than root All items in right sub-tree are greater than or equal to the root Each sub-tree is a binary search tree AVL trees (balanced ). - PowerPoint PPT Presentation

Citation preview

Page 1: AVL Tree

Chapter 8 (cont’)Part31

AVL TREE

Page 2: AVL Tree

2

Search Trees

Two standard search trees:

Binary Search Trees (non-balanced)All items in left sub-tree are less than

rootAll items in right sub-tree are greater

than or equal to the rootEach sub-tree is a binary search tree

AVL trees (balanced)

Page 3: AVL Tree

3

1- Binary Search Trees(review)

Page 4: AVL Tree

4

BST Traversals

A preorder traversal of the BST produces: 23 18 12 20 44 35 52

A postorder traversal of the BST produces: 12 20 18 35 52 44 23

An inorder traversal of the BST produces: 12 18 20 23 35 44 52 (a sorted list!)

Page 5: AVL Tree

5

2- AVL Trees• Invented by Adelson-Velskii and Landis• Height-balanced binary search tree where the heights of sub-

trees differ by no more than 1:

| HL – HR | <= 1• Search effort for an AVL tree is O(log2n)

• Each node has a balance factor• Balance factors may be:

– Left High (LH) = +1 (left sub-tree higher than right sub-tree)

– Even High (EH) = 0 (left and right sub-trees same height)– Right High (RH) = -1 (right sub-tree higher than left sub-

tree)

Page 6: AVL Tree

6 Figure 8-12

Page 7: AVL Tree

7 Figure 8-13 An AVL tree

Page 8: AVL Tree

8

Balancing Trees

Insertions and deletions potentially cause a tree to be imbalanced

When a tree is detected as unbalanced, nodes are balanced by rotating nodes to the left or right

Four imbalance cases: Left of left: the sub-tree of a left high tree has become left

high Right of right: the sub-tree of a right high tree has become

right high Right of left: the sub-tree of a left high tree has become

right high Left of right: the sub-tree of a right high tree has become

left high Each imbalance case has simple and complex rotation cases

Page 9: AVL Tree

9

Page 10: AVL Tree

10

Page 11: AVL Tree

11

Case 1: Left of Left

• The sub-tree of a left high tree has become left high

• Simple right rotation: Rotate the out of balance node (the root) to the right

• Complex right rotation: Rotate root to the right, so the old root is the right sub-tree of the new root; the new root's right sub-tree is connected to the old root's left sub-tree

Page 12: AVL Tree

12

Page 13: AVL Tree

13

Case 2: Right of Right• Mirror of Case 1: The sub-tree of a right

high tree has become right high• Simple left rotation: Rotate the out of

balance node (the root) to the left• Complex left rotation: Rotate root to the

left, so the old root is the left sub-tree of the new root; the new root's left sub-tree is connected to the old root's right sub-tree

Page 14: AVL Tree

14

Page 15: AVL Tree

15

Case 3: Right of Left

• The sub-tree of a left high tree has become right high

• Simple double rotation right: Rotate left sub-tree to the left; rotate root to the right, so the old root's left node is the new root

• Complex double rotation right: Rotate the right-high left sub-tree to the left; rotate the left-high left sub-tree to the right

Page 16: AVL Tree

16

Page 17: AVL Tree

17

Case 4: Left of Right

• Mirror of Case 3: The sub-tree of a right high tree has become left high

• Simple double rotation right: Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root

• Complex double rotation right: Rotate the left-high right sub-tree to the right; rotate the right-high right sub-tree to the left

Page 18: AVL Tree

18

Page 19: AVL Tree

19AVL Node Structure

Node

key <keyType>

data <dataType>

Left <pointer to Node>

right <pointer to Node>

bal <LH, EH, RH> // Balance factor

End Node

Page 20: AVL Tree

A tree was defined as either an empty structure or a structure whose children are disjoint trees.

This means that each node in some kind of trees can have more than two children. This tree is called a multiway tree of

order m, or m-way tree

multiway search tree

Page 21: AVL Tree

21

multiway search tree

A multiway search tree of order m is a multiway tree in which :1. Each node has m children and m-1

keys.2. The keys in each node are in ascending

order.3. The keys in the first i children are

smaller than the ith key.4. The keys in the last m-i children are

larger than the ith key.

Page 22: AVL Tree
Page 23: AVL Tree

B- Trees operates closely with secondary storage. Property: the size of a node can be made as large

as a block. A B- tree of order m is a multi way search tree with

the following properties:1. The root has at least two subtrees unless it is a

leaf.2. Each nonroot and each nonleaf node holds k-1

keys and k pointers to subtrees where [m/2] ≤ k ≤ m.

3. Each leaf node holds k-1 keys where [m/2] ≤ k ≤ m.

4. All leaves are usually on the same level.

B-Tree

Page 24: AVL Tree
Page 25: AVL Tree

25

References:• Text book, chapter 8: Binary Trees

End Of Chapter