Docsity

Complexity Analysis of Dynamic Dictionaries: Get, Put, Remove, and Other Operations, Slides of Computer Science

Partial preview of the text

Download Complexity Analysis of Dynamic Dictionaries: Get, Put, Remove, and Other Operations and more Slides Computer Science in PDF only on Docsity!

Dynamic Dictionaries

• Primary Operations:

ƒ get(key) => search

ƒ put(key, element) => insert

ƒ remove(key) => delete

• Additional operations:

ƒ ascend()

ƒ get(index)

ƒ remove(index)

Complexity Of Dictionary Operations

get(), put() and remove()

Data Structure Worst Case Expected

Hash Table O(n) O(1)

Binary Search

Tree

O(n) O(log n)

Balanced

Binary Search

Tree

O(log n) O(log n)

n is number of elements in dictionary

Complexity Of Other Operations

ascend(), get(index), remove(index)

Data Structure ascend get and

remove

Hash Table O(D + n log n) O(D + n log n)

Indexed BST O(n) O(n)

Indexed

Balanced BST

O(n) O(log n)

D is number of buckets

The Operation put()

Put a pair whose key is 35.

Remove From A Degree 1 Node

Remove from a degree 1 node. key = 40

Remove From A Degree 1 Node (contd.)

Remove from a degree 1 node. key = 15

Remove From A Degree 2 Node

Remove from a degree 2 node. key = 10

Remove From A Degree 2 Node

Replace with largest key in left subtree (or

smallest in right subtree).

Remove From A Degree 2 Node

Largest key must be in a leaf or degree 1 node.

Another Remove From A Degree 2 Node

Remove from a degree 2 node. key = 20

Remove From A Degree 2 Node

Replace with largest in left subtree.

Remove From A Degree 2 Node

Replace with largest in left subtree.

Yet Other Operations

• Priority Queue Motivated Operations:

ƒ find max and/or min

ƒ remove max and/or min

ƒ initialize

ƒ meld

Max And/Or Min

• Follow rightmost path to max element.

• Follow leftmost path to min element.

• Search and/or remove => O(h) time.

Initialize

• Sort ƒ Initialize search tree. n elements.

ƒ Output in inorder (O(n)).

• Initialize must takeisn’t possible to sort faster than O(n log n) time, because it O(n log n).

10 Meld

O(log n) Height Trees

• Full binary trees.

ƒ Exist only when n = 2k^ –1.

• Complete binary trees.

ƒ Exist for all n.

ƒ Cannot insert/delete in O(log n) time.

15 +^1

Balanced Search Trees

• Height balanced.

WAVL (Adelson-Velsky and Landis) trees

• Weight Balanced.

• Degree Balanced.

W2-3 trees

W2-3-4 trees

Wred-black trees

WB-trees

AVL Tree

• binary tree

• for every node x, define its balance factor

balance factor of x = height of left subtree of x

– height of right subtree of x

• balance factor of every node x is – 1, 0 , or 1

Balance Factors

This is an AVL tree.

Nh, h > 1

• Both L and R are AVL trees.

• The height of one is h-1.

• The height of the other is h-2.

• The subtree whose height is h-1 has Nh-1 nodes.

• The subtree whose height is h-2 has Nh-2 nodes.

• So, Nh = Nh-1 + Nh-2 + 1.

L R

Fibonacci Numbers

• F 0 = 0, F 1 = 1.

• Fi = Fi-1 + Fi-2 , i > 1.

• N 0 = 0, N 1 = 1.

• Nh = Nh-1 + Nh-2 + 1, i > 1.

• Nh = Fh+2 – 1.

• Fi ~ φi/sqrt(5).

• φ = (1 + sqrt(5))/2.

AVL Search Tree