Compsci 201 201 More o e on T Trees es a and d Compu puter er S Scien ence Par art 1 1 of of 4 Susan Rodger April 22, 2020 4/22/2020 Compsci 201, Spring 2020 1
X Y Z is for … • XOR • (A || B) && !(A && B) aka A^B • 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 • Zip • Magic number is 0x4b50, “PK” 4/22/2020 Compsci 201, Spring 2020 2
Announcements • APT APT-8 8 due T Tues esday, A April 2 l 21 • Assignment P P6 6 Huf uffman due A ue April il 22 22 • All late work turned in by April 22 (APTs and Asgns) • Except Huffman grace through April 23 • Assig ignm nment nt P P7 Optio ional o nal out – Extra C a Cred edit it! • Can turn in through Sunday night, April 26 • Final l Exam am w will b ill be e on n April il 30 30 – any any t tim ime o on n thi his day • Fill o l out c course e e eval alua uatio ion b n by Sat atur urday 4 4/25 • 75% - 1 extra point added to assignment total • 85% - 2 extra points added to assignment total 4/22/2020 Compsci 201, Spring 2020 3
PfLDOC • Wher here d do you u go from her here in in CompS pSci? ? • More o e on trees ees – gener eral al tree, b balanc nced ed tre ree • What can an we d e do w wit ith computers • So so many things, power of scale • Data, computers, and storage: oh my! • Wha hat c can an't we e do wit ith c h computers? • Today or every day now and forever? • Chess and go: we'll never compete, but now? • Final E l Exam: d : det etails ils 4/22/2020 Compsci 201, Spring 2020 4
Beyond CompSci 201 4/22/2020 Compsci 201, Spring 2020 5
More on trees • General tree 4/22/2020 Compsci 201, Spring 2020 6
More on trees • General tree • Implementation 4/22/2020 Compsci 201, Spring 2020 7
General Tree Node 4/22/2020 Compsci 201, Spring 2020 8
What else can you do with Trees? • I invented a new tree data structure back in the day…. 4/22/2020 Compsci 201, Spring 2020 9
Graphics leads to careers in Animation, computer vision 4/22/2020 Compsci 201, Spring 2020 10
Example: Convex Hull problems 4/22/2020 Compsci 201, Spring 2020 11
Problem: Dynamic Maintenance of 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 12
Definition of Maximal Points in a Plane 4/22/2020 Compsci 201, Spring 2020 13
Points in the (x,y) plane, which are maximal points? 1 4 5 2 8 3 7 6 4/22/2020 Compsci 201, Spring 2020 14
Deleting point 9 4/22/2020 Compsci 201, Spring 2020 16
Presenting the Dynamic Contour 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 17
Compsci 201 More on Trees and Computer Science Part 2 of 4 Susan Rodger April 22, 2020 4/22/2020 Compsci 201, Spring 2020 18
Balanced Trees • Splay trees • AVL trees • Red-black trees • B-trees 4/22/2020 Compsci 201, Spring 2020 19
Red-Black Tree • Invented by Bayr (1972) – (though called them something else) • Robert Tarjan (Turing Award Winner) – noticed the rotations were O(1) • Type of balanced tree – uses color scheme, recoloring and rotations to balance 4/22/2020 Compsci 201, Spring 2020 20
Red-Black Tree • Is a Binary Search Tree • Properties: – Every node is red or black – The root is black – If a node is red, then its children are black – 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 21
Example red-black tree • In the figure, black nodes are shaded and red nodes are non-shaded • Check properties 4/22/2020 Compsci 201, Spring 2020 22
Example • The five properties ensure that no path is more than twice as long as any other path • Def. The height (h) of a node is the length of 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 23
Example red-black tree • In the figure, black nodes are shaded and red nodes are non-shaded h(19): bh(19): h(8) bh(8): 4/22/2020 Compsci 201, Spring 2020 24
Height of Red-Black Tree • Lemma: A red-black tree with n internal nodes has height at most 2 log (n+1) • Operations: – Time for search for x : – Time for min: – Time for list inorder: 4/22/2020 Compsci 201, Spring 2020 26
Rotations • 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 tree by performing a rotation (change some pointers). • Note the inorder traversal in both is: abcde 4/22/2020 Compsci 201, Spring 2020 28
Right Rotate • Note the rotations change the pointer structure while preserving the inorder property. 4/22/2020 Compsci 201, Spring 2020 29
Example of rotation 4/22/2020 Compsci 201, Spring 2020 30
Insertion • 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 31
Cases for Insert 4/22/2020 Compsci 201, Spring 2020 32
Insertion – Case 1 – How to Fix • Case 1 – sibling of parent of x (called y) is red • To fix: recolor three nodes, then fix up new “x” 4/22/2020 Compsci 201, Spring 2020 33
Insertion – Case 2 How to Fix • Sibling of parent of x (call y) is black, x right child • To fix: set x to parent of x and left rotate x, then it becomes a case 3 4/22/2020 Compsci 201, Spring 2020 34
Insertion – Case 3 – How to Fix • Case 3 – sibling of parent of x (call y) is black, x left child • To fix: two recolorings and one right rotate of grandparent of x 4/22/2020 Compsci 201, Spring 2020 35
Example of Insert 4 w/ double rotation 4/22/2020 Compsci 201, Spring 2020 36
Analysis – Red Black Tree • Insert • Deletion 4/22/2020 Compsci 201, Spring 2020 37
WOTO http://bit.ly/201spring20-0422-1 4/22/2020 Compsci 201, Spring 2020 39
Admiral Grace Hopper • One of the first programmers – Harvard Mark 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 40
Compsci 201 More on Trees and Computer Science Part 3 of 4 Susan Rodger April 22, 2020 4/22/2020 Compsci 201, Spring 2020 41
What can computers do? 4/22/2020 Compsci 201, Spring 2020 42
What can computers do? 4/22/2020 Compsci 201, Spring 2020 43
What can computers do? 4/22/2020 Compsci 201, Spring 2020 44
What can you do? • Not e everyone w ne wants t to be a software e e engine ineer • Diplomat, lawyer, physician, entrepreneur, • Musician, teacher, data scientist, … • Pro roblems ms w with p pro rogra grams ms, p pro roblems ms w with p people • UI, UX, PM, SWE • Wha hat you u kno now and and w wha hat you u kno now ho how to do • More of the latter, perhaps 4/22/2020 Compsci 201, Spring 2020 45
Please write this program I guess I'm too dumb 4/22/2020 Compsci 201, Spring 2020 46
Some Better Scenarios I can't write this program because it ’ s provably impossible 4/22/2020 Compsci 201, Spring 2020 47
Some Better Scenarios I can't write this I can't write this program but neither program because can all these famous it ’ s provably people impossible 4/22/2020 Compsci 201, Spring 2020 48
Solving Problems • Some p e problem lems can annot b be e solv lved a at all all • One program detects all infinite loops • Some p e problem lems canno nnot b be solved ed ef efficie icient ntly • Listing all N-bit sequences of 0's and 1's • Some p e problem lems h have a approxim imate e solut lutio ions ns • Siri: not exact, but close or good enough 4/22/2020 Compsci 201, Spring 2020 49
What's a Hard Problem? • Efficie icient nt solut lutio ions ns? S Some y yes, s , some n e no, s some … … • We don't know, but if we found one? • We'd solve many, many unknown ones • Clay P Prize: do does P P == NP? P? • Efficient solutions versus guess and check • Theoretical aspects of computer science 4/22/2020 Compsci 201, Spring 2020 50
Recommend
More recommend