X Y Z is for … Compsci 201 More on Trees and Computer Science • XOR • (A || B) && !(A && B) aka A^B Part 1 of 4 • XML • Extensible Markup Language • Yesterday's JSON • Y-Combinator • https://www.ycombinator.com/ • YouTube • Scale made/makes it work • Zero • There are two bits in the universe, or 10 Susan Rodger • Zip April 22, 2020 • Magic number is 0x4b50, “PK” 4/22/2020 Compsci 201, Spring 2020 1 4/22/2020 Compsci 201, Spring 2020 2 Announcements PfLDOC • APT-8 due Tuesday, April 21 • Where do you go from here in CompSci? • Assignment P6 Huffman due April 22 • More on trees – general tree, balanced tree • All late work turned in by April 22 (APTs and • What can we do with computers Asgns) • So so many things, power of scale • Except Huffman grace through April 23 • Data, computers, and storage: oh my! • Assignment P7 Optional out – Extra Credit! • What can't we do with computers? • Can turn in through Sunday night, April 26 • Today or every day now and forever? • Final Exam will be on April 30 – any time on this day • Chess and go: we'll never compete, but now? • Fill out course evaluation by Saturday 4/25 • Final Exam: details • 75% - 1 extra point added to assignment total • 85% - 2 extra points added to assignment total 4/22/2020 Compsci 201, Spring 2020 3 4/22/2020 Compsci 201, Spring 2020 4
More on trees Beyond CompSci 201 • General tree 4/22/2020 Compsci 201, Spring 2020 5 4/22/2020 Compsci 201, Spring 2020 6 More on trees General Tree Node • General tree • Implementation 4/22/2020 Compsci 201, Spring 2020 7 4/22/2020 Compsci 201, Spring 2020 8
Graphics leads to careers in Animation, What else can you do with Trees? computer vision • I invented a new tree data structure back in the day…. 4/22/2020 Compsci 201, Spring 2020 9 4/22/2020 Compsci 201, Spring 2020 10 Problem: Dynamic Maintenance of Example: Convex Hull problems Maximal Points in a Plane • Points in the x-y plane • We will calculate which points are maximal • As the points come and go, we want to be able to quickly list out the maximal points (dynamically) 4/22/2020 Compsci 201, Spring 2020 11 4/22/2020 Compsci 201, Spring 2020 12
Points in the (x,y) plane, which are Definition of Maximal Points in a Plane maximal points? 1 4 5 2 8 3 7 6 4/22/2020 Compsci 201, Spring 2020 13 4/22/2020 Compsci 201, Spring 2020 14 Presenting the Dynamic Contour Deleting point 9 Search Tree • Here is the data structure I invented to solve this problem • Insertions: O(log n) • Deletions: O((log n) 2 ) • List m maximal points: O(m) 4/22/2020 Compsci 201, Spring 2020 16 4/22/2020 Compsci 201, Spring 2020 17
Compsci 201 Balanced Trees More on Trees and Computer Science Part 2 of 4 • Splay trees • AVL trees • Red-black trees • B-trees Susan Rodger April 22, 2020 4/22/2020 Compsci 201, Spring 2020 18 4/22/2020 Compsci 201, Spring 2020 19 Red-Black Tree Red-Black Tree • Invented by Bayr (1972) – (though called them • Is a Binary Search Tree something else) • Properties: • Robert Tarjan (Turing Award Winner) – noticed – Every node is red or black the rotations were O(1) – The root is black • Type of balanced tree – uses color scheme, – If a node is red, then its children are black recoloring and rotations to balance – Every leaf is a null node and black (external node) – Every simple path from a node to a descendant leaf contains the same number of black nodes. 4/22/2020 Compsci 201, Spring 2020 20 4/22/2020 Compsci 201, Spring 2020 21
Example red-black tree Example • The five properties ensure that no path is • In the figure, black nodes are shaded and red more than twice as long as any other path nodes are non-shaded • Def. The height (h) of a node is the length of • Check properties the longest path from the node (downward) to a leaf (including external nodes). • Def. The black height (bh) of a node x is the number of black nodes on any path from x (not including x) to a leaf 4/22/2020 Compsci 201, Spring 2020 22 4/22/2020 Compsci 201, Spring 2020 23 Example red-black tree Height of Red-Black Tree • In the figure, black nodes are shaded and red • Lemma: A red-black tree with n nodes are non-shaded internal nodes has height at h(19): most 2 log (n+1) bh(19): h(8) • Operations: bh(8): – Time for search for x : – Time for min: – Time for list inorder: 4/22/2020 Compsci 201, Spring 2020 24 4/22/2020 Compsci 201, Spring 2020 26
Rotations Right Rotate • We want to perform insertions and deletions in O(log n) time. Adding or deleting a node may disrupt one of its properties, so in addition to some recolorings, we may also have to restructure the • Note the rotations change the pointer structure tree by performing a rotation (change some pointers). while preserving the inorder property. • Note the inorder traversal in both is: abcde 4/22/2020 Compsci 201, Spring 2020 28 4/22/2020 Compsci 201, Spring 2020 29 Example of Insertion rotation • Insert node as RED using a binary search tree insert – Means insert as a Red leaf with two black NULL nodes • Then fix-up so that properties still hold – Recoloring and/or 1-2 rotations • Several cases to consider 4/22/2020 Compsci 201, Spring 2020 30 4/22/2020 Compsci 201, Spring 2020 31
Insertion – Case 1 – How to Fix Cases • Case 1 – sibling of parent of x (called y) is red for Insert • To fix: recolor three nodes, then fix up new “x” 4/22/2020 Compsci 201, Spring 2020 32 4/22/2020 Compsci 201, Spring 2020 33 Insertion – Case 2 How to Fix Insertion – Case 3 – How to Fix • Case 3 – sibling of parent of x (call y) is black, x • Sibling of parent of x (call y) is black, x right child left child • To fix: set x to parent of x and left rotate x, then • To fix: two recolorings and one right rotate of it becomes a case 3 grandparent of x 4/22/2020 Compsci 201, Spring 2020 34 4/22/2020 Compsci 201, Spring 2020 35
Analysis – Red Black Tree Example of Insert 4 w/ double rotation • Insert • Deletion 4/22/2020 Compsci 201, Spring 2020 36 4/22/2020 Compsci 201, Spring 2020 37 WOTO Admiral Grace Hopper • One of the first programmers – Harvard Mark 1 http://bit.ly/201spring20-0422-1 • PhD Math at Yale • Admiral in the Navy • On Letterman show • https://youtu.be/lGTEUtS5H7I • Gave out nanoseconds – Wire 11.8 inches long It is often easier to ask for forgiveness than to ask for permission. 4/22/2020 Compsci 201, Spring 2020 39 4/22/2020 Compsci 201, Spring 2020 40
Compsci 201 What can computers do? More on Trees and Computer Science Part 3 of 4 Susan Rodger April 22, 2020 4/22/2020 Compsci 201, Spring 2020 41 4/22/2020 Compsci 201, Spring 2020 42 What can computers do? What can computers do? 4/22/2020 Compsci 201, Spring 2020 43 4/22/2020 Compsci 201, Spring 2020 44
What can you do? Please write this program • Not everyone wants to be a software engineer I guess I'm • Diplomat, lawyer, physician, entrepreneur, too dumb � • Musician, teacher, data scientist, … • Problems with programs, problems with people • UI, UX, PM, SWE • What you know and what you know how to do • More of the latter, perhaps 4/22/2020 Compsci 201, Spring 2020 45 4/22/2020 Compsci 201, Spring 2020 46 Some Better Scenarios Some Better Scenarios I can't write this I can't write this I can't write this program but neither program because program because can all these famous it � s provably it � s provably people impossible impossible 4/22/2020 Compsci 201, Spring 2020 47 4/22/2020 Compsci 201, Spring 2020 48
Solving Problems What's a Hard Problem? • Some problems cannot be solved at all • Efficient solutions? Some yes, some no, some … • One program detects all infinite loops • We don't know, but if we found one? • We'd solve many, many unknown ones • Some problems cannot be solved efficiently • Listing all N-bit sequences of 0's and 1's • Clay Prize: does P == NP? • Efficient solutions versus guess and check • Some problems have approximate solutions • Theoretical aspects of computer science • Siri: not exact, but close or good enough 4/22/2020 Compsci 201, Spring 2020 49 4/22/2020 Compsci 201, Spring 2020 50 Let’s look at Math now Good and Bad Websites • How many Natural numbers are there? 1,2,3,… • How do we identify these? • What about Integers, …, -2, -1, 0, 1, 2, … • Go to goodwebsites.com • Both infinity, but the same? • Go to badwebsites.com • What about rational numbers? ½, ¾, and so on? • What about real numbers: sqrt(2), pi • Are both listed on goodwebsites.com? • There are degrees of infinity • More reals than the others which are the same • If you enjoy degrees of infinity, … 4/22/2020 Compsci 201, Spring 2020 51 4/22/2020 Compsci 201, Spring 2020 52
Recommend
More recommend