1 / 17

CS102 – Data Structures

Learn about different data structures such as lists, stacks, queues, trees, and hash tables. Understand their implementations, operations, and common use cases.

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 & 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, … head tail dog cat mouse horse

  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 animals{ String[] } valid{ int } 4

  6. 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; } }

  7. Linked List operations… cat dog mouse horse bird head{ Node } { List } { Node } { Node } { Node } { Node } { 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. 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)

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

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

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

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

  14. Trees • have • a Root • Nodes • Branches {children} • Leaves root • Examples: • Family trees • Files/folders • GUI containers & ui components

  15. / + - 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

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

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

More Related