Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011.

Similar presentations


Presentation on theme: "CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011."— Presentation transcript:

1 CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011

2 Quiz 1.Compared to vectors, linked lists are terrible at _______, but are much better at _______. 2.With stacks, items are popped in the following order: A.First-in-first-out B.Last-in-first-out 3.(Extra credit!) Complete this recursive algorithm that displays a list by iterating through using a typical node class with a next pointer. Recall that the last element in a list will have its next point to NULL. void printList(Node *node){ if (node == _____) return; // base case – when do we quit cout object << endl; printList(__________); // recursive call }

3 Reminder Office hours immediately after class in TA office (Smith Hall 103) on Wednesdays Or by appointment ….except today, will start at noon Please come see me if you are stuck with lab 2 Sooner rather than later!

4 Lab 1 Solution -Solution will be posted online on Friday evening. -Lab 3 will hopefully be ready by then as well.

5 Recap: Object arrays Foo localArray[20]; /* creates on stack, all objects destructed after scope. 20 “Constructed” on scope, 20 “Destroyed” after scope. */ Foo *arrayOfPointers[2000]; /* creates an array of pointers, not instantiated. Destroyed after scope. No “Constructed” messages. Objects being pointed at are not destroyed after scope.*/ Foo *pointerToAnArray = new Foo[20]; /* uses heap; array persists. developer must delete[] the array. 20 “constructed” messages, no destructed messages until array is destroyed. */ Foo **pointerToArrayOfPointers = new Foo*[2000]; /* creates an array of pointers on heap. Array persists. Objects not instantiated. No “Constructed”. */

6 Abstract Data Types So far, we have discussed Lists, Vectors, and Stacks All three are abstract data types (ADT) What they contain is irrelevant You’ll see ADT quite a bit in the text and throughout computer science literature.

7 Hybrid List / Vector Can we create a hybrid list and vector? What if we had a list of vectors? Or a vector of lists?

8 Hypothetical vector of lists -(don’t dwell on this for the exam) -Each element in the vector is a linked list of size MAX_SIZE -To access the nth element, get the floor(N/MAX_SIZE) list and the (N%MAX_SIZE) element in that list. -Pros / cons? -Constant time access as in vectors, although have to chase up to MAX_SIZE pointers. -Linear insertion as in vectors, but it will probably be offset by an order of magnitude (only updating N/MAX_SIZE pointers instead of N, but it’s still order N) -Conclusion? Same theoretical running time as vectors, but it may be a compromise if you’re facing a ton of insertions and deletions but you still want fast random access.

9 Stack Recap Last-in, first-out Push, pop, top Can be implemented with either array or linked list Linked list is trivial – just add to the tail, and pop the tail. Array is also trivial – on push, add to size. On pop, return index at size.

10 More stack uses Function calls / recursion Prefix to postfix conversion Language parsing Virtual machine instructions Pancakes!

11 Queues Like a grocery line Usually first-in-first-out. Typically only supports add and remove. (Commonly “enqueue” for add and “dequeue” for remove.) New objects are added to the back Oldest objects are first to leave.

12 Implementing Queues with Linked List Extremely trivial Lists already have head and tail pointer Enqueue – update tail pointer Dequeue – update head pointer

13 Implementing Queues with Arrays Why implement with an array when linked lists are so much easier? – Cache – No pointer chasing Front index, Back index Enqueue updates back index Dequeue updates front index When at capacity, just wrap indices using modulo – (as long as size is always less than capacity)

14 Practical uses for queues Printer Queues File access Any real line – Ticket counter – Grocery – Call center – Waiting list Etc.

15 Deque Double-ended queue Abstract data structure that implements queue Two additional operations: – addFront – removeBack Example: thread scheduling, resource management

16 Trees Has a root node that has no parents Each node may edges for children nodes A node cannot have more than one parent A childless node is called a leaf.

17 Trees A path from root to node N k is defined as N 1, N 2, N 3, … N k-1, N k such that N 1 is a parent of N 2, N 2 is a parent of N 3, and etc. A sibling to a node N k is a node whose parent is the same as N K ’s

18 Trees The depth is the length of the path from root to N k The height is the length of the path from N k to the nearest leaf. Depth(6) = 3 Height(6) = 1

19 Tree Traversal Postfix – Recurse children first, then perform operation on current node. Prefix – Perform operation on current node, then recurse. Infix – Recurse on one or more children, perform operation on current node, finish off recursing rest of children.

20 Tree Traversal Prefix: void processNode(Node *node){ if (node == null) return; cout object <<endl; processNode(node->left); processNode(node->right); } 2, 7, 2, 6, 5, 11, 5, 9, 4

21 Tree Traversal Postfix: void processNode(Node *node){ if (node == null) return; processNode(node->left); processNode(node->right); cout object <<endl; } 2, 5, 11, 6, 7, 4, 9, 5, 2

22 Tree Traversal Infix: void processNode(Node *node){ if (node == null) return; processNode(node->left); cout object <<endl; processNode(node->right); } 2, 7, 5, 6, 11, 2, 5, 4, 9

23 Binary Trees Can have at most two children. Typically have a maximum depth of log(n), where n is the number of nodes in the tree Great for expression parsing

24 Binary Tree applications BSP – Binary Space Partition. Used in graphics A radix “trie” (prefix tree) Abstract Syntax Tree (although not always binary) Heaps (more on this later in the course) Huffman Coding Tree (compression) Binary Search Tree

25 Elements are sorted Simple rule: – Left descendants are less than parent – Right descendants are greater than parent

26 Binary Search Tree FindMin: – Just keep going left! FindMax: – Just keep going right!

27 Binary Search Tree Contains(int someValue): – Recursively navigate until you find it or run into a leaf. – If current node is greater than number, go left. Otherwise go right. If it’s null, return false.

28 Binary Search Tree FindMin, findMax, contains, insert: all typically log(n) Contrast to searching through an unsorted list: you have to look at every element, so order(n)

29 Binary Search Tree Insert: recursively navigate, going left if the current node is greater, going right if current node is less, until you run into a leaf. Then add the node. Ex: Inserting 12 would go under 10’s left child.

30 Binary Search Tree Absolute worst case: Inserting an already sorted list.

31 Binary Search Tree Delete: – Leafs are deleted with no further updates. – Nodes with one child are simply replaced by their child. – Nodes with two children: Typically, just replace the data of this node with smallest data of the right subtree and recursively delete that node (which will only have one child) – Lazy delete: just mark the node as deleted!


Download ppt "CISC 220 - Data Structures Ben Perry University of Delaware Summer 2011."

Similar presentations


Ads by Google