206
Data Structures Search Trees Dimitrios Michail Dept. of Informatics and Telematics Harokopio University of Athens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

Data StructuresSearch Trees

Dimitrios Michail

Dept. of Informatics and TelematicsHarokopio University of Athens

. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .

Page 2: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

The problem

SearchWe would like to maintain items with keys and except from add/removeto also be able to quickly search an item based on a key.

Page 3: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example

Assume items are bank transactions we would like to search based on thedate.

Page 4: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Search

We can implement a data structure for searching in multiple ways.

Our goal is to implement the search operations efficiently while keepingthe remaining operations also efficient.

e.g. searching on a linked list is not efficient!

Page 5: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Binary Search Tree (BST)

DefinitionA binary search tree is a binary tree where each node is associated witha key with the additional property that the key of a node is larger (orequal) from the keys of all nodes in its left subtree, and smaller that allthe keys of the nodes in its right subtree.

Page 6: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Binary Search Tree (BST)

k

≤ k > k

k−∞ +∞

Page 7: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

ExampleBinary Search Tree (BST)

15

13 18

6 14 19

95

16

Page 8: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BST

Algorithm▶ start from the root▶ if we are at a node which has the same key as the one we are

looking for, then return the node▶ otherwise go left or right depending on the comparison between the

key we are looking for and the key of the node

Page 9: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 9

15

13 18

6 14 19

95

16

Page 10: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 9

15

13 18

6 14 19

95

16

Page 11: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 9

15

13 18

6 14 19

95

16

9 < 15

Page 12: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 9

15

13 18

6 14 19

95

16

9 < 15

9 < 13

Page 13: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 9

15

13 18

6 14 19

95

16

9 < 15

9 < 13

9 > 6

Page 14: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 17

15

13 18

6 14 19

95

16

Page 15: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 17

15

13 18

6 14 19

95

16

Page 16: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 17

15

13 18

6 14 19

95

16

17 > 15

Page 17: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 17

15

13 18

6 14 19

95

16

17 > 15

17 < 18

Page 18: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Searching on a BSTSearching for 17

15

13 18

6 14 19

95

16

17 > 15

17 < 18

17 > 16

NULL

Page 19: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Inorder Traversal

RuleFirst visit the left subtree recursively, then the root then the right subtreerecursively.

Returns nodes in non-decreasing key order.

The traversal needs linear time O(n) where n are the number of items inthe tree.

Page 20: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BST

Algorithm▶ first perform a search▶ if the key is found, do nothing▶ otherwise add a new node with the new key at the point where the

search ended

Page 21: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

Page 22: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

Page 23: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

Page 24: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

Page 25: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

Page 26: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

17

Page 27: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a BSTInsertion of 17

15

13 18

6 14 19

95

16

17

Page 28: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BST

Algorithm▶ first perform a search▶ if the key is not found, do nothing▶ otherwise there are 3 cases based on the number of children of the

node we want to delete

Page 29: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 1: Deletion of 17 (no child)

15

13 18

6 14 19

95

16

17

Page 30: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 1: Deletion of 17 (no child)

15

13 18

6 14 19

95

16

17

Page 31: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 1: Deletion of 17 (no child)

15

13 18

6 14 19

95

16

17

Page 32: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 1: Deletion of 17 (no child)

15

13 18

6 14 19

95

16

Page 33: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 1: Deletion of 17 (no child)

15

13 18

6 14 19

95

16

Page 34: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95

16

17

Page 35: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95

16

17

Page 36: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95

16

17

Page 37: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95 17

Page 38: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95 17

Page 39: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95

17

Page 40: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 2: Deletion of 16 (one child)

15

13 18

6 14 19

95

17

Page 41: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

15

13 18

6 14 19

95

16

17

Page 42: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Calculate the successorUseful routine for deletion

The problemGiven a subtree we would like to find the node with the successor keyfrom the root of the subtree

Page 43: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Finding the successor of the root key

The successor of the root has at most one child. (why?)

Page 44: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Finding the successor of the root key

The successor of the root has at most one child. (why?)

Page 45: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Finding the successor of the root key

The successor of the root has at most one child. (why?)

