Upcoming ACM Events Linux Crash Course Date: Oct. 2nd and 3rd Time: 1:15 pm - 3:15 pm Location: UW1-210 (10/02) and UW1-221 (10/03) Weekly Crack the Coding Interview Date: Weekly Fridays from Oct. 5th - Nov. 16th Time: 11:00 am - 1:00 pm Location: DISC-162 Weekly IOT Crash Course Date: Weekly Fridays from Oct. 5th - Nov. 9th Time: 1:30 pm - 3:30 pm Location: Makerspace Professional Development Workshops Date: Oct. 8th, Oct. 11th, and Oct. 12th Time: 6:00 pm - 8:00 pm Location: UW1-061 1
CSS 343 Data Structures, Algorithms, and Discrete Math II Lecture 1 Introduction Yusuf Pisan
Overview 1. Course Structure http://courses.washington.edu/css343/pisan/ 2. 342 Review 3. Trees 4. Huffman Encoding 5. Arithmetic Expression Trees 6. Assignment-1 3
4
342 Review 1. C++ classes, overloaded operators 2. Pointers & References 3. Array and link-based ADT implementations 4. Templates - for classes and functions 5. Recursion 6. Mathematical induction 7. Boolean algebra 8. Stack 9. List 10. Queue 11. Algorithm complexity - O(1), O(n), O(n log n),... 12. Trees - Binary trees, Binary Search Trees 5
Complexity? int foo(int n) { if (n == 0) return 1; else return n * foo(n - 1); } int mystery(const int anarray[], int first, int last, int target) { int index; if (first > last) index = - 1; else { int mid = first + (last - first) / 2; if (target == anarray[mid]) index = mid; else if (target < anarray[mid]) index = mystery(anarray, first, mid - 1, target); else index = mystery(anarray, mid + 1, last, target); } return index; } template<class ItemType> bool Grouch<ItemType>::mumble(const ItemType& newentry) { Node<ItemType>* nextnodeptr = new Node<ItemType>(); nextnodeptr->setitem(newentry); nextnodeptr->setnext( headptr); headptr = nextnodeptr; itemcount++; return true; } 6
Trees Single root, hierarchical - more than one immediate successor nodes, edges, leaves child, parent, sibling, ancestor, descendant subtree general, n-ary, binary tree height (root at level 1, empty tree has height 0) perfect or full - all nodes (except leaves) have 2 children, all leaves at same level complete - every level except last is filled AND the nodes at last level are as left as possible balanced - the height of the subtrees do not differ more than one degenerate - all nodes have 1 child 7
Why Trees? Good for searching when there is both add/delete operations. Used internally for libraries Games use Binary Space Partition, render only what is visible from front to back Radix Trees for IP routing Huffman Coding Tree for compression GGM Trees for generating random numbers Merkle Tree for cryptography Syntax Tree for parsing languages B-Tree for databases Directory structures, web pages, anything hierarchical 8
Binary Tree Traversal Visit all the nodes Preorder - root, left subtree, right subtree 1, 2, 4, 5, 3, 6, 7 Inorder - left subtree, root, right subtree 4, 2, 5, 1, 6, 3, 7 Postorder - left subtree, right subtree, root 4, 5, 2, 6, 7, 3, 1 9
Trees Exercise 1. What is the minimum height of a binary tree given the number of nodes: 0, 1, 2, 3, 4, 7, 8, 16, 32, 128, 1024? 2. What is the maximum height of a binary tree given the number of nodes above? 3. Preorder, Inorder, Postorder traversal for the tree given 4. Is the given tree full, complete, balanced, degenerate or neither? 5. Draw a binary tree that is a. full b. complete, but not full c. balanced, but not complete d. degenerate 10
Compression How do we compress a text file? Uncompressed file: 8 bits per character x Number of Characters A - 65-0100 0001 B - 66-0100 0010... Z - 90-0101 0000... a - 97-0110 0001 Characters are repeated, some are more frequent than others Words, sentences and paragraphs are repeated 11
Huffman Encoding Prefix codes - the bit string for a letter never occurs as the first part of the bit string for another letter 1. Each letter is a tree with a weight as its frequency 2. Combine the trees with the lowest weight, creating a tree with a higher weight 3. Repeat step-2 until we get a single tree 4. Label left edges with "0", right edges with "1" to get the encoding for each letter gzip uses LZ77 and Huffman coding, lots of details at http://www.infinitepartitions.com/art001.html and https://stackoverflow.com/questions/20762094/how-are-zlib-gzip-and-zip-related-what-do-they-have-in-com mon-and-how-are-they 12
Final encoding A: 111 B: 110 C: 011 D: 010 E: 10 F: 00 Decode these 111 110111010 110101000 00111010 The average number of bits used to encode a symbol using this encoding is 3 * 0.08 + 3 * 0.10 + 3 * 0.12 + 3 * 0.15 + 2 * 0.20 + 2 * 0.35 = 2.45. 13
Huffman Exercise 1. Use Huffman coding to encode these symbols with given frequencies and the average number of bits required to encode a character 1.1. a: 0.20, b: 0.10, c: 0.15, d: 0.25, e: 0.30. 1.2. A: 0.10, B: 0.25, C: 0.05, D: 0.15, E: 0.30, F: 0.07, G: 0.08. 2. Construct two different Huffman codes for these symbols and frequencies: t : 0.2, u: 0.3, v: 0.2, w: 0.3. 3. Use Huffman coding to encode these symbols with frequencies a: 0.4, b: 0.2, c: 0.2, d: 0.1, e: 0.1 in two different ways by breaking ties in the algorithm differently. 3.1. Select two trees with the largest number of vertices 3.2. Select two trees with the smallest number of vertices 14
Arithmetic Expression as Trees Inorder traversal gives infix form: ((x + y) 2) + ((x 4)/3 but needs parenthesis Preorder traversal gives prefix or polish notation: + + x y 2 / x 4 3 easy to parse, evaluate right to left when operator found, apply it to the operands and substitute result see https://en.wikipedia.org/wiki/polish_notation for example 15
Exercise: Arithmetic Expressions 1. Evaluate prefix expression + 2 3 5/ 2 3 4? 2. Postorder gives us postfix (reverse polish notation). What is the postfix form of ((x + y) 2) + ((x 4)/3)? 3. What is the value of the postfix expression 7 2 3 4 9 3/+ Evaluate it from left-to-right 4. Draw the corresponding trees a. + + 5 3 2 1 4 b. + 2 3 5 1 c. / 9 3 + 2 4 7 6 16
Assignment-1 17
Read Chapter-15: Trees Before Next Class Complete Canvas Quiz on Chapter-15 Implement constructors, operator<<, operator== and operator!= Write test cases Test your program on CSS Linux Lab Use valgrind to check for memory leaks Review lecture notes 18