balanced search trees chapter 27 copyright ©2012 by pearson education, inc. all rights reserved

69
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Upload: horace-hamilton

Post on 31-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Balanced Search Trees

Chapter 27

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents

• AVL Trees Single Rotations Double Rotations Implementation Details

• 2-3 Trees Searching a 2-3 Tree Adding Entries to a 2-3 Tree Splitting Nodes During Addition

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents

• 2-4 Trees Adding Entries to a 2-4 Tree Comparing AVL, 2-3, and 2-4 Trees

• Red-Black Trees Properties of a Red-Black Tree Adding Entries to a Red-Black Tree Java Class Library: The Class TreeMap

• B-Trees

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives

• Perform rotation to restore balance of an AVL tree after addition

• Search for or add entry to 2-3 tree

• Search for or add entry to 2-4 tree

• Form red-black tree from given 2-4 tree

• Search for or add entry to red-black tree

• Describe purpose of a B-tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Balance

• Recall: operations on binary search tree are O(log n) Tree must be balanced

• Add and remove operations do not necessarily maintain that balance

• We seek ways to maintain balance And thus maintain efficiency

Copyright ©2012 by Pearson Education, Inc. All rights reserved

AVL Trees

• Binary search tree that rearranges nodes whenever it becomes unbalanced. Happens during addition or removal of node

• Uses Left rotation Right rotation Balanced node – the root of a balanced tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-1 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

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-2 (a) Adding 80 to the tree in Figure 27-1d does not change the balance of the tree;

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

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-3 Before and after an addition to an AVL subtree that requires a right rotation to maintain its balance

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-4 Before and after a right rotation restores balance to an AVL tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-5 Before and after an addition to an AVL subtree that requires a left rotation to maintain its balance

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-6 (a) Adding 70 to the tree in Figure 27-2c destroys its balance; to restore the balance, perform both (b) a right

rotation and (c) a left rotationCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-7 Before and after an addition to an AVL subtree that requires both a right rotation and

a left rotation to maintain its balanceCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-7 Before and after an addition to an AVL subtree that requires both a right rotation and

a left rotation to maintain its balanceCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-8 (a) The AVL tree in Figure 27-6c after additions that maintain its balance; (b) after an addition

that destroys the balance;

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-8 (c) after a left rotation; (d) after a right rotation

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-9 Before and after an addition to an AVL subtree that requires both a left rotation and

a right rotation to maintain its balanceCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-9 Before and after an addition to an AVL subtree that requires both a left rotation and

a right rotation to maintain its balanceCopyright ©2012 by Pearson Education, Inc. All rights reserved

Double Rotations

• Double rotation is accomplished by performing two single rotations: A rotation about node N’s grandchild G (its

child’s child) A rotation about node N’s new child

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Double Rotations

• Imbalance at node N of an AVL tree can be corrected by a double rotation if: Addition occurred in the left subtree of N’s

right child (right-left rotation), or Addition occurred in the right subtree of N’s

left child (left-right rotation)

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Rotation after Addition

• Following an addition to an AVL tree, temporary imbalance might occur. Let N be an unbalanced node closest to new

leaf Either a single or double rotation will restore

the tree’s balance

• Note: similar action restores balance after removal

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-10 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 treeCopyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation Details

• Note source code of class AVLTree,Listing 27-1 Note private methods Note public methods Note method rotateLeftRight is left as an

exercise

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Note: Code listing filesmust be in same folder

as PowerPoint filesfor links to work

Note: Code listing filesmust be in same folder

as PowerPoint filesfor links to work

2-3 Trees

• A general search tree Interior nodes must have either two or three

children

• A 2-Node has one data item, s Has two children Data s greater than any data in left subtree Data s less than any data in right subtree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

2-3 Trees

• A 3-Node has two data item, s and l Item s is the smaller Item l is the larger

• Has three children Data that is less than s is in left subtree Data greater than l is in right subtree Data between s and l is in middle subtree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-11 (a) A 2-node; (b) a 3-node

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-12 A 2-3 tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-14 The 2-3 tree after adding (a) 80; (b) 90; (c) 70

Copyright ©2012 by Pearson Education, Inc. All rights reserved