Page 46: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Finding the successor of the root key

The successor of the root has at most one child. (why?)

Page 47: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

15

13 18

6 14 19

95

16

17

Page 48: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

15

13 18

6 14 19

95

16

17

Page 49: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

15

13 18

6 14 19

95

16

17

Page 50: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

15

13 18

6 14 19

95

16

17

Page 51: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

16

13 18

6 14 19

95

16

17

Page 52: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

16

13 18

6 14 19

95 17

Page 53: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a BSTCase 3: Deletion of 15 (two children)

16

13 18

6 14 19

95

17

Page 54: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Is it fast?

All operations require time proportional to the height of the tree.

Page 55: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Problems

4

2 6

1 3 75

7

6

5

4

3

2

1

There are many trees for the same set of items

▶ worst case on the left: height h = O(n)▶ best case on the right: height h = O(log n)

The tree depends on the order that the insertions and deletions areperformed, something that we cannot know beforehand

Page 56: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Balanced BSTs

Search trees which are balanced at any point in time independently of theorder that the insertions and/or deletions of items are performed.

h = O(log n)

Page 57: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

2-3-4 Search Treestop-down

We allow nodes which have 2, 3 or 4 children.

13 13 29 13 29 58

(−∞, 13] (13,+∞] (−∞, 13] (−∞, 13](13, 29] (13, 29](29,+∞] (29, 58] (58,+∞]

Page 58: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

2-3-4 Search Treestop-down

DefinitionA balanced 2-3-4 search tree is a tree which:▶ is either empty▶ or is comprised of tree different node types: 2-nodes, 3-nodes ���

4-nodes▶ all the external nodes (nulls - null links) are at the exact same

distance from the root

Page 59: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example of a Balanced 2-3-4 Search Tree

1

9 11

3 4 5

2

7 8 12 1310

6

Page 60: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Height of a Balanced 2-3-4 Search Tree

TheoremA 2-3-4 search tree with n keys has height O(log n).

Proof.Let h be the height of the tree.

Every level i has at least 2i nodes.Thus n ≥ 1 + 2 + 4 + . . . 2h−1 = 2h − 1.Taking logarithms we get that log2(n + 1) ≥ h.

Page 61: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Height of a Balanced 2-3-4 Search Tree

TheoremA 2-3-4 search tree with n keys has height O(log n).

Proof.Let h be the height of the tree.Every level i has at least 2i nodes.

Thus n ≥ 1 + 2 + 4 + . . . 2h−1 = 2h − 1.Taking logarithms we get that log2(n + 1) ≥ h.

Page 62: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Height of a Balanced 2-3-4 Search Tree

TheoremA 2-3-4 search tree with n keys has height O(log n).

Proof.Let h be the height of the tree.Every level i has at least 2i nodes.Thus n ≥ 1 + 2 + 4 + . . . 2h−1 = 2h − 1.

Taking logarithms we get that log2(n + 1) ≥ h.

Page 63: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Height of a Balanced 2-3-4 Search Tree

TheoremA 2-3-4 search tree with n keys has height O(log n).

Proof.Let h be the height of the tree.Every level i has at least 2i nodes.Thus n ≥ 1 + 2 + 4 + . . . 2h−1 = 2h − 1.Taking logarithms we get that log2(n + 1) ≥ h.

Page 64: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Search on a Balanced 2-3-4 Search Tree

1

9 11

3 4 5

2

7 8 12 1310

6

Search on a Balanced 2-3-4 Search Tree: Generalization of the BSTalgorithm

Page 65: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeWhile maintaining the balance!

We search for the new key in order to find where we are supposed to addit.▶ if the search ends on a 2-node we simply make it a 3-node▶ if the search ends on a 3-node we make it a 4-node▶ if the search ends on a 4-node

▶ break the 4-node into two 2-nodes by sending the middle keyupwards, and then

▶ add the new key in one of the two 2-nodes (we might need to do thesame procedure at the parent node, possibly multiple times all theway up to the root)

Page 66: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 2-node (insertion of 14)

1

13 17

3 5 6

2

9 11 19 2115

8

Page 67: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 2-node (insertion of 14)

1

13 17

