Top Banner
Data Structures and Algorithms – COMS21103 Dynamic Search Structures Self-balancing Trees and Skip Lists Benjamin Sach
436

Self-balancing Trees and Skip Lists

Jan 20, 2017

Download

Education

Benjamin Sach
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Self-balancing Trees and Skip Lists

Data Structures and Algorithms – COMS21103

Dynamic Search Structures

Self-balancing Trees and Skip Lists

Benjamin Sach

Page 2: Self-balancing Trees and Skip Lists

Dynamic Search Structures

A dynamic search structure,

Each element x must have a unique key - x.key

The following operations are supported:

DELETE(k) - deletes the (unique) element x with x.key = k

INSERT(x, k) - inserts x with key k = x.key

FIND(k) - returns the (unique) element x with x.key = k

(or reports that it doesn’t exist)

stores a set of elements

(or reports that it doesn’t exist)

Page 3: Self-balancing Trees and Skip Lists

Dynamic Search Structures

A dynamic search structure,

Each element x must have a unique key - x.key

The following operations are supported:

DELETE(k) - deletes the (unique) element x with x.key = k

INSERT(x, k) - inserts x with key k = x.key

FIND(k) - returns the (unique) element x with x.key = k

(or reports that it doesn’t exist)

stores a set of elements

(or reports that it doesn’t exist)

We would also like it to support (among others):

PREDECESSOR(k) - returns the (unique) element xwith the largest key such that x.key < k

RANGEFIND(k1, k2) - returns every element x with k1 6 x.key 6 k2

Page 4: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 5: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

We could implement a Dynamic Search Structure using an unsorted linked list:

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 6: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 7: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 8: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 9: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 10: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 11: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 12: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 13: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 14: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 15: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure

5Bob

5Bob x

x.key

x

x.key

We could implement a Dynamic Search Structure using an unsorted linked list:

4Dawn

4Dawn

3Alice

35Emma

3Alice

36Emma

3Alice

33Alice

INSERT is very efficient,

- add the new item to the head of the list in O(1) time

8Chris

7Chris

FIND and DELETE are very inefficient, they take O(n) time

- we have to look through the entire linked list to find an item (in the worst case)

There are many ways in which we could implement a search structure. . .but they aren’t all efficient

4Dawn

4Dawn

Let n denote the number of elements stored in the structure- our goal is to implement a structure with operations which scale well as n grows

Page 16: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Page 17: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

Page 18: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

node

Page 19: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

nodeleft subtree

(smaller keys)

Page 20: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

nodeleft subtree

(smaller keys)right subtree

(larger keys)

Page 21: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

Page 22: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

node

left subtree(smaller keys)

right subtree(larger keys)

Page 23: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

Page 24: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

Page 25: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

Page 26: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

Page 27: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left

Page 28: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left

Page 29: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left21 > 20 so go right

Page 30: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left21 > 20 so go right

Page 31: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left21 > 20 so go right21 < 23 so go left

Page 32: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left21 > 20 so go right21 < 23 so go left

Page 33: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

FIND(21)

21 < 27 so go left21 > 20 so go right21 < 23 so go left21 = 21 found it!

Page 34: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

Page 35: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

Page 36: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

Page 37: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

Page 38: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 39: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 40: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 41: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 42: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 43: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 44: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right21 < 23 so go left

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 45: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right21 < 23 so go left

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 46: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right21 < 23 so go left21 = 21 found it!

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

DELETE(21)

Page 47: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 21 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right21 < 23 so go left21 = 21 found it!

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

now delete it!

DELETE(21)

Page 48: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .

21 < 27 so go left21 > 20 so go right21 < 23 so go left21 = 21 found it!

this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

now delete it!

DELETE(21)

Page 49: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

Page 50: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

INSERT(62)

Page 51: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

INSERT(62)

Page 52: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

INSERT(62)

Page 53: Self-balancing Trees and Skip Lists

Binary Search Trees

35

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

INSERT(62)

Page 54: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

INSERT(62)

Page 55: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

Recall that in a binary search tree, for any node

- all the nodes in the left subtree have smaller keys

- all the nodes in the right subtree have larger keys

We perform a FIND operation by following a path from the root. . .this takes O(h) time - where h is the height

h

The INSERT and DELETE operations are similar and also take O(h) time

Page 56: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

Page 57: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

How big is h?

Page 58: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

How big is h?

n is the numberof elements

Page 59: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

Page 60: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

Page 61: Self-balancing Trees and Skip Lists

Binary Search Trees

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

h

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

In particular, each INSERT could increase h by one

Page 62: Self-balancing Trees and Skip Lists

Binary Search Trees

63

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

In particular, each INSERT could increase h by one

h

Page 63: Self-balancing Trees and Skip Lists

Binary Search Trees

63

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

64

In particular, each INSERT could increase h by one

h

Page 64: Self-balancing Trees and Skip Lists

Binary Search Trees

63

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

64

In particular, each INSERT could increase h by one

h

Page 65: Self-balancing Trees and Skip Lists

Binary Search Trees

63

6235

A classic choice for a dynamic search structure isa binary search tree. . .

key

27

20 36

15 23 31 61

16 26

How big is h?

It might be as small as log2 n (if the tree is perfectly balanced)

n is the numberof elements

but it might be as big as n (if the tree is completely unbalanced)

64

In particular, each INSERT could increase h by one

h

how can we overcome this?

Page 66: Self-balancing Trees and Skip Lists

Part one

Self-balancing trees

inspired by slides by Inge Li Gørtzin turn inspired by slides by Kevin Wayne

Page 67: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

Page 68: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

Page 69: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

2-node

Page 70: Self-balancing Trees and Skip Lists

2-3-4 Trees

11 18

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

3-node

Page 71: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

4-node

Page 72: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

Page 73: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Page 74: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

Page 75: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

2-node

Page 76: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

2-node

smaller than 24

Page 77: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

2-node

smaller than 24

bigger than 24

Page 78: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

Page 79: Self-balancing Trees and Skip Lists

2-3-4 Trees

11 18

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

3-node

Page 80: Self-balancing Trees and Skip Lists

2-3-4 Trees

11 18

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

3-node

smaller than 11

Page 81: Self-balancing Trees and Skip Lists

