balanced search trees cs 302 - data structures mehmet h gunes modified from authors’ slides

99
Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides

Upload: erica-walker

Post on 19-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Balanced Search Trees

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

Page 2: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Contents

• Balanced Search Trees• 2-3 Trees• 2-3-4 Trees• Red-Black Trees• AVL Trees

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 3: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Balanced Search Trees

• Height of a binary search tree sensitive to order of insertions and removals– Minimum = log2 (n + 1)

– Maximum = n

• Various search trees can retain balance despite insertions and removals

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 4: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Balanced Search Trees

• (a) A binary search tree of maximum height; (b) a binary search tree of minimum height

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 5: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3 Trees

• A 2-3 tree of height 3

• A 2-3 tree is not a binary tree• A 2-3 tree never taller than a minimum-

height binary tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 6: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3 Trees

• Placing data items in nodes of a 2-3 tree– A 2-node (has two children) must contain single

data item greater than left child’s item(s) and less than right child’s item(s)

– A 3-node (has three children) must contain two data items, S and L , such that

• S is greater than left child’s item(s) and less than middle child’s item(s);

• L is greater than middle child’s item(s) and less than right child’s item(s).

– Leaf may contain either one or two data items.Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 7: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3 Trees

• Nodes in a 2-3 tree: (a) a 2-node; (b) a 3-node

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013`

Page 8: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3 Trees

• A 2-3 tree

View Header file for a class of nodes for a 2-3 tree, Listing 19-1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 9: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Traversing a 2-3 Tree

• Traverse 2-3 tree in sorted order by performing analogue of inorder traversal on binary tree:

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 10: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

• Retrieval operation for 2-3 tree similar to retrieval operation for binary search tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 11: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 12: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

• Possible to search 2-3 tree and shortest binary search tree with approximately same efficiency, because:– Binary search tree with n nodes cannot be shorter

than log2 (n + 1)

– 2-3 tree with n nodes cannot be taller than log2 (n + 1)

– Node in a 2-3 tree has at most two items

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 13: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

• A balanced binary search tree;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 14: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

• a 2-3 tree with the same entries

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 15: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Searching a 2-3 Tree

• (a) The binary search tree of Figure 19-5a after inserting the sequence of values 32 through 39

• (b) the 2-3 tree of Figure 19-5 b after the same insertions

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 16: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• After inserting 39 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 17: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• The steps for inserting 38 into the tree: (a) The located node has no room;

(b) the node splits; (c) the resulting tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 18: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• After inserting 37 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 19: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• (a), (b), (c) The steps for inserting 36 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 20: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• (d) the resulting tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 21: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• The tree after the insertion of 35, 34, and 33 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 22: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• Splitting a leaf in a 2-3 tree when the leaf is a (a) left child; (b) right child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 23: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• Splitting an internal node in a 2-3 tree when the node is a (a) left child; (b) right child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 24: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• Splitting the root of a 2-3 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 25: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• Summary of insertion strategy

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 26: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Inserting Data into a 2-3 Tree

• Summary of insertion strategy

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 27: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• A 2-3 tree; (b), (c), (d), (e) the steps for removing 70;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 28: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• (f) the resulting tree;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 29: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• (a), (b), (c) The steps for removing 100 from the tree; (d) the resulting tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 30: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• The steps for removing 80 from the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 31: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• The steps for removing 80 from the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 32: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• Results of removing 70, 100, and 80 from (a) the 2-3 tree and (b) the binary search tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 33: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• Algorithm for removing data from a 2-3 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 34: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• Algorithm for removing data from a 2-3 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 35: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• Algorithm for removing data from a 2-3 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 36: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• (a) Redistributing values; (b) merging a leaf;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 37: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• (c) redistributing values and children; • (d) merging internal nodes

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 38: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Removing Data from a 2-3 Tree

• (e) deleting the root

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 39: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• A 2-3-4 tree with the same data items as the 2-3 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 40: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Rules for placing data items in the nodes of a 2-3-4 tree– 2-node (two children), must contain a single data

item that satisfies relationships – 3-node (three children), must contain two data

items that satisfies relationships – . . .

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 41: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees– 4-node (four children) must contain three data

items S , M , and L that satisfy:• S is greater than left child’s item(s) and less than

middle-left child’s item(s)• M is greater than middle-left child’s item(s) and less

than middle-right child’s item(s);• L is greater than middle-right child’s item(s) and less

than right child’s item(s).

– A leaf may contain either one, two, or three data items

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 42: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• A 4-node in a 2-3-4 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 43: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Has more efficient insertion and removal operations than a 2-3 tree

• Has greater storage requirements due to the additional data members in its 4-nodes

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 44: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Searching and Traversing a 2-3-4 Tree– Simple extensions of the corresponding algorithms

for a 2-3 tree

• Inserting Data into a 2-3-4 Tree– Insertion algorithm splits a node by moving one of

its items up to its parent node– Splits 4-nodes as soon as it encounters them on

the way down the tree from the root to a leaf

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 45: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Inserting 20 into a one-node 2-3-4 tree (a) the original tree; (b) after splitting the node; (c) after inserting 20

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 46: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• After inserting 50 and 40 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 47: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• The steps for inserting 70 into the tree : (a) after splitting the 4-node; (b) after inserting 70

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 48: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• After inserting 80 and 15 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 49: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• The steps for inserting 90 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 50: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• The steps for inserting 100 into the tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 51: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Splitting a 4-node root during insertion into a 2-3-4 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 52: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Splitting a 4-node whose parent is a 2-node during insertion into a 2-3-4 tree, when the 4-node is a (a) left child; (b) right child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 53: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Splitting a 4-node whose parent is a 3-node during insertion into a 2-3-4 tree, when the 4-node is a (a) left child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 54: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Splitting a 4-node whose parent is a 3-node during insertion into a 2-3-4 tree, when the 4-node is a (b) middle child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 55: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Splitting a 4-node whose parent is a 3-node during insertion into a 2-3-4 tree, when the 4-node is a (c) right child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 56: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

2-3-4 Trees

• Removing Data from a 2-3-4 Tree– Removal algorithm has same beginning as removal

algorithm for a 2-3 tree– Locate the node n that contains the item I you

want to remove– Find I ’s inorder successor and swap it with I so

that the removal will always be at a leaf– If leaf is either a 3-node or a 4-node, remove I .

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 57: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Modified from Dr George Bebis and Dr Monica Nicolescu

Page 58: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Use a special binary search tree—a red-black tree —to represent a 2-3-4 tree

• Retains advantages of a 2-3-4 tree without storage overhead

• The idea is to represent each 3-node and 4-node as an equivalent binary search tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 59: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Red-black representation of (a) a 4-node; (b) a 3-node

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 60: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• A red-black tree that represents the 2-3-4 tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 61: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Searching and traversing– Red-black tree is a binary search tree, search and

traverse it by using algorithms for binary search tree

• Inserting, removing with a red-black tree– Adjust the 2-3-4 insertion algorithms to

accommodate the red-black representation

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 62: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node that is the root

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 63: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node whose parent is a 2-node, when the 4-node is a (a) left child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 64: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node whose parent is a 2-node, when the 4-node is a (b) right child

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 65: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node whose parent is a 3-node

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 66: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node whose parent is a 3-node

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 67: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black Trees

• Splitting a red-black representation of a 4-node whose parent is a 3-node

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 68: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Red-Black-Trees Properties(**Binary search tree property is satisfied**)

1. Every node is either red or black

2. The root is black

3. Every leaf (NIL) is black

4. If a node is red, then both its children are black

• No two consecutive red nodes on a simple path from the root to a leaf

5. For each node, all paths from that node to a leaf contain the same number of black nodes

68

Page 69: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Example: RED-BLACK-TREE

• For convenience, we add NIL nodes and refer to them as the leaves of the tree.– Color[NIL] = BLACK

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

69

Page 70: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Definitions

• Height of a node: the number of edges in the longest path to a leaf

• Black-height bh(x) of a node x: the number of black nodes (including NIL) on the path from x to a leaf, not counting x

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

h = 4bh = 2

h = 3bh = 2

h = 2bh = 1

h = 1bh = 1

h = 1bh = 1

h = 2bh = 1 h = 1

bh = 1

70

Page 71: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Height of Red-Black-Trees

A red-black tree with n internal nodes has height at most 2log(N+1)

71

Page 72: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Insert Item

What color to make the new node?• Red?

– Let’s insert 35!

• Property 4 is violated: if a node is red, then both children are black• Black?

– Let’s insert 14!

• Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes

26

17 41

30 47

38 50

Page 73: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Delete Item

What color was the node that was removed? Red? 1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

OK!

OK!

OK!

26

17 41

30 47

38 50

73

OK!

OK!

Page 74: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Delete Item

What color was the node that was removed? Black? 1. Every node is either red or black2. The root is black3. Every leaf (NIL) is black4. If a node is red, then both its children are black

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

OK!

OK!

Not OK! Could createtwo red nodes in a row

Not OK! Could change theblack heights of some nodes

Not OK! If removing the root and the child that replaces it is red

74

26

17 41

30 47

38 50

Page 75: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Rotations• Operations for re-structuring the tree after

insert and delete operations– Together with some node re-coloring, they help

restore the red-black-tree property

– Change some of the pointer structure

– Preserve the binary-search tree property

• Two types of rotations:– Left & right rotations

75

Page 76: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Left Rotations• Assumptions for a left rotation on a node x:

– The right child y of x is not NIL

• Idea:– Pivots around the link from x to y– Makes y the new root of the subtree– x becomes y ’s left child– y ’s left child becomes x ’s right child

76

Page 77: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Example: LEFT-ROTATE

77

Page 78: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

LEFT-ROTATE(T, x)1. y ← right[x] ►Set y

2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree

3. if left[y] NIL4. then p[left[y]] ← x ► Set the parent relation from left[y] to x

5. p[y] ← p[x] ► The parent of x becomes the parent of y

6. if p[x] = NIL7. then root[T] ← y8. else if x = left[p[x]]9. then left[p[x]] ← y10. else right[p[x]] ← y11.left[y] ← x ► Put x on y’s left

12.p[x] ← y ► y becomes x’s parent

Page 79: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

• Assumptions for a right rotation on a node x:– The left child x of y is not NIL

• Idea:– Pivots around the link from y to x– Makes x the new root of the subtree– y becomes x ’s right child– x ’s right child becomes y ’s left child

Right Rotations

79

Page 80: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Insert Item• Goal:

– Insert a new node z into a red-black tree

• Idea:– Insert node z into the tree as for an ordinary

binary search tree– Color the node red– Restore the red-black tree properties

80

Page 81: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT(T, z)1. y ← NIL

2. x ← root[T]

3. while x NIL

4. do y ← x

5. if key[z] < key[x]

6. then x ← left[x]

7. else x ← right[x]

8. p[z] ← y

• Initialize nodes x and y• Throughout the algorithm y points to the parent of x

• Go down the tree untilreaching a leaf• At that point y is theparent of the node to beinserted

• Sets the parent of z to be y

26

17 41

30 47

38 50

Page 82: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT(T, z)

9. if y = NIL

10. then root[T] ← z

11. else if key[z] < key[y]

12. then left[y] ← z

13. else right[y] ← z

14. left[z] ← NIL

15. right[z] ← NIL

16. color[z] ← RED

17. RB-INSERT-FIXUP(T, z)

The tree was empty: set the new node to be the root

Otherwise, set z to be the left orright child of y, depending on whether the inserted node is smaller or larger than y’s key

Set the fields of the newly added node

Fix any inconsistencies that could have been introduced by adding this new red node

Page 83: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB Properties Affected by Insert

1. Every node is either red or black

2. The root is black

3. Every leaf (NIL) is black

4. If a node is red, then both its children are black

5. For each node, all paths

from the node to descendant

leaves contain the same number

of black nodes

OK!

If z is the root not OK

OK!

26

17 41

4738

50

If p(z) is red not OKz and p(z) are both redOK!

Page 84: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT-FIXUP

84

Case 1: z ’s “uncle” (y) is red (z could be either left or right child)

Idea: • p[p[z]] (z ’s grandparent) must be black

• color p[z] black

• color y black

• color p[p[z]] red

• z = p[p[z]]

– Push the “red” violation up the tree

Page 85: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT-FIXUP

Case 2: • z ’s “uncle” (y) is black• z is a left child

85

Case 2

Idea:• color p[z] black • color p[p[z]] red• RIGHT-ROTATE(T, p[p[z]])• No longer have 2 reds in a row• p[z] is now black

Page 86: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT-FIXUP

Case 3: • z ’s “uncle” (y) is black• z is a right childIdea:• z p[z]• LEFT-ROTATE(T, z) now z is a left child, and both z and p[z] are red case 2

86

Case 3 Case 2

Page 87: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Example

11Insert 4

2 14

1 157

85

4

y

11

2 14

1 157

85

4

z

Case 1

y

z and p[z] are both redz’s uncle y is redz

z and p[z] are both redz’s uncle y is blackz is a right child

Case 3

11

2

14

1

15

7

8

5

4

z

y Case 2

z and p[z] are redz’s uncle y is blackz is a left child

112

141

15

7

85

4

z

87

Page 88: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

RB-INSERT-FIXUP(T, z)1. while color[p[z]] = RED

2. if p[z] = left[p[p[z]]]

3. then y ← right[p[p[z]]]

4. if color[y] = RED

5. then Case1

6. else if z = right[p[z]]

7. then Case3

8. Case2

9. else (same as then clause with “right” and “left” exchanged for lines 3-4)

10. color[root[T]] ← BLACK

The while loop repeats only whencase1 is executed: O(logN) times

Set the value of x’s “uncle”

We just inserted the root, orThe red violation reached the root

Page 89: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Analysis of InsertItem

• Inserting the new element into the tree O(logN)

• RB-INSERT-FIXUP– The while loop repeats only if CASE 1 is executed– The number of times the while loop can be

executed is O(logN)

• Total running time of Insert Item: O(logN)

89

Page 90: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Delete Item• Delete as usually, then re-color/rotate

• A bit more complicated though …

• Demo– http://gauss.ececs.uc.edu/RedBlack/redblack.html

90

Page 91: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Problems

91

Page 92: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

• What red-black tree property is violated in the tree below? How would you restore the red-black tree property in this case?– Property violated: if a node is red, both its children are black– Fixup: color 7 black, 11 red, then right-rotate around 11

Problems

92

112

141

15

7

85

4

z

Page 93: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Problems

93

• Let a, b, c be arbitrary nodes in subtrees , , in the tree below.

• How do the depths of a, b, c change when a left rotation is performed on node x?– a: increases by 1– b: stays the same– c: decreases by 1

Page 94: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

Problems

• When we insert a node into a red-black tree, we initially set the color of the new node to red.

Why didn’t we choose to set the color to black?

• Would inserting a new node to a red-black tree and then immediately deleting it, change the tree?

94

Page 95: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

AVL Trees

• Named for inventors– Adel’son-Vel’skii and Landis

• A balanced binary search tree– Maintains height close to the minimum– After insertion or deletion, check the tree is still

AVL tree • determine whether any node in tree has left and right

subtrees whose heights differ by more than 1

• Can search AVL tree almost as efficiently as minimum-height binary search tree.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 96: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

AVL Trees

• (a) An unbalanced binary search tree; (b) a balanced tree after rotation; (c) a balanced tree after insertion

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 97: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

• (a) Before;• (b) and after a single left rotation that decreases the

tree’s height; • (c) the rotation in general

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees

Page 98: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

• (a) Before; • (b) and after a single left rotation that does not affect the tree’s height; • (c) the rotation in general

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

AVL Trees

Page 99: Balanced Search Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides

AVL Trees

• (d) the double rotation in general

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013