3 5 6

2

9 11 19 2115

8

Page 68: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 2-node (insertion of 14)

1

13 17

3 5 6

2

9 11 19 21

8

14 15

Page 69: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 2-node (insertion of 14)

1

13 17

3 5 6

2

9 11 19 21

8

14 15

Page 70: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 3-node (insertion of 10)

1

13 17

3 5 6

2

9 11 19 2115

8

Page 71: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 3-node (insertion of 10)

1

13 17

3 5 6

2

9 11 19 2115

8

Page 72: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 3-node (insertion of 10)

1

13 17

3 5 6

2

19 2115

8

9 10 11

Page 73: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 3-node (insertion of 10)

1

13 17

3 5 6

2

19 2115

8

9 10 11

Page 74: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

3 5 6

2

9 11 19 2115

8

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 75: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

3 5 6

2

9 11 19 2115

8

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 76: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

5

2

9 11 19 2115

8

3 6

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 77: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

9 11 19 2115

8

3 6

2 5

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 78: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

9 11 19 2115

8

3 6

2 5

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 79: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

9 11 19 2115

8

6

2 5

3 4

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 80: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

9 11 19 2115

8

6

2 5

3 4

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 81: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeEnds at a 4-node (insertion of 4)

1

13 17

9 11 19 2115

8

6

2 5

3 4

There is also the case where the father is also a 4-node.In this case we need to perform the split again, possible multiple times allthe way to the root

Page 82: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search Tree

In order to simplify insertion and to avoid such cascades, we use thefollowing technique:

▶ we make sure that the search path does not contain 4-nodes, byspliting any 4-node during our descend on the tree

Page 83: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

20 30 40

10

60 70 110 12090

50

Page 84: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

20 30 40

10

60 70 110 12090

50

Page 85: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

20 30 40

10

60 70 110 12090

50

Page 86: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

30

10

60 70 110 12090

50

20 40

Page 87: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

60 70 110 12090

50

20 40

10 30

Page 88: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

60 70 110 12090

50

20 40

10 30

Page 89: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 25

1

80 100

60 70 110 12090

50

40

10 30

20 25

Page 90: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 29

1

80 100

60 70 110 12090

50

40

10 30

20 25

Page 91: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 29

1

80 100

60 70 110 12090

50

40

10 30

20 25

Page 92: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 29

1

80 100

60 70 110 12090

50

40

10 30

20 25 29

Page 93: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 29

1

80 100

60 70 110 12090

50

40

10 30

20 25 29

Page 94: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

40

10 30

20 25 29

Page 95: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

40

10 30

25

20 29

Page 96: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

4020 29

10 25 30

Page 97: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

4020 29

10 25 30

Page 98: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

4020

10 25 30

28 29

Page 99: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 28

1

80 100

60 70 110 12090

50

4020

10 25 30

28 29

Page 100: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 12090

50

4020

10 25 30

28 29

Page 101: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 12090

50

4020

10 25 30

28 29

Page 102: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 12090

50

4020

25

28 29

10 30

Page 103: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 120904020 28 29

10 30

25 50

Page 104: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 120904020 28 29

10 30

25 50

Page 105: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 120904020 28 29

10 30

25 50

Page 106: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 27

1

80 100

60 70 110 120904020

10 30

25 50

27 28 29

Page 107: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10 30

25 50

27 28 29

Page 108: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10 30

25 50

27 28 29

Page 109: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10 30

25 50

27

28

29

Page 110: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10

25 50

27 29

28 30

Page 111: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10

25 50

29

28 30

26 27

Page 112: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on a Balanced 2-3-4 Search TreeInsertion of 26

1

80 100

60 70 110 120904020

10

25 50

29

28 30

26 27

Page 113: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a non-leaf

▶ We reduce the problem to the deletion of a leaf▶ Same technique as in the BSTs

e.g. to remove 25, we put in its place the inorder successor (26) or theinorder predecessor (20).

1

80 100

60 70 110 120904020

10

25 50

29

28 30

26 27

Page 114: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a non-leaf

▶ We reduce the problem to the deletion of a leaf▶ Same technique as in the BSTs

