CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees...

63
CS302 - Data Structures using C++ Kostas Alexis Topic: Balanced Search Trees

Transcript of CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees...

Page 1: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

CS302 - Data Structuresusing C++

Kostas Alexis

Topic: Balanced Search Trees

Page 2: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Balanced Search Trees

• Height of binary search tree

• Sensitive to order of additions and removals

• Various search trees can retain balance

• Despite additions and removals

Page 3: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Balanced Search TreesThe tallest and shortest binary search trees containing the same data

Page 4: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL Trees

• An AVL (Adelson-Velskii and Landis) tree

• A balanced binary search tree

• Maintains its height close to the minimum

• Rotations restore the balance

Page 5: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL TreesAn unbalanced binary search tree

Page 6: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL TreesCorrecting an imbalance in an AVL tree due to an addition by using a single rotation to the left

Page 7: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL Trees[Continued]

Page 8: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL TreesA single rotation to the left that does not affect the height of an AVL tree

Page 9: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL Trees[Continued]

Page 10: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL TreesA double rotation that decreases the height of an AVL tree

Page 11: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

AVL Trees[Continued]

Page 12: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3 TreesA 2-3 tree of height 3

Page 13: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3 TreesNodes in a 2-3 tree

Page 14: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3 TreesA 2-3 tree

Page 15: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3 TreesA header file for a class of nodes for a 2-3 tree

Page 16: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3 Trees[Continued]

Page 17: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Traversing a 2-3 TreePerforming the analogue of an inorder traversal on a binary tree:

Page 18: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Searching a 2-3 TreeRetrieval operation for a 2-3 tree

Page 19: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Searching a 2-3 TreeRetrieval operation for a 2-3 tree

Page 20: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Searching a 2-3 Tree

• Search of a 2-3 and shortest binary search tree approximately same efficiency

• A binary search tree with n nodes cannot be shorter than log2(𝑛 + 1)

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

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

• Searching 2-3 tree is 𝑂(log 𝑛)

Page 21: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Searching a 2-3 TreeA balanced binary search tree & a 2-3 tree

Page 22: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Searching a 2-3 TreeThe trees shown before after adding the values 39 down to 32

Page 23: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeAfter inserting 39 into the tree in previous slide

Page 24: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeThe steps for adding 38 to the tree in previous figure

Page 25: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeAfter adding 37 to the tree in Figure

Page 26: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeThe steps for adding 36 to the tree in Figure

Page 27: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeThe steps for adding 36 to the tree in Figure

bubbles up

Page 28: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 Tree[Continued]

Page 29: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 Tree (6 of 11)

The tree after the adding 35, 34, and 33 to the tree in Figure

Page 30: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeSplitting a leaf in a 2-3 tree in general and in a specific example

Page 31: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 Tree[Continued]

Page 32: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeSplitting an internal node in a 2-3 tree in general and in a specific example

Page 33: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 Tree[Continued]

Page 34: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to a 2-3 TreeSplitting the root of a 2-3 tree general and in a specific example

Page 35: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 TreeThe steps for removing 70 from the 2-3 tree in Figure - slide 21

Page 36: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 37: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 38: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 39: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 TreeThe steps for removing 100 from the tree in Figure

Page 40: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 41: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 TreeThe steps for removing 80 from the 2-3 tree in Figure

Page 42: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 43: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 44: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 TreePossible situations during the removal of a data item from a 2-3 tree

Page 45: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 46: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 47: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3 Tree[Continued]

Page 48: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3-4 TreesA 2-3-4 tree with the same data items as the 2-3 tree in Figure - slide 22

Page 49: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3-4 TreesA 4-node in a 2-3-4 tree

Page 50: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

2-3-4 Trees

• Searching and traversing

• Simple extensions of corresponding algorithms for a 2-3 tree

• Adding data

• Like addition algorithm for 2-3 tree

• Splits node by moving one data item up to parent node

Page 51: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesAdding 20 to a one-node 2-3-4 tree

Page 52: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesAfter adding 50 and 40 to the tree in Figure

Page 53: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesThe steps for adding 70 to the tree in Figure

Page 54: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesAfter adding 80 and 15 to the tree in Figure

Page 55: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesThe steps for adding 90 to the tree in Figure

Page 56: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesThe steps for adding 100 to the tree in Figure

Page 57: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesSplitting a 4-node root when adding data to a 2-3-4 tree

Page 58: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesSplitting a 4-node whose parent is a 2-node when adding data to a 2-3-4 tree

Page 59: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 TreesSplitting a 4-node whose parent is a 3-node when adding data to a 2-3-4 tree

Page 60: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 Trees[Continued]

Page 61: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Adding Data to 2-3-4 Trees[Continued]

Page 62: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Removing Data from a 2-3-4 Tree

• Has same beginning as removal algorithm for a 2-3 tree

• Transform each 2-node into a 3-node or a 4-node

• Insertion and removal algorithms for 2-3-4 tree require fewer steps than for 2-3 tree

Page 63: CS302 – Data Structures€¦ · CS302 - Data Structures using C++ ... Balanced Search Trees •Height of binary search tree •Sensitive to order of additions and removals •Various

Thank you