170 likes | 189 Views
Learn about different data structures such as lists, stacks, queues, trees, and hash tables. Understand their implementations, operations, and common use cases.
E N D
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, … head tail dog cat mouse horse
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 animals{ String[] } valid{ int } 4
Lists: using object/ref… cat dog mouse horse head{ Node } { List } { Node } { Node } { Node } { Node } public class List { Node head; public List() { head = null; } // inner class Node } private class Node { String data; Node next; public Node( String data, Node next) { this.data = data; this.next = next; } }
Linked List operations… cat dog mouse horse bird head{ Node } { List } { Node } { Node } { Node } { Node } { 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
head data next 0 0 1 2 3 4 5 6 7 A B D G C 1 4 3 -1 2 Linked Lists – misc. • Alternative array implementation How can new be implemented? & dispose? Need to keep track of free space… how? Can also do this withRandom Access Files(simply replace X[i] with seek(i) & read)
head data next free 0 1 0 1 2 3 4 5 6 7 A D G C 4 5 3 -1 2 6 7 -1 Linked Lists – misc. • Free space as list! New: Remove & return first element of free space list Dispose: add to beginning of free space list What sort of data structure is this? How would itbe initialized? If data items occupied varying numbers of consecutive array elements how would this affect allocation/deallocation of free space?
push pop banana orange apple 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 top StackOverflow & StackUnderflow
Expression Evaluation… • 5 + 3 / 4 - 2 ~ambiguous! • 8 / 2 = 4 • 5 + 0.75 – 2 = 3.75 • 5 + 3 / 2 = 6.5 • 8 / 4 – 2 = 2 • Notations • Infix 5 + 3 • Prefix + 5 3 • Postfix 5 3 + HP 35 Polish notation: Jan Łukasiewicz
A D C B E Queues • Abstract – FIFO (First In, First out) • Methods • enqueue, dequeue & isEmpty • isFull & constructor • Uses • simulations • in event handling • Implementation • Arrays & linked lists enqueue(rear) dequeue(front)
Trees • have • a Root • Nodes • Branches {children} • Leaves root • Examples: • Family trees • Files/folders • GUI containers & ui components
/ + - 5 3 4 2 Binary Trees • Nodes with 0, 1 or 2 children • Recursive – children are trees too! • Traversals - inOrder, preOrder, postOrder root On this tree, each traversal produces corresponding expression; inFix, preFix, postFix left right
root Gunes left David right Mehmet Ayse Derya Kadriye Tankut < root > root Binary Search Trees • Efficient insert/delete • & search? O(log2N) if balanced!insert/delete O(1)
0 1 2 3 4 5 6 7 david gunes derya Hash Tables • What’s the fastest way to find something? • Remember where you put it & look there! • Hashing - computes location from data Hash function valuesdavid -- 0 gunes -- 2 derya -- 3 “derya” hash Collisions? ayse -- 2 Solutions:linear probing linked lists