e.g. to remove 25, we put in its place the inorder successor (26) or theinorder predecessor (20).

1

80 100

60 70 110 120904020

10

26 50

29

28 30

26 27

Page 115: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has ≥ 2 keys (e.g. delete 26 elow), which means that it is a3-node or a 4-node.

▶ Easy case, just delete by switching node types.

1

80 100

60 70 110 120904020

10

25 50

29

28 30

26 27

Page 116: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has ≥ 2 keys (e.g. delete 26 elow), which means that it is a3-node or a 4-node.

▶ Easy case, just delete by switching node types.

1

80 100

60 70 110 120904020

10

25 50

29

28 30

27

Page 117: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ some sibling node has ≥ 2 keys▶ balance: e.g. delete 90

1

80 100

60 70 110 120904020

10

25 50

29

28 30

27

Page 118: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ some sibling node has ≥ 2 keys▶ balance: e.g. delete 90

1

80

100

60 70

110

1204020

10

25 50

29

28 30

27

Page 119: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ some sibling node has ≥ 2 keys▶ balance: e.g. delete 90

1

80

60 70

110

4020

10

25 50

29

28 30

27 100 120

Page 120: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ all sibling nodes have 1 key (they are 2-nodes)▶ fusion: e.g. delete of 1

1

80

60 70

110

4020

10

25 50

29

28 30

27 100 120

Page 121: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ all sibling nodes have 1 key (they are 2-nodes)▶ fusion: e.g. delete of 1

80

60 70

110

40

20

10

25 50

29

28 30

27 100 120

Page 122: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ The leaf has 1 key (underflow), which means it is a 2-node.▶ all sibling nodes have 1 key (they are 2-nodes)▶ fusion: e.g. delete of 1

80

60 70

110

40

25 50

29

28 30

27 100 12010 20

Page 123: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ Now the parent has a problem (underflow)▶ repeat the same sequence, either balance or fusion based on the

sibling nodes

80

60 70

110

40

25 50

29

28 30

27 100 12010 20

Page 124: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ Now the parent has a problem (underflow)▶ repeat the same sequence, either balance or fusion based on the

sibling nodes

80

60 70

110

40

25 50

29

2830

27 100 12010 20

Page 125: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeDeletion from a leaf

▶ Now the parent has a problem (underflow)▶ repeat the same sequence, either balance or fusion based on the

sibling nodes

80

60 70

110

40

25

50

29

28

30

27 100 12010 20

Page 126: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search Tree

▶ Fusion can cascade up until the root.▶ In case it reaches the root, the tree’s height is reduced by one.

Deletion costs O(log n) time.

Page 127: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

10

1 15

50

7530

Delete key 1

Page 128: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

10

15

50

7530

Delete key 1

Page 129: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

10

15

50

7530

Node with underflow - test first balance, then fusion

Page 130: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

10

15

50

7530

Balance not possible - siblings do not have spare keys

Page 131: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

50

753010 15

Fusion - use both keys from brother and parent

Page 132: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

50

753010 15

Node with underflow - one level up

Page 133: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

50

753010 15

Node with underflow - test first balance, then fusion

Page 134: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

25

50

753010 15

Balance not possible - siblings do not have spare keys

Page 135: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

753010 15

25 50

Fusion - use both keys from brother and parent

Page 136: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

753010 15

25 50

Node with underflow - one level up

Page 137: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

753010 15

25 50

If the root has underflow - simply delete it

Page 138: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a Balanced 2-3-4 Search TreeExample of cascade up to the root

753010 15

25 50

If the root has underflow - simply delete it

Page 139: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Balanced 2-3-4 Search Trees

Positive:▶ simple insertion algorithm▶ complexity O(log n) for insertion, deletion and search

negative:▶ 3 different node types▶ complex procedured due to multiple links, copying of links, etc.

Page 140: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black Trees

A Red-Black tree is a binary search tree (BST) with the followingadditional properties:

▶ Every node is eitherred or black

▶ The root is black▶ Every red node does

not have any redchild

▶ Every path from anexternal node (null)to the root has topass through thesame number ofblack nodes

100

9090 150

12550 200

110

Page 141: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black Trees

