Dynamic Dictionaries

32
Dynamic Dictionaries Primary Operations: get(key) => search put(key, element) => insert remove(key) => delete Additional operations: ascend() get(index) remove(index)

description

Dynamic Dictionaries. Primary Operations: get(key) => search put(key, element) => insert remove(key) => delete Additional operations: ascend() get(index) remove(index). n is number of elements in dictionary. Complexity Of Dictionary Operations get(), put() and remove(). - PowerPoint PPT Presentation

Transcript of Dynamic Dictionaries

Page 1: Dynamic Dictionaries

Dynamic Dictionaries

• Primary Operations: get(key) => search put(key, element) => insert remove(key) => delete

• Additional operations: ascend() get(index) remove(index)

Page 2: Dynamic Dictionaries

Complexity Of Dictionary Operationsget(), put() and remove()

Data Structure Worst Case Expected

Hash Table

O(n)/O(log 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

Page 3: Dynamic Dictionaries

Complexity Of Other Operationsascend(), get(index), remove(index)Data Structure ascend get and

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

Indexed BST O(n) O(n)

IndexedBalanced BST

O(n) O(log n)

D is number of buckets

Page 4: Dynamic Dictionaries

The Operation put()20

10

6

2 8

15

40

30

25

Put a pair whose key is 35.

35

Page 5: Dynamic Dictionaries

The Operation remove()

Three cases:

Element is in a leaf.

Element is in a degree 1 node.

Element is in a degree 2 node.

Page 6: Dynamic Dictionaries

Remove From A Leaf

Remove a leaf element. key = 7

20

10

6

2 8

15

40

30

25 35

7

18

Page 7: Dynamic Dictionaries

Remove From A Degree 1 Node

Remove from a degree 1 node. key = 40

20

10

6

2 8

15

40

30

25 35

7

18

Page 8: Dynamic Dictionaries

Remove From A Degree 1 Node (contd.)

Remove from a degree 1 node. key = 15

20

10

6

2 8

15

40

30

25 35

7

18

Page 9: Dynamic Dictionaries

Remove From A Degree 2 Node

Remove from a degree 2 node. key = 10

20

10

6

2 8

15

40

30

25 35

7

18

Page 10: Dynamic Dictionaries

Remove From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 11: Dynamic Dictionaries

Remove From A Degree 2 Node20

10

6

2 8

15

40

30

25 35

7

18

Replace with largest key in left subtree (or smallest in right subtree).

Page 12: Dynamic Dictionaries

Remove From A Degree 2 Node20

8

6

2 8

15

40

30

25 35

7

18

Replace with largest key in left subtree (or smallest in right subtree).

Page 13: Dynamic Dictionaries

Remove From A Degree 2 Node20

8

6

2 8

15

40

30

25

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

35

7

18

Page 14: Dynamic Dictionaries

Another Remove From A Degree 2 Node

Remove from a degree 2 node. key = 20

20

10

6

2 8

15

40

30

25 35

7

18

Page 15: Dynamic Dictionaries

Remove From A Degree 2 Node20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 16: Dynamic Dictionaries

Remove From A Degree 2 Node20

10

6

2 8

15

40

30

25 35

7

18

Replace with largest in left subtree.

Page 17: Dynamic Dictionaries

Remove From A Degree 2 Node18

10

6

2 8

15

40

30

25 35

7

18

Replace with largest in left subtree.

Page 18: Dynamic Dictionaries

Remove From A Degree 2 Node18

10

6

2 8

15

40

30

25

Complexity is O(height).

35

7

Page 19: Dynamic Dictionaries

Yet Other Operations

• Priority Queue Motivated Operations: find max and/or min remove max and/or min initialize meld

Page 20: Dynamic Dictionaries

Max And/Or Min

• Follow rightmost path to max element.• Follow leftmost path to min element.• Search and/or remove => O(h) time.

20

10

6

2 8

15

40

30

25

Page 21: Dynamic Dictionaries

Initialize

• Sort n elements. Initialize search tree. Output in inorder (O(n)).

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

20

10

6

2 8

15

40

30

25

Page 22: Dynamic Dictionaries

Meld10

6

2 8

15

5

1

7 17

12

10

6

2

1 5

8

17

15

127

Page 23: Dynamic Dictionaries

Meld And Merge

• Worst-case number of comparisons to merge two sorted lists of size n each is n-1.

• So, complexity of melding two binary search trees of size n each is (n).

• So, logarithmic time melding isn’t possible.

Page 24: Dynamic Dictionaries

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.

10

6

2 8

15 1+

8

2

1 6

15

10

=

Page 25: Dynamic Dictionaries

Balanced Search Trees

• Height balanced.AVL (Adelson-Velsky and Landis) trees

• Weight Balanced.• Degree Balanced.

2-3 trees2-3-4 treesred-black treesB-trees

Page 26: Dynamic Dictionaries

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

Page 27: Dynamic Dictionaries

Balance Factors

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

This is an AVL tree.

Page 28: Dynamic Dictionaries

Height Of An AVL Tree

The height of an AVL tree that has n nodes is at most 1.44 log2 (n+2).

The height of every n node binary tree is at least log2 (n+1).

log2 (n+1) <= height <= 1.44 log2 (n+2)

Page 29: Dynamic Dictionaries

Proof Of Upper Bound On Height

• Let Nh = min # of nodes in an AVL tree whose height is h.

• N0 = 0.

• N1 = 1.

Page 30: Dynamic Dictionaries

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

Page 31: Dynamic Dictionaries

Fibonacci Numbers

• F0 = 0, F1 = 1.

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

• N0 = 0, N1 = 1.

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

• Nh = Fh+2 – 1.

• Fi ~ i/sqrt(5). sqrt

Page 32: Dynamic Dictionaries

AVL Search Tree

0 0

0 0

1

0

-1 0

1

0

-1

1

-110

7

83

1 5

30

40

20

25

35

45

60