CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
-
Upload
julie-ryan -
Category
Documents
-
view
225 -
download
0
Transcript of CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
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)
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
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
Lists: using arrays… (simple)
Implicit Succ/Pred relationship
dog cat mouse horse
0 1 2 3 4 5
4
animals{ String[] }
valid{ int }
{ 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 }
Linked List operations…
{ List }
head{ Node }
cat
{ Node }
dog
{ Node }
mouse
{ Node }
horse
{ Node }
bird
{ Node }
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
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)
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?
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
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
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)
Trees have
a Root Nodes Branches {children} Leaves
root
Examples:Family treesFiles/foldersGUI containers & ui components
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
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)
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