TheoremA red-black tree with n nodes has height O(log n).

▶ Instead of a proof we will see a correspondence between read-blacktrees and 2-3-4 trees.

▶ We can view red-black trees as a representation of 2-3-4 trees.▶ This correspondence allows us to easily show the properties of

red-black trees.

Page 142: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black Trees

60 70

2510 25 30

10 30

70

60

60

70

60 60

Page 143: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black Trees

60 70

2510 25 30 10 30

7060 60 70

60 60

Page 144: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesSearch, insert and delete

SearchRed-Black keys are binary search trees (BSTs)!

Insertion and DeletionIn correspondence with the 2-3-4 trees, describe the moves by changingcolors and rotations.

Page 145: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotations (on any BST)

≤ a> a≤ b

> b

a

b a

b

≤ a

> b> a≤ b

Page 146: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesExample

1

80 100

60 70 110 12090

50

4020

10 25 30

28 29

Page 147: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesExample

50

25 3010

1 20 402928

80 100

7060 90 110 120

Page 148: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (first)

2510 25 30 10 30

5 5

Page 149: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (first)

2510

25

30 10 30

5 5

Page 150: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (first)

2510

25

30 10 30

5 5 25

Page 151: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (second)

2 5

2510 25 30 10 30

2 5

Page 152: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (second)

2 5

2510

25

30 10 30

2 5

Page 153: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (second)

2 5

2510

25

30 10 30

2 5

Page 154: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotation (second)

2 5 25

10

25

30 10 30

2 5

Page 155: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesRotations

There are more cases...

Page 156: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesInsertion and Deletion Complexity

▶ There are a lot of complicated rules! All of them make sense if weview the corresponding 2-3-4 tree.

▶ Simulating the changes of a node of a 2-3-4 tree with thecorresponding nodes of a red-black tree takes O(1) time.

▶ Since 2-3-4 trees support insertion in O(log n) time, so do red-blacktrees.

▶ The same idea also works for deletions.

Page 157: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Red-Black TreesHeight

TheoremA red-black tree with n nodes has height O(log n).

Proof.We can collect all red nodes to their parents in order to convert the treeto a 2-3-4 tree.The height of the tree can at most lose half of its size.The resulting 2-3-4 tree has height O(log n), thus the original red-blacktree has height at most 2 · O(log n) = O(log n).

Page 158: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Other Balanced Trees

▶ AVL trees: O(log n) height using rotations▶ splay trees▶ scapegoat trees▶ etc.

Page 159: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL Trees

The first balanced tree!

G.M. Adelson-Velskii and E.M. Landis. An algorithm for the organizationof information, Proceedings of the USSR Academy of Sciences 146:263–266, 1962.

Named from the initials of its inventors.

Page 160: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL Trees

▶ Binary Search Tree▶ Additional balance property

The additional balance property needs to be maintained easily and to beable to preserve the height of the tree to O(log n).

Page 161: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL Trees

DefinitionAn AVL tree is a binary search tree T where for each node v ∈ T theheight of its two subtrees differ by at most 1. The height of an emptytree is defined as -1.

Page 162: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL Trees

2

4

8

12

16

14

6

10

2

4

8

12

16

14

6

10

7

Left AVL tree, right non-AVL tree.

Page 163: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL TreesHeight

TheoremAn AVL tree with n keys has height O(log n).

Proof.Let n(h) be the smallest number of nodes of an AVL tree with height h.

Page 164: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL TreesHeight

TheoremAn AVL tree with n keys has height O(log n).

Proof.Let n(h) be the smallest number of nodes of an AVL tree with height h.

Page 165: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL TreesHeight

TheoremAn AVL tree with n keys has height O(log n).

Proof.Let n(h) be the smallest number of nodes of an AVL tree with height h.We can easily show that n(1) = 1 and n(2) = 2.

For h > 2 an AVL tree has a rootand two subtrees, one AVLsubtree with height h− 1 and oneAVL subtree with height h − 2. h-1

h-2

h

Thus n(h) = 1 + n(h − 1) + n(h − 2).

Page 166: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

AVL TreesHeight

TheoremAn AVL tree with n keys has height O(log n).

