1 / 13

CS102 – Data Structures

CS102 – Data Structures. Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002. Abstract Data Structures. Data Structures - collections of data Already seen two (~ fixed/static) arrays - elements of same type objects - elements of differing types

Download Presentation

CS102 – Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002

  2. Abstract 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, … head dog cat mouse horse

  4. List - implementation • Using Java Vector class • Using arrays (simple approach) • Implicit succ/pred • Linked lists (singly & doubly) • Using objects & references • using arrays and/or files! • Java Collections Framework… • ArrayList & LinkedList

  5. 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

  6. 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)

  7. 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?

  8. 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

  9. 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)

  10. / + - 5 3 4 2 Binary Trees • Nodes with 0, 1 or 2 children • Recursive – children are trees too! • Traversals - inOrder, preOrder, postOrder root Each traversal produces corresponding expression; inFix, preFix, postFix left right

  11. root Gunes left David right Mehmet Ayse Derya Kadriye Tankut < root > root Binary Trees • Efficient insert/delete • & search! (binary search tree) O(log2N) if balanced!insert/delete O(1)

  12. 0 1 2 3 4 5 6 7 david gunes derya Hash • 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

More Related