Docsity

Dictionary - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Dictionary - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures

& Algorithm Analysis

Dictionaries

  • A dictionary is a collection of elements each of which has a unique search key - Uniqueness criteria may be relaxed (multiset) - (I.e. do not force uniqueness)
  • Keep track of current members, with periodic insertions and deletions into the set
  • Examples
    • Membership in a club, course records
    • Symbol table (contains duplicates)
    • Language dictionary (WordSmith, Webster, WordNet)
  • Similar to database

Dictionary ADT

  • simple container methods: size()

isEmpty() elements()

  • query methods: findElement(k)

findAllElements(k)

  • update methods: insertItem(k, e)

removeElement(k) removeAllElements(k)

  • special element NO_SUCH_KEY , returned by an unsuccessful search

How to Implement a Dictionary?

• Sequences / Arrays

  • ordered
  • unordered

• Binary Search Trees

• Skip lists

• Hashtables

More Arrays

• Ordered array

• searching takes O(log n) time (binary search)

• inserting and removing takes O(n) time

• application to look-up tables (frequent

searches, rare insertions and removals)

• Apply binary search

Binary Searches

  • narrow down the search range in stages
  • “high-low” game
  • findElement(22)

An Alternative to Arrays

• Unordered Array:

  • insertion: O(1)
  • search: O(n)

• Ordered Array

  • insertion: O(n)
  • search: O(log n)

• Skip Lists:

  • insertion: O(log n)
  • search: O(log n)
  • And avoid the fixed-size drawback of arrays!

Skip Lists

• good implementation for a dictionary

• a series of lists {S0, S1, …, Sk} • each list Si stores a sorted subset of the dictionary D

- ∞ 12 18 25 28 72 74 ∞

  • ∞ 18 25 74 ∞
  • ∞ 18 ∞
  • ∞ ∞ S S S S

Traversing Positions in a Skip List

• Assume a node P in the skip list

  • after(p)
  • before(p)
  • below(p)
  • above(p)

• Running time of each operation?

Operations in a Skip List

• Use skip lists to implement dictionaries 

• Need to deal with

  • Search
  • Insert
  • Remove

Searching

• Search for 27

S

S

S

S

More Searching

• Search for 74

S

S

S

S

Running Time Analysis

• log n levels  O(log n) for going down in the

skip list

• at each level, O(1) for moving forward

  • why? works like a binary search
  • in skip lists, the elements in list S(i+1) play the role

of search dividers for elements in S(i)

  • (in binary search: mid-list elements to divide the

search)

• total running time: O(log n) Docsity.com

Insertion in Skip Lists

• First: identify the place to insert new key k

  •  node p in S0 with largest key less or equal than

k

• Insert new item(k,e) after p

• with probability 50%, the new item is inserted

in list S

  • with probability 25% , the new item is inserted in

list S

  • with probability 12.5% , the new item is inserted in list S - with probability 6.25% , the new item is inserted in list S