Proof.Let n(h) be the smallest number of nodes of an AVL tree with height h.Since n(h − 1) > n(h − 2) we get that

n(h) = 1+n(h−1)+n(h−2) > 2n(h−2) > 4n(h−4) > 8n(h−6) > . . .

using inductionn(h) > 2in(h − 2i)

Since n(1) = 1 we have that n(h) > 2(h−1)/2 and by taking logarithms wehave h < 2 log n(h) + 1.

Page 167: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on an AVL tree

Insertion is performed like in any other BST but afterwards we mightneed to fix the tree.

We make sure we perform O(d) operations where d is the depth whereinsertion is performed.

Page 168: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion on an AVL tree

We fix the tree with rotations. Let x be the node with the largest depththat is non-balanced.

There are 4 cases based on whether the insertion happened at1. the left subtree of the left child of x2. the right subtree of the left child of x3. the left subtree of the right child of x4. the right subtree of the right child of x

Cases 1&4 are resolved with a single rotation while cases 2&3 needs adouble rotation.

Page 169: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Single Rotation

a

b

hZ

hY

h+1

X

X < b < Y < a < Z

Page 170: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Single Rotation

a

b

hZ

hY

h+1

X

X < b < Y < a < Z

Page 171: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Single Rotation

a

b

hZ

hY

h+1

X

X < b < Y < a < Z

Page 172: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Single Rotation

15

20

17 21

5

103

2 4

1

Page 173: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Single Rotation

15

20

17 215

10

3

2

41

Page 174: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Other casesIn cases 2&3 the single rotation does not work.

a

b

hZ

hX h+1

Y

X < b < Y < a < Z

Page 175: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Other casesIn cases 2&3 the single rotation does not work.

a

b

hZ

hX

h+1

Y

X < b < Y < a < Z

Page 176: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Double Rotation

a

b

hY1

h

Y2

c

h-1h

Z

X

X < b < Y1 < c < Y2 < a < Z

Page 177: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Double Rotation

a

b

hY1

h

Y2

c

h-1hX

Z

X < b < Y1 < c < Y2 < a < Z

Page 178: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Double Rotation

a

b

hY1

h

Y2

c

h-1

hX

Z

X < b < Y1 < c < Y2 < a < Z

Page 179: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Double Rotation

a

b

hY1

h

Y2

c

h-1

hX

Z

X < b < Y1 < c < Y2 < a < Z

Page 180: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Double Rotation

ab

hY1

Y2

c

h-1

hX Z

h

X < b < Y1 < c < Y2 < a < Z

Page 181: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion

Deleting elements from an AVL tree happens in the same way as in BSTswith additional rotations.

O(log n) in the worst case

Page 182: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Memory Hierarchy(Year 2010)

A big dilemma exists between speed and size when it comes to memory.SRAM (Static Random Access Memory)▶ Registers are made using SRAM▶ Very fast (1ns = 10−9sec - handles GHz clock speeds)▶ Very expensive (1GB ≈ 5000$)

DRAM (Dynamic Random Access Memory)▶ Memory is made using this technology▶ Fast (25ns)▶ Less expensive (16GB ≈ 100$)

Hard Disk▶ Very slow (5ms = 5 · 10−3sec)▶ Very cheap

Page 183: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Memory Hierarchy(Year 2010)

Idea: Use many different types of memory for better performance.

Registers

L1 Cache

L2 Cache

Main Memory

Solid State Disks

Hard Disks

Tapes

0.25 - 1ns

1 - 5ns

5 - 25ns

25 -100ns

100 - 150ns

2 - 20ms

sequential access

Page 184: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

External Memory

▶ seek time: time in order for the heads to move to the right location(track)

▶ rotational delay: waiting time in order for the cylinder to come tothe right sector that we are looking for

Page 185: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

External Memory

▶ computer memory (DRAM) has access time of nanoseconds: 10−9

sec, e.g. 25 - 100 nsec▶ hard disks have access time of milliseconds: 10−3, e.g. 2 - 20 ms

ConclusionIt is not a good idea to read small amounts of data from the disk. Forthis reason disks always read/write a block which is in the order ofKilobytes, e.g. 512K.

