Compsci 201 201 More o e on T Trees es a and d Compu puter - - PowerPoint PPT Presentation

compsci 201 201 more o e on t trees es a and d compu
SMART_READER_LITE
LIVE PREVIEW

Compsci 201 201 More o e on T Trees es a and d Compu puter - - PowerPoint PPT Presentation

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)


slide-1
SLIDE 1

Compsci 201 201 More o e on T Trees es a and d Compu puter er S Scien ence Par art 1 1 of

  • f 4

4/22/2020 Compsci 201, Spring 2020 1

Susan Rodger April 22, 2020

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

  • n

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

Beyond CompSci 201

4/22/2020 Compsci 201, Spring 2020 5

slide-6
SLIDE 6

More on trees

  • General tree

Compsci 201, Spring 2020 6 4/22/2020

slide-7
SLIDE 7

More on trees

  • General tree
  • Implementation

Compsci 201, Spring 2020 7 4/22/2020

slide-8
SLIDE 8

General Tree Node

Compsci 201, Spring 2020 8 4/22/2020

slide-9
SLIDE 9

What else can you do with Trees?

  • I invented a new tree data structure back in

the day….

Compsci 201, Spring 2020 9 4/22/2020

slide-10
SLIDE 10

Graphics leads to careers in Animation, computer vision

4/22/2020 10 Compsci 201, Spring 2020

slide-11
SLIDE 11

Example: Convex Hull problems

4/22/2020 11 Compsci 201, Spring 2020

slide-12
SLIDE 12

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 12 Compsci 201, Spring 2020

slide-13
SLIDE 13

Definition of Maximal Points in a Plane

4/22/2020 13 Compsci 201, Spring 2020

slide-14
SLIDE 14

Points in the (x,y) plane, which are maximal points?

1 2 3 4 5 6 7 8

4/22/2020 14 Compsci 201, Spring 2020

slide-15
SLIDE 15

Deleting point 9

4/22/2020 16 Compsci 201, Spring 2020

slide-16
SLIDE 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 17 Compsci 201, Spring 2020

slide-17
SLIDE 17

Compsci 201 More on Trees and Computer Science Part 2 of 4

4/22/2020 Compsci 201, Spring 2020 18

Susan Rodger April 22, 2020

slide-18
SLIDE 18

Balanced Trees

  • Splay trees
  • AVL trees
  • Red-black trees
  • B-trees

Compsci 201, Spring 2020 19 4/22/2020

slide-19
SLIDE 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

Compsci 201, Spring 2020 20 4/22/2020

slide-20
SLIDE 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.

Compsci 201, Spring 2020 21 4/22/2020

slide-21
SLIDE 21

Example red-black tree

Compsci 201, Spring 2020 22

  • In the figure, black nodes are shaded and red

nodes are non-shaded

  • Check properties

4/22/2020

slide-22
SLIDE 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

Compsci 201, Spring 2020 23 4/22/2020

slide-23
SLIDE 23

Example red-black tree

Compsci 201, Spring 2020 24

  • In the figure, black nodes are shaded and red

nodes are non-shaded

4/22/2020

h(19): bh(19): h(8) bh(8):

slide-24
SLIDE 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:

Compsci 201, Spring 2020 26 4/22/2020

slide-25
SLIDE 25

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

Compsci 201, Spring 2020 28 4/22/2020

slide-26
SLIDE 26

Right Rotate

  • Note the rotations change the pointer structure

while preserving the inorder property.

Compsci 201, Spring 2020 29 4/22/2020

slide-27
SLIDE 27

Example of rotation

Compsci 201, Spring 2020 30 4/22/2020

slide-28
SLIDE 28

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

Compsci 201, Spring 2020 31 4/22/2020

slide-29
SLIDE 29

Cases for Insert

Compsci 201, Spring 2020 32 4/22/2020

slide-30
SLIDE 30

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”

Compsci 201, Spring 2020 33 4/22/2020

slide-31
SLIDE 31

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

Compsci 201, Spring 2020 34 4/22/2020

slide-32
SLIDE 32

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

Compsci 201, Spring 2020 35 4/22/2020

slide-33
SLIDE 33

