dynamic dictionaries
Download
Skip this Video
Download Presentation
Dynamic Dictionaries

Loading in 2 Seconds...

play fullscreen
1 / 32

Dynamic Dictionaries - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

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().

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Dynamic Dictionaries' - lou


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
dynamic dictionaries
Dynamic Dictionaries
  • Primary Operations:
    • get(key) => search
    • put(key, element) => insert
    • remove(key) => delete
  • Additional operations:
    • ascend()
    • get(index)
    • remove(index)
the operation put
20

10

40

6

15

30

25

2

8

The Operation put()

35

Put a pair whose key is 35.

the operation remove
Three cases:
    • Element is in a leaf.
    • Element is in a degree 1 node.
    • Element is in a degree 2 node.

The Operation remove()

remove from a leaf
20

10

40

6

15

30

18

25

35

2

8

7

Remove From A Leaf

Remove a leaf element. key = 7

remove from a degree 1 node
20

10

40

6

15

30

18

25

35

2

8

7

Remove From A Degree 1 Node

Remove from a degree 1 node. key = 40

remove from a degree 1 node contd
20

10

40

6

15

30

18

25

35

2

8

7

Remove From A Degree 1 Node (contd.)

Remove from a degree 1 node. key = 15

remove from a degree 2 node
20

10

40

6

15

30

18

25

35

2

8

7

Remove From A Degree 2 Node

Remove from a degree 2 node. key = 10

remove from a degree 2 node1
20

Remove From A Degree 2 Node

10

40

6

15

30

18

25

35

2

8

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

7

remove from a degree 2 node2
20

Remove From A Degree 2 Node

10

40

6

15

30

18

25

35

2

8

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

7

remove from a degree 2 node3
20

Remove From A Degree 2 Node

8

40

6

15

30

18

25

35

2

8

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

7

remove from a degree 2 node4
20

Remove From A Degree 2 Node

8

40

6

15

30

18

25

35

2

8

7

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

another remove from a degree 2 node
20

10

40

6

15

30

18

25

35

2

8

7

Another Remove From A Degree 2 Node

Remove from a degree 2 node. key = 20

remove from a degree 2 node5
20

Remove From A Degree 2 Node

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

remove from a degree 2 node6
20

Remove From A Degree 2 Node

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

remove from a degree 2 node7
18

Remove From A Degree 2 Node

10

40

6

15

30

18

25

35

2

8

7

Replace with largest in left subtree.

remove from a degree 2 node8
18

Remove From A Degree 2 Node

10

40

6

15

30

25

35

2

8

7

Complexity is O(height).

yet other operations
Yet Other Operations
  • Priority Queue Motivated Operations:
    • find max and/or min
    • remove max and/or min
    • initialize
    • meld
max and or min
20

10

40

6

15

30

25

2

8

Max And/Or Min
  • Follow rightmost path to max element.
  • Follow leftmost path to min element.
  • Search and/or remove => O(h) time.
initialize
20

10

40

6

15

30

25

2

8

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).
slide22
5

10

1

12

6

15

10

17

7

2

8

6

17

8

2

15

7

12

1

5

Meld
meld and merge
Meld And Merge
  • Worst-case number of comparisons to merge two sorted lists of size n each is 2n-1.
  • So, complexity of melding two binary search trees of size n each is W(n).
  • So, logarithmic time melding isn’t possible.
o log n height trees
8

10

2

15

1

6

15

1

6

10

2

8

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.

=

+

balanced search trees
Balanced Search Trees
  • Height balanced.
    • AVL (Adelson-Velsky and Landis) trees
  • Weight Balanced.
  • Degree Balanced.
    • 2-3 trees
    • 2-3-4 trees
    • red-black trees
    • B-trees
avl tree
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
-1

1

1

Balance Factors

-1

0

1

0

0

-1

0

This is an AVL tree.

0

0

0

height of an avl tree
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)

proof of upper bound on height
Proof Of Upper Bound On Height
  • Let Nh = min # of nodes in an AVL tree whose height is h.
  • N0 = 0.
  • N1 = 1.
n h h 1
L

R

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-1nodes.
  • The subtree whose height is h-2 has Nh-2nodes.
  • So, Nh =Nh-1 + Nh-2 + 1.
fibonacci numbers
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 ~ fi/sqrt(5).
  • f = (1 + sqrt(5))/2.
avl search tree
-1

10

1

1

7

40

-1

0

1

0

45

3

8

30

0

-1

0

0

0

60

35

1

20

5

0

25

AVL Search Tree

ad