data structures and object-oriented design viicarola/teaching/cmps1600/spring14… ·  ·...

17
Data Structures and Object-Oriented Design VII Spring 2014 Carola Wenk

Upload: lamkien

Post on 09-Mar-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Data Structures and Object-Oriented Design

VIISpring 2014

Carola Wenk

Page 2: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Data Structures We Know

...

• We’ve seen arrays and linked structures. All queue and stack operations take constant time, but what about if we want to add, remove and find from the items being stored?

...

.

.

.

Collection interface

array

Linked list

(binary) tree

Page 3: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Collection Interfacepublic interface Collection<T>{

public void add(T item);public void remove(T item);public boolean contains(T item);

} or find

Page 4: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Collections• Java uses “growable” arrays and linked lists to implement

various interfaces derived from Collection.

Some of these collections require ordered elements, others do not. What is a “hash” table?

Page 5: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Dynamic Lists

55L: 99

L: 55 990 1

somewherein memory

In a dynamic list each element is indirectly adjacent to its neighbor.

Static

Dynamic

...

...

Page 6: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Dynamic Lists

55L: 99

L: 55 990 1

How do we add an item to the dynamic list?

Static

Dynamic

...

...

somewherein memory

Page 7: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Dynamic Lists

55L: 99

L: 55 750 1

How do we add an item to the dynamic list?

Static

Dynamic

...

...

99

somewherein memory

Page 8: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Dynamic Lists

55L: 99

L: 55 750 1

Because it is indirectly defined, to add an element to the dynamic list, we just need to reassign neighbor relationships.

Static

Dynamic

...

...

75

99

somewherein memory

Page 9: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Dynamic Lists

55L: 99

L: 55 750 1

In contrast to the static list, we only need to perform a constant amount of work to add an item to the dynamic list.

Static

Dynamic

...

...

75

99

somewherein memory

Page 10: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Remember Binary Search?median

data

smaller larger

The two halves of a binary search tree can be defined recursively.

Page 11: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Binary Search Treesmedian

data

smaller larger

• How do we define this type of structure in Java?

Page 12: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Summary of Binary Search Trees

• The time to perform operations in binary search trees is highly dependent on how they are built.

• The best-case depth of a binary tree is logarithmic in the number of elements; there are sophisticated techniques (AVL, red-black) for ensuring this depth in the worst-case.

55

33 100

32 45 56 101

between logarithmic and linear

Page 13: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Collections• Java uses “growable” arrays and linked lists to implement

various interfaces derived from Collection.

Some of these collections require ordered elements, others do not. What is a “hash” table?

Page 14: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Collections and Maps• The Collection interface is for storage and access, while a

Map interface is geared towards associating keys with objects.

Page 15: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Student database problem

Tulane’s student database D stores n records:record

key Operations on D:

•D.put(key,value)

•D.get(key)

•D.remove(key)

How should the data structure D be organized?

valueName

Address

Grades

ID“add”

“find”

Page 16: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Direct-Access Table (array)• Suppose every key is a different number: K {0, 1, …, m–1}• Set up an array D[0 . . m–1] such that D[key] = value for every record, and D[key]=null for keys without records.

put, get, remove take (1) time.

D

00000006

John Welch Jones

. . .000747111

David Filo

Page 17: Data Structures and Object-Oriented Design VIIcarola/teaching/cmps1600/spring14… ·  · 2014-02-17Data Structures and Object-Oriented Design VII ... Static Dynamic..... 75 99 somewhere

Direct-Access Table (array)• Suppose every key is a different number: K {0, 1, …, m–1}• Set up an array D[0 . . m–1] such that D[key] = value for every record, and D[key]=null for keys without records.

D

00000006

John Welch Jones

. . .000747111

David Filo

Problem: The range of keys can be large:•64-bit numbers (which represent 18,446,744,073,709,551,616 different keys),

•Character strings (even more!).