Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists, Stacks, Queues, Trees & HashTables. David Davenport

Similar presentations


Presentation on theme: "Lists, Stacks, Queues, Trees & HashTables. David Davenport"— Presentation transcript:

1 Lists, Stacks, Queues, Trees & HashTables. David Davenport
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport Last Updated: 04/5/2015 ~added list implementation slides, expression eval slide & notes, trees, plus some animation & tidying! Previous: 28/4/2015, Spring 2002 Note: The Collections Framework was moved to another file!

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 Operations
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 Note: There is also Java’s Vector class, which is essentially a thread-safe ArrayList… (it was in Java from 1.0)

5 Lists: using arrays… (simple)
Implicit Succ/Pred relationship dog cat mouse horse animals { String[] } 1 2 3 4 5 valid { int } 4 Already done this in CS101 & Lab01… c.f. IntBag What is the complexity of each operation? insert/delete? Iterate? search? succ, pred, get? create?

6 Lists: using object/ref…
dog { Node } cat { Node } mouse { Node } horse { Node } head { 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…
dog { Node } cat { Node } mouse { Node } horse { Node } head { Node } bird { Node }

8 Linked Lists – misc. Implementation Methods
Node class – data & next {Node} List class – head Methods print print in reverse! add (at head) append search insert (& in order) delete Note difficulty of finding previous node, problem for insert/append --- include link to tail in list class --- use circular list, with tail only? --- use prev link (doubly linked list) - Write pred. succ, get methods…? Complexity of operations – print/search O(N), insert/delete O(1) Note: may do binary search if data is ordered in list, O(logN) but… insert/delete O(N) ? Can we do better?

9 Linked Lists – misc. Alternative array implementation
How can new be implemented? & dispose? Need to keep track of free space… how? 1 2 3 4 5 6 7 -1 A B D G C data next head What is the complexity of new? Of dispose? (N, 1)! Bad news, since insert no longer O(1)! We need O(1) for new & dispose… how? Can also do this with Random Access Files (simply replace X[i] with seek(i) & read)

10 Linked Lists – misc. Free space as list! New:
Remove & return first element of free space list Dispose: add to beginning of free space list 1 2 3 4 5 6 7 -1 A D G C data next head free Good, but what is the complexity of the initialisation? Can we improve on that too? Note: this is effectively the same problem Java/OS must solve! What sort of data structure is this? How would it be initialized? If data items occupied varying numbers of consecutive array elements how would this affect allocation/deallocation of free space?

11 StackOverflow & StackUnderflow
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 Array & LinkedList implementations are straightforward. Method calling pushes local variables & parameters onto system stack Pops when method returns. Especially important for recursive method calls… Note that infinite recursion or recursion involving very large data often results in StackOverflowException! StackOverflow & StackUnderflow

12 Expression Evaluation…
5 + 3 / ~ambiguous! 8 / 2 = 4 – 2 = 3.75 5 + 3 / 2 = 6.5 8 / 4 – 2 = 2 Notations Infix 5 + 3 Prefix + 5 3 Postfix 5 3 + Polish notation, due to Jan Łukasiewicz in 1920’s Reverse Polish, from 1950’s & 60’s due to various people including Dijkstra 5 + 3 / {ambiguous, hence need for brackets & expression eval rules in Java} a) = 8 / 2 = 4 b) = – 2 = 3.75 c) = / 2 = 6.5 d) = 8 / 4 – 2 = 2 Infix notation – operand operator operand Alternatives Prefix = operator operand operand Postfix = operand operand operator Polish & reverse Polish! {not ambiguous!} eg. In Postfix notation… (each has unique expansion) / 5 3 4 / + 2 – / + / 2 – e.g. in Prefix notation… a) b) c) d) First scientific hand-held electronic calculator, The HP 35 (35 keys… the calculator “without equals!”), used Postfix notation Because simple to evaluate using a hardware Stack. Eight+ months later TI brought out infix caculator. Only other infix one was Sinclair kit calculator? Using Stack to evaluate… While stack is not empty do get next input token if token is number then push onto stack else pop two items from stack apply operator to them push result back onto stack Pop stack into result If stack is empty return result Else error, invalid expression! Polish notation: Jan Łukasiewicz HP 35

13 Queues Abstract – FIFO (First In, First out) Methods Uses
enqueue, dequeue & isEmpty isFull & constructor Uses simulations in event handling Implementation Arrays & linked lists B C D E A Array implementation… Contiguous from 0 to E, add to E, remove from 0 (inefficient… better to use S & E, move S & E… use “circular” array, i.e. (S + 1) % array.length) LinkedList implementation - Easy to add to tail & add/remove from head so… head is front –dequeue, & rear is tail – enqueue. & Priority Queues? Each element has priority, higher priority elements “jump” lower priority ones! Implement as (linked) list (or using heap –in CS201?) enqueue (rear) dequeue (front)

14 Trees have a Root Nodes Branches {children} Leaves root Examples:
Data may be stored in each of the nodes or (in effect) only in the leaf nodes. Conceptually, tree nodes may have any number of branches (children) - In practice, there is probably no advantage to having more than two children (unless the tree will not reside in primary memory, but in secondary memory where the huge differential in access speed can make it advantageous to have nodes with thousands of children). 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 root + 5 / 3 - 2 4 On this tree, each traversal produces corresponding expression; inFix, preFix, postFix left right Problem: print out the data from all the nodes in the tree…. Naturally recursive! Think of the solution in terms of the three pieces: the root, the left sub-tree and the right sub-tree. Note that the printing all the nodes in the left & right sub-trees are just smaller cases of the original problem! What is the simplest case of the problem?

16 if balanced! insert/delete O(1)
Binary Search Trees Efficient insert/delete & search? David Ayse Gunes Derya Mehmet Tankut Kadriye root left right < root > root O(log2N) if balanced! insert/delete O(1) By recursively applying the policy that left-subtree nodes are less than root and right sub-tree nodes greater than it Can perform binary search like search, i.e. O(logN) And have rapid insert and delete, effectivly O(1) plus search time. - Insert at location of search, e.g. Ece would go to the right of Derya. Unfortunately, no guarantee resulting tree is balanced (has same number of nodes in left & right sub-trees). So worst case search is O(N). Various algorithms to ensure balanced trees, e.g. AVL trees & Red-Black tree… more in CS202! Array implementation of tree: (Define complete & full trees) Using array, store root in first node, its children in second & third nodes, then their children in the next fours nodes, and so on. Can easily write methods to get left/right children, and parent! Wastes space if tree is sparse! So, trees give very fast search and insert/delete… can we do better?

17 Hash function values david -- 0
Hash Tables What’s the fastest way to find something? Remember where you put it & look there! Hashing - computes location from data 1 2 3 4 5 6 7 david gunes derya Hash function values david -- 0 gunes -- 2 derya -- 3 Also called Maps. Key-value pairs. Methods: add( key, value) value get( key) Hash function… e.g. add up character codes in key mod table size Always potential for collisions: Input range mapped into small table size! Data determines whether collisions actually occur Linear probing: load factor around 75-80% needed for O(1). Linked Lists: should be short for O(1) Perfect hashes? Given fixed set of data, find hash function such that the data can be placed into a table just large enough to hold it, without any collisions. “derya” hash Collisions? ayse -- 2 Solutions: linear probing linked lists


Download ppt "Lists, Stacks, Queues, Trees & HashTables. David Davenport"

Similar presentations


Ads by Google