Example of Insert 4 w/ double rotation

Compsci 201, Spring 2020 36 4/22/2020

slide-34
SLIDE 34

Analysis – Red Black Tree

  • Insert
  • Deletion

Compsci 201, Spring 2020 37 4/22/2020

slide-35
SLIDE 35

WOTO

http://bit.ly/201spring20-0422-1

4/22/2020 Compsci 201, Spring 2020 39

slide-36
SLIDE 36

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

4/22/2020 Compsci 201, Spring 2020 40

It is often easier to ask for forgiveness than to ask for permission.

slide-37
SLIDE 37

Compsci 201 More on Trees and Computer Science Part 3 of 4

4/22/2020 Compsci 201, Spring 2020 41

Susan Rodger April 22, 2020

slide-38
SLIDE 38

What can computers do?

4/22/2020 Compsci 201, Spring 2020 42

slide-39
SLIDE 39

What can computers do?

4/22/2020 Compsci 201, Spring 2020 43

slide-40
SLIDE 40

What can computers do?

4/22/2020 Compsci 201, Spring 2020 44

slide-41
SLIDE 41

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

slide-42
SLIDE 42

Please write this program

4/22/2020 Compsci 201, Spring 2020 46

I guess I'm too dumb 

slide-43
SLIDE 43

Some Better Scenarios

I can't write this program because it’s provably impossible

4/22/2020 Compsci 201, Spring 2020 47

slide-44
SLIDE 44

Some Better Scenarios

I can't write this program because it’s provably impossible I can't write this program but neither can all these famous people

4/22/2020 Compsci 201, Spring 2020 48

slide-45
SLIDE 45

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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

Let’s look at Math now

  • How many N

Nat atur ural n l number ers a are there? 1 1,2,3,… 2,3,…

  • What about Integers, …, -2, -1, 0, 1, 2, …
  • Both infinity, but the same?
  • Wha

hat ab about ut r rational num numbers? ½ ½, , ¾, , and and s so on? n?

  • Wha

hat ab about ut rea eal num l numbers: sqrt(2), p pi

  • Ther

There ar are d deg egrees o

  • f inf

infin inity

  • More reals than the others which are the same
  • If you enjoy degrees of infinity, …

4/22/2020 Compsci 201, Spring 2020 51

slide-48
SLIDE 48

Good and Bad Websites

  • How d

do w we ident ntif ify these?

  • Go to goodwebsites.com
  • Go to badwebsites.com
  • Are b

e both lis h listed o

  • n

n goodwebsite tes. s.com?

4/22/2020 Compsci 201, Spring 2020 52

slide-49
SLIDE 49

How much does that cost?

4/22/2020 Compsci 201, Spring 2020 53

slide-50
SLIDE 50

One step closer to …

  • Is

Is goodwebsi sites. s.com lis listed o

  • n

n goodwebsi sites. s.com

  • Yes, it is, of course it is
  • Wha

hat ab about ut sites es_tha hat_lis list_the hemselv elves es.com

  • Is goodwebsites.com listed on this site?
  • Is badwebsites.com listed on this site?

4/22/2020 Compsci 201, Spring 2020 54

slide-51
SLIDE 51

Contradiction

  • Wha

hat ab about ut t thi his w web ebsite?

  • sites_that_do_not_list_themselves.com
  • Does s

sites_tha hat_do_no not_lis list_them hemselv elves.com l list st itself lf?

  • If so, it doesn't
  • But, if it doesn't, it does
  • Oh no. We cannot have this website, no way.

4/22/2020 Compsci 201, Spring 2020 55

slide-52
SLIDE 52

Infinite Loop Testing

  • Can

an w we e writ ite doesHalt alt as as s specif ecified ied? Suppo ppose s so!

  • Reads a program and an input
  • Like the Java Compiler: reads a program

4/22/2020 Compsci 201, Spring 2020 56

public class ProgramUtils /** * Returns true if progname halts on input, * otherwise returns false (infinite loop) */ public static boolean doesHalt(String progname, String input){ } }

slide-53
SLIDE 53

Self-reference, … please, no

  • Wha

