Download presentation
Presentation is loading. Please wait.
Published byJulie Ryan Modified over 9 years ago
1
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport
2
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)
3
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, … dogcatmousehorse headtail
4
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
5
Lists: using arrays… (simple) Implicit Succ/Pred relationship dogcatmousehorse 012345 4 animals { String[] } valid { int }
6
{ List } Lists: using object/ref… head { Node } cat { Node } dog { Node } mouse { Node } private class Node { Stringdata; Nodenext; public Node( String data, Node next) { this.data = data; this.next = next; } public class List { Nodehead; public List() { head = null; } // inner class Node } horse { Node }
7
Linked List operations… { List } head { Node } cat { Node } dog { Node } mouse { Node } horse { Node } bird { Node }
8
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
9
Linked Lists – misc. Alternative array implementation How can new be implemented? & dispose? Need to keep track of free space… how? 0123456701234567 1 4 3 2 ABDGCABDGC data next head 0 Can also do this with Random Access Files (simply replace X[i] with seek(i) & read)
10
Linked Lists – misc. Free space as list! What sort of data structure is this? 0123456701234567 4 5 3 2 6 7 ADGCADGC data next head 0 free 1 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 it be initialized?
11
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 pushpop top StackOverflow & StackUnderflow
12
Expression Evaluation… 5 + 3 / 4 - 2 ~ambiguous! a)8 / 2 = 4 b)5 + 0.75 – 2 = 3.75 c)5 + 3 / 2 = 6.5 d)8 / 4 – 2 = 2 Notations Infix5 + 3 Prefix+ 5 3 Postfix5 3 + HP 35 Polish notation: Jan Łukasiewicz
13
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)
14
Trees have a Root Nodes Branches {children} Leaves root Examples: Family trees Files/folders GUI containers & ui components
15
Binary Trees Nodes with 0, 1 or 2 children Recursive – children are trees too! Traversals - inOrder, preOrder, postOrder + 5 / 3 - 24 root leftright On this tree, each traversal produces corresponding expression; inFix, preFix, postFix
16
Binary Search Trees Efficient insert/delete & search? David Ayse Gunes Derya Mehmet TankutKadriye root leftright < root> root O(log 2 N) if balanced! insert/delete O(1)
17
Hash Tables What’s the fastest way to find something? Remember where you put it & look there! Hashing - computes location from data 0123456701234567 david gunes derya “derya” hash Hash function values david -- 0 gunes -- 2 derya -- 3 Collisions? ayse -- 2 Solutions: linear probing linked lists
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.