balanced search trees avl trees 2-3 trees 2-4 trees

37
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

Upload: percival-robertson

Post on 17-Dec-2015

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

Balanced Search Trees

AVL Trees

2-3 Trees

2-4 Trees

Page 2: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

2

AVL Trees

A binary search tree

Whenever it becomes unbalanced• Rearranges its nodes to get a balanced binary

search tree

Balance of a binary search tree upset when• Add a node• Remove a node

Thus during these operations, the AVL tree must rearrange nodes to maintain balance

Page 3: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

3

AVL Trees

After inserting (a) 60; (b) 50; and (c) 20 into an initially empty binary search tree, the tree is not

balanced; d) a corresponding AVL tree rotates its nodes to restore balance.

Page 4: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

4

AVL Trees

(a) Adding 80 to the previous tree does not change the balance of the tree;

(b) a subsequent addition of 90 makes tree unbalanced; (c) left rotation restores balance.

Page 5: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

5

Single Rotations

Before and after an addition to an AVL subtree that requires a right rotation to maintain its balance.

Page 6: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

6

Single Rotations

Before and after an addition to an AVL subtree that requires a left rotation to maintain balance.

Page 7: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

7

Single Rotations

Algorithm for right rotation

Algorithm for left rotation

Algorithm rotateRight(nodeN) nodeC = left child of nodeN Set nodeN’s left child to nodeC’s right child Set nodeC’s right child to nodeNreturn nodeC

Algorithm rotateLeft(nodeN) nodeC = right child of nodeN Set nodeN’s right child to nodeC’s left child Set nodeC’s left child to nodeNreturn nodeC

Key: An imbalance at node N in an AVL tree can be corrected by a single rotation ifa) the add occurred in the left subtree of N’s left child (rotate right)b) the add occurred in the right subtree of N’s right child (rotate left)

Page 8: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

8

Single RotationsAlgorithm rotateRight(nodeN) nodeC = left child of nodeN Set nodeN’s left child to nodeC’s right child Set nodeC’s right child to nodeNreturn nodeC

Algorithm rotateLeft(nodeN) nodeC = right child of nodeN Set nodeN’s right child to nodeC’s left child Set nodeC’s left child to nodeNreturn nodeC

Page 9: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

9

Double Rotations

(a) Adding 70 to the previous tree destroys its balance; to restore the balance, perform both

(b) a right rotation and (c) a left rotation.

Page 10: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

10

Double Rotations

(a-b) Before and after an addition to an AVL subtree that requires both a right rotation and a

left rotation to maintain its balance.

Page 11: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

11

Double Rotations

(c-d) Before and after an addition to an AVL subtree that requires both a right rotation and a

left rotation to maintain its balance.

Page 12: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

12

Double RotationsAlgorithm to perform a right-left double rotation

Algorithm rotateRightLeft(nodeN)nodeC = right child of nodeNSet nodeN’s right child to the subtree produced by rotateRight(nodeC)

return rotateLeft(nodeN)

Page 13: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

13

Double Rotations

(a) The previous AVL tree after additions that maintain its balance; (b) after an addition that

destroys the balance … continued →

Page 14: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

14

Double Rotations

Previous tree (c) after a left rotation; (d) after a right rotation.

Page 15: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

15

Double Rotations

Before and after an addition to an AVL

subtree that requires both a left and right rotation to maintain

its balance.

Page 16: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

16

Double RotationsAlgorithm to perform a left-right double rotation

Algorithm rotateLeftRight(nodeN) nodeC = left child of nodeN Set nodeN’s left child to the subtree produced by rotateLeft(nodeC)return rotateRight(nodeN)

Page 17: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

17

Double RotationsAn imbalance at node N of an AVL tree can be corrected by a double rotation if• The addition occurred in the left subtree of N's

right child (right-left rotation) or …• The addition occurred in the right subtree of

N's left child (left-right rotation)

A double rotation is accomplished by performing two single rotations• A rotation about N's grandchild • A rotation about node N's new child

Page 18: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

18

Double Rotations

The result of adding 60, 50, 20, 80, 90, 70, 55, 10, 40, and 35 to an initially empty(a) AVL tree; (b) binary search tree.

Page 19: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

19