2-3-4 Trees

11 18

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

3-node

smaller than 11 bigger than 18

Page 82: Self-balancing Trees and Skip Lists

2-3-4 Trees

11 18

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

3-node

smaller than 11 bigger than 18between 11 and 18

Page 83: Self-balancing Trees and Skip Lists

2-3-4 Trees

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

Page 84: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

4-node

Page 85: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

4-node

smaller than 2

Page 86: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

4-node

smaller than 2

between 2 and 4

Page 87: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

4-node

smaller than 2

between 2 and 4 between 4 and 6

Page 88: Self-balancing Trees and Skip Lists

2-3-4 Trees

2 4 6

Key idea: Nodes can have between 2 and 4 children (hence the name)

Perfect balance - every path from the root to a leaf has the same length(always, all the time)

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

2-node: 2 children and 1 key3-node: 3 children and 2 keys4-node: 4 children and 3 keys

The are “dummy leaves” (they don’t do or contain anything)

Like in a binary search tree,the keys held at a node determine

the contents of its subtrees

4-node

smaller than 2

between 2 and 4 between 4 and 6

bigger than 6

Page 89: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 90: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 91: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 92: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 93: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 94: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

Page 95: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13

Page 96: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13

Page 97: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13

Page 98: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13found it!

Page 99: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13found it!

What is the time complexity of the FIND operation?

Page 100: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13found it!

What is the time complexity of the FIND operation?

It’s O(h) again

h

Page 101: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13found it!

What is the time complexity of the FIND operation?

It’s O(h) again

h

(each step down the path takes O(1) time)

Page 102: Self-balancing Trees and Skip Lists

The FIND operation

25 261 3 5 12 14

1424

19 227 10 317

2 4 6 13 15

11 18

we perform a FIND operation by following a path from the root. . .Just like in a binary search tree,

12 is between 11 and 18 FIND(12)

descisions are made by inspecting the key(s) at the current nodeand following the appropriate edge

12 is smaller than 13found it!

What is the time complexity of the FIND operation?

It’s O(h) again

h

(each step down the path takes O(1) time)What is the height, h of a 2-3-4 tree?

Page 103: Self-balancing Trees and Skip Lists

The height of a 2-3-4 tree

Perfect balance - every path from the root to a leaf has the same length(we’ll justify this as we go along)

This implies that the height, h of a 2-3-4 tree with n nodes is

Worst case: log2 n (all 2-nodes)

Best case: log4 n =log2 n

2 (all 4-nodes)

h is between 10 and 20 for a million nodes

( is an element)

Page 104: Self-balancing Trees and Skip Lists

The height of a 2-3-4 tree

Perfect balance - every path from the root to a leaf has the same length(we’ll justify this as we go along)

This implies that the height, h of a 2-3-4 tree with n nodes is

Worst case: log2 n (all 2-nodes)

Best case: log4 n =log2 n

2 (all 4-nodes)

h is between 10 and 20 for a million nodes

The time complexity of the FIND operation is O(h)

( is an element)

Page 105: Self-balancing Trees and Skip Lists

The height of a 2-3-4 tree

Perfect balance - every path from the root to a leaf has the same length(we’ll justify this as we go along)

This implies that the height, h of a 2-3-4 tree with n nodes is

Worst case: log2 n (all 2-nodes)

Best case: log4 n =log2 n

2 (all 4-nodes)

h is between 10 and 20 for a million nodes

( is an element)

The time complexity of the FIND operation is O(h) = O(logn)

Page 106: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

To perform INSERT(x, k),

11 18

17

Page 107: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

11 18

17

Page 108: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

INSERT(x, 16)11 18

17

Page 109: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

INSERT(x, 16)11 18

17

Page 110: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

INSERT(x, 16)11 18

17

Page 111: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

INSERT(x, 16)11 18

17

Page 112: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

INSERT(x, 16)11 18

17

Page 113: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

INSERT(x, 16)11 18

16 17

Page 114: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Page 115: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Page 116: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Page 117: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Page 118: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Page 119: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

Page 120: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

INSERT(x, 9)

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

Page 121: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

Page 122: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Page 123: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Page 124: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Page 125: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Page 126: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Step 4: If the leaf is a 4-node,

Page 127: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Step 4: If the leaf is a 4-node, ???

Page 128: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

Step 4: If the leaf is a 4-node, ??? We will make sure this never happens

Page 129: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 9232 51

Page 130: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 9232 51

BEFORE

AFTER

71

86

9232 51

41 63

Page 131: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 9232 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

9232 51

41 63

Page 132: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 92

these subtrees could have any size

32 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

9232 51

41 63

Page 133: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 92

these subtrees could have any size

32 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

92

these subtrees haven’t changed

32 51

41 63

Page 134: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 92

these subtrees could have any size

32 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

92

these subtrees haven’t changed

32 51

41 63

no path lengths have changed

Page 135: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 92

these subtrees could have any size

32 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

92

these subtrees haven’t changed

32 51

41 63

no path lengths have changed

(if it was perfectly balanced, it still is)

Page 136: Self-balancing Trees and Skip Lists

SPLITTING 4-nodes

We can SPLITany 4-node into two 2-nodesif it’s parent isn’t a 4-node

41 63

71 86 92

these subtrees could have any size

32 51

BEFORE

AFTER

The extra key is pushed up to the parent(so it won’t work if the parent is a 4-node)

71

86

92

these subtrees haven’t changed

32 51

41 63

no path lengths have changed

(if it was perfectly balanced, it still is)

SPLIT takes O(1) time

Page 137: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

2 4 6

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

SPLIT 4-nodes as we go down

5

13 15

Page 138: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

2 4 6

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

5

13 15

Page 139: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

2 4 6

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

5

13 15

Page 140: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

2 4 6

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

5

13 15

Page 141: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

2 4 6

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

SPLIT this!

5

13 15

Page 142: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

11 18

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4

SPLIT this!

5

13 15

Page 143: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18SPLIT this!

5

13 15

Page 144: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18SPLIT this!

5

13 15

Page 145: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18

5

13 15

Page 146: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18

5

13 15

Page 147: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18

5

13 15

Page 148: Self-balancing Trees and Skip Lists

The INSERT operation

7 9 10

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