Page 186: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-Tree (Beta Tree)

▶ search tree which is efficient for storing to disk▶ generalizes the 2-3-4 trees using nodes with number of keys between

t and 2t for any t ≥ 2.

Page 187: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

A B-tree T is a tree with a root and the following properties:1. every node x has the following fields:

(a) n[x], the number of keys that x has(b) the n[x] keys in order: key1[x] ≤ key2[x] ≤ . . . ≤ keyn[x][x](c) a value leaf[x] which is true iff node x is a leaf

2. if x is an internal node it also contains n[x] + 1 pointersc1[x], c2[x], . . . , cn[x]+1[x] to its children. Leafs do not have childrenin which case these pointers are not defined.

3. The keys keyi[x] split the range of the keys which are stored in eachsubtree: if ki is the key stored in subtree with root ci[x]

k1 ≤ key1[x] ≤ k2 ≤ key2[x] ≤ · · · ≤ keyn[x][x] ≤ kn[x]+1.

Page 188: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

A B-tree T is a tree with a root and the following properties:1. every node x has the following fields:

(a) n[x], the number of keys that x has(b) the n[x] keys in order: key1[x] ≤ key2[x] ≤ . . . ≤ keyn[x][x](c) a value leaf[x] which is true iff node x is a leaf

2. if x is an internal node it also contains n[x] + 1 pointersc1[x], c2[x], . . . , cn[x]+1[x] to its children. Leafs do not have childrenin which case these pointers are not defined.

3. The keys keyi[x] split the range of the keys which are stored in eachsubtree: if ki is the key stored in subtree with root ci[x]

k1 ≤ key1[x] ≤ k2 ≤ key2[x] ≤ · · · ≤ keyn[x][x] ≤ kn[x]+1.

Page 189: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

A B-tree T is a tree with a root and the following properties:1. every node x has the following fields:

(a) n[x], the number of keys that x has(b) the n[x] keys in order: key1[x] ≤ key2[x] ≤ . . . ≤ keyn[x][x](c) a value leaf[x] which is true iff node x is a leaf

2. if x is an internal node it also contains n[x] + 1 pointersc1[x], c2[x], . . . , cn[x]+1[x] to its children. Leafs do not have childrenin which case these pointers are not defined.

3. The keys keyi[x] split the range of the keys which are stored in eachsubtree: if ki is the key stored in subtree with root ci[x]

k1 ≤ key1[x] ≤ k2 ≤ key2[x] ≤ · · · ≤ keyn[x][x] ≤ kn[x]+1.

Page 190: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

4. Every leaf has the same depth, the height of the tree h.

5. Let t ≥ 2 be the minimum order of the tree:5.1 Every node besides the root must have at least t − 1 keys (which

translates to at least t children). If the tree is not null, the root musthave at least one key.

5.2 Every node can contain at most 2t − 1 keys, e.g. at most 2tchildren. A node is called full if it contains exactly 2t − 1 keys.

The simplest B-tree is for t = 2. Every internal node has 2, 3 or 4children, thus, is the 2-3-4 tree.In practice we use much larger values for t.

Page 191: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

4. Every leaf has the same depth, the height of the tree h.5. Let t ≥ 2 be the minimum order of the tree:

5.1 Every node besides the root must have at least t − 1 keys (whichtranslates to at least t children). If the tree is not null, the root musthave at least one key.

5.2 Every node can contain at most 2t − 1 keys, e.g. at most 2tchildren. A node is called full if it contains exactly 2t − 1 keys.

The simplest B-tree is for t = 2. Every internal node has 2, 3 or 4children, thus, is the 2-3-4 tree.In practice we use much larger values for t.

Page 192: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Definition

4. Every leaf has the same depth, the height of the tree h.5. Let t ≥ 2 be the minimum order of the tree:

5.1 Every node besides the root must have at least t − 1 keys (whichtranslates to at least t children). If the tree is not null, the root musthave at least one key.

5.2 Every node can contain at most 2t − 1 keys, e.g. at most 2tchildren. A node is called full if it contains exactly 2t − 1 keys.