AVL Trees (continued)Algorithm rebalance(nodeN)if (nodeN’s left subtree is taller than right subtree by more than 1) {

//insertion was in nodeN’s left subtreeif (left child of nodeN has left subtree taller than its right subtree)

rotateRight(nodeN) //addition was in left subtree of left childelse

rotateLeftRight(nodeN) //addition was in right subtree of left child}else if (nodeN’s right subtree is taller than left subtree by more than 1) {

//insertion was in nodeN’s right subtreeif (right child of nodeN has right subtree taller than its left subtree)

rotateLeft(nodeN) //addition was in right subtree of right childelse

rotateRightLeft(nodeN) //addition was in left subtree of right child}

//Note: requires method getHeightDifference()

Page 20: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

20

2 – 3 TreesA general search treeInterior nodes must have either 2 or 3 childrenA 2-node • Contains one data item s• Has 2 children• The data item s > data in left sub tree• The data item s < data in right sub tree

A 3-node• Contains 2 data items, s (the smaller) and l (the larger)• Has 3 children• Data < s is in left subtree• Data > l is in right subtree• When s < data < l, occurs in middle subtree

Page 21: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

21

2 – 3 Trees

(a) a 2-node; (b) a 3-node.

A 2-3 tree is completely balanced.

A 2-3 tree is completely balanced.

Page 22: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

22

2 – 3 Trees

A 2-3 tree.

The search algorithm for a 2-3 tree is an

extension of the search algorithm for a binary

search tree.

The search algorithm for a 2-3 tree is an

extension of the search algorithm for a binary

search tree.

Page 23: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

23

Adding Entries to a 2-3 Tree

An initially empty 2-3 tree after adding (a) 60 and (b) 50; (c), (d) adding 20 causes the 3-node to split.

Page 24: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

24

Adding Entries to a 2-3 Tree

The 2-3 tree after adding (a) 80; (b) 90; (e) 70.

Adding 55 to the 2-3 tree causes a leaf and then the root to split.

Page 25: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

25

Adding Entries to a 2-3 Tree

The 2-3 tree after adding (a) 10; (b), (c) 40. Now add 35…

(previous figure)

Page 26: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

26

Adding Entries to a 2-3 Tree

The 2-3 tree after adding 35.

Page 27: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

27

Splitting a Leaf

Splitting a leaf to accommodate a new entry when the leaf's parent contains

(a) one entry; (b) two entries

The first node to split is leaf that

already contains two entries

The first node to split is leaf that

already contains two entries

Page 28: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

28

Splitting a Leaf

Splitting an internal node to accommodate a new entry.

Page 29: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

29

Splitting the root

Splitting the root to accommodate a new entry.

Page 30: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

30

2-4 Trees

A general search tree

Interior nodes must have 2, 3, or 4 children

Leaves occur on the same level

Completely balanced

A 4-node.

fig 28-20

Page 31: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

31

Adding Entries to a 2-4 Tree

When adding entries to a 2-4 tree• Split any 4-node as soon as you encounter it

during the search for the new entry's position in the tree

• The addition is complete right after this search ends

Adding to a 2-4 tree is more efficient than adding to a 2-3 tree.

Page 32: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

32

Adding Entries to a 2-4 Tree

An initially empty 2-4 tree after adding (a) 60; (b) 50; (c) 20. Now add 80…

Page 33: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

33

Adding Entries to a 2-4 Tree

The 2-4 tree after (a) splitting the root;(b) adding 80; (c) adding 90. Now add 70…

Page 34: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

34

Adding Entries to a 2-4 Tree

The 2-4 tree after (a) splitting a 4-node; (b) adding 70.

Page 35: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

35

Adding Entries to a 2-4 Tree

The 2-4 tree after adding (a) 55; (b) 10; (c) 40. Now add 35…

Page 36: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

36

Adding Entries to a 2-4 Tree

The 2-4 tree after (a) splitting the leftmost 4-node; (b) adding 35.

Page 37: Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees

37

Comparing AVL, 2-3, and 2-4 Trees

Three balanced search trees obtained by adding 60, 50, 20, 80, 90, 70, 55, 10, 40, and 35;

(a) AVL; (b) 2-3; (c) 2-4.