7 9 10

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18SPLIT this!

5

13 15

Page 149: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

2 6

4 11 18SPLIT this!

9

75 10

13 15

Page 150: Self-balancing Trees and Skip Lists

The INSERT operation

6 9

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

SPLIT this!

2

4 11 18

6 9

75 10

13 15

Page 151: Self-balancing Trees and Skip Lists

The INSERT operation

6 9

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

2

4 11 18

6 9

75 10

13 15

Page 152: Self-balancing Trees and Skip Lists

The INSERT operation

6 9

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

2

4 11 18

6 9

75 10

13 15

Page 153: Self-balancing Trees and Skip Lists

The INSERT operation

6 9

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

Page 154: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

INSERT(x, 8)

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

Page 155: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

Page 156: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

OK, one more thing. . .

Page 157: Self-balancing Trees and Skip Lists

The INSERT operation

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

OK, one more thing. . .

INSERT(x, 20)

Page 158: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

OK, one more thing. . .

INSERT(x, 20)

Page 159: Self-balancing Trees and Skip Lists

The INSERT operation

4 11 18

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

2

4 11 18

6 9

5

13 15

107 8

OK, one more thing. . .

INSERT(x, 20)

what happens when we SPLIT the root?

Page 160: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

5

13 15

107 8

OK, one more thing. . .

INSERT(x, 20)

what happens when we SPLIT the root?

184

11

Page 161: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 22

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the leaf is a 2-node,insert (x, k), converting it into a 3-node

16 17

Step 3: If the leaf is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

5

13 15

107 8

OK, one more thing. . .

INSERT(x, 20)

what happens when we SPLIT the root?

184

11

Page 162: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 2216 175

13 15

107 8

INSERT(x, 20)184

11

Page 163: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 2216 175

13 15

107 8

INSERT(x, 20)

SPLITTING the root increases the height of the treeand increases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

184

11

Page 164: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 2216 175

13 15

107 8

INSERT(x, 20)

SPLITTING the root increases the height of the treeand increases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

This is the only way INSERT can affect the length of pathsso it also maintains the perfect balance property

184

11

Page 165: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 2216 175

13 15

107 8

INSERT(x, 20)

SPLITTING the root increases the height of the treeand increases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

This is the only way INSERT can affect the length of pathsso it also maintains the perfect balance property

As each SPLIT takes O(1) time, overall INSERT takes O(logn) time

184

11

Page 166: Self-balancing Trees and Skip Lists

The INSERT operation

2 6 9

25 261 3 12 14

1424

19 2216 175

13 15

107 8

INSERT(x, 20)

As each SPLIT takes O(1) time, overall INSERT takes O(logn) time

184

11

Step 1: Search for the key k as if performing FIND(k).

To perform INSERT(x, k),

Step 2: If the bottom node is a 2-node,insert (x, k), converting it into a 3-node

Step 3: If the bottom node is a 3-node,insert (x, k), converting it into a 4-node

SPLIT 4-nodes as we go down

Page 167: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

11 18

16 177 9 10

Page 168: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

11 18

16 177 9 10

Page 169: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

DELETE(16)11 18

16 177 9 10

Page 170: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

DELETE(16)11 18

16 177 9 10

Page 171: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

DELETE(16)11 18

16 177 9 10

Page 172: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

DELETE(16)11 18

16 177 9 10

Page 173: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

DELETE(16)11 18

16 177 9 10

Page 174: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

DELETE(16)11 18

177 9 10

Page 175: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

177 9 10

Page 176: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

7 9 10

Page 177: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

7 9 10

Page 178: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

7 9 10

Page 179: Self-balancing Trees and Skip Lists

The DELETE operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

7 9 10

Page 180: Self-balancing Trees and Skip Lists

The DELETE operation

7 9 10 25 261 3 5 12 14

1424

19 22

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

7 9 10

Page 181: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

DELETE(9)

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

Page 182: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

Page 183: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Page 184: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Page 185: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Page 186: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Page 187: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Step 4: If the leaf is a 2-node,

Page 188: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Step 4: If the leaf is a 2-node, ???

Page 189: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

2 4 6 13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

Step 4: If the leaf is a 2-node, ??? We will make sure this never happens

Page 190: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node71

86

92

41 63

into a 4-node

Page 191: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

71

86

92

41 63

into a 4-node

Page 192: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

41 63

into a 4-node

Page 193: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

41 63

This is the opposite of aSPLIT operation

into a 4-node

Page 194: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

these subtrees haven’t changed

41 63

This is the opposite of aSPLIT operation

into a 4-node

Page 195: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

these subtrees haven’t changed

41 63

no path lengths have changed

This is the opposite of aSPLIT operation

into a 4-node

Page 196: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

these subtrees haven’t changed

41 63

no path lengths have changed

(if it was perfectly balanced, it still is)

This is the opposite of aSPLIT operation

into a 4-node

Page 197: Self-balancing Trees and Skip Lists

FUSING 2-nodes

We can FUSE two 2-nodes (with the same parent)

if that parent isn’t a 2-node

41 63

71 86 92

BEFORE

AFTER

The extra key is pulled down from the parent(so it won’t work if the parent is a 2-node)

71

86

92

these subtrees haven’t changed

41 63

no path lengths have changed

(if it was perfectly balanced, it still is)

This is the opposite of aSPLIT operation

FUSE takes O(1) time

into a 4-node

Page 198: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node(with the same parent)

we can perform a TRANSFER71

8641 63

91 97(even if the parent is the root)

Page 199: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTER

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63 91

978671

91 97(even if the parent is the root)

Page 200: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63 91

978671

91 97(even if the parent is the root)

Page 201: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63

these subtrees haven’t changed

91

978671

91 97(even if the parent is the root)

Page 202: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

no path lengths have changed

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63

these subtrees haven’t changed

91

978671

91 97(even if the parent is the root)

Page 203: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

no path lengths have changed

(if it was perfectly balanced, it still is)

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63

these subtrees haven’t changed

91

978671

91 97(even if the parent is the root)

Page 204: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

no path lengths have changed

(if it was perfectly balanced, it still is)

TRANSFER takes O(1) time

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63

these subtrees haven’t changed

91

978671

91 97(even if the parent is the root)

Page 205: Self-balancing Trees and Skip Lists