The simplest B-tree is for t = 2. Every internal node has 2, 3 or 4children, thus, is the 2-3-4 tree.In practice we use much larger values for t.

Page 193: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Height

TheoremA B-tree with n ≥ 1 keys and minimum order t ≥ 2 has height

h ≤ logtn + 1

2 .

Proof.A B-tree with height h has the least number of nodes if the root has onekey and the rest have t − 1 keys.

At lever 0 there is one node. At level 1 there are 2 nodes. At level 2there are 2t nodes, at level 3 2t2 nodes, etc.

Page 194: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

B-tree Height

TheoremA B-tree with n ≥ 1 keys and minimum order t ≥ 2 has height

h ≤ logtn + 1

2 .

Proof.The number of keys are:

n ≥ 1 + (t − 1)h∑

i=12ti−1

= 1 + 2(t − 1)(

th − 1t − 1

)= 2th − 1

Page 195: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Disk Accesses

Making sure that each node fits exactly on a disk block, we can use diskaccesses equal to the height of the tree.

1 κόµβος

1000 κλειδιά

1001 κόµβοι

1.001.000 κλειδιά

1.002.001 κόµβοι

1.002.001.000 κλειδιά

· · ·

· · ·

1000

1000 1000 1000

1000 1000 1000

1001

1001 10011001

The tree has height O(logt n).

Page 196: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Basic Principles when Implementing B-trees

(i) The root of a b-tree always remains in memory(ii) We need two functions which read and write nodes to disk

▶ DISK-READ(x)▶ DISK-WRITE(x)

Page 197: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Search on a B-tree

Search works just like in BSTs except now we have more choices pernode.B − TREE − SEARCH(x, k)i = 1while i ≤ n[x] and k > keyi[x] do

i = i + 1endif i ≤ n[x] and k = keyi[x] then

return (x, i)endif leaf[x] then

return nilelse

DISK − READ(ci[x])endreturn B − TREE − SEARCH(ci[x], k)

Page 198: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Insertion in a B-tree

▶ While searching for the insertion location, we split full nodes (having2t − 1 keys) into two nodes

▶ When a node splits, the middle key goes up the tree.▶ This makes enough room for possible splits at lower levels of the

tree.

Page 199: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Node splitt = 4

· · · · · ·N W

x key i−

1[x]

key i[x]

P Q R S T U V

y = ci[x]

· · · N S W · · ·

key i−

1[x]

key i[x]

key i+

1[x]

P Q R

T1 T2 T3 T4 T5 T6 T7 T8 T1 T2 T3 T4

T U V

T5 T6 T7 T8

y = ci[x] z = ci+1[x]

x

Page 200: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Examplet = 3

G M P X

A C D E N OJ K R S T U Y ZV

Page 201: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Examplet = 3, insertion of B

G M P X

A C D E N OJ K R S T U Y ZVB

Page 202: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Examplet = 3, insertion of Q

G M P X

A C D E N OJ K R S

T

U Y ZVB Q

Page 203: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Examplet = 3, insertion of L

G M

P

X

A C D E N OJ K R S

T

U Y ZVB QL

Page 204: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Examplet = 3, insertion of F

G M

P

X

A D E N OJ K R S

T

U Y ZVB QL

C

F

Page 205: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deletion from a B-tree

Works the same as in 2-3-4 trees.

We may need to either▶ balance, or▶ fuse

Page 206: Data Structures - Search Trees · A binary search tree is a binary tree where each node is associated with a key with the additional property that the key of a node is larger (or

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Reading and Sources

▶ Sections 7.1, 7.2, 7.3Kurt Mehlhorn ��� Peter Sanders. Algorithms and Data Structures,The basic toolbox, 1/e, 2014.

▶ Sections 12.5, 12.6, 12.8, 12.9, 13.3, 13.4, 16.3Robert Sedgewick. Algorithms in C: Fundamentals, Data Structures,Sorting, Searching. 3/e, Addison-Wesley Professional, 1997.

▶ Sections 12.1, 12.2, 12.3, 13.1, 13.2, 13.3, 13.4Cormen, Leiserson, Rivest and Stein. Introduction to Algorithms.3/e, MIT Press. 2009.