hat ab about ut Confuse.java va ?

  • Specifies program and input, calls doesHalt

4/22/2020 Compsci 201, Spring 2020 57

public class Confuse { public static void main(String[] args){ String prog = "Confuse.java"; if (ProgramUtils.doesHalt(prog,prog)) { while (true) { // do nothing forever } } } }

slide-54
SLIDE 54

Two alternatives, none work

  • Wha

hat if if doesHalt retur urns ns t true? ue? M Meaning ning s stops!

  • If Confuse/confuse stops, then infinite loop, oh no!
  • Wha

hat if if doesHalt retur urns ns f fals lse?

  • What happens in main? Program ends. But …

4/22/2020 Compsci 201, Spring 2020 58

public class Confuse { public static void main(String[] args){ String prog = "Confuse.java"; if (ProgramUtils.doesHalt(prog,prog)) { while (true) { // do nothing forever } } } }

slide-55
SLIDE 55

This was (or is) earth-shattering

  • Alan

lan T Tur uring f fir irst s sho howed t thi his f for programs: 1936 1936

  • Had to formally specify what a program was
  • Also demonstrated by Alonzo Church
  • Can

antor s sho howed R Real eal N Num umbers > > Ratio ionals nals

  • So-called diagonalization, 1891
  • Ridiculed by establishment

4/22/2020 Compsci 201, Spring 2020 59

slide-56
SLIDE 56

Dijkstra's Shortest Path Algorithm

  • Similar

ilar t to BFS, U , Use P PQ not Q Queue ue

  • Only works with positive edge-weights
  • Starting at one vertex, S, find shortest paths to

every other vertex.

  • Work to find path, not just length of path
  • Add a map!
  • Remind

inder: N No effic icient ient w way t to find l longes est p pat ath! h!

4/22/2020 Compsci 201, Spring 2020 60

slide-57
SLIDE 57

WOTO

http:// //bi bit.ly/2 /201spr pring20-04 0422 22-2

4/22/2020 Compsci 201, Spring 2020 61

slide-58
SLIDE 58

Compsci 201 201 More o e on T Trees es a and d Compu puter er S Scien ence Pa Part 4 4 of f 4

4/22/2020 Compsci 201, Spring 2020 62

Susan Rodger April 22, 2020

slide-59
SLIDE 59

Exam 2

  • 93

93 poin ints ts tot

  • tal
  • Media

ian 76 76.5

  • Mean 73

73.55 55

  • Std

td Dev ev 13 13.59 59

  • Reques

uest regrad ades es through S Sunda day, A April 2 il 26

4/22/2020 Compsci 201, Spring 2020 63

slide-60
SLIDE 60

Exam 2 Reflect – 235 responses

  • How muc

uch t tim ime d did id you u spen end p prep eparin ing?

4/22/2020 Compsci 201, Spring 2020 64

slide-61
SLIDE 61

Think you did vs How you did

4/22/2020 Compsci 201, Spring 2020 65

slide-62
SLIDE 62

Was Exam 2 Fair?

4/22/2020 Compsci 201, Spring 2020 66

slide-63
SLIDE 63

How was it taking Exam2 on Gradescope (more difficult -> easier)

4/22/2020 Compsci 201, Spring 2020 67

slide-64
SLIDE 64

I read your comments

4/22/2020 Compsci 201, Spring 2020 68

slide-65
SLIDE 65

Final Exam

  • Exam

am is is Thur Thursday, Apr April 3 30

  • You take it any time during this 24 hour period
  • Once you start, you have 3 hours plus one

additional hour because you are taking it online – so you have 4 hours total.

  • Format will

ill be be simila ilar to to Exa Exam 2 fo forma mat

  • Encoura

rage ge yo you to to take ke the he ex exam am and and g giv ive it it your ur bes est s sho hot

  • Great practice for later CompSci courses
  • Solidifies knowledge in CompSci 201

4/22/2020 Compsci 201, Spring 2020 69

slide-66
SLIDE 66

More Final Exam Details - GradeScope

  • MC, sho

hort ans nswer, a and nd s sho hort co code s segments.

  • You will

ill typ ype in in, or c cli lick o

  • n a

answers