TRANSFERING keys

If there is a 2-node and a 3-node

BEFORE

AFTERThe keys have been rearranged

no path lengths have changed

(if it was perfectly balanced, it still is)

TRANSFER also works with a 2-node and a 4-node

TRANSFER takes O(1) time

(with the same parent)

we can perform a TRANSFER71

8641 63

41 63

these subtrees haven’t changed

91

978671

91 97(even if the parent is the root)

Page 206: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

use FUSE and TRANSFER to convert 2-nodes as we go down

2 4 6

Page 207: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

2 4 6

Page 208: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

2 4 6

Page 209: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

2 4 6

Page 210: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

2 4 6

Page 211: Self-balancing Trees and Skip Lists

The DELETE operation

5 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

2 4 6

Page 212: Self-balancing Trees and Skip Lists

The DELETE operation

3 5 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

2 4 6

Page 213: Self-balancing Trees and Skip Lists

The DELETE operation

3 5 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

3 5

2 4 6

Page 214: Self-balancing Trees and Skip Lists

The DELETE operation

3 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

3 5

2 4 6

Page 215: Self-balancing Trees and Skip Lists

The DELETE operation

3 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

3 5

2 6

4

Page 216: Self-balancing Trees and Skip Lists

The DELETE operation

3 25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

3 54

2 6

Page 217: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

FUSE this!

3 54

2 6

Page 218: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

3 54

2 6

Page 219: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 3 5 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

3 54

2 6

Now we can DELETE the 5

Page 220: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

DELETE(5)

use FUSE and TRANSFER to convert 2-nodes as we go down

2 6

Now we can DELETE the 5

3 4

Page 221: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

use FUSE and TRANSFER to convert 2-nodes as we go down

2 6

3 4

Page 222: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

use FUSE and TRANSFER to convert 2-nodes as we go down

2 6

3 4

OK, one more thing. . .

Page 223: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

use FUSE and TRANSFER to convert 2-nodes as we go down

2 6

3 4

OK, one more thing. . . what happens when we FUSE the root?

Page 224: Self-balancing Trees and Skip Lists

FUSING the root

FUSING the root can decrease the height of the treewhich in turn decreases the length of all root-leaf paths by one

71 92

We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .we make an exception for the root

83root

71 92 fused root83

Page 225: Self-balancing Trees and Skip Lists

FUSING the root

FUSING the root can decrease the height of the treewhich in turn decreases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

71 92

We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .we make an exception for the root

83root

71 92 fused root83

Page 226: Self-balancing Trees and Skip Lists

FUSING the root

FUSING the root can decrease the height of the treewhich in turn decreases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

This is the only way DELETE can affect the length of pathsso it also maintains the perfect balance property

71 92

We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .we make an exception for the root

83root

71 92 fused root83

Page 227: Self-balancing Trees and Skip Lists

FUSING the root

FUSING the root can decrease the height of the treewhich in turn decreases the length of all root-leaf paths by one

- i.e every path from the root to a leaf has the same lengthSo it maintains the perfect balance property

This is the only way DELETE can affect the length of pathsso it also maintains the perfect balance property

As each FUSE or TRANSFER takes O(1) time, overall DELETE takes O(logn) time

71 92

We said that we could only FUSE two 2-nodes if the parent was not a 2-node. . .we make an exception for the root

83root

71 92 fused root83

Page 228: Self-balancing Trees and Skip Lists

The DELETE operation

As each FUSE or TRANSFER takes O(1) time, overall DELETE takes O(logn) time

25 261 12 14

1424

19 227 10

13 15

Step 1: Search for the key k using FIND(k).

To perform DELETE(k) on a leaf (we’ll deal with other nodes later)

Step 2: If the leaf is a 3-node,delete (x, k), converting it into a 2-node

11 18

17

Step 3: If the leaf is a 4-node,delete (x, k), converting it into a 3-node

use FUSE and TRANSFER to convert 2-nodes as we go down

2 6

3 4

Page 229: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Page 230: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Page 231: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

DELETE(11)

Page 232: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

DELETE(11)

Page 233: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

DELETE(11)

Page 234: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

DELETE(11)

Page 235: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

DELETE(11)

10 is the predecessor of 11

Page 236: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 227 10

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

DELETE(11)

10 is the predecessor of 11

Page 237: Self-balancing Trees and Skip Lists

The DELETE operation

7 25 261 12 14

1424

19 22

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

DELETE(11)

10 is the predecessor of 11

7 10

Page 238: Self-balancing Trees and Skip Lists

The DELETE operation

7 25 261 12 14

1424

19 22

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′

DELETE(11)

10 is the predecessor of 11

7 10

Page 239: Self-balancing Trees and Skip Lists

The DELETE operation

7 25 261 12 14

1424

19 22

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′

DELETE(11)

7 10

Page 240: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 22

13 15

11 18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′

DELETE(11)

7 10

Page 241: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′

DELETE(11)

7

10

Page 242: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′

7

10

Page 243: Self-balancing Trees and Skip Lists

The DELETE operation

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4

What if we want to DELETE something other than a leaf?

Step 1: Find the PREDECESSOR of k (this is essentially the same as FIND)

- that’s the element with the largest key k′such that k′ < k

Step 2: Call DELETE(k′)- fortunately k′ is always a leaf

Step 3: Overwrite k with another copy of k′ This also takes O(logn) time

7

10

Page 244: Self-balancing Trees and Skip Lists

2-3-4 tree summary

A 2-3-4 is a data structure based on a tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4 7

10

Page 245: Self-balancing Trees and Skip Lists

2-3-4 tree summary

A 2-3-4 is a data structure based on a tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4 7

10

Unfortunately, 2-3-4 trees are awkward to implementbecause the nodes don’t all have the same number of children

Page 246: Self-balancing Trees and Skip Lists

2-3-4 tree summary

A 2-3-4 is a data structure based on a tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

25 261 12 14

1424

19 22

13 15

18

17

2 6

3 4 7

10

Unfortunately, 2-3-4 trees are awkward to implementbecause the nodes don’t all have the same number of children

So, what is used in practice?

Page 247: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

51 53 5556 9

55 57

Page 248: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

51 53 5556 9

55 57

Page 249: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

