cs102 – data structures lists, stacks, queues, trees & hashtables. david davenport

17
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Upload: julie-ryan

Post on 13-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

CS102 – Data Structures

Lists, Stacks, Queues, Trees & HashTables.

David Davenport

Page 2: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Data Structures Data Structures - collections of data Already seen two (~ fixed/static)

arrays - elements of same type objects - elements of differing types

Dynamic Data Structures space allocated as needed later released & available for reuse

Abstract Data Structures common conceptual (list, stack, queues, trees...) multiple implementations (static & dynamic)

Page 3: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Lists (linked-lists)

Familiar eg. shopping list, phone no’s, …

set of items, each (except first & last) with a

unique successor & predecessor

Operations insert, delete, search, iterate, …

dog cat mouse horsehead tail

Page 4: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

List - implementation Using arrays (simple approach)

Implicit succ/pred

Linked lists (singly & doubly) Using objects & references using arrays and/or files!

• Explict succ/pred

Java Collections Framework… ArrayList & LinkedList

Page 5: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Lists: using arrays… (simple)

Implicit Succ/Pred relationship

dog cat mouse horse

0 1 2 3 4 5

4

animals{ String[] }

valid{ int }

Page 6: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

{ List }

Lists: using object/ref…

head{ Node }

cat

{ Node }

dog

{ Node }

mouse

{ Node }

private class Node {String data;Node next;

public Node( String data, Node next) {this.data = data;this.next = next;

}}

public class List {Node head;

public List() {head = null;

}

// inner class Node}

horse

{ Node }

Page 7: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Linked List operations…

{ List }

head{ Node }

cat

{ Node }

dog

{ Node }

mouse

{ Node }

horse

{ Node }

bird

{ Node }

Page 8: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Linked Lists – misc.

Implementation Node class – data & next {Node}

List class – head Methods

print print in reverse! add (at head) append search insert (& in order) delete

Page 9: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Linked Lists – misc.

Alternative array implementationHow can new be implemented?

& dispose?

Need to keep track of free space… how?

0

1

2

3

4

5

6

7

1

4

3

-1

2

A

B

D

G

C

data next

head0

Can also do this withRandom Access Files(simply replace X[i] with seek(i) & read)

Page 10: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Linked Lists – misc.

Free space as list!

What sort of data structure

is this?

0

1

2

3

4

5

6

7

4

5

3

-1

2

6

7

-1

A

D

G

C

data next

head0

free1

New: Remove & return first element of free space list

Dispose:add to beginning of free space list

If data items occupied varying numbers of consecutive array elements how would this affect allocation/deallocation of free space?

How would itbe initialized?

Page 11: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Stacks Abstract - LIFO (Last In, First Out)

Methods push, pop & isEmpty isFull & constructor

Uses in method calling,

in interrupt handling, calculator (postfix expressions!)

Implementation Java Stack class arrays & linked-lists

apple

orange

banana

push pop

top

StackOverflow & StackUnderflow

Page 12: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Expression Evaluation… 5 + 3 / 4 - 2 ~ambiguous!

a) 8 / 2 = 4b) 5 + 0.75 – 2 = 3.75c) 5 + 3 / 2 = 6.5d) 8 / 4 – 2 = 2

Notations Infix 5 + 3 Prefix+ 5 3 Postfix 5 3 +

HP 35Polish notation: Jan Łukasiewicz

Page 13: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Queues Abstract – FIFO (First In, First out)

Methods enqueue, dequeue & isEmpty isFull & constructor

Uses simulations in event handling

Implementation Arrays & linked lists

BCD

E

A

enqueue(rear)

dequeue(front)

Page 14: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Trees have

a Root Nodes Branches {children} Leaves

root

Examples:Family treesFiles/foldersGUI containers & ui components

Page 15: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Binary Trees Nodes with 0, 1 or 2 children Recursive – children are trees too! Traversals - inOrder, preOrder, postOrder

+

5

/

3

-

24

root

left right

On this tree, each traversal produces corresponding expression;

inFix, preFix, postFix

Page 16: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Binary Search Trees Efficient insert/delete & search?

David

Ayse

Gunes

Derya

Mehmet

TankutKadriye

root

left right

< root > root

O(log2N)if balanced!

insert/delete O(1)

Page 17: CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport

Hash Tables What’s the fastest way to find something?

Remember where you put it & look there! Hashing - computes location from data

0

1

2

3

4

5

6

7

david

gunes

derya

“derya”

hash

Hash function valuesdavid -- 0gunes -- 2derya -- 3

Collisions? ayse -- 2

Solutions: linear probinglinked lists