  • Sugges

est: w write c e code e in in s sim imple t text file ile, and nd t the hen copy py pas aste i e it

  • Gradesc

scop

  • pe saves e

each a ans nswer as y s you

  • u g

go.

  • .
  • Lose internet, just connect back in
  • Exam i

m is a about t 2 hour urs l long ng, b but y you h u have 3 e 3 hour urs

  • Plus

us you g u get et o

  • ne e

extra h a hour ur for

  • r l

log

  • gist

stics

  • To

Total time y e you u get et: 4 4 hour urs

  • Those w

with a accommo mmoda datio tions, K Kate te w will ll email il

4/8/2020 Compsci 201, Spring 2020 70

slide-67
SLIDE 67

Final Exam– Honor Code

  • The ex

The exam am is is y your ur w work o

  • nl

nly

  • Use b

e books, o

  • pen

en no notes, c code e you u ha have w writ itten

  • DO NOT

T writ ite c code and and r run it un it in in Int ntelliJ, Jshe hell ll or

  • r
  • ther c

comput uter means ns

  • DO N

NOT S T Sea earch on t n the he web eb f for ans answers to pro roblems ems

  • DO NOT

T Tal alk t to any any hum humans ab about t the he ex exam am d dur uring the he ex exam am p per eriod

  • DO NOT

T Tal alk t to any any hum humans ab about t the he ex exam am af after you u tak ake t the he ex exam am o

  • r before y

you u tak ake t the he ex exam am.

4/8/2020 Compsci 201, Spring 2020 71

slide-68
SLIDE 68

How to Study

  • Covers a

all t topics ics w we have c cover ered ed

  • How
  • w to

to study dy

  • Go over lecture notes
  • Consider 1 MC question from each lecture
  • Programming assignments, APTs
  • Discussion
  • Practice writing code
  • Do a problem again
  • Write code on paper or type in text editor
  • Do APTs you haven’t done yet. OR Redo old ones

4/22/2020 Compsci 201, Spring 2020 72

slide-69
SLIDE 69

Sample MC

HashSet<String> hs and nd TreeSet<String> ts each ch c cont ntai ain n the s e same N e N string

  • ngs. Which

ch o

  • f the

e following best c char arac acter erizes es the r runtime times of prin intin ting a all N l N values from e each set et in in alph phabe beti tical o

  • rde

der?

A.

  • A. For ts

ts this i is O O(N) N) for

  • r hs

hs this his is is O(N) B.

  • B. For ts

ts this i is O O(N) N) for

  • r hs

hs this is O O(N N log

  • g N)

N) C.

  • C. For ts

ts this i is O O(N N log

  • g N)

N) for

  • r hs

hs this i is O(N) N) D.

  • D. For ts

ts this i is O O(N N log

  • g N)

N) for

  • r hs

hs this i is O(N l N log

  • g N)

N)

4/22/2020 Compsci 201, Spring 2020 73

slide-70
SLIDE 70

Learning Objectives

  • Design, develop, debug, test Java program using

standard libraries to efficiently solve problems with real-world components

  • Write programs that effectively implement and use

arrays, maps, linked lists, stacks, queues, trees

  • Evaluate time and space complexities of

algorithms, especially those that scale

  • Apply basic object-oriented design and

programming principles in developing software

4/22/2020 Compsci 201, Spring 2020 75

slide-71
SLIDE 71

Who to Thank…

201 UTAs, TAs and Teaching Assoc Kate Discussion, Helper hours, Piazza, grading…

Compsci 201, Spring 2020 76

slide-72
SLIDE 72

CompSci 201 is over Time for a computer science story…

4/22/2020 Compsci 201, Spring 2020 77

slide-73
SLIDE 73

4/22/2020 Compsci 201, Spring 2020 78

slide-74
SLIDE 74

4/22/2020 Compsci 201, Spring 2020 79

slide-75
SLIDE 75

4/22/2020 Compsci 201, Spring 2020 80

slide-76
SLIDE 76

4/22/2020 Compsci 201, Spring 2020 81

slide-77
SLIDE 77

Recursion has saved the day!

4/22/2020 Compsci 201, Spring 2020 82