All root-to-leaf paths have the same number of black nodes

51 53 5556 9

55 57

Page 250: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

All root-to-leaf paths have the same number of black nodes

Red nodes cannot have red children

51 53 5556 9

55 57

Page 251: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

All root-to-leaf paths have the same number of black nodes

Red nodes cannot have red children

51 53 5556 9

55 57

If these are used in practice, why did you waste our time with 2-3-4 trees?

Page 252: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

All root-to-leaf paths have the same number of black nodes

Red nodes cannot have red children

51 53 5556 9

55 57

If these are used in practice, why did you waste our time with 2-3-4 trees?

1. 2-3-4 trees are conceptually much nicer

Page 253: Self-balancing Trees and Skip Lists

Red-Black tree summary

A Red-Black tree is a data structure based on a binary tree structure

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes worst case O(logn) time

4

52 58

The root is black

All root-to-leaf paths have the same number of black nodes

Red nodes cannot have red children

51 53 5556 9

55 57

If these are used in practice, why did you waste our time with 2-3-4 trees?

1. 2-3-4 trees are conceptually much nicer 2. they are secretly the same :)

Page 254: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

Page 255: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

Page 256: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

1 2 3

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

Page 257: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

5 6 71 2 3

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

Page 258: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

95 6 71 2 3

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

Page 259: Self-balancing Trees and Skip Lists

2-3-4 trees vs. Red-Black trees

95 6 71 2 3

Any 2-3-4 tree can be converted into a Red-Black tree (and visa-versa)

The operations on 2-3-4 trees also have equivalent operations on a Red-Black tree

(the details of the Red-Black tree operations are in CLRS Chapter 13)

4

52 58

51 53 5556 9

55 57

2. they are secretly the same :)

4 8

1 2 3 5 6 7 9

You can think of a Red-Black tree asa way to implement a 2-3-4 tree

Page 260: Self-balancing Trees and Skip Lists

Dynamic Search Structure Summary

Binary Search Tree

Unsorted Linked List

INSERT DELETE FIND

O(1) O(n) O(n)

DELETE FIND

A dynamic search structure supports (at least) the following three operations

DELETE(k) - deletes the (unique) element x with x.key = k

INSERT(x, k) - inserts x with key k = x.key

FIND(k) - returns the (unique) element x with x.key = k

Here are the worst case time complexities of the structures we have seen. . .

O(logn) O(logn) O(logn)2-3-4 Tree

Red-Black Tree O(logn) O(logn) O(logn)

O(n) O(n) O(n)

Page 261: Self-balancing Trees and Skip Lists

End of part one

Page 262: Self-balancing Trees and Skip Lists

Part two

Skip lists

inspired by slides by Ashley Montanaro

Page 263: Self-balancing Trees and Skip Lists

Dynamic Search Structures

A dynamic search structure,

Each element x must have a unique key - x.key

The following operations are supported:

DELETE(k) - deletes the (unique) element x with x.key = k

INSERT(x, k) - inserts x with key k = x.key

FIND(k) - returns the (unique) element x with x.key = k

(or reports that it doesn’t exist)

stores a set of elements

(or reports that it doesn’t exist)

We would also like it to support:

PREDECESSOR(k) - returns the (unique) element xwith the largest key such that x.key < k

RANGEFIND(k1, k2) - returns every element x with k1 6 x.key 6 k2

Page 264: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

1

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

5 9 16 18 25

Page 265: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

1

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 266: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

11

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 267: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 268: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

5211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 269: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

55211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 270: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

555211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 271: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

5555211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

Page 272: Self-balancing Trees and Skip Lists

Using a Linked List as a Dynamic Search Structure (again)

5555211

dynamic search structureEarlier we briefly considered using an unsorted Linked List as a

What about using a sorted Linked List?

key

2

The bottleneck is FIND, which is very inefficient,

INSERT and DELETE also take O(n) time but only because they rely on FIND

(in the worst case)

How can we speed up the FIND operation?

- we have to look through the entire linked list to find an item

FIND(18)

5 9 16 18 25

found it!

Page 273: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16 18 25

Page 274: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

Page 275: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

Page 276: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

Page 277: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

Page 278: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

Page 279: Self-balancing Trees and Skip Lists

Making Shortcuts

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

Page 280: Self-balancing Trees and Skip Lists

Making Shortcuts

1

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

Page 281: Self-balancing Trees and Skip Lists

Making Shortcuts

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9

Page 282: Self-balancing Trees and Skip Lists

Making Shortcuts

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9 18 < 25

Page 283: Self-balancing Trees and Skip Lists

Making Shortcuts

9

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9 18 < 25

Page 284: Self-balancing Trees and Skip Lists

Making Shortcuts

169

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9 18 < 25

Page 285: Self-balancing Trees and Skip Lists

Making Shortcuts

16169

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9 18 < 25

Page 286: Self-balancing Trees and Skip Lists

Making Shortcuts

16169

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

18 > 9 18 < 25

found it!

Page 287: Self-balancing Trees and Skip Lists

Making Shortcuts

16169

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

How long does this take?

18 > 9 18 < 25

found it!

Page 288: Self-balancing Trees and Skip Lists

Making Shortcuts

16169

91

How about adding some shortcuts?

1 2 5 9 16

1 9

18 25

25

We’ve attached a second linked list containing only some of the keys. . .

To perform FIND(k) we start in the top listand go right until we come to a key k′ > k

then we move down to the bottom listand go right until we find k

FIND(18)

How long does this take?

18 > 9 18 < 25

found it!

That depends on where we place the shortcuts

Page 289: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .(the bottom list always contains all n keys)

Page 290: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 291: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 292: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

finding this is slowfinding this is quick

Page 293: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 294: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 295: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

finding this is quickfinding this is slow

Page 296: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 297: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

Page 298: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

If we spread out the m keys in the top list evenly . . .

Page 299: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

If we spread out the m keys in the top list evenly . . .

the worst case time for a FIND operation becomes O(m+ n/m)

Page 300: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

If we spread out the m keys in the top list evenly . . .

the worst case time for a FIND operation becomes O(m+ n/m)

m

Page 301: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

If we spread out the m keys in the top list evenly . . .

the worst case time for a FIND operation becomes O(m+ n/m)

