1 / 22

# Advanced Data Structures - PowerPoint PPT Presentation

Advanced Data Structures. Chapter 16. Advanced Data Structures. Data structures that take control of organizing elements Elements not in fixed positions Advantage – better performance Adding Removing Finding. Set. Unordered collection of distinct elements Fundamental operations

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

## PowerPoint Slideshow about ' Advanced Data Structures' - laszlo

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

### Advanced Data Structures

Chapter 16

• Data structures that take control of organizing elements

• Elements not in fixed positions

• Advantage – better performance

• Removing

• Finding

• Unordered collection of distinct elements

• Fundamental operations

• Adding an element

• Removing an element

• Containment testing (is something there)

• Listing – arbitrary order

• Duplicates

• Aren’t permitted

• Attempt to add – ignored

• Attempt to remove non existent data - ignored

Hash tables

Trees

Uses set interface

• Create a Hash Set of Strings

Set<String> names = new HashSet<String>();

• Adding and deleting

names.remove(“Juliet”);

• Test to see if element is in set

if (names.contains(“Juliet”);

Iterator <String> iter=names.iterator();

while (iter.hasNext())

{

String name = iter.next();

do something with the string

………

}

for (String name: names)

{

do something with names

{

• Iterating through elements

• Does not visit in order inserted

• Set implementation rearranges for quick location

• Cannot add in iterator position

• Create a print method

• Should be usable by has or tree

• Data type

• Associates keys and values

• A function from one set(key) to another set(value)

• Every key has unique value

• A value may have multiple keys

• Two kinds of implementation

• Hash Map

• Tree Map

• Both implement Map Interface

Values

Keys

Romeo

Eve

Juliet

Green

Red

Pink

Map<String,Color> favoriteColors = new

HashMap<String, Color?();

• Add an association

• favorite.put(“Juliet”,Color.PINK);

• Change a value

• favorite.put(“Juliet”,Color.RED);

• Return the value

• Color julietFavoriteColor = favorite.get(“Juliet”);

• If you ask for a value with no key return a null

• Remove key and value

• favorite.remove(“Juliet”);

Find all keys and values in map

Set<String> keySet = m.keySet();

For(String key: KeySet)

{

Color value=m.get(key);

System.out.println(Key + “->” + value);

}

• Find elements in a data structure quickly

• No linear search through all elements

• Can implement sets and maps

• Utilizes hash codes

• Function that computes an integer value from an object

• Integer value = has code

• Object class has a hashCode method

• Int h = x.hashCode();

• Collision – 2 objects with same hash code

• Good has functions minimizes collision

• Hash Code – index into hash table

• Simplest form

• Array index to hash table

• In array insert each object @ location of its hash code

• If anything already in array – object is present

[70068]

[74478]

[74656]

• Problem with this idea

• Not possible to allocate an array that is large enough to hold all possible integer index positions

• Must reduce hash function to fit the array size

int h = x.hashCode();

if (h<0) h = -h;

position = h.%buckets.length;

• This approach increases likely hood of collisions

[65]

Harry

Sue

Nina

[66]

Susannah

[67]

[68]

Larry

[69]

[70]

[71]

[72]

Juliet

Katherine

Tony

Create a hash table that is implemented as an array of buckets.

Buckets are a sequence of nodes that hold elements with the same hash code.

• Compute hash code

• Reduce to table size using modulo

• Produces index into hash table

int h = x.hashCode();

if (h<0) h = -h;

position = h.%buckets.length;

• Iterate through the elements of the bucket at position h

• Check for match

Depends on number of collisions or buckets

Few buckets – quick O(1)

Many buckets – much slower

• From String

int h = 0;

for (int i=0; I < s.length(); i++)

h = h+s.charAt(i);

• What is the problem with this?

• Same hash code for permutations of same letters (eat tea)

final int HASH_MULTIPLIER = 31;

Int h=0;

for(int i=0;i<s.length();i++)

h = HASH_MULTIPLIER*h+s.charAt(i);

eat

31*(31*’e’ +’a’) + ‘t’ = 100184

31*(31*101+97) + 116

tea

31*(31*’t’ +’e’) + ‘a’ = 114704

31*(31*116+101) + 97

• Example Coin class

public class Coin

{

final int HASH_MULTIPLIER = 29;

public int hashCode()

{

int h1 = name.hashCode();

int h2 – new Double(value.hashCode();

int h = HASH_MULTIPLIER*h1+h2;

return h;

}

• In a hash map only the keys are hashed.