figure 27-15 Adding 55 to the 2-3 tree causes a leaf and then the root to split

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-16 The 2-3 tree after adding (a) 10; (b), (c) 40

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-17 The 2-3 tree after adding 35

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-18 Splitting a leaf to accommodate a new entry when the leaf’s parent contains (a) one entry;

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-18 Splitting a leaf to accommodate a new entry when the leaf’s parent contains (b) two entries

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-19 Splitting an internal node to accommodate a new entry

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-20 Splitting the root to accommodate a new entry

Copyright ©2012 by Pearson Education, Inc. All rights reserved

2-4 Trees

• Sometimes called a 2-3-4 tree

• General search tree whose interior nodes must have Either two, three, or four children Leaves occur on the same level

• 4-node contains three data items s, m, and l Has four children

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-21 A 4-node

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-22 An initially empty 2-4 tree after adding (a) 60; (b) 50; (c) 20

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-23 The 2-4 tree after (a) splitting the root; (b) adding 80; (c) adding 90

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-24 The 2-4 tree after (a) splitting a 4-node; (b) adding 70

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-25 The 2-4 tree after adding (a) 55; (b) 10; (c) 40

Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-27 Three balanced search trees obtained by adding 60, 50, 20, 80, 90, 70, 55, 10, 40,

and 35: (a) AVL tree; (b) 2-3 tree; (c) 2-4 treeCopyright ©2012 by Pearson Education, Inc. All rights reserved

Red-Black Trees

• Red-black tree is a binary tree that is equivalent to a 2-4 tree.

• Adding entry to a red-black tree like adding entry to a 2-4 tree Only one pass from root to leaf is necessary.

• But is a binary tree Uses simpler operations to maintain i balance

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-28 Using 2-nodes to represent (a) a 4-node

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-28 Using 2-nodes to represent (b) a 3-node

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-29 A red-black tree that is equivalent to the 2-4 tree in Figure 27-27c

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Properties of a Red-Black Tree

• The root is black.

• Every red node has black parent.

• Any children of red node are black; Red node cannot have red children.

• Every path from the root to a leaf contains same number of black nodes.

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Adding Entries to a Red-Black Tree

• Adding a leaf Use black for a new leaf, we increase number

of black nodes on paths to that leaf But this violates fourth property of a red-black

tree Thus, new node must be red

• Adding or removing entries can change color of various nodes

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-30 The result of adding a new entry e to a one-node red-black tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-31 The possible results of adding a new entry e to a two-node red-black tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-31 The possible results of adding a new entry e to a two-node red-black tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-31 The possible results of adding a new entry e to a two-node red-black tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-31 The possible results of adding a new entry e to a two-node red-black tree

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-32 The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure 27-31

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-32 The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure 27-31

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-32 The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure 27-31

Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 27-32 The possible results of adding a new entry e to a two-node red-black tree: mirror images of Figure 27-31

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-33 Splitting a 4-node whose parent is a 2-node in (a) a 2-4 tree; (b) a red-black treeCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-34 Splitting a 4-node that has a 3-node parent within (a) a 2-4 tree; (b) a red-black treeCopyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-35 Splitting a 4-node that has a red parent within a red-black tree: Case 1

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-36 Splitting a 4-node that has a red parent within a red-black tree: Case 2

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-37 Splitting a 4-node that has a red parent within a red-black tree: Case 3

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 27-38 Splitting a 4-node that has a red parent within a red-black tree: Case 4

Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class TreeMap

• Uses a red-black tree to implement the methods in the interface SortedMap<K, V>

• SortedMap extends the interface Map<K, V>

• Methods such as get, put, remove, and containsKey are each an O(log n) operation.

Copyright ©2012 by Pearson Education, Inc. All rights reserved

B-Trees

• M-way search tree General tree whose nodes have up to m

children each

• Node that has k - 1 data items and k children is called a k-node

Copyright ©2012 by Pearson Education, Inc. All rights reserved

B-Trees

• B-tree of order m is a balanced multiway search tree of order m The root has either no children or between 2

and m children. Other interior nodes (nonleaves) have

between m/2 and m children each. All leaves are on the same level

Copyright ©2012 by Pearson Education, Inc. All rights reserved

End

Chapter 27

Copyright ©2012 by Pearson Education, Inc. All rights reserved