≈ nm ≈ n

m ≈ nm

m

Page 302: Self-balancing Trees and Skip Lists

Linked Lists with two levels

Imagine that we decide to place m keys in the top list. . .

where should we put them to minimisethe worst case time for a FIND operation?

(the bottom list always contains all n keys)

If we spread out the m keys in the top list evenly . . .

the worst case time for a FIND operation becomes O(m+ n/m)

≈ nm ≈ n

m ≈ nm

m

By setting m =√n, we get

the worst case time for a FIND operation is O(√n)

Page 303: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

Page 304: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

They are chosen to be as evenly spread as possible

Each level will now contain half of the keys (rounding up) from the level below

Page 305: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

Each level will now contain half of the keys (rounding up) from the level below

Page 306: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

Each level will now contain half of the keys (rounding up) from the level below

Page 307: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

Each level will now contain half of the keys (rounding up) from the level below

Page 308: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

1 16 38

Each level will now contain half of the keys (rounding up) from the level below

Page 309: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

1 16 38

Each level will now contain half of the keys (rounding up) from the level below

Page 310: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

1 16 38

1 38

Each level will now contain half of the keys (rounding up) from the level below

Page 311: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

1 5 16 25 31 38

1 16 38

1 38

Each level will now contain half of the keys (rounding up) from the level below

Page 312: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

The bottom level contains every key

and every level contains the leftmost and rightmost keys

1 5 16 25 31 38

1 16 38

1 38

Each level will now contain half of the keys (rounding up) from the level below

Page 313: Self-balancing Trees and Skip Lists

Linked Lists with many levels

How about adding even more lists? (each list is called a level)

1 2 5 9 16 18 25 27 31 35 38

They are chosen to be as evenly spread as possible

The bottom level contains every key

and every level contains the leftmost and rightmost keys

As each level contains half of the keys from the level below,there are O(logn) levels

1 5 16 25 31 38

1 16 38

1 38

Each level will now contain half of the keys (rounding up) from the level below

Page 314: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

27 31 35 38

Page 315: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

27 31 35 38

Page 316: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

27 31 35 38

Page 317: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

27 31 35 38

Page 318: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

27 31 35 38

Page 319: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

1

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

27 31 35 38

Page 320: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35

27 31 35 38

Page 321: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

27 31 35 38

Page 322: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

16

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

27 31 35 38

Page 323: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

2516

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

25 6 35

27 31 35 38

Page 324: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

312516

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

25 6 35 31 6 35

27 31 35 38

Page 325: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

312516

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

25 6 35 31 6 35

27 31 35 38

Page 326: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

31

312516

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

25 6 35 31 6 35

27 31 35 38

Page 327: Self-balancing Trees and Skip Lists

FIND in multi-level linked lists

3531

312516

161

1

1 2 5 9 16 18 25

1 5 16 25 31 38

1 16 38

1 38

How do we perform FIND(k) in multi-level linked list?(essentially just like before)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

ElseMove down

consider FIND(35)

38 > 35

16 6 35 38 > 35

25 6 35 31 6 35

27 31 35 38

35 = 35!

Page 328: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Page 329: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Page 330: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Page 331: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

Page 332: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

Page 333: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

Page 334: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

Page 335: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

Page 336: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

Page 337: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

k′ > k

Page 338: Self-balancing Trees and Skip Lists

The complexity of FIND

How long does FIND(k) take in a multi-level linked list?

Observation 1 We only move down at most O(logn) times

because there are only O(logn) levels

Observation 2 Between any two nodes on level i,there are at most 2 nodes on level i+ 1

because we took half the nodes and spread them evenly

level i+ 1

level i

Observation 3 We only move right at most 2 times on any level i+ 1because we stopped moving right on level i

k′

k′

k′ > k

k′ > k

Fact We only move at most O(logn) times while performing a FIND

Page 339: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

Page 340: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

How are we going to do INSERTS and DELETES?

Page 341: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

How are we going to do INSERTS and DELETES?

Which levels should we put an INSERTED key into?

Page 342: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

How are we going to do INSERTS and DELETES?

Which levels should we put an INSERTED key into?

How can we keep a good spread of keys at each levels?

Page 343: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

How are we going to do INSERTS and DELETES?

Which levels should we put an INSERTED key into?

How can we keep a good spread of keys at each levels?

especially when we don’t know what will be INSERTED and DELETED in the future

Page 344: Self-balancing Trees and Skip Lists

Multi-level Linked Lists

1 2 5 9 16 18 25 27 31 35 38

1 5 16 25 31 38

1 16 38

1 38

If we had a multi-level linked list with O(logn) levels

and the keys were evenly spread as possiblewhere each level contained half of the keys from the level below

then we could perform FIND in O(logn) time

How are we going to do INSERTS and DELETES?

Which levels should we put an INSERTED key into?

How can we keep a good spread of keys at each levels?

especially when we don’t know what will be INSERTED and DELETED in the future

If you can’t get organised,get randomised1

Page 345: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Page 346: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

Page 347: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

H T H TH T HT H TT TT TT H

Page 348: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

T T TT TT TT TT1 5 9 25 38

Page 349: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

1 5 9 25 38

Page 350: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

1 5 9 25 38

H TH HT

Page 351: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

1 5 9 25 38

T1 9 38T

Page 352: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

1 5 9 25 38

1 9 38

Page 353: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

1 5 9 25 38

1 9 38

H HT

Page 354: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

H H H HH H1 5 9 25 38

H H H1 9 38

H HT1 38

Page 355: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

Flip one coin for each key. . .

For each key that got a head, put it in the new top level

Repeat with the keys from the new top level(stop when the top level contains only the smallest and largest keys)

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

Page 356: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

Page 357: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

This doesn’t look quite perfect but actually, it’s very good with high probability(more on this later)

Page 358: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

This doesn’t look quite perfect but actually, it’s very good with high probability(more on this later)

The intuition is that n coin flips contain about n2 heads and about n

2 tails

Page 359: Self-balancing Trees and Skip Lists

Building Multi-level Linked Lists by flipping coins

1 2 5 9 16 18 25 27 31 35 38

Before we formally introduce Skip Lists, we let’s rewindand try building another Multi-level Linked List. . .

by flipping coins

(we still always include the smallest and largest keys in every level)

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

This doesn’t look quite perfect but actually, it’s very good with high probability(more on this later)

The intuition is that n coin flips contain about n2 heads and about n

2 tails

and the heads are roughly evenly spread out

Page 360: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

Page 361: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop(if there is no ‘next level up’, create a new level at the top)

Page 362: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 363: Self-balancing Trees and Skip Lists

Skip Lists

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 364: Self-balancing Trees and Skip Lists

Skip Lists

1

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 365: Self-balancing Trees and Skip Lists

Skip Lists

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 366: Self-balancing Trees and Skip Lists

Skip Lists

9

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 367: Self-balancing Trees and Skip Lists

Skip Lists

9

9

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 368: Self-balancing Trees and Skip Lists

Skip Lists

169

9

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 369: Self-balancing Trees and Skip Lists

Skip Lists

16169

9

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

(if there is no ‘next level up’, create a new level at the top)

Page 370: Self-balancing Trees and Skip Lists

Skip Lists

16169

9

91

1

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

1 2 5 9 16 18 25 27 31 35 38

H H H HH H1 5 9 25 38

H H H1 9 38

H H1 38

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

(if there is no ‘next level up’, create a new level at the top)

Page 371: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

16169

9

91

1

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

25 27 31 35 38

HH H25 38

H38

H38

(if there is no ‘next level up’, create a new level at the top)

Page 372: Self-balancing Trees and Skip Lists

Skip Lists

25

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

16169

9

91

1

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

25 27 31 35 38

HH H25 38

H38

H38

19

(if there is no ‘next level up’, create a new level at the top)

Page 373: Self-balancing Trees and Skip Lists

Skip Lists

25

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

16169

9

91

1

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

25 27 31 35 38

HH H25 38

H38

H38

19

H

(if there is no ‘next level up’, create a new level at the top)

Page 374: Self-balancing Trees and Skip Lists

Skip Lists

19

25

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

16169

9

91

1

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

25 27 31 35 38

HH H25 38

H38

H38

19

H19

(if there is no ‘next level up’, create a new level at the top)

Page 375: Self-balancing Trees and Skip Lists

Skip Lists

19

25

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

16169

9

91

1

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

25 27 31 35 38

HH H25 38

H38

H38

19

H19

T

(if there is no ‘next level up’, create a new level at the top)

Page 376: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

consider INSERT(x, 19)

19 goes in here

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

(if there is no ‘next level up’, create a new level at the top)

Page 377: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

(if there is no ‘next level up’, create a new level at the top)

Page 378: Self-balancing Trees and Skip Lists

Skip Lists

A skip list is a multi-level linked list wherethe INSERTS are done by flipping coins

i.e. this is a skip list. . .

To perform INSERT(x, k),

Step 1: Use FIND(k) to insert (x, k) into the bottom level

Step 2: Flip a coin repeatedly:

If you get a heads, insert (x, k) into the next level up

If you get a tails, stop

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

(if there is no ‘next level up’, create a new level at the top)

Page 379: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

Page 380: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

Page 381: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 382: Self-balancing Trees and Skip Lists

Skip Lists

1

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 383: Self-balancing Trees and Skip Lists

Skip Lists

1

1

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 384: Self-balancing Trees and Skip Lists

Skip Lists

91

1

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 385: Self-balancing Trees and Skip Lists

Skip Lists

9

9

H9

91

1

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 386: Self-balancing Trees and Skip Lists

Skip Lists

9

9

H9

91

1

That about DELETES?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Page 387: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 16 18

H H1 5

H1

H1

Page 388: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 16 18

H H1 5

H1

H1

Step 3: Remove any empty levels(ones containing only the smallest and largest keys)

Page 389: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

consider DELETE(x, 9)

Step 3: Remove any empty levels(ones containing only the smallest and largest keys)

25 27 31 35 38

HH H25 38

H38

19

H19

1 2 5 16 18

H H1 5

H1

Page 390: Self-balancing Trees and Skip Lists

Skip Lists

That about DELETES?

DELETING is straightforward, just FIND the key and DELETE it from all levels

To perform DELETE(k),

Step 1: Use FIND(k) to find (x, k)

Step 2: Delete (x, k) from all levels

Step 3: Remove any empty levels(ones containing only the smallest and largest keys)

25 27 31 35 38

HH H25 38

H38

19

H19

1 2 5 16 18

H H1 5

H1

Page 391: Self-balancing Trees and Skip Lists

Skip Lists (pre-proof) summary

25 27 31 35 38

HH H25 38

H38

19

H19

1 2 5 16 18

H H1 5

H1

A skip list is a randomised data structure, based on link lists with shortcuts

which supports INSERT(x, k), FIND(k) and DELETE(k)

We will show that each of these operations takes expected O(logn) time

That is, they take O(logn) time ‘on average’

Important There is no randomness in the data,

On the worst case input sequence, the expected time is O(logn)

the only randomness is in the coin flips

Page 392: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Page 393: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

Page 394: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

Page 395: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 1 level is 12

(the first coin flip is H)

Page 396: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 1 level is 12

(the first coin flip is H)

The probability (x, k) is inserted into more than 2 levels is 14

(we throw HH. . . )

Page 397: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 1 level is 12

(the first coin flip is H)

The probability (x, k) is inserted into more than 2 levels is 14

(we throw HH. . . )

The probability (x, k) is inserted into more than 3 levels is 18

(we throw HHH. . . )

Page 398: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 1 level is 12

(the first coin flip is H)

The probability (x, k) is inserted into more than 2 levels is 14

(we throw HH. . . )

The probability (x, k) is inserted into more than 3 levels is 18

(we throw HHH. . . )

The probability (x, k) is inserted into more than j levels is 12j

Page 399: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than j levels is 12j

Page 400: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n

Page 401: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n = 1

n2

Page 402: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n = 1

n2

The union bound

Let E1, E2 . . . En be events where Ej occurs with probability pj

The probability of at least one Ej occuring is at most∑

j pj

Page 403: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n = 1

n2

The union bound

Let E1, E2 . . . En be events where Ej occurs with probability pj

The probability of at least one Ej occuring is at most∑

j pj

Let Ej be the event that the j-th INSERT puts its element in more than 2 logn levels

Page 404: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n = 1

n2

The union bound

Let E1, E2 . . . En be events where Ej occurs with probability pj

The probability of at least one Ej occuring is at most∑

j pj

Let Ej be the event that the j-th INSERT puts its element in more than 2 logn levels

The probability of at least one Ej occuring is at most∑

j1n2

Page 405: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

We begin by proving that after n INSERT operations, a skip listis very unlikely to have more than 2 logn levels. . .

Consider some INSERT(x, k) operation

An empty skip list contains only one leveland the only way this can increase is during an INSERT operation

The probability (x, k) is inserted into more than 2 logn levels is 122 log n = 1

n2

The union bound

Let E1, E2 . . . En be events where Ej occurs with probability pj

The probability of at least one Ej occuring is at most∑

j pj

Let Ej be the event that the j-th INSERT puts its element in more than 2 logn levels

The probability of at least one Ej occuring is at most∑

j1n2 = 1

n

Page 406: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

After n INSERT operations, the probability that a skip list

has more than 2 logn levels. . .

is at most1n

Page 407: Self-balancing Trees and Skip Lists

How many levels are in a Skip list?

After n INSERT operations, the probability that a skip list

has more than 2 logn levels. . .

is at most1n

It gets better as n increases!

Page 408: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 409: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 410: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 411: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 412: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

1

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 413: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 414: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

9

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 415: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 416: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 417: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 418: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

2725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 419: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

312725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 420: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

35312725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

Start at the top-left (the head of the top level)

To perform FIND(k),While you haven’t found k:

If the node to the right’s key, k′ 6 kMove right

Else Move down

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 421: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

35312725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

consider FIND(35)

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 422: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

35312725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

consider FIND(35)

How long is this path?

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 423: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

35312725

25199

H91

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

consider FIND(35)

How long is this path?

1. Reverse it

HH H25 38

H38

H38

H19H H H1 5 9

H H1 9

H1

25 27 31 35 381 2 5 9 16 18 19

Page 424: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25 27 31 35

25199

1 9

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1consider FIND(35)

How long is this path?

1. Reverse it

Page 425: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25 27 31 35

25199

1 9

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1consider FIND(35)

How long is this path?

1. Reverse it2. Convince yourself this is the same path:

Start at k

While not at the top-left:

If you can,Move up

Else Move left

Page 426: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25 27 31 35

25199

1 9

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1consider FIND(35)

How long is this path?

1. Reverse it2. Convince yourself this is the same path:

Start at k

While not at the top-left:

If (flip a coin)Move up

Else Move left

3. Now convince yourselfit takes the same time as this:

(in expectation)

Page 427: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25 27 31 35

25199

1 9

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1consider FIND(35)

How long is this path?

1. Reverse it2. Convince yourself this is the same path:

Start at k

While not at the top-left:

If (flip a coin)Move up

Else Move left

3. Now convince yourselfit takes the same time as this:

T

T T

T T T

(in expectation)

Page 428: Self-balancing Trees and Skip Lists

So how long does a FIND take? (sketch proof)

25 27 31 35

25199

1 9

1

As the number of levels is O(logn) (with high probability),

we can conclude that the number of times we move down is very likely to be O(logn)

but how many times do we move right?

25 27 31 35 38

HH H25 38

H38

H38

19

H19

1 2 5 9 16 18

H H H1 5 9

H H1 9

H1consider FIND(35)

How long is this path?

1. Reverse it2. Convince yourself this is the same path:

Start at k

While not at the top-left:

If (flip a coin)Move up

Else Move left

3. Now convince yourselfit takes the same time as this:

O(logn) in expectation

T

T T

T T T

(in expectation)

Page 429: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

Page 430: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

as each move takes O(1) time, the expected time complexity is O(logn)

Page 431: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

as each move takes O(1) time, the expected time complexity is O(logn)

The number of levels is also O(logn) in expectation

Page 432: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

as each move takes O(1) time, the expected time complexity is O(logn)

The number of levels is also O(logn) in expectation

Both INSERT and DELETE also take expected O(logn) time

this is because they both call FIND and then spend O(1) time per level

Page 433: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

as each move takes O(1) time, the expected time complexity is O(logn)

The number of levels is also O(logn) in expectation

Both INSERT and DELETE also take expected O(logn) time

this is because they both call FIND and then spend O(1) time per level

In fact, all three operations actually take O(logn) timewith high probability

i.e. the probability of an operation taking longer is at most 1n

Page 434: Self-balancing Trees and Skip Lists

Time complexities

When performing a FIND operation, the number of moves is O(logn) in expectation

as each move takes O(1) time, the expected time complexity is O(logn)

The number of levels is also O(logn) in expectation

Both INSERT and DELETE also take expected O(logn) time

this is because they both call FIND and then spend O(1) time per level

In fact, all three operations actually take O(logn) timewith high probability

i.e. the probability of an operation taking longer is at most 1n

(this is a stronger claim but proving it is harder)

Page 435: Self-balancing Trees and Skip Lists

Skip Lists (post-proof) summary

25 27 31 35 38

HH H25 38

H38

19

H19

1 2 5 16 18

H H1 5

H1

A skip list is a randomised data structure, based on link lists with shortcuts

which supports INSERT(x, k), FIND(k) and DELETE(k)

each of these operations takes expected O(logn) time

That is, they take O(logn) time ‘on average’

Important There is no randomness in the data,

On the worst case input sequence, the expected time is O(logn)

the only randomness is in the coin flips

Page 436: Self-balancing Trees and Skip Lists

Dynamic Search Structure Summary

Binary Search Tree

Unsorted Linked List

INSERT DELETE FIND

O(1) O(n) O(n)

DELETE FIND

A dynamic search structure supports (at least) the following three operations

DELETE(k) - deletes the (unique) element x with x.key = k

INSERT(x, k) - inserts x with key k = x.key

FIND(k) - returns the (unique) element x with x.key = k

Here are the time complexities of the structures we have seen. . .

O(logn) O(logn) O(logn)2-3-4 Tree

Red-Black Tree O(logn) O(logn) O(logn)

O(n) O(n) O(n)

Skip list O(logn) O(logn) O(logn)

The time complexities for the Skip list are expected, for the others, they are worst case