[ { "text": " ", "start": 0.0, "duration": 17.255 }, { "text": "SPEAKER 1: All right, this is\nCS50 and this is week five.", "start": 17.255, "duration": 3.685 }, { "text": "And let's take a look at\nwhere we left off last time.", "start": 20.94, "duration": 2.62 }, { "text": "You may recall this guy here,\nBinky from our friends at Stanford.", "start": 23.56, "duration": 4.14 }, { "text": "And we used Binky to start\ntalking about pointers.", "start": 27.7, "duration": 3.25 }, { "text": "What is a pointer?", "start": 30.95, "duration": 1.209 }, { "text": "So, a pointer is just\nan address, the location", "start": 32.159, "duration": 2.336 }, { "text": "of some piece of data in memory,\nbecause recall at the end of the day", "start": 34.495, "duration": 2.875 }, { "text": "your computer just has a few pieces\nof hardware inside of it, one of which", "start": 37.37, "duration": 4.36 }, { "text": "is RAM or Random Access Memory.", "start": 41.73, "duration": 2.18 }, { "text": "And in RAM you have the ability to\nstore bunches and bunches of bytes,", "start": 43.91, "duration": 4.04 }, { "text": "or kilobytes, or\nmegabytes, or gigabytes,", "start": 47.95, "duration": 2.117 }, { "text": "depending on how much memory you have.", "start": 50.067, "duration": 1.583 }, { "text": "And if you assume that no\nmatter how much RAM you have you", "start": 51.65, "duration": 3.41 }, { "text": "can enumerate the bytes-- this is byte\n0, this is byte 1, this is byte 2,", "start": 55.06, "duration": 4.06 }, { "text": "and so forth-- you can give each of\nthe bytes of your computer's memory", "start": 59.12, "duration": 3.19 }, { "text": "an address and those addresses\nare simply called pointers.", "start": 62.31, "duration": 3.149 }, { "text": "And now in C we have the\nability to use pointers", "start": 65.459, "duration": 3.471 }, { "text": "both to go to any location\nin memory that we want", "start": 68.93, "duration": 3.5 }, { "text": "and even to dynamically allocate\nmemory in case we don't necessarily", "start": 72.43, "duration": 3.43 }, { "text": "know a priori how much memory\nwe might need for a program.", "start": 75.86, "duration": 4.177 }, { "text": "Now, in terms of your\ncomputer's RAM, recall", "start": 80.037, "duration": 1.833 }, { "text": "that we divided the\nworld into this picture", "start": 81.87, "duration": 2.0 }, { "text": "here whereby if this rectangular region,\narbitrarily, represents your computer's", "start": 83.87, "duration": 5.01 }, { "text": "memory, here is how the\ncomputer divvies it up", "start": 88.88, "duration": 2.02 }, { "text": "when you're actually using a program.", "start": 90.9, "duration": 2.37 }, { "text": "At the bottom of your computer's area\nof memory, you have the so-called stack.", "start": 93.27, "duration": 4.31 }, { "text": "And recall that the stack is where\nany time you call a function,", "start": 97.58, "duration": 3.17 }, { "text": "it gets a slice of\nmemory-- a frame of memory,", "start": 100.75, "duration": 2.51 }, { "text": "if you will-- for all of its local\nvariables, all of its arguments", "start": 103.26, "duration": 2.84 }, { "text": "and anything else that it might need.", "start": 106.1, "duration": 1.69 }, { "text": "On top of that might go another\nslice or frame of memory", "start": 107.79, "duration": 3.14 }, { "text": "if that first function calls another.", "start": 110.93, "duration": 1.93 }, { "text": "And if that second function in\nturn calls another function,", "start": 112.86, "duration": 2.56 }, { "text": "you might have a third\nframe on the stack.", "start": 115.42, "duration": 2.064 }, { "text": "Of course, this doesn't end well if you\nkeep calling function after function", "start": 117.484, "duration": 3.166 }, { "text": "after function after function.", "start": 120.65, "duration": 1.2 }, { "text": "And so, hopefully you don't accidentally\ninduce some kind of infinite loop", "start": 121.85, "duration": 3.13 }, { "text": "such that these frames pile on\ntop of each other infinitely", "start": 124.98, "duration": 3.08 }, { "text": "many times, because eventually they'll\nrun the risk of hitting the heap.", "start": 128.06, "duration": 3.39 }, { "text": "Now, the heap is the same\ntype of physical memory.", "start": 131.45, "duration": 2.1 }, { "text": "You're just using it in\na slightly different way.", "start": 133.55, "duration": 2.59 }, { "text": "The heap is used any time you want\nto dynamically allocate memory,", "start": 136.14, "duration": 3.95 }, { "text": "when you don't know in\nadvance how many bytes", "start": 140.09, "duration": 2.23 }, { "text": "you need but you do know once the\nprogram is running how many you now", "start": 142.32, "duration": 2.95 }, { "text": "want.", "start": 145.27, "duration": 0.57 }, { "text": "You can ask via functions\nlike malloc the operating", "start": 145.84, "duration": 3.13 }, { "text": "system for some number\nof bytes, and those bytes", "start": 148.97, "duration": 2.26 }, { "text": "are allocated from the heap.", "start": 151.23, "duration": 1.78 }, { "text": "So, those two have addresses or numbers.", "start": 153.01, "duration": 1.862 }, { "text": "And so, the operating\nsystem, by way of malloc,", "start": 154.872, "duration": 1.958 }, { "text": "just figures out which of\nthose bytes are not yet", "start": 156.83, "duration": 2.041 }, { "text": "being used so that you can\nnow put whatever piece of data", "start": 158.871, "duration": 3.229 }, { "text": "you have in that particular place.", "start": 162.1, "duration": 1.972 }, { "text": "Now, beyond that [? appear ?]\nthings like initialized data,", "start": 164.072, "duration": 2.458 }, { "text": "uninitialized data.", "start": 166.53, "duration": 0.91 }, { "text": "That's where things like global\nvariables that are initialized or not", "start": 167.44, "duration": 3.26 }, { "text": "end up that might be outside\nof your main function.", "start": 170.7, "duration": 2.36 }, { "text": "And then above that is the\nso-called text segment,", "start": 173.06, "duration": 2.12 }, { "text": "which are these zeros and ones\nthat actually compose your program.", "start": 175.18, "duration": 3.75 }, { "text": "So when you double click an\nicon on Windows or Mac OS", "start": 178.93, "duration": 3.33 }, { "text": "to run a program or you type dot slash\nsomething in the Linux command line", "start": 182.26, "duration": 3.99 }, { "text": "environment in order to run a program,\nthe bits that compose your program", "start": 186.25, "duration": 3.83 }, { "text": "are loaded also into memory\nup into this region here.", "start": 190.08, "duration": 4.1 }, { "text": "So, at the end of the day, you have\naccess to just pretty generic memory,", "start": 194.18, "duration": 3.92 }, { "text": "but we use it in these different ways.", "start": 198.1, "duration": 1.74 }, { "text": "And it allows us to\nultimately solve problems", "start": 199.84, "duration": 2.88 }, { "text": "that we might not have\nbeen able to in the past.", "start": 202.72, "duration": 2.646 }, { "text": "Recall for instance this example here,\ndeliberately shown in red because it", "start": 205.366, "duration": 3.124 }, { "text": "was [? buggy. ?] This does not work.", "start": 208.49, "duration": 2.09 }, { "text": "Now, logically, it does do the swap that\nwe intend whereby a goes into b and b", "start": 210.58, "duration": 6.24 }, { "text": "goes into a.", "start": 216.82, "duration": 0.74 }, { "text": "And we achieve that result by\nway of this temporary variable", "start": 217.56, "duration": 2.5 }, { "text": "so that we have a temporary placeholder\ninto which to store one of those values", "start": 220.06, "duration": 4.14 }, { "text": "while doing the swap.", "start": 224.2, "duration": 1.31 }, { "text": "But it had no permanent impact on the\ntwo variables that were passed into it.", "start": 225.51, "duration": 5.02 }, { "text": "And that was because by default in C any\ntime you pass arguments to a function,", "start": 230.53, "duration": 3.46 }, { "text": "those arguments are passed\nso to speak, by value.", "start": 233.99, "duration": 2.83 }, { "text": "You get copies of those values\nbeing passed into a function.", "start": 236.82, "duration": 3.49 }, { "text": "And so, if main, for instance,\nhas two variables, x and y--", "start": 240.31, "duration": 3.29 }, { "text": "as they did last time--\nand you pass x and y", "start": 243.6, "duration": 2.19 }, { "text": "into a function like this\none here swap, x and y", "start": 245.79, "duration": 3.16 }, { "text": "are going to get copied\nas a and b respectively.", "start": 248.95, "duration": 2.75 }, { "text": "So you might perfectly,\nlogically, correctly swap a and b,", "start": 251.7, "duration": 3.63 }, { "text": "but you're having no permanent\nimpact on x and y themselves.", "start": 255.33, "duration": 5.129 }, { "text": "But what if, per this\ngreen version here,", "start": 260.459, "duration": 3.071 }, { "text": "we reimplement swap to be\na little more complicated", "start": 263.53, "duration": 3.15 }, { "text": "looking, but at the end of\nthe day actually correct?", "start": 266.68, "duration": 3.47 }, { "text": "Notice now we've declared\na and b not to be", "start": 270.15, "duration": 2.38 }, { "text": "integers but to be pointers to\nintegers, the addresses of integers.", "start": 272.53, "duration": 4.61 }, { "text": "And that's what's implied by\nthe star that we're putting", "start": 277.14, "duration": 2.51 }, { "text": "right there before the variable's name.", "start": 279.65, "duration": 1.89 }, { "text": "Meanwhile, inside of the\nbody of this function,", "start": 281.54, "duration": 2.004 }, { "text": "we still have three lines of code.", "start": 283.544, "duration": 1.416 }, { "text": "And we're still using a temporary\nvariable, and that in itself", "start": 284.96, "duration": 2.86 }, { "text": "is not a pointer.", "start": 287.82, "duration": 0.73 }, { "text": "It's just an integer\nas before, but notice", "start": 288.55, "duration": 2.44 }, { "text": "we're using this star notation\nagain, albeit for a different purpose", "start": 290.99, "duration": 3.18 }, { "text": "to actually dereference these pointers.", "start": 294.17, "duration": 2.58 }, { "text": "Recall that int star a and int star\nb means give me a variable that", "start": 296.75, "duration": 5.4 }, { "text": "can store the address of an integer.", "start": 302.15, "duration": 1.86 }, { "text": "That's declaring a pointer.", "start": 304.01, "duration": 1.81 }, { "text": "Meanwhile, if you just say star\na without declaring something", "start": 305.82, "duration": 3.8 }, { "text": "to the left of it with\na data type like int,", "start": 309.62, "duration": 1.99 }, { "text": "you're saying go to the\naddress that is in a.", "start": 311.61, "duration": 3.02 }, { "text": "So if a is an address,\nstar a is at that address,", "start": 314.63, "duration": 4.02 }, { "text": "which of course per its declaration\nis going to be an integer.", "start": 318.65, "duration": 3.11 }, { "text": "Similarly, star b means\ngo to the address in b.", "start": 321.76, "duration": 2.4 }, { "text": "Star a means go to the address in a\nand put the former into the latter,", "start": 324.16, "duration": 5.35 }, { "text": "ultimately putting the value of temp at\nthe address in b-- so absolutely more", "start": 329.51, "duration": 4.36 }, { "text": "complicated at first glance,\nbut if you consider again", "start": 333.87, "duration": 2.25 }, { "text": "the first principles of what's\ngoing on here, all we are doing", "start": 336.12, "duration": 3.31 }, { "text": "are moving things around in memory.", "start": 339.43, "duration": 3.027 }, { "text": "And we can do that now\nbecause we have the ability", "start": 342.457, "duration": 2.083 }, { "text": "to express the locations, the\nnumeric locations of where", "start": 344.54, "duration": 2.91 }, { "text": "things are in memory.", "start": 347.45, "duration": 1.05 }, { "text": "But nicely enough, we,\nthe programmer, don't have", "start": 348.5, "duration": 2.28 }, { "text": "to care where things are in memory.", "start": 350.78, "duration": 1.97 }, { "text": "We can access things symbolically\nas we're doing here with a and b.", "start": 352.75, "duration": 4.33 }, { "text": "So even though we might\nhave seen on the screen", "start": 357.08, "duration": 2.18 }, { "text": "or you might see while debugging\nactual addresses of memory,", "start": 359.26, "duration": 3.37 }, { "text": "rarely does that actually\nmatter in practice.", "start": 362.63, "duration": 3.02 }, { "text": "We can deal with everything we've\nlearned thus far symbolically.", "start": 365.65, "duration": 3.58 }, { "text": "Now, last time we also took a\nlook at the world of forensics,", "start": 369.23, "duration": 3.57 }, { "text": "and we took a look at how images are\nimplemented and specifically file", "start": 372.8, "duration": 3.1 }, { "text": "formats like BNP, and JPEG,\nand GIF, and yet others.", "start": 375.9, "duration": 3.61 }, { "text": "And we glanced into [? Asmila's ?]\nhere as we tried to enhance this image,", "start": 379.51, "duration": 4.13 }, { "text": "but of course, there was only\nfinite amount of information.", "start": 383.64, "duration": 3.64 }, { "text": "So, what you see is what you get\nin terms of any kind of glint", "start": 387.28, "duration": 3.07 }, { "text": "or suspect in her eyes.", "start": 390.35, "duration": 1.91 }, { "text": "But we did this in part\nso that we could also", "start": 392.26, "duration": 2.1 }, { "text": "introduce another feature of C that\nallows us to declare our own data", "start": 394.36, "duration": 4.06 }, { "text": "types, indeed our own data structures.", "start": 398.42, "duration": 2.697 }, { "text": "For instance, we proposed\nthat if you wanted", "start": 401.117, "duration": 1.833 }, { "text": "to write a program\nthat stores a student,", "start": 402.95, "duration": 3.1 }, { "text": "you could actually declare\nyour own student data type", "start": 406.05, "duration": 2.93 }, { "text": "inside of which is a name and\ninside of which is a dorm,", "start": 408.98, "duration": 3.87 }, { "text": "and anything else that\nyou might actually want.", "start": 412.85, "duration": 2.15 }, { "text": "Meanwhile, this syntax here gives\nus a new data type called student", "start": 415.0, "duration": 4.35 }, { "text": "so that if we want to write a\nprogram that implements students,", "start": 419.35, "duration": 2.73 }, { "text": "we can actually wrap related\ninformation together like name and dorm", "start": 422.08, "duration": 4.18 }, { "text": "without having to maintain\na whole bunch of strings", "start": 426.26, "duration": 2.872 }, { "text": "for just names and a whole\nbunch of strings for just dorms.", "start": 429.132, "duration": 2.458 }, { "text": "We can actually encapsulate things\nall inside of one structure.", "start": 431.59, "duration": 3.69 }, { "text": "And indeed encapsulation is another\nprinciple of computer science", "start": 435.28, "duration": 3.09 }, { "text": "that you'll see throughout program\nand throughout the field itself.", "start": 438.37, "duration": 3.98 }, { "text": "So, what do we now do this time?", "start": 442.35, "duration": 2.38 }, { "text": "So, today we introduce more\nsophisticated ingredients", "start": 444.73, "duration": 4.07 }, { "text": "with which we can solve problems and\nwe revisit a problem from the past", "start": 448.8, "duration": 3.7 }, { "text": "that we thought we had rather\nknocked off and had solved.", "start": 452.5, "duration": 2.86 }, { "text": "So, this might represent\na whole bunch of names,", "start": 455.36, "duration": 2.91 }, { "text": "a whole bunch of numbers, a whole bunch\nof telephone numbers in a phone book", "start": 458.27, "duration": 3.87 }, { "text": "back to back to back to\nback stored in this case", "start": 462.14, "duration": 2.67 }, { "text": "in the form of an array, the simplest\nof data structure, so to speak,", "start": 464.81, "duration": 3.11 }, { "text": "that we've discussed thus far.", "start": 467.92, "duration": 1.25 }, { "text": "And an array, again, is a contiguous\nblock of memory each of whose element--", "start": 469.17, "duration": 4.5 }, { "text": "typically are of the same data type,\nintegers, or strings, or the like--", "start": 473.67, "duration": 3.82 }, { "text": "and they are by definition\nback to back to back to back,", "start": 477.49, "duration": 3.03 }, { "text": "which allows you random access.", "start": 480.52, "duration": 1.56 }, { "text": "Which means you can jump\nto any of these locations", "start": 482.08, "duration": 2.083 }, { "text": "instantly just by using in C\nthat square bracket notation", "start": 484.163, "duration": 2.957 }, { "text": "or as we saw last time\nusing pointer arithmetic,", "start": 487.12, "duration": 3.46 }, { "text": "actually using the star operator\nand maybe adding some number two", "start": 490.58, "duration": 3.29 }, { "text": "and address to get at\nsome subsequent address.", "start": 493.87, "duration": 3.44 }, { "text": "But it turns out there's a few problems\nwith this fundamental approach.", "start": 497.31, "duration": 4.01 }, { "text": "Nice and as simple as it is, it would\nseem that we rather paint ourselves", "start": 501.32, "duration": 4.13 }, { "text": "into a corner with this approach.", "start": 505.45, "duration": 1.96 }, { "text": "This array has 1, 2, 3, 4, 5, 6 total\nelements, at least as depicted here.", "start": 507.41, "duration": 5.86 }, { "text": "So that's fine if you want\nto insert a number, and then", "start": 513.27, "duration": 2.36 }, { "text": "another number, and\nthen four more numbers.", "start": 515.63, "duration": 1.791 }, { "text": "But what if you want to then\ninsert a seventh number,", "start": 517.421, "duration": 2.636 }, { "text": "not to mention an eighth number\nor a ninth number or the like?", "start": 520.057, "duration": 2.583 }, { "text": "Well, where do you put them?", "start": 522.64, "duration": 1.14 }, { "text": "Well, you might think,\nwell, that's fine.", "start": 523.78, "duration": 1.708 }, { "text": "I'm just going to go put the\nseventh number over here,", "start": 525.488, "duration": 2.732 }, { "text": "or the eighth number over here,\nor the ninth number over there.", "start": 528.22, "duration": 3.37 }, { "text": "But you can't just blindly do that.", "start": 531.59, "duration": 3.1 }, { "text": "If this memory is being\nmanaged not by you", "start": 534.69, "duration": 2.19 }, { "text": "per se but by malloc and by the computer\nitself inside-- and your program,", "start": 536.88, "duration": 4.25 }, { "text": "this memory over here, while\nit might physically exist,", "start": 541.13, "duration": 2.79 }, { "text": "might be used by some other part\nof your program all together.", "start": 543.92, "duration": 4.065 }, { "text": "It doesn't necessarily belong to\nyou unless you've asked for it.", "start": 547.985, "duration": 2.775 }, { "text": "And the problem with an array is\nthat as we've seen it typically", "start": 550.76, "duration": 3.14 }, { "text": "you declare their size in advance,\nas with the square bracket notation,", "start": 553.9, "duration": 3.2 }, { "text": "and say give me six integers or give me\nsix something or others, but that's it.", "start": 557.1, "duration": 4.42 }, { "text": "You have to decide in advance.", "start": 561.52, "duration": 1.58 }, { "text": "You can't just grow it as you can in\nsome programming languages thereafter.", "start": 563.1, "duration": 3.67 }, { "text": "You've rather painted\nyourself into a corner.", "start": 566.77, "duration": 2.54 }, { "text": "But with malloc and other\nfunctions like we saw last time,", "start": 569.31, "duration": 3.77 }, { "text": "you can actually allocate\nmore memory using malloc.", "start": 573.08, "duration": 3.24 }, { "text": "Unfortunately, it might end up in\nanother location in your computer's", "start": 576.32, "duration": 3.897 }, { "text": "memory, so you might\nhave to do some copying", "start": 580.217, "duration": 1.833 }, { "text": "to take the original six\nelements and move them", "start": 582.05, "duration": 1.75 }, { "text": "elsewhere just to make room for more.", "start": 583.8, "duration": 1.57 }, { "text": "And there is a data function for\nthat, something called [? re-alloc ?]", "start": 585.37, "duration": 2.916 }, { "text": "or reallocate.", "start": 588.286, "duration": 0.884 }, { "text": "And indeed it can do exactly that.", "start": 589.17, "duration": 1.56 }, { "text": "It can give you a bigger\nchunk of memory and reallocate", "start": 590.73, "duration": 2.84 }, { "text": "what was previously there\nto be a larger [? size. ?]", "start": 593.57, "duration": 2.864 }, { "text": "But you have to do a little bit of work.", "start": 596.434, "duration": 1.666 }, { "text": "You have to invoke it\nin order achieve that.", "start": 598.1, "duration": 1.833 }, { "text": "You can't just blindly keep adding\nthings at the end of this array.", "start": 599.933, "duration": 3.327 }, { "text": "Now, unfortunately, while a solution\nthat might not be very efficient.", "start": 603.26, "duration": 4.46 }, { "text": "Even if you can allocate a bigger\nchunk of memory that's bigger than six", "start": 607.72, "duration": 3.841 }, { "text": "because you have more numbers,\nfor instance, to store,", "start": 611.561, "duration": 2.249 }, { "text": "what if that takes a bit of time?", "start": 613.81, "duration": 3.059 }, { "text": "And indeed it's going to.", "start": 616.869, "duration": 1.041 }, { "text": "If you allocate more integers\nsomewhere else in memory", "start": 617.91, "duration": 2.52 }, { "text": "you still have to copy\nthose original values,", "start": 620.43, "duration": 1.93 }, { "text": "and now it just feels\nlike you're wasting time.", "start": 622.36, "duration": 1.97 }, { "text": "Now, instead of just inserting\nthings into the list,", "start": 624.33, "duration": 2.18 }, { "text": "you might have to copy it into a\nbigger space, reallocate things, grow.", "start": 626.51, "duration": 4.01 }, { "text": "It's a lot more work.", "start": 630.52, "duration": 0.9 }, { "text": "And all of that discussion of\nrunning time and performance", "start": 631.42, "duration": 3.22 }, { "text": "comes back into play, because if that\nwhole copying process and reallocating", "start": 634.64, "duration": 4.02 }, { "text": "is costing you time, your\nalgorithm or your program", "start": 638.66, "duration": 2.86 }, { "text": "ultimately might not really be\nas fast as you might want it.", "start": 641.52, "duration": 3.73 }, { "text": "So, what could we do instead?", "start": 645.25, "duration": 1.46 }, { "text": "What could we do instead\nin order to solve", "start": 646.71, "duration": 2.54 }, { "text": "this problem dynamically, so to\nspeak, that being the operative word.", "start": 649.25, "duration": 3.81 }, { "text": "And luckily enough, last week we learned\nthat there is dynamic memory allocation", "start": 653.06, "duration": 4.86 }, { "text": "in C by way of that function malloc.", "start": 657.92, "duration": 2.2 }, { "text": "And we also learned that there is\nways of representing structures", "start": 660.12, "duration": 3.21 }, { "text": "in C that you don't necessarily\nget with the language itself,", "start": 663.33, "duration": 3.949 }, { "text": "because they're not primitives.", "start": 667.279, "duration": 1.291 }, { "text": "They're not built in.", "start": 668.57, "duration": 0.96 }, { "text": "In other words, let me propose\nthis as a solution to our problem.", "start": 669.53, "duration": 4.35 }, { "text": "This is a list of, let's see,\nfive numbers it would seem,", "start": 673.88, "duration": 4.14 }, { "text": "9, 17, 22, 26, and 34.", "start": 678.02, "duration": 4.16 }, { "text": "Pretty arbitrary right\nnow, but you might", "start": 682.18, "duration": 2.16 }, { "text": "imagine very simply drawing those\nsame numbers-- 9, 17, 22, 26,", "start": 684.34, "duration": 3.51 }, { "text": "34-- in the form of an array and\nthey're clearly deliberately sorted.", "start": 687.85, "duration": 5.37 }, { "text": "But again, what if you wanted to grow\nthat array or even shrink that array", "start": 693.22, "duration": 3.87 }, { "text": "dynamically over time?", "start": 697.09, "duration": 1.76 }, { "text": "Well, let me propose that we not draw\nthose numbers back to back to back", "start": 698.85, "duration": 3.52 }, { "text": "to back literally next to each other\nbut allow ourselves potentially", "start": 702.37, "duration": 3.58 }, { "text": "a little bit of space?", "start": 705.95, "duration": 1.65 }, { "text": "But if that's the case and nine is here\nin my computer's memory and 17 is here", "start": 707.6, "duration": 4.32 }, { "text": "and 22 is here, or over here,\nor over here-- in other words,", "start": 711.92, "duration": 3.09 }, { "text": "what if I relax the constraint that\nmy numbers or my data types more", "start": 715.01, "duration": 3.43 }, { "text": "generally have to be stored contiguously\nback to back to back to back in memory", "start": 718.44, "duration": 4.27 }, { "text": "and instead allow them to be anywhere,\nindeed anywhere a function like malloc", "start": 722.71, "duration": 3.6 }, { "text": "wants to give me more\nmemory, that's fine.", "start": 726.31, "duration": 2.12 }, { "text": "If it wants to give me memory up here\nin my computer, I'll deal with that.", "start": 728.43, "duration": 2.53 }, { "text": "If it wants to give me extra\nmemory over here, that's fine.", "start": 730.96, "duration": 2.458 }, { "text": "I'll deal with it, because I'll use\nthese conceptual arrows to stitch", "start": 733.418, "duration": 4.092 }, { "text": "together my data structure this time.", "start": 737.51, "duration": 3.096 }, { "text": "And now, where have we seen\nthese kinds of arrows before?", "start": 740.606, "duration": 2.374 }, { "text": "What feature of C allows\nus to connect one thing", "start": 742.98, "duration": 3.64 }, { "text": "to another where a la chutes and\nladders get from one place to another?", "start": 746.62, "duration": 4.48 }, { "text": "Well, that's exactly what we saw\nlast time which was pointers.", "start": 751.1, "duration": 3.13 }, { "text": "While we've drawn these here per the\nsnippet from a textbook using arrows,", "start": 754.23, "duration": 4.67 }, { "text": "those are really just pointers.", "start": 758.9, "duration": 1.51 }, { "text": "And what does each of\nthese rectangles represent?", "start": 760.41, "duration": 3.22 }, { "text": "Well, clearly a number in the\ntop half of the rectangle,", "start": 763.63, "duration": 3.07 }, { "text": "but I claim that at the bottom\nhalf of these rectangles", "start": 766.7, "duration": 3.11 }, { "text": "let's consider that bottom rectangle\nto just be another piece of data,", "start": 769.81, "duration": 3.22 }, { "text": "specifically an int star, a pointer.", "start": 773.03, "duration": 2.79 }, { "text": "Or rather not a pointer because it seems\nto be pointing not just to the number", "start": 775.82, "duration": 4.13 }, { "text": "but to this whole rectangle,\nso I need some new terminology.", "start": 779.95, "duration": 3.37 }, { "text": "I need some kind of structure to\ncontain an integer and this pointer.", "start": 783.32, "duration": 5.58 }, { "text": "And for that, I think I'm\ngoing to need a struct.", "start": 788.9, "duration": 2.4 }, { "text": "And indeed let me propose that to\nsolve this problem we give ourselves", "start": 791.3, "duration": 4.54 }, { "text": "this building block as a new\nC data type called a node.", "start": 795.84, "duration": 3.802 }, { "text": "You can call it anything\nyou want, but the convention", "start": 799.642, "duration": 2.208 }, { "text": "would be to call something like\nthis in a data structure-- that's", "start": 801.85, "duration": 2.27 }, { "text": "like a puzzle piece or a building\nblock in a data structure", "start": 804.12, "duration": 2.458 }, { "text": "would be called a node.", "start": 806.578, "duration": 1.192 }, { "text": "Let me propose that we\ndefine it as follows.", "start": 807.77, "duration": 2.98 }, { "text": "I'm using that same syntax from last\ntime with which we declared a student", "start": 810.75, "duration": 3.31 }, { "text": "data type, but here I'm saying\ninside of this data structure,", "start": 814.06, "duration": 3.66 }, { "text": "this node shall be an int.", "start": 817.72, "duration": 2.154 }, { "text": "And that's pretty straightforward.", "start": 819.874, "duration": 1.416 }, { "text": "Just like a student might\nhave a name and a dorm,", "start": 821.29, "duration": 2.041 }, { "text": "this node will have an\nint called n arbitrarily.", "start": 823.331, "duration": 3.589 }, { "text": "And then the only piece of detail\nthat's a little bit new now is", "start": 826.92, "duration": 3.67 }, { "text": "the second line, struct node star next.", "start": 830.59, "duration": 2.769 }, { "text": "Now, what does that mean?", "start": 833.359, "duration": 1.041 }, { "text": "It's pretty verbose, but struct node\nis just recursively, if you will,", "start": 834.4, "duration": 4.73 }, { "text": "referring to this same\ntype of data structure.", "start": 839.13, "duration": 2.95 }, { "text": "Star means this is going to be a\npointer, the address of one such thing,", "start": 842.08, "duration": 3.72 }, { "text": "and next is just an arbitrary\nbut pretty reasonable", "start": 845.8, "duration": 2.36 }, { "text": "name to give to such a pointer.", "start": 848.16, "duration": 2.06 }, { "text": "So this line here,\nstruct node star next,", "start": 850.22, "duration": 2.83 }, { "text": "is the incantation in C\nwith which you declare", "start": 853.05, "duration": 3.74 }, { "text": "one of those arrows that will\npoint from one node, one rectangle", "start": 856.79, "duration": 3.9 }, { "text": "to another node, another rectangle.", "start": 860.69, "duration": 2.57 }, { "text": "And the fact that we have a\nlittle bit of additional verbiage", "start": 863.26, "duration": 3.44 }, { "text": "up here, typedef struct\nnode, is because again C", "start": 866.7, "duration": 3.58 }, { "text": "is a language that is read\ntop to bottom, left to right,", "start": 870.28, "duration": 2.53 }, { "text": "so words have to exist\nbefore you actually use them.", "start": 872.81, "duration": 2.77 }, { "text": "So, whereas last time when\nwe declared a student,", "start": 875.58, "duration": 2.064 }, { "text": "we didn't actually mention struct\nstudent or anything like that.", "start": 877.644, "duration": 2.666 }, { "text": "We just said typedef open curly brace.", "start": 880.31, "duration": 2.26 }, { "text": "Today, when declaring\na node, we actually", "start": 882.57, "duration": 2.26 }, { "text": "have to have some additional syntax\nhere just called struct node.", "start": 884.83, "duration": 3.167 }, { "text": "And technically this\nword could be anything,", "start": 887.997, "duration": 1.833 }, { "text": "but I'll leave it as\nnode for consistency.", "start": 889.83, "duration": 2.09 }, { "text": "And that allows me\ninside of this definition", "start": 891.92, "duration": 2.68 }, { "text": "or to specify that the\nsecond data member is", "start": 894.6, "duration": 3.75 }, { "text": "going to be a pointer to exactly\nthat kind of data structure.", "start": 898.35, "duration": 3.08 }, { "text": "But typedef, just to be clear, allows\nme to type a smaller name for this data", "start": 901.43, "duration": 4.75 }, { "text": "structure here, which I will\nsimply called node at this point.", "start": 906.18, "duration": 4.89 }, { "text": "So, what can we actually do with\nthis kind of data structure now?", "start": 911.07, "duration": 3.066 }, { "text": "And, indeed, let's give\nthis data structure a name.", "start": 914.136, "duration": 2.124 }, { "text": "Let's start calling a linked list.", "start": 916.26, "duration": 1.89 }, { "text": "Previously, we had arrays, but now\nwe have linked lists, both of which", "start": 918.15, "duration": 3.482 }, { "text": "at the end of the day are types\nof lists, but linked lists,", "start": 921.632, "duration": 2.458 }, { "text": "as the name suggests, are linked or\nthreaded together using pointers.", "start": 924.09, "duration": 3.66 }, { "text": "Now, when you have a linked list,\nwhat might be some operations,", "start": 927.75, "duration": 3.146 }, { "text": "some algorithms that you\nmight want to run on them?", "start": 930.896, "duration": 2.124 }, { "text": "Well, if you've got a linked list of\nsay numbers, for the sake of discussion,", "start": 933.02, "duration": 3.208 }, { "text": "you might want to insert a\nnew number into that list.", "start": 936.228, "duration": 2.253 }, { "text": "You might want to delete\na number from that list", "start": 938.481, "duration": 1.999 }, { "text": "and you might want to search that list.", "start": 940.48, "duration": 3.034 }, { "text": "And that allows us to then\nconsider how we might implement", "start": 943.514, "duration": 2.416 }, { "text": "each of these kinds of things.", "start": 945.93, "duration": 1.32 }, { "text": "But it turns out while all simply--\nwhile fairly simple intuitively,", "start": 947.25, "duration": 4.36 }, { "text": "we're going to have to be a little\ncareful now by way of our pointers.", "start": 951.61, "duration": 3.25 }, { "text": "So, let's more formally declare a\nlinked list to look something like this.", "start": 954.86, "duration": 3.3 }, { "text": "It's a collection of nodes\nthat are linked together", "start": 958.16, "duration": 2.95 }, { "text": "with pointers as represented\nby these arrows here,", "start": 961.11, "duration": 2.9 }, { "text": "but we're going to need some\nspecial pointer, at least", "start": 964.01, "duration": 2.8 }, { "text": "at the beginning of the list.", "start": 966.81, "duration": 1.259 }, { "text": "Let's just call it first.", "start": 968.069, "duration": 1.041 }, { "text": "It doesn't necessarily\nstore a actual integer.", "start": 969.11, "duration": 3.15 }, { "text": "It itself first is just a\npointer to the start of the list.", "start": 972.26, "duration": 4.28 }, { "text": "And by way of that pointer can we access\nthe first actual node in the list.", "start": 976.54, "duration": 3.52 }, { "text": "From there can we get at the second,\nfrom there can we get at the third,", "start": 980.06, "duration": 3.0 }, { "text": "and the fourth, and the fifth,\nand any number of others.", "start": 983.06, "duration": 2.0 }, { "text": "And this syntax over here\nmight just represent null.", "start": 985.06, "duration": 2.46 }, { "text": "Because you don't want\nto have that pointer just", "start": 987.52, "duration": 1.38 }, { "text": "pointing off into no man's land, that\nwill have to be a null pointer so", "start": 988.9, "duration": 3.12 }, { "text": "that if we check for that\nwith a condition we know,", "start": 992.02, "duration": 2.125 }, { "text": "OK, we're at the end of the list.", "start": 994.145, "duration": 1.627 }, { "text": "So, let's pause for just a moment\nand consider these three algorithms--", "start": 995.772, "duration": 2.958 }, { "text": "insert, delete, and search, and\nconsider what's going to be involved.", "start": 998.73, "duration": 5.115 }, { "text": "Well, how would you go about\nsearching for an element of this list?", "start": 1003.845, "duration": 3.565 }, { "text": "Suppose I wanted to find the number 22?", "start": 1007.41, "duration": 2.86 }, { "text": "What do you do?", "start": 1010.27, "duration": 0.88 }, { "text": "Well, me, I, the human can just\nlook at this and be like all right,", "start": 1011.15, "duration": 2.791 }, { "text": "22 is right there.", "start": 1013.941, "duration": 1.489 }, { "text": "But a computer can't do that.", "start": 1015.43, "duration": 1.26 }, { "text": "A computer every time\nwe've had this discussion", "start": 1016.69, "duration": 1.64 }, { "text": "can only look at one thing at a time.", "start": 1018.33, "duration": 1.76 }, { "text": "But moreover the computer this\ntime is even more constrained", "start": 1020.09, "duration": 2.9 }, { "text": "because it can't just use\nour old friend binary search", "start": 1022.99, "duration": 4.04 }, { "text": "or divide and conquer, because how do\nyou get to the middle of a linked list?", "start": 1027.03, "duration": 4.285 }, { "text": "Well, you have to find your way there.", "start": 1031.315, "duration": 1.764 }, { "text": "The only thing you have in a\nlinked list from the outset", "start": 1033.079, "duration": 3.231 }, { "text": "is one pointer called first or whatever\nit is, but one pointer that leads you", "start": 1036.31, "duration": 4.44 }, { "text": "to the beginning of the list,\nthe first node in the list.", "start": 1040.75, "duration": 3.19 }, { "text": "So, if you want to get to\nthe second node in the list,", "start": 1043.94, "duration": 2.25 }, { "text": "you can't just go to bracket one,\nor bracket two, or bracket three", "start": 1046.19, "duration": 2.81 }, { "text": "to get any number of other\nelements in the list.", "start": 1049.0, "duration": 2.0 }, { "text": "You have to follow these\nbread crumbs, if you will.", "start": 1051.0, "duration": 2.2 }, { "text": "You have to follow these arrows or these\naddresses to go from one node's address", "start": 1053.2, "duration": 3.67 }, { "text": "to the other to the other.", "start": 1056.87, "duration": 1.62 }, { "text": "And so, we've paid a price already.", "start": 1058.49, "duration": 3.14 }, { "text": "And we'll see that there\nis still an advantage here,", "start": 1061.63, "duration": 2.3 }, { "text": "but what's the running time?", "start": 1063.93, "duration": 1.42 }, { "text": "What's an upper bound on the running\ntime of search for a linked list,", "start": 1065.35, "duration": 5.71 }, { "text": "even if it is sorted?", "start": 1071.06, "duration": 1.195 }, { "text": " ", "start": 1072.255, "duration": 2.754 }, { "text": "Any thoughts?", "start": 1075.009, "duration": 0.541 }, { "text": " ", "start": 1075.55, "duration": 3.07 }, { "text": "Is it constant time like big O of 1?", "start": 1078.62, "duration": 2.38 }, { "text": "Is it log of n?", "start": 1081.0, "duration": 1.4 }, { "text": "Is it n, n squared?", "start": 1082.4, "duration": 3.79 }, { "text": "What's the running time going to be?", "start": 1086.19, "duration": 1.79 }, { "text": "Well, they're sorted, and that was this\nmagical ingredient, this assumption", "start": 1087.98, "duration": 3.27 }, { "text": "we've been allowed to make in\nthe past which was helpful,", "start": 1091.25, "duration": 2.57 }, { "text": "but that assumed that\nwe had random access.", "start": 1093.82, "duration": 2.4 }, { "text": "In C, we had square bracket notation,\nso that using some simple arithmetic", "start": 1096.22, "duration": 3.28 }, { "text": "we could jump roughly to the\nmiddle, and then the next middle,", "start": 1099.5, "duration": 2.14 }, { "text": "and the next middle looking for Mike\nSmith or whatever element it is we're", "start": 1101.64, "duration": 3.083 }, { "text": "looking for.", "start": 1104.723, "duration": 0.697 }, { "text": "Unfortunately here, one\nprice we have already", "start": 1105.42, "duration": 3.15 }, { "text": "paid already by taking this step\ntoward linked lists is linear time.", "start": 1108.57, "duration": 5.77 }, { "text": "Big O of n would seem to be the running\ntime of searching a linked list,", "start": 1114.34, "duration": 3.71 }, { "text": "because the only way you can\nstart is at the beginning,", "start": 1118.05, "duration": 2.58 }, { "text": "and the only way you can get through\nthe list is by following these arrows.", "start": 1120.63, "duration": 3.9 }, { "text": "And if there's n nodes\nin the list, you're", "start": 1124.53, "duration": 2.7 }, { "text": "going to need as many as n steps to\nfind, something like 22, or 26, or 34,", "start": 1127.23, "duration": 4.26 }, { "text": "or any elements all together.", "start": 1131.49, "duration": 2.277 }, { "text": "Well, that's not all that great.", "start": 1133.767, "duration": 1.333 }, { "text": "What about insert?", "start": 1135.1, "duration": 2.62 }, { "text": "What's an upper bound on\nthe running time of insert?", "start": 1137.72, "duration": 4.51 }, { "text": "Well, here too it depends.", "start": 1142.23, "duration": 1.74 }, { "text": "Suppose that we don't care\nabout keeping the list sorted.", "start": 1143.97, "duration": 4.06 }, { "text": "That's kind of a nice advantage,\nso I can be a little lazy here.", "start": 1148.03, "duration": 2.98 }, { "text": "So, what's the running\ntime going to be if I", "start": 1151.01, "duration": 2.22 }, { "text": "want to insert a new number like\nthe number 50 into this list,", "start": 1153.23, "duration": 3.54 }, { "text": "but I don't care about\nkeeping it sorted?", "start": 1156.77, "duration": 2.39 }, { "text": "Well, instinctively, where\nwould you put this element?", "start": 1159.16, "duration": 3.44 }, { "text": "Where would you put it?", "start": 1162.6, "duration": 2.01 }, { "text": "You might be inclined-- you kind\nof want to put it over here,", "start": 1164.61, "duration": 2.78 }, { "text": "because it's the biggest element.", "start": 1167.39, "duration": 1.54 }, { "text": "But again, if you don't care\nabout keeping it sorted,", "start": 1168.93, "duration": 1.52 }, { "text": "where is the fastest, the quickest\nand dirtiest place to put it?", "start": 1170.45, "duration": 2.666 }, { "text": "I would propose let's just put\nit at the front of the list.", "start": 1173.116, "duration": 2.544 }, { "text": "Let's take this first pointer,\npoint it at the new number", "start": 1175.66, "duration": 4.11 }, { "text": "50 that we've have somehow\nadded to the picture", "start": 1179.77, "duration": 2.52 }, { "text": "as by calling malloc, asking\nmalloc for a new node.", "start": 1182.29, "duration": 2.63 }, { "text": "And then have 50, in turn, point to the\nnumber 9, and then 9 can point to 17,", "start": 1184.92, "duration": 4.99 }, { "text": "and 22, and so forth.", "start": 1189.91, "duration": 1.17 }, { "text": "What if we want to insert\nanother number, 42,", "start": 1191.08, "duration": 1.876 }, { "text": "and we don't care about where it goes?", "start": 1192.956, "duration": 1.582 }, { "text": "Well, why don't we just put it\nat the beginning of the list?", "start": 1194.538, "duration": 2.562 }, { "text": "Then we have the first\npointers pointing at 42,", "start": 1197.1, "duration": 2.76 }, { "text": "which in turn should point at 50, which\nin turn can point at 9, then 17, then", "start": 1199.86, "duration": 3.77 }, { "text": "22, and so forth.", "start": 1203.63, "duration": 1.267 }, { "text": "So, if we're just lazy\nabout this, we can actually", "start": 1204.897, "duration": 2.083 }, { "text": "achieve a great running time\nfor insert constant time.", "start": 1206.98, "duration": 3.54 }, { "text": "Unfortunately, if we want\nto keep things sorted then", "start": 1210.52, "duration": 3.76 }, { "text": "we're going to have to incur a\nlinear time cost again, right?", "start": 1214.28, "duration": 3.55 }, { "text": "Because if we have to\ninsert 42 or 50, worst case", "start": 1217.83, "duration": 2.616 }, { "text": "they might belong all the way at\nthe end of the list and that's", "start": 1220.446, "duration": 2.624 }, { "text": "Big O of n steps.", "start": 1223.07, "duration": 2.02 }, { "text": "And delete, too, unfortunately,\nwhether it's sorted or unsorted", "start": 1225.09, "duration": 3.75 }, { "text": "is also like search\ngoing to be Big O of n", "start": 1228.84, "duration": 2.175 }, { "text": "because you don't necessarily know\nwhen you're searching for a number", "start": 1231.015, "duration": 2.875 }, { "text": "to delete if it's going to be at the\nbeginning, the middle, and the end.", "start": 1233.89, "duration": 2.999 }, { "text": "So, in the worst case, it\nmight indeed be at the end.", "start": 1236.889, "duration": 2.551 }, { "text": "You know what?", "start": 1239.44, "duration": 0.94 }, { "text": "Why don't we instead of\nwalking through this verbally,", "start": 1240.38, "duration": 2.59 }, { "text": "let's see if we can't\nget some volunteers?", "start": 1242.97, "duration": 1.782 }, { "text": "Can we get seven volunteers to play--\nwow, to play the role of numbers here.", "start": 1244.752, "duration": 4.338 }, { "text": "1, 2, 3, 4, 5, 6, and\nyes, 7, come on up.", "start": 1249.09, "duration": 4.78 }, { "text": "All right, so I have here some\nprintouts for all seven of you", "start": 1253.87, "duration": 2.93 }, { "text": "that represent exactly the nodes\nthat we have here on the screen.", "start": 1256.8, "duration": 3.35 }, { "text": "Let's meet one of our first contestants.", "start": 1260.15, "duration": 1.67 }, { "text": "What is your name?", "start": 1261.82, "duration": 0.5 }, { "text": "AUDIENCE: Scully.", "start": 1262.32, "duration": 0.59 }, { "text": "SPEAKER 1: Scully, nice to see you.", "start": 1262.91, "duration": 1.51 }, { "text": "So, you shall be literally first\nand represent our first pointer.", "start": 1264.42, "duration": 2.89 }, { "text": "So, if you want to come and\nstand roughly over here.", "start": 1267.31, "duration": 2.38 }, { "text": "And then what is your name?", "start": 1269.69, "duration": 0.64 }, { "text": "AUDIENCE: Maria.", "start": 1270.33, "duration": 0.39 }, { "text": "SPEAKER 1: Maria, nice to see you.", "start": 1270.72, "duration": 1.416 }, { "text": "And you can be the number 9 right\nnext to our first contestant.", "start": 1272.136, "duration": 4.694 }, { "text": "And your name?", "start": 1276.83, "duration": 0.59 }, { "text": "AUDIENCE: Sarah.", "start": 1277.42, "duration": 0.36 }, { "text": "SPEAKER 1: Sarah, nice to see you.", "start": 1277.78, "duration": 1.416 }, { "text": "You shall be the number 17.", "start": 1279.196, "duration": 1.341 }, { "text": "And your name?", "start": 1280.537, "duration": 0.583 }, { "text": "[? AUDIENCE: Satoshi. ?]", "start": 1281.12, "duration": 0.42 }, { "text": "[? SPEAKER 1: Satoshi, ?]\nnice to see you.", "start": 1281.54, "duration": 1.75 }, { "text": "You shall be 20.", "start": 1283.29, "duration": 1.367 }, { "text": "And your name?", "start": 1284.657, "duration": 0.583 }, { "text": "[? AUDIENCE: Mosof. ?]", "start": 1285.24, "duration": 0.63 }, { "text": "[? SPEAKER 1: Mosof, ?] nice to see you.", "start": 1285.87, "duration": 1.666 }, { "text": "And you shall be 22.", "start": 1287.536, "duration": 1.361 }, { "text": "AUDIENCE: Jed.", "start": 1288.897, "duration": 0.583 }, { "text": "SPEAKER 1: Jed, nice to\nsee you-- 29, formerly 26.", "start": 1289.48, "duration": 4.85 }, { "text": "And your name?", "start": 1294.33, "duration": 0.695 }, { "text": "AUDIENCE: Erin.", "start": 1295.025, "duration": 0.625 }, { "text": "SPEAKER 1: Erin, nice to see you.", "start": 1295.65, "duration": 1.375 }, { "text": "You shall be 34.", "start": 1297.025, "duration": 0.915 }, { "text": "All right, so what we have here\nis seven elements, six of which", "start": 1297.94, "duration": 4.69 }, { "text": "are very similar to themselves, one\nof which is fundamentally different.", "start": 1302.63, "duration": 3.63 }, { "text": "So, Scully here represents first,\nand indeed her sheet of paper", "start": 1306.26, "duration": 3.11 }, { "text": "is horizontal to suggest\nthat she is just a node.", "start": 1309.37, "duration": 2.71 }, { "text": "She is just going to be the\npointer of to a node in a list.", "start": 1312.08, "duration": 3.69 }, { "text": "Everyone else's nodes\nare vertical, as have", "start": 1315.77, "duration": 1.972 }, { "text": "been the rectangles we've\nbeen drawing on the screen,", "start": 1317.742, "duration": 2.208 }, { "text": "because each of these guys represents\na number as well as a next pointer.", "start": 1319.95, "duration": 4.4 }, { "text": "Now of course, you're only seeing\nin front of you the number.", "start": 1324.35, "duration": 2.59 }, { "text": "So we're going to go ahead\nand if you wouldn't mind,", "start": 1326.94, "duration": 1.56 }, { "text": "use your left hand to represent the\narrow that we've long had on the screen", "start": 1328.5, "duration": 3.67 }, { "text": "to point to the person next to you.", "start": 1332.17, "duration": 1.65 }, { "text": "And Erin, you're a bit\nof an anomaly, but also", "start": 1333.82, "duration": 2.86 }, { "text": "because you need to have a null\npointer at the end of the list,", "start": 1336.68, "duration": 3.079 }, { "text": "so that you're not just\npointing aimlessly.", "start": 1339.759, "duration": 1.791 }, { "text": "And pointing to the ground\nseems fine, so literally", "start": 1341.55, "duration": 2.27 }, { "text": "pointing to the ground will\nrepresent-- will infer as null.", "start": 1343.82, "duration": 4.3 }, { "text": "So, Scully, you are the only\nthing keeping this list together,", "start": 1348.12, "duration": 2.75 }, { "text": "so to speak.", "start": 1350.87, "duration": 0.64 }, { "text": "So, you two need to point with\nyour one pointer to Maria there.", "start": 1351.51, "duration": 4.48 }, { "text": "So, here we have a linked list.", "start": 1355.99, "duration": 1.88 }, { "text": "And just to make this clear, could\neveryone separate from each other", "start": 1357.87, "duration": 3.26 }, { "text": "by a step or two in any direction?", "start": 1361.13, "duration": 2.58 }, { "text": "Notice that the list is still--\nit's almost identical to before.", "start": 1363.71, "duration": 3.319 }, { "text": "Can some of you take a\nstep forward, a step back,", "start": 1367.029, "duration": 2.041 }, { "text": "but still point at the other person?", "start": 1369.07, "duration": 1.94 }, { "text": "So, now we're capturing a\nlittle more accurately the fact", "start": 1371.01, "duration": 2.53 }, { "text": "that these nodes and these\npointers can be anywhere in memory,", "start": 1373.54, "duration": 3.11 }, { "text": "so long as they're linking themselves\ntogether by way of these pointers.", "start": 1376.65, "duration": 4.43 }, { "text": "All right, so suppose\nnow that I want to insert", "start": 1381.08, "duration": 2.24 }, { "text": "an element like 55, which happens\nto belong at the end of this list.", "start": 1383.32, "duration": 5.27 }, { "text": "Let me go ahead and malloc\nChristian if I could.", "start": 1388.59, "duration": 3.82 }, { "text": "So we have asked malloc for\na chunk of memory equivalent", "start": 1392.41, "duration": 2.6 }, { "text": "to the size of one\ninteger and one pointer.", "start": 1395.01, "duration": 2.81 }, { "text": "That is going to be represented\nwith this rectangle here.", "start": 1397.82, "duration": 2.53 }, { "text": "Nice to see you.", "start": 1400.35, "duration": 0.796 }, { "text": "AUDIENCE: Nice to see you.", "start": 1401.146, "duration": 0.406 }, { "text": "SPEAKER 1: You shall be 55.", "start": 1401.552, "duration": 1.448 }, { "text": "And I'll play the role of the temporary\npointer as predecessor pointer", "start": 1403.0, "duration": 3.069 }, { "text": "or pointer just using\nmy left or right hand", "start": 1406.069, "duration": 1.791 }, { "text": "to try to figure out\nwhere Christian belongs.", "start": 1407.86, "duration": 3.06 }, { "text": "So, just like you might-- just like\nwe might want to search the list,", "start": 1410.92, "duration": 5.45 }, { "text": "inserting is fundamentally\nrather the same.", "start": 1416.37, "duration": 2.75 }, { "text": "The only thing I have access to\nat the outset of this algorithm", "start": 1419.12, "duration": 2.82 }, { "text": "is my first pointer, and\nit's only by way of Scully", "start": 1421.94, "duration": 2.74 }, { "text": "that I can even access\nthe rest of the list.", "start": 1424.68, "duration": 1.95 }, { "text": "I cannot jump to the\nmiddle, jump to the end.", "start": 1426.63, "duration": 2.3 }, { "text": "I can only start at the beginning\nand literally follow the pointer.", "start": 1428.93, "duration": 3.377 }, { "text": "So, let me go ahead and do that.", "start": 1432.307, "duration": 1.333 }, { "text": "Let me go ahead and\npoint at whatever Scully", "start": 1433.64, "duration": 1.37 }, { "text": "is pointing at, which happens to\nbe Maria, which is the number 9.", "start": 1435.01, "duration": 3.066 }, { "text": "55, of course, is\nbigger than that, and I", "start": 1438.076, "duration": 2.134 }, { "text": "do want to keep the list\nsorted for today's purposes.", "start": 1440.21, "duration": 2.34 }, { "text": "So I'm going to very carefully\nfollow the next pointer, 17.", "start": 1442.55, "duration": 4.32 }, { "text": "Follow the next pointer, 20.", "start": 1446.87, "duration": 1.5 }, { "text": "Follow the next pointer, 22.", "start": 1448.37, "duration": 1.75 }, { "text": "Follow the next pointer, 29.", "start": 1450.12, "duration": 1.84 }, { "text": "Follow the next pointer, 34.", "start": 1451.96, "duration": 1.83 }, { "text": "Follow the next pointer,\nah dammit, null.", "start": 1453.79, "duration": 2.844 }, { "text": "And so this is why it is important\nwith some of these algorithms", "start": 1456.634, "duration": 2.666 }, { "text": "to have a predecessor pointer, a\nsecond pointer or really my left hand", "start": 1459.3, "duration": 4.15 }, { "text": "so that maybe my left hand\ncan still point at Erin.", "start": 1463.45, "duration": 2.71 }, { "text": "My right hand can realize,\nah, null, so that I still", "start": 1466.16, "duration": 3.46 }, { "text": "have access to the last node\nin the list so that Christian--", "start": 1469.62, "duration": 2.92 }, { "text": "if you could come over here.", "start": 1472.54, "duration": 1.73 }, { "text": "I'm going to go ahead and tell Erin\nquite simply to point at Christian.", "start": 1474.27, "duration": 6.48 }, { "text": "Good, and let's just say for\nstudents' sake come on over here,", "start": 1480.75, "duration": 4.045 }, { "text": "but technically we could have\nleft Christian there and just had", "start": 1484.795, "duration": 2.625 }, { "text": "Erin pointing at him.", "start": 1487.42, "duration": 0.89 }, { "text": "It's just going to get a\nlittle confusing before long,", "start": 1488.31, "duration": 2.25 }, { "text": "so we'll just cheat and\nmove you right over here.", "start": 1490.56, "duration": 2.299 }, { "text": "But now we have a linked list\nthat has one additional member.", "start": 1492.859, "duration": 2.541 }, { "text": "Suppose now that we want to\nmake another insertion-- pardon.", "start": 1495.4, "duration": 4.03 }, { "text": "Let me go ahead and propose\nthat we insert say the number 5.", "start": 1499.43, "duration": 4.86 }, { "text": "Well, the number 5, of course,\nbelongs at the beginning of the list.", "start": 1504.29, "duration": 5.6 }, { "text": "So you know what?", "start": 1509.89, "duration": 0.71 }, { "text": "I need to malloc.", "start": 1510.6, "duration": 1.02 }, { "text": "Can I malloc Jordan off\ncamera five, perhaps?", "start": 1511.62, "duration": 4.506 }, { "text": "So malloc, a very slow return value.", "start": 1516.126, "duration": 2.684 }, { "text": "OK, we're going to store your\nnode your n value five here.", "start": 1518.81, "duration": 4.12 }, { "text": "His pointer is undefined right\nnow, because he's not actually", "start": 1522.93, "duration": 2.6 }, { "text": "pointing at anything.", "start": 1525.53, "duration": 0.99 }, { "text": "And so where does he ultimately belong?", "start": 1526.52, "duration": 1.911 }, { "text": "Well, he belongs at\nthe start of the list.", "start": 1528.431, "duration": 1.749 }, { "text": "So, let me deliberately make a mistake.", "start": 1530.18, "duration": 2.03 }, { "text": "Let me go ahead and update\nScully to point at Jordan,", "start": 1532.21, "duration": 3.24 }, { "text": "thereby putting Jordan effectively\nat the front of the list.", "start": 1535.45, "duration": 2.75 }, { "text": "Unfortunately, whom should\nJordan now point at technically?", "start": 1538.2, "duration": 4.62 }, { "text": "It should be Maria, but this is code.", "start": 1542.82, "duration": 3.52 }, { "text": "The only thing we can do\nis copy pointers in memory,", "start": 1546.34, "duration": 2.66 }, { "text": "and if Scully's left hand is\nno longer pointing at Maria,", "start": 1549.0, "duration": 2.79 }, { "text": "I have literally orphaned\nthe entirety of this list.", "start": 1551.79, "duration": 3.01 }, { "text": "I have leaked 1, 2, 3, 4,\n5, 6, 7 chunks of memory,", "start": 1554.8, "duration": 4.1 }, { "text": "seven nodes, because I got my\norder of operations out of order.", "start": 1558.9, "duration": 3.78 }, { "text": "Indeed, I should have done\nwhat-- let's undo, control Z.", "start": 1562.68, "duration": 3.69 }, { "text": "And now let me go ahead and do what?", "start": 1566.37, "duration": 1.82 }, { "text": "Jordan should point at the exact\nsame thing Scully is pointing at,", "start": 1568.19, "duration": 3.8 }, { "text": "which has no downside.", "start": 1571.99, "duration": 1.53 }, { "text": "Even though it feels redundant,\nwe've not lost any information.", "start": 1573.52, "duration": 2.68 }, { "text": "And now that Jordan\nis pointing at Maria,", "start": 1576.2, "duration": 2.83 }, { "text": "Scully's pointer can\nbe pointed at Jordan.", "start": 1579.03, "duration": 2.935 }, { "text": "And now, even though the\nlist looks a little weird,", "start": 1581.965, "duration": 2.125 }, { "text": "this is a key feature\nof the linked list.", "start": 1584.09, "duration": 1.708 }, { "text": "These nodes could have\nbeen malloc from anywhere.", "start": 1585.798, "duration": 2.432 }, { "text": "So indeed, even though we initially\nkept everyone physically sorted", "start": 1588.23, "duration": 3.3 }, { "text": "left to right-- and you've all cleaned\nthe list up even since-- that's OK.", "start": 1591.53, "duration": 5.04 }, { "text": "The point is that all of these\nnodes are linked together.", "start": 1596.57, "duration": 3.024 }, { "text": "So, thank you so much to our volunteers.", "start": 1599.594, "duration": 1.666 }, { "text": "You can keep these pieces\nof paper and later on we'll", "start": 1601.26, "duration": 2.208 }, { "text": "have some stress balls for you.", "start": 1603.468, "duration": 1.442 }, { "text": "But that's the key idea\nhere behind a linked list.", "start": 1604.91, "duration": 6.23 }, { "text": "Thank you.", "start": 1611.14, "duration": 1.66 }, { "text": "So, of course, there are some\nmore complicated operations", "start": 1612.8, "duration": 3.31 }, { "text": "that we might have to deal with.", "start": 1616.11, "duration": 1.334 }, { "text": "For instance, if we want to insert\ninto the middle of the list,", "start": 1617.444, "duration": 2.624 }, { "text": "that's going to be a little more\nof a burden on me, the program,", "start": 1620.068, "duration": 2.682 }, { "text": "keeping track of where\nthings have to go.", "start": 1622.75, "duration": 2.77 }, { "text": "But nicely enough, there's\nonly these three cases--", "start": 1625.52, "duration": 2.38 }, { "text": "the beginning of the list, the end of\nthe list, and the middle of the list,", "start": 1627.9, "duration": 2.72 }, { "text": "because middle of the list doesn't\nhave to mean literally the middle,", "start": 1630.62, "duration": 2.7 }, { "text": "just anywhere that's not\nthe beginning or the end.", "start": 1633.32, "duration": 2.127 }, { "text": "Of course, we should\nbe careful to make sure", "start": 1635.447, "duration": 1.833 }, { "text": "that we handle the empty\nlist scenario, which", "start": 1637.28, "duration": 2.73 }, { "text": "is equivalent to putting something\nat both the beginning of the list", "start": 1640.01, "duration": 3.19 }, { "text": "and the end of the list.", "start": 1643.2, "duration": 1.63 }, { "text": "But that would be perhaps a special\ncase we could deal with separately.", "start": 1644.83, "duration": 3.92 }, { "text": "Of course, there are other operations\nlike inserting-- or rather removing", "start": 1648.75, "duration": 3.71 }, { "text": "from the tail of the list,\nremoving from the head of the list,", "start": 1652.46, "duration": 2.68 }, { "text": "and removing in the middle.", "start": 1655.14, "duration": 1.51 }, { "text": "And that would be the opposite\nof malloc, if you will.", "start": 1656.65, "duration": 2.73 }, { "text": "And in those cases, we\nhave to take care to call", "start": 1659.38, "duration": 2.29 }, { "text": "our friend free to free\nthose bytes of memory,", "start": 1661.67, "duration": 3.0 }, { "text": "give them back to the operating\nsystem so that we don't leak memory.", "start": 1664.67, "duration": 2.86 }, { "text": "But there, too, I'm probably going to\nhave to be careful as to what order", "start": 1667.53, "duration": 4.28 }, { "text": "I change my pointers and free nodes.", "start": 1671.81, "duration": 2.15 }, { "text": "Because what you don't want to\ndo, and what unfortunately you", "start": 1673.96, "duration": 2.79 }, { "text": "might very well accidentally\ndo at some point,", "start": 1676.75, "duration": 2.45 }, { "text": "is free a pointer and then try to access\nthat pointer or change the pointer,", "start": 1679.2, "duration": 4.57 }, { "text": "even after you've told the operating\nsystem I'm done with this address.", "start": 1683.77, "duration": 3.094 }, { "text": "That can give you what's\ncalled a segmentation", "start": 1686.864, "duration": 1.916 }, { "text": "fault, which is just one\nof the ways in which you", "start": 1688.78, "duration": 2.05 }, { "text": "can deduce that kind of mistake.", "start": 1690.83, "duration": 3.29 }, { "text": "So, let's actually implement\none of these methods.", "start": 1694.12, "duration": 2.779 }, { "text": "And we'll pluck off one that\nallows us to actually take", "start": 1696.899, "duration": 2.291 }, { "text": "a look at the syntax with which\nwe can manipulate pointers.", "start": 1699.19, "duration": 2.559 }, { "text": "And let's go ahead and\nimplement a function", "start": 1701.749, "duration": 1.791 }, { "text": "called search, for\ninstance, where search", "start": 1703.54, "duration": 2.02 }, { "text": "I [? proposed ?] just returns a\nbool, true or false, this number n", "start": 1705.56, "duration": 4.15 }, { "text": "is in the given the list.", "start": 1709.71, "duration": 1.61 }, { "text": "And now, why have I said node star list?", "start": 1711.32, "duration": 2.26 }, { "text": "Well, at the end of the day, a linked\nlist is just a whole bunch of nodes.", "start": 1713.58, "duration": 4.76 }, { "text": "But the first of those nodes that\nwe keep calling first is of what", "start": 1718.34, "duration": 3.95 }, { "text": "data type?", "start": 1722.29, "duration": 1.24 }, { "text": "If you have a pointer,\na variable, that's", "start": 1723.53, "duration": 2.6 }, { "text": "pointing to a linked list, that means\nit's storing the address of a node,", "start": 1726.13, "duration": 6.17 }, { "text": "otherwise known as a node star.", "start": 1732.3, "duration": 1.48 }, { "text": "So, this would be the syntax with which\nyou can pass to a function something", "start": 1733.78, "duration": 4.16 }, { "text": "like a linked list.", "start": 1737.94, "duration": 0.87 }, { "text": "You simply have to pass it a pointer\nto the first element in that list.", "start": 1738.81, "duration": 3.99 }, { "text": "And if I want to go ahead\nnow and implement this,", "start": 1742.8, "duration": 3.17 }, { "text": "let me go ahead and\npropose the following.", "start": 1745.97, "duration": 2.7 }, { "text": "Let me go ahead here and give myself a\ntemporary value, so node star pointer", "start": 1748.67, "duration": 5.19 }, { "text": "we'll call it, PTR.", "start": 1753.86, "duration": 1.33 }, { "text": "And that's going to equal\nthe start of the list.", "start": 1755.19, "duration": 2.36 }, { "text": "So, I'm just creating\nanother box of memory", "start": 1757.55, "duration": 1.975 }, { "text": "and I'm storing inside\nof it the same address", "start": 1759.525, "duration": 1.875 }, { "text": "that I was passed in, just so that\nI have a temporary variable that I", "start": 1761.4, "duration": 2.874 }, { "text": "can use to update.", "start": 1764.274, "duration": 1.366 }, { "text": "After this, let me go ahead and\nsay while that pointer is not", "start": 1765.64, "duration": 3.46 }, { "text": "equal to null-- because recall that\nnull is this special sentinel value that", "start": 1769.1, "duration": 4.04 }, { "text": "means end of the list.", "start": 1773.14, "duration": 1.53 }, { "text": "So inside of this loop,\nwhat do I want to do?", "start": 1774.67, "duration": 3.41 }, { "text": "I'm going to go ahead\nand say if pointer--", "start": 1778.08, "duration": 4.25 }, { "text": "and now I have to get at\nthe number inside of it.", "start": 1782.33, "duration": 4.03 }, { "text": "So, if I recall from the last time,\nwe only spent a little bit of time", "start": 1786.36, "duration": 3.42 }, { "text": "on the student example, but\nwe said something like student", "start": 1789.78, "duration": 3.07 }, { "text": "dot name or student dot dorm.", "start": 1792.85, "duration": 2.055 }, { "text": "And in this case I'm inclined\nto say pointer dot n, where n is", "start": 1794.905, "duration": 4.125 }, { "text": "the number, the integer that's inside.", "start": 1799.03, "duration": 2.44 }, { "text": "But pointer this time\nis not a struct, per se.", "start": 1801.47, "duration": 2.53 }, { "text": "It's the address of a node.", "start": 1804.0, "duration": 1.83 }, { "text": "It's the address of a struct.", "start": 1805.83, "duration": 1.46 }, { "text": "And so, perhaps the most\nintuitive piece of syntax in C,", "start": 1807.29, "duration": 5.54 }, { "text": "at least retrospectively\nnow, is that if you", "start": 1812.83, "duration": 2.35 }, { "text": "want to access a piece of\ndata that's inside of a node", "start": 1815.18, "duration": 4.22 }, { "text": "and you have a pointer to that node much\nlike our arrows in the pictures imply,", "start": 1819.4, "duration": 4.35 }, { "text": "you literally draw an\narrow using a hyphen", "start": 1823.75, "duration": 2.43 }, { "text": "and then using a right angle bracket.", "start": 1826.18, "duration": 4.57 }, { "text": "So, now if we do see-- whoops,\nlet me finish my thought.", "start": 1830.75, "duration": 3.7 }, { "text": "If pointer n equals equals the n we're\nlooking for, let me go ahead in here", "start": 1834.45, "duration": 4.8 }, { "text": "and say return true.", "start": 1839.25, "duration": 1.97 }, { "text": "Or else, let me go ahead\nand not return false,", "start": 1841.22, "duration": 2.597 }, { "text": "because I don't want to just check one\nelement and then blindly say false.", "start": 1843.817, "duration": 3.083 }, { "text": "I instead want to say pointer\nshould get pointer arrow next.", "start": 1846.9, "duration": 5.03 }, { "text": "And then only after that\nloop is all complete", "start": 1851.93, "duration": 3.28 }, { "text": "should I say something\nlike nope, return false.", "start": 1855.21, "duration": 3.83 }, { "text": "So, what's actually going on here?", "start": 1859.04, "duration": 2.05 }, { "text": "The function declaration,\nagain, took in two arguments--", "start": 1861.09, "duration": 2.52 }, { "text": "one, an int n that we're looking\nfor, two a pointer to a node,", "start": 1863.61, "duration": 4.68 }, { "text": "otherwise known as a\nnode in a linked list.", "start": 1868.29, "duration": 1.867 }, { "text": "And per the pictures\nwe've been drawing, you", "start": 1870.157, "duration": 1.833 }, { "text": "can access any other element in that\nlinked list by way of the first element", "start": 1871.99, "duration": 4.61 }, { "text": "in that list, as suggested here.", "start": 1876.6, "duration": 1.99 }, { "text": "So, now I'm just giving myself a\ntemporary variable called pointer,", "start": 1878.59, "duration": 2.914 }, { "text": "but I can call it anything I want.", "start": 1881.504, "duration": 1.416 }, { "text": "And I'm declaring it as node star, so\nthat it can store the address of a node", "start": 1882.92, "duration": 3.57 }, { "text": "as well, and then I'm\njust initializing it to be", "start": 1886.49, "duration": 2.2 }, { "text": "the exact value that was passed in.", "start": 1888.69, "duration": 1.491 }, { "text": "So, I don't want to accidentally\nbreak the list that I was passed.", "start": 1890.181, "duration": 2.749 }, { "text": "I don't want to change\nthe value of my parameter", "start": 1892.93, "duration": 2.0 }, { "text": "unnecessarily and complicate things.", "start": 1894.93, "duration": 1.73 }, { "text": "I just really want a\ntemporary variable, much", "start": 1896.66, "duration": 2.14 }, { "text": "like I in the world of loops that\nallows me to constantly iterate", "start": 1898.8, "duration": 4.33 }, { "text": "through something and update it as\nI go while the whole along the way", "start": 1903.13, "duration": 5.476 }, { "text": "I want to be checking this.", "start": 1908.606, "duration": 1.124 }, { "text": "While pointer is not null.", "start": 1909.73, "duration": 1.97 }, { "text": "If pointer is null, that means\nI'm at the end of the list,", "start": 1911.7, "duration": 3.02 }, { "text": "or maybe more curiously, I was passed\nnull, in which case there is no list.", "start": 1914.72, "duration": 4.601 }, { "text": "And that's a valid scenario that could\nhappen, even though it's a bit strange.", "start": 1919.321, "duration": 3.249 }, { "text": "But if pointer is null, I don't want\nto proceed further inside of this loop.", "start": 1922.57, "duration": 5.8 }, { "text": "But so long as pointer is not\nnull, let me go ahead and do this.", "start": 1928.37, "duration": 3.89 }, { "text": "Let me follow that pointer and\ngo inside that node and say", "start": 1932.26, "duration": 3.98 }, { "text": "is your n value equal\nequal to the [? end ?]", "start": 1936.24, "duration": 2.84 }, { "text": "value that I've been\nasked to search for?", "start": 1939.08, "duration": 2.05 }, { "text": "And if so, return true.", "start": 1941.13, "duration": 1.482 }, { "text": "I don't want to just return\nfalse now because otherwise I'd", "start": 1942.612, "duration": 2.458 }, { "text": "only ever be checking the\nfirst element in a linked list.", "start": 1945.07, "duration": 2.97 }, { "text": "So, I now want to do the equivalent\nin spirit of i plus plus.", "start": 1948.04, "duration": 6.45 }, { "text": "But I'm not using i's.", "start": 1954.49, "duration": 0.917 }, { "text": "I don't need to use\npointer arithmetic here,", "start": 1955.407, "duration": 1.832 }, { "text": "and indeed it won't work because I\nhave this thing stitched together.", "start": 1957.239, "duration": 2.971 }, { "text": "It's not an array of contiguous memory.", "start": 1960.21, "duration": 2.26 }, { "text": "I want to say that my\ncurrent temporary value", "start": 1962.47, "duration": 2.37 }, { "text": "pointer should get\nwhatever pointer arrow next", "start": 1964.84, "duration": 3.59 }, { "text": "is, and then let this loop continue.", "start": 1968.43, "duration": 2.46 }, { "text": "If it's not null, check again.", "start": 1970.89, "duration": 1.38 }, { "text": "If it's [? end ?] value equals what\nI'm looking for and repeat, repeat,", "start": 1972.27, "duration": 3.2 }, { "text": "repeat until pointer equals null.", "start": 1975.47, "duration": 2.234 }, { "text": "So, let's make this more concrete.", "start": 1977.704, "duration": 1.416 }, { "text": "Let me go ahead and just draw\na temporary picture here.", "start": 1979.12, "duration": 2.75 }, { "text": "And let me suppose here\nthat what I have been passed", "start": 1981.87, "duration": 3.21 }, { "text": "is something like the following.", "start": 1985.08, "duration": 2.84 }, { "text": "Let's do a very simple linked list\nthat has maybe the number one,", "start": 1987.92, "duration": 4.53 }, { "text": "and has the number two,\nand has the number three.", "start": 1992.45, "duration": 5.74 }, { "text": "And, again, I've drawn\ngaps between these nodes,", "start": 1998.19, "duration": 2.0 }, { "text": "because they could be\nanywhere in memory.", "start": 2000.19, "duration": 1.39 }, { "text": "So, technically they don't need\nto be left to right like this,", "start": 2001.58, "duration": 2.2 }, { "text": "but that'll keep us sane.", "start": 2003.78, "duration": 1.21 }, { "text": "And if this is indeed\na correct linked list,", "start": 2004.99, "duration": 2.19 }, { "text": "there are pointers in\neach of those fields", "start": 2007.18, "duration": 3.11 }, { "text": "that point to the next node\nin the list, and that slash", "start": 2010.29, "duration": 2.415 }, { "text": "I drew in the last one just means null.", "start": 2012.705, "duration": 1.625 }, { "text": "You can draw it however you want.", "start": 2014.33, "duration": 1.43 }, { "text": "But for a linked list to work, we need\nto know the beginning of this thing.", "start": 2015.76, "duration": 3.39 }, { "text": "So, we'll call this\nfirst, and that of course", "start": 2019.15, "duration": 3.34 }, { "text": "has to point to the first\nelement in my linked list.", "start": 2022.49, "duration": 2.314 }, { "text": "So, here's the state of our world.", "start": 2024.804, "duration": 1.416 }, { "text": "It's a linked list quite similar\nin spirit to the other one.", "start": 2026.22, "duration": 2.59 }, { "text": "It's a little shorter, just\nfor the sake of discussion.", "start": 2028.81, "duration": 2.291 }, { "text": "And now, let's consider\nmy function search,", "start": 2031.101, "duration": 2.959 }, { "text": "which again takes two arguments.", "start": 2034.06, "duration": 2.04 }, { "text": "So, that the first argument\nis of type int called n.", "start": 2036.1, "duration": 3.4 }, { "text": "And suppose I'm searching\nfor the number three.", "start": 2039.5, "duration": 2.72 }, { "text": "The second argument is a node star,\nso the address of a node called list.", "start": 2042.22, "duration": 3.82 }, { "text": "So, what does that mean?", "start": 2046.04, "duration": 1.11 }, { "text": "When this function\nsearch is called, let's", "start": 2047.15, "duration": 3.759 }, { "text": "suppose that we currently\nhave the value n, which", "start": 2050.909, "duration": 4.191 }, { "text": "is going to be 3, because\nthat's arbitrarily", "start": 2055.1, "duration": 1.91 }, { "text": "the number of decided to search for.", "start": 2057.01, "duration": 1.629 }, { "text": "And then this other value pointer\nis going to be initialized-- sorry,", "start": 2058.639, "duration": 7.031 }, { "text": "not that.", "start": 2065.67, "duration": 1.219 }, { "text": "List is going to be\nwhatever pointer is passed", "start": 2066.889, "duration": 5.6 }, { "text": "in as the second argument\nto this function.", "start": 2072.489, "duration": 2.281 }, { "text": "So, let's suppose that this linked list,\nthis sample linked list at the top,", "start": 2074.77, "duration": 4.07 }, { "text": "is indeed what is passed\nin to this function.", "start": 2078.84, "duration": 3.682 }, { "text": "So, I've passed in 3, because\nI want to search for 3.", "start": 2082.522, "duration": 2.208 }, { "text": "So what goes here?", "start": 2084.73, "duration": 1.199 }, { "text": "If I pass this sample linked\nlist into my search function,", "start": 2085.929, "duration": 6.29 }, { "text": "what is the value of list?", "start": 2092.219, "duration": 3.23 }, { "text": "Well, list if I'm past\nthis first pointer", "start": 2095.449, "duration": 4.411 }, { "text": "is really going to be the pointer\nto the first element in the list.", "start": 2099.86, "duration": 3.72 }, { "text": "That's all we're saying.", "start": 2103.58, "duration": 1.0 }, { "text": "Node star list just\nmeans give me the address", "start": 2104.58, "duration": 1.949 }, { "text": "of a linked list, which means give\nme the address of the first node", "start": 2106.529, "duration": 2.791 }, { "text": "in the linked list, which\nmeans that initially when", "start": 2109.32, "duration": 2.125 }, { "text": "I call search my picture--\nmy stack frame, if you will,", "start": 2111.445, "duration": 3.775 }, { "text": "in terms of my local arguments--\nis going to look like this.", "start": 2115.22, "duration": 2.88 }, { "text": "All right, so with that said,\nhow does this code work?", "start": 2118.1, "duration": 3.44 }, { "text": "We recall in this code\nhave this while loop that", "start": 2121.54, "duration": 2.94 }, { "text": "just sits in a loop checking\nwhether the current nodes", "start": 2124.48, "duration": 3.69 }, { "text": "n equals equals the one we're looking\nfor, and if not, it updates it.", "start": 2128.17, "duration": 3.08 }, { "text": "So, we need one more local\nvariable called pointer that's", "start": 2131.25, "duration": 3.19 }, { "text": "initialized to the start of this list.", "start": 2134.44, "duration": 3.37 }, { "text": "So, this will be a pointer and\nit's initialized to the same thing", "start": 2137.81, "duration": 5.15 }, { "text": "that my second argument\nis initialized to.", "start": 2142.96, "duration": 3.48 }, { "text": "So, this now is the state of our world\nonce one line of code has executed,", "start": 2146.44, "duration": 4.87 }, { "text": "that very first one in there.", "start": 2151.31, "duration": 2.196 }, { "text": "So, now let's implement the loop.", "start": 2153.506, "duration": 1.374 }, { "text": "While pointer does not equal\nnull, so here's pointer.", "start": 2154.88, "duration": 3.18 }, { "text": "Does it equal null?", "start": 2158.06, "duration": 1.252 }, { "text": "No, because if it did,\nwe would just draw", "start": 2159.312, "duration": 1.708 }, { "text": "a slash or some other piece of syntax.", "start": 2161.02, "duration": 1.9 }, { "text": "But it's pointing at clearly\nsomething that exists.", "start": 2162.92, "duration": 2.25 }, { "text": "So, this node here has\nsome valid address.", "start": 2165.17, "duration": 2.79 }, { "text": "Pointer is pointing at\nit, so it's not null.", "start": 2167.96, "duration": 2.5 }, { "text": "So, what do I do inside of my code?", "start": 2170.46, "duration": 1.93 }, { "text": "I check if the n value\ninside of pointer, PTR,", "start": 2172.39, "duration": 7.84 }, { "text": "equals equals the\nnumber I'm looking for,", "start": 2180.23, "duration": 2.11 }, { "text": "and if so, return true, otherwise,\nif not I update pointer.", "start": 2182.34, "duration": 3.68 }, { "text": "So let's check.", "start": 2186.02, "duration": 0.91 }, { "text": "Let's follow the arrow, PTR,\nand look at the value n.", "start": 2186.93, "duration": 3.42 }, { "text": "Recall that the top of these boxes is n.", "start": 2190.35, "duration": 2.02 }, { "text": "The bottom of them is called\nnext-- n, next, n next.", "start": 2192.37, "duration": 3.91 }, { "text": "So, I followed this pointer.", "start": 2196.28, "duration": 1.21 }, { "text": "I'm looking at the box called n.", "start": 2197.49, "duration": 3.08 }, { "text": "Does 3 equal 1?", "start": 2200.57, "duration": 1.15 }, { "text": "No, obviously not.", "start": 2201.72, "duration": 1.5 }, { "text": "So I update-- pointer gets pointer next.", "start": 2203.22, "duration": 4.0 }, { "text": "So, to be clear, pointer\ngets pointer next,", "start": 2207.22, "duration": 4.6 }, { "text": "that second to last line of actual code.", "start": 2211.82, "duration": 2.09 }, { "text": "So, what does that mean I need to do?", "start": 2213.91, "duration": 2.14 }, { "text": "That means I need to update pointer\nto be equal to pointer next.", "start": 2216.05, "duration": 4.57 }, { "text": "What is pointer next?", "start": 2220.62, "duration": 0.93 }, { "text": "Well, here's pointer and here's next.", "start": 2221.55, "duration": 2.5 }, { "text": "We were looking a moment ago at n.", "start": 2224.05, "duration": 1.43 }, { "text": "Now I'm looking at next.", "start": 2225.48, "duration": 1.5 }, { "text": "So, pointer next means that I should\nupdate whatever is inside this box--", "start": 2226.98, "duration": 7.11 }, { "text": "and a lot more on the screen-- to\nbe equal to pointer next, which", "start": 2234.09, "duration": 6.35 }, { "text": "is this field.", "start": 2240.44, "duration": 0.62 }, { "text": "This field is pointing at\nthat, so that line of code", "start": 2241.06, "duration": 3.08 }, { "text": "has the effect of updating PTR to\nsimply point at the second node.", "start": 2244.14, "duration": 5.05 }, { "text": "So, what happens next?", "start": 2249.19, "duration": 1.31 }, { "text": "I seem to still be in that loop and I\nsay, well, pointer does not equal null,", "start": 2250.5, "duration": 3.645 }, { "text": "and it doesn't.", "start": 2254.145, "duration": 0.625 }, { "text": "It's pointing at that second node.", "start": 2254.77, "duration": 1.416 }, { "text": "If pointer arrow n equals\nequals n, but no that's not", "start": 2256.186, "duration": 4.034 }, { "text": "the case, because I'm looking for three.", "start": 2260.22, "duration": 1.74 }, { "text": "I'm pointing at two,\nso that is again false.", "start": 2261.96, "duration": 2.45 }, { "text": "So, again, I don't return true.", "start": 2264.41, "duration": 1.61 }, { "text": "I instead update pointer\nto equal pointer next.", "start": 2266.02, "duration": 3.13 }, { "text": "So, what has to happen here, at the\nrisk of deleting my handiwork again,", "start": 2269.15, "duration": 7.81 }, { "text": "now pointer gets pointer next,\nwhich is this element, which", "start": 2276.96, "duration": 4.48 }, { "text": "is equivalent to pointing\nat this node here.", "start": 2281.44, "duration": 2.84 }, { "text": "And so, now I'm still inside that loop\nwhile pointer-- not equal to null.", "start": 2284.28, "duration": 5.35 }, { "text": "It's not null.", "start": 2289.63, "duration": 0.82 }, { "text": "If pointer arrow n equals equals\nn, well, let's follow that logic.", "start": 2290.45, "duration": 3.94 }, { "text": "If pointer, follow the\narrow, n equals equals n,", "start": 2294.39, "duration": 4.36 }, { "text": "three-- which is the one I'm\nlooking for-- returned true.", "start": 2298.75, "duration": 4.33 }, { "text": "And so, how then does this\nfunction ultimately behave?", "start": 2303.08, "duration": 2.8 }, { "text": "It would seem in this case to return\ntrue, because I have eventually", "start": 2305.88, "duration": 4.04 }, { "text": "found that number three.", "start": 2309.92, "duration": 2.39 }, { "text": "What would happen by contrast if I were\nlooking not for three, but for four", "start": 2312.31, "duration": 4.27 }, { "text": "with this code?", "start": 2316.58, "duration": 1.42 }, { "text": "In other words, what if I'm not\nlooking for three and I want to go one", "start": 2318.0, "duration": 3.65 }, { "text": "step further?", "start": 2321.65, "duration": 0.58 }, { "text": "Well, one step further\nis going to update PTR", "start": 2322.23, "duration": 3.11 }, { "text": "to equal null, that\nslash in my last node.", "start": 2325.34, "duration": 5.21 }, { "text": "And that means code wise,\nI'm going to break out", "start": 2330.55, "duration": 2.65 }, { "text": "of that loop, because\npointer now does equal null.", "start": 2333.2, "duration": 2.89 }, { "text": "And so, by default that very last\nline of code return false, not found.", "start": 2336.09, "duration": 4.98 }, { "text": "So, complicated at first\nglance, and it certainly", "start": 2341.07, "duration": 2.147 }, { "text": "looks more complicated than\nthings we've written before,", "start": 2343.217, "duration": 2.333 }, { "text": "but again, if you go back to basics,\nwhat does each of these lines mean?", "start": 2345.55, "duration": 3.46 }, { "text": "Consider that there's no magic here.", "start": 2349.01, "duration": 2.21 }, { "text": "This first line means give me a\nvariable that's a pointer to a node.", "start": 2351.22, "duration": 3.967 }, { "text": "It'd be in other words\nthe address of a node", "start": 2355.187, "duration": 1.833 }, { "text": "and assign it whatever I was passed in.", "start": 2357.02, "duration": 1.816 }, { "text": "While pointer [? naught ?] equals\nnull, we've seen null before.", "start": 2358.836, "duration": 2.624 }, { "text": "It's this special zero\nvalue, and I'm just", "start": 2361.46, "duration": 2.15 }, { "text": "making sure that the pointer I'm using,\nPTR, does not equal that special value.", "start": 2363.61, "duration": 3.63 }, { "text": "And then inside of this loop I'm using\none piece of new syntax, this arrow", "start": 2367.24, "duration": 3.21 }, { "text": "notation, which just like the picture\nsuggests means go there, and then", "start": 2370.45, "duration": 3.51 }, { "text": "look at the field called n and check\nif it equals the n you're looking for,", "start": 2373.96, "duration": 3.81 }, { "text": "and if so return true.", "start": 2377.77, "duration": 1.0 }, { "text": "Otherwise, update yourself\nmuch like i plus plus", "start": 2378.77, "duration": 3.27 }, { "text": "but specifically update pointer to\nbe whatever the value is when you", "start": 2382.04, "duration": 4.48 }, { "text": "follow the arrow in that next field.", "start": 2386.52, "duration": 4.234 }, { "text": "So, this of course is just search.", "start": 2390.754, "duration": 1.416 }, { "text": "We've not actually changed the\nlist, but imagine, if you will,", "start": 2392.17, "duration": 3.69 }, { "text": "that you could now\nimplement insert and delete,", "start": 2395.86, "duration": 3.07 }, { "text": "not simply by following these\npointers but actually changing", "start": 2398.93, "duration": 3.74 }, { "text": "the value of next in a node to the left,\na node to the right, or a new node all", "start": 2402.67, "duration": 6.9 }, { "text": "together.", "start": 2409.57, "duration": 1.47 }, { "text": "So, who cares?", "start": 2411.04, "duration": 1.66 }, { "text": "Why did we add all of this complexity?", "start": 2412.7, "duration": 2.01 }, { "text": "We had arrays, which were working\nreally well for a whole bunch of weeks,", "start": 2414.71, "duration": 3.43 }, { "text": "and now we've claimed that\narrays are not so good.", "start": 2418.14, "duration": 2.92 }, { "text": "We want to use linked lists instead.", "start": 2421.06, "duration": 1.8 }, { "text": "But why might we want\nto use linked lists?", "start": 2422.86, "duration": 1.81 }, { "text": "Well, linked lists gives us dynamism.", "start": 2424.67, "duration": 1.63 }, { "text": "We can call malloc and give ourselves\nmore, and more, and more nodes", "start": 2426.3, "duration": 3.21 }, { "text": "and grow our list of numbers, even\nif we don't know in advance how", "start": 2429.51, "duration": 2.91 }, { "text": "many such numbers we need.", "start": 2432.42, "duration": 1.44 }, { "text": "And we can shrink them,\nsimilarly, so we don't", "start": 2433.86, "duration": 2.19 }, { "text": "have to allocate a massive\narray unnecessarily.", "start": 2436.05, "duration": 2.81 }, { "text": "We can shrink our data structure based\non how many numbers we actually need.", "start": 2438.86, "duration": 3.31 }, { "text": "But we're paying a price.", "start": 2442.17, "duration": 1.37 }, { "text": "Search is a little bit slower,\ndelete is a little bit slower.", "start": 2443.54, "duration": 3.15 }, { "text": "Insert would be slower if we\ninsist on keeping things sorted,", "start": 2446.69, "duration": 3.42 }, { "text": "so we've paid this price.", "start": 2450.11, "duration": 1.182 }, { "text": "And indeed, this is thematic.", "start": 2451.292, "duration": 1.208 }, { "text": "In CS50, in computer\nscience more generally,", "start": 2452.5, "duration": 2.45 }, { "text": "there's often going to be\nthese trade-offs of time,", "start": 2454.95, "duration": 2.68 }, { "text": "or space, or just complexity,\nor your human time.", "start": 2457.63, "duration": 3.39 }, { "text": "Any number of resources\ncan be in scarce supply.", "start": 2461.02, "duration": 3.26 }, { "text": "And, indeed, we've seen\nby way of linked lists", "start": 2464.28, "duration": 3.14 }, { "text": "that we're solving one problem\nwhile introducing another.", "start": 2467.42, "duration": 2.87 }, { "text": "It's like that silly\nsituation you might see", "start": 2470.29, "duration": 2.71 }, { "text": "memes of where you cover your hands--\nput your hand around a hose that", "start": 2473.0, "duration": 3.44 }, { "text": "has a leak and all of a sudden\nanother leak springs up over there.", "start": 2476.44, "duration": 2.94 }, { "text": "We're just moving the problem\nelsewhere, but maybe that leak", "start": 2479.38, "duration": 2.72 }, { "text": "is less problematic to\nus than this one here.", "start": 2482.1, "duration": 2.95 }, { "text": "So, again, it's this\ntheme of trade-offs.", "start": 2485.05, "duration": 1.74 }, { "text": "Now, this here is Mather Dining Hall.", "start": 2486.79, "duration": 1.664 }, { "text": "And this, of course, is\na whole bunch of trays", "start": 2488.454, "duration": 1.916 }, { "text": "where you might go and get\nsome food, lunch, breakfast,", "start": 2490.37, "duration": 2.71 }, { "text": "or dinner, or the like, and you pick\nup this tray and you put food on it.", "start": 2493.08, "duration": 3.041 }, { "text": "But what's interesting about trays,\nas Mather and a lot of cafeterias do,", "start": 2496.121, "duration": 3.109 }, { "text": "is trays are stacked\none on top of the other.", "start": 2499.23, "duration": 3.46 }, { "text": "And it turns out now that\nwe have this second building", "start": 2502.69, "duration": 3.45 }, { "text": "block with which to create data\nstructures-- we're not just", "start": 2506.14, "duration": 3.15 }, { "text": "using arrays anymore.", "start": 2509.29, "duration": 1.29 }, { "text": "We now have pointers and this general\nidea of linking nodes together", "start": 2510.58, "duration": 5.46 }, { "text": "in our toolkit, we can now start\nto imagine more interesting data", "start": 2516.04, "duration": 4.3 }, { "text": "structures that solve problems\nin slightly different ways.", "start": 2520.34, "duration": 3.72 }, { "text": "For instance, suppose\nthat I wanted to implement", "start": 2524.06, "duration": 2.1 }, { "text": "this paradigm of stacking things on\none on top of the other like this.", "start": 2526.16, "duration": 4.34 }, { "text": "Indeed, this is a data\nstructure called a stack,", "start": 2530.5, "duration": 2.13 }, { "text": "and it generally has two\noperations associated with it,", "start": 2532.63, "duration": 2.32 }, { "text": "push to push something on the stack and\npop to take something off of the stack.", "start": 2534.95, "duration": 4.839 }, { "text": "And this is perhaps a\nuseful data structure", "start": 2539.789, "duration": 1.791 }, { "text": "if you just want to store numbers\nor something else in really just", "start": 2541.58, "duration": 4.56 }, { "text": "the most efficient way for you\nwithout regard really to fairness.", "start": 2546.14, "duration": 3.28 }, { "text": "So, for instance, if this table\nhere is my initial data structure", "start": 2549.42, "duration": 3.95 }, { "text": "and it's empty and I have a piece\nof information that I want to store,", "start": 2553.37, "duration": 3.602 }, { "text": "I'm just going to going\nahead and put it right there.", "start": 2556.972, "duration": 2.208 }, { "text": "And now suppose I want to push\nanother number onto the stack.", "start": 2559.18, "duration": 2.87 }, { "text": "I'm just going to go\nahead and simply put it", "start": 2562.05, "duration": 2.91 }, { "text": "on top, third number on\ntop, fourth number on top.", "start": 2564.96, "duration": 4.95 }, { "text": "But I've now committed to a\ncertain property, if you will,", "start": 2569.91, "duration": 5.11 }, { "text": "a certain property whereby the last\ntray in has to be the first tray out,", "start": 2575.02, "duration": 5.2 }, { "text": "otherwise known in computer\nscience as LIFO-- last in,", "start": 2580.22, "duration": 2.68 }, { "text": "first out-- because if I want to\nget that first number, I mean,", "start": 2582.9, "duration": 3.06 }, { "text": "I've created a mess for myself.", "start": 2585.96, "duration": 1.52 }, { "text": "I have to lift these all up or\nmove them just to get at it.", "start": 2587.48, "duration": 3.34 }, { "text": "That just seems stupid.", "start": 2590.82, "duration": 1.23 }, { "text": "Intuitively, the easiest thing to\ngrab is probably going to be the top,", "start": 2592.05, "duration": 3.62 }, { "text": "but that's not necessarily\nthe first element I put in.", "start": 2595.67, "duration": 2.89 }, { "text": "But that's OK.", "start": 2598.56, "duration": 0.647 }, { "text": "This might still be a\nvalid data structure--", "start": 2599.207, "duration": 1.833 }, { "text": "and indeed later in the term when\nwe introduced web programming", "start": 2601.04, "duration": 2.63 }, { "text": "and we look at languages\nlike HTML, there's", "start": 2603.67, "duration": 2.07 }, { "text": "actually a number of applications\nwhere it's actually super useful", "start": 2605.74, "duration": 2.87 }, { "text": "to have a data structure where you can\njust stack stuff on top of each other", "start": 2608.61, "duration": 3.3 }, { "text": "in order to tuck some data\naway for subsequent use.", "start": 2611.91, "duration": 3.39 }, { "text": "And, indeed, when we talked\nabout memory in our own computer,", "start": 2615.3, "duration": 3.02 }, { "text": "stacks clearly have some value.", "start": 2618.32, "duration": 2.23 }, { "text": "We talked about main and then swap,\nand then maybe other functions.", "start": 2620.55, "duration": 2.83 }, { "text": "There are many contexts, one of which\nwe've seen already, where in life you", "start": 2623.38, "duration": 3.48 }, { "text": "actually want to stack\nthings on top of each other", "start": 2626.86, "duration": 2.64 }, { "text": "so as to keep track of really what\ndid I do most recently, because that's", "start": 2629.5, "duration": 3.63 }, { "text": "the next problem I'm going to\ndeal with or the next frame--", "start": 2633.13, "duration": 2.61 }, { "text": "in the case of memory-- that I'm\ngoing to pop off of the stack.", "start": 2635.74, "duration": 2.74 }, { "text": "So, how might we implement\nthis data structure?", "start": 2638.48, "duration": 2.43 }, { "text": "Let me propose that if we want\nto define our own data type", "start": 2640.91, "duration": 3.5 }, { "text": "called the stack that implements that\nidea of cafeteria trays or frames", "start": 2644.41, "duration": 4.9 }, { "text": "in memory, let me go ahead and typedef\na structure called stack inside", "start": 2649.31, "duration": 5.65 }, { "text": "of which are two data members.", "start": 2654.96, "duration": 1.682 }, { "text": "Suppose that, for the\nsake of discussion,", "start": 2656.642, "duration": 1.708 }, { "text": "I'm not going to try to store trays\nor something that doesn't really", "start": 2658.35, "duration": 2.833 }, { "text": "exist computationally but\nrather numbers, just integers.", "start": 2661.183, "duration": 2.697 }, { "text": "Inside of this structure\ncalled a stack is", "start": 2663.88, "duration": 2.21 }, { "text": "going to be an array\ncalled numbers of type int", "start": 2666.09, "duration": 2.716 }, { "text": "and it's going to store capacity.", "start": 2668.806, "duration": 1.374 }, { "text": "So capacity, let's assume, is hash\ndefined elsewhere to be some constant.", "start": 2670.18, "duration": 3.61 }, { "text": "So, maybe it's 10, maybe it's 1,000,\nbut it's some constant integer elsewhere", "start": 2673.79, "duration": 4.16 }, { "text": "that limits, ultimately, the capacity\nof the stack to some integral value.", "start": 2677.95, "duration": 4.53 }, { "text": "And then size-- this seems weird.", "start": 2682.48, "duration": 2.91 }, { "text": "I have capacity here and then size here.", "start": 2685.39, "duration": 3.1 }, { "text": "Well, there's the\nsemantic distinction here.", "start": 2688.49, "duration": 2.2 }, { "text": "Just because you have\na stack ready to go,", "start": 2690.69, "duration": 2.21 }, { "text": "as I did a moment ago-- just because\nI had this stack ready to go,", "start": 2692.9, "duration": 4.63 }, { "text": "empty initially, it's going\nto have some capacity.", "start": 2697.53, "duration": 2.37 }, { "text": "Realistically, I can only\ngo as high as the ceiling", "start": 2699.9, "duration": 2.13 }, { "text": "or until the things fall over.", "start": 2702.03, "duration": 1.76 }, { "text": "But there's also a size here,\nand the size is currently zero,", "start": 2703.79, "duration": 3.03 }, { "text": "but the capacity might be\nlike 1,000 or whatever.", "start": 2706.82, "duration": 2.41 }, { "text": "So, that's the difference\nthere and the size now is 4", "start": 2709.23, "duration": 2.82 }, { "text": "and the capacity is like\n1,000 minus 4 at this point--", "start": 2712.05, "duration": 3.25 }, { "text": "or rather, capacity is still 1,000,\nbecause that's the total possible size,", "start": 2715.3, "duration": 4.29 }, { "text": "not the actual size.", "start": 2719.59, "duration": 2.1 }, { "text": "But what if that's a limit?", "start": 2721.69, "duration": 1.54 }, { "text": "What if I don't want to restrict myself\nto some fixed, finite number of trays", "start": 2723.23, "duration": 6.16 }, { "text": "or a fixed number of numbers?", "start": 2729.39, "duration": 1.97 }, { "text": "Well, I could instead declare\nmy stack as being a pointer.", "start": 2731.36, "duration": 5.82 }, { "text": "Now, this pointer\ninitially has no value,", "start": 2737.18, "duration": 2.24 }, { "text": "so let's assume that it's probably going\nto be initialized to null in my code,", "start": 2739.42, "duration": 4.355 }, { "text": "but that too is not going to be useful.", "start": 2743.775, "duration": 1.625 }, { "text": "Why would I declare numbers now\nnot to be in an array, which", "start": 2745.4, "duration": 3.767 }, { "text": "felt very straightforward.", "start": 2749.167, "duration": 1.083 }, { "text": "Normally we draw arrays left\nto right, but with the trays,", "start": 2750.25, "duration": 2.63 }, { "text": "just imagine turning an array vertically\nand thinking of the number stacked", "start": 2752.88, "duration": 3.53 }, { "text": "on top of each other.", "start": 2756.41, "duration": 1.35 }, { "text": "But this is just a pointer to a number.", "start": 2757.76, "duration": 3.07 }, { "text": "Why might I want to implement a\nstack as just a pointer to an int?", "start": 2760.83, "duration": 3.64 }, { "text": "That seems wrong.", "start": 2764.47, "duration": 2.83 }, { "text": "I want lots of numbers, not one number.", "start": 2767.3, "duration": 3.75 }, { "text": "So, what could I do?", "start": 2771.05, "duration": 1.95 }, { "text": "Well, what if in this world to implement\na stack I invoke our friend malloc", "start": 2773.0, "duration": 4.86 }, { "text": "and I say to malloc, malloc,\ngive me enough memory", "start": 2777.86, "duration": 2.54 }, { "text": "for 2,000 numbers or 5,000 numbers.", "start": 2780.4, "duration": 3.32 }, { "text": "What is malloc going to return?", "start": 2783.72, "duration": 1.43 }, { "text": "Well, by definition,\nwe know malloc is going", "start": 2785.15, "duration": 1.98 }, { "text": "to return the address of a chunk\nof memory, and that chunk of memory", "start": 2787.13, "duration": 5.17 }, { "text": "is going to be of whatever\nsize I ask malloc for,", "start": 2792.3, "duration": 3.48 }, { "text": "and the address of the first is\nreally just equivalent to the address", "start": 2795.78, "duration": 3.44 }, { "text": "of one integer.", "start": 2799.22, "duration": 1.28 }, { "text": "And so long as I, the programmer,\nremember that I asked malloc", "start": 2800.5, "duration": 2.65 }, { "text": "for 2,000 integers or\nfor 5,000 integers,", "start": 2803.15, "duration": 3.28 }, { "text": "I know implicitly the end of\nthat chunk of memory and malloc", "start": 2806.43, "duration": 3.24 }, { "text": "just need to tell me the beginning.", "start": 2809.67, "duration": 1.61 }, { "text": "So, it's perfectly fine to implement\nthe stack by way of a single pointer,", "start": 2811.28, "duration": 4.53 }, { "text": "because all I need to\nknow is, hey, malloc,", "start": 2815.81, "duration": 2.19 }, { "text": "where should I put my first integer?", "start": 2818.0, "duration": 2.26 }, { "text": "Because I know via pointer\narithmetic, per last week,", "start": 2820.26, "duration": 2.99 }, { "text": "that I can put my next\ninteger four bytes later,", "start": 2823.25, "duration": 3.03 }, { "text": "four bytes later, four bytes later.", "start": 2826.28, "duration": 1.78 }, { "text": "And I'm deliberately going\nup this time, but it really", "start": 2828.06, "duration": 2.249 }, { "text": "is just an array where you can think\nof the array as left and right.", "start": 2830.309, "duration": 2.961 }, { "text": "So, this would be a way of giving\nourselves a data structure called", "start": 2833.27, "duration": 3.6 }, { "text": "a stack that is not fixed from the\noutset like this previous version", "start": 2836.87, "duration": 4.64 }, { "text": "to some specific capacity.", "start": 2841.51, "duration": 1.87 }, { "text": "Now, we are limited only by how much\nphysical memory or virtual memory", "start": 2843.38, "duration": 4.24 }, { "text": "my computer actually has.", "start": 2847.62, "duration": 3.37 }, { "text": "So, suppose Apple or someone\nsimilar implemented the lines", "start": 2850.99, "duration": 6.22 }, { "text": "outside their stores for the\nrelease of the iPhone as a stack.", "start": 2857.21, "duration": 5.214 }, { "text": "So, it's weird maybe to think of\npeople stacking on top of each other,", "start": 2862.424, "duration": 2.916 }, { "text": "but maybe you could imagine Apple\nfunneling everyone into the glass store", "start": 2865.34, "duration": 6.89 }, { "text": "here in Manhattan, and then whoever is\nthe last one in gets their phone first.", "start": 2872.23, "duration": 5.5 }, { "text": "Because why?", "start": 2877.73, "duration": 0.65 }, { "text": "They're closest to the exit.", "start": 2878.38, "duration": 1.31 }, { "text": "So, you have all these people show\nup super early in the morning or days", "start": 2879.69, "duration": 2.58 }, { "text": "before, you pile them all into\nthe store saying everyone, hey,", "start": 2882.27, "duration": 2.47 }, { "text": "please go into the corner there.", "start": 2884.74, "duration": 1.333 }, { "text": "Please get into the store.", "start": 2886.073, "duration": 1.097 }, { "text": "And then as soon as 9:00\nAM rolls around and it's", "start": 2887.17, "duration": 2.217 }, { "text": "time to give out the iPhones, just for\nlogistical convenience you realize,", "start": 2889.387, "duration": 3.083 }, { "text": "all right, why don't we just\ngive the person who came in", "start": 2892.47, "duration": 1.65 }, { "text": "last their phone first because\nthey're closest to the exit", "start": 2894.12, "duration": 2.45 }, { "text": "and get them out, last in, first out?", "start": 2896.57, "duration": 3.21 }, { "text": "Good design, bad design?", "start": 2899.78, "duration": 2.1 }, { "text": "It's correct in so far\nas everyone's going", "start": 2901.88, "duration": 2.92 }, { "text": "to get an iPhone if supply is there,\nand that's never going to be the case.", "start": 2904.8, "duration": 4.27 }, { "text": "So, it's not necessarily\nvery equitable or fair,", "start": 2909.07, "duration": 2.802 }, { "text": "and indeed the humans are not\ngoing to be very pleased with Apple", "start": 2911.872, "duration": 2.708 }, { "text": "if they used a LIFO data\nstructure or a stack.", "start": 2914.58, "duration": 4.34 }, { "text": "What would these fans of Apple\nhardware prefer that Apple use?", "start": 2918.92, "duration": 6.25 }, { "text": "We call it a line.", "start": 2925.17, "duration": 0.87 }, { "text": "If you go to the UK,\nthey call it a queue,", "start": 2926.04, "duration": 1.85 }, { "text": "which is actually a perfect answer,\nbecause there's this other data", "start": 2927.89, "duration": 3.49 }, { "text": "structure in the world\ncalled a queue, which", "start": 2931.38, "duration": 2.65 }, { "text": "is exactly what you would hope\nthe Apple store line would be,", "start": 2934.03, "duration": 3.1 }, { "text": "a line whereby it's first in, first out.", "start": 2937.13, "duration": 3.41 }, { "text": "So, the first person\nthere three days before,", "start": 2940.54, "duration": 1.875 }, { "text": "at 5:00 AM gets his or her\nphone first, and the one person", "start": 2942.415, "duration": 3.325 }, { "text": "who comes in at 9:01 AM\ndoesn't get their phone", "start": 2945.74, "duration": 2.26 }, { "text": "because they're at the last\nposition in the queue or the list.", "start": 2948.0, "duration": 3.015 }, { "text": "And a queue, nicely enough, might\njust have at least two operations--", "start": 2951.015, "duration": 2.875 }, { "text": "enqueue and dequeue\nwhereby enqueue means get", "start": 2953.89, "duration": 2.84 }, { "text": "into line d queue means\nget out of the line,", "start": 2956.73, "duration": 2.32 }, { "text": "but these happen at different places.", "start": 2959.05, "duration": 1.604 }, { "text": "For instance, if there's a whole bunch\nof people lined up here on the stage,", "start": 2960.654, "duration": 3.166 }, { "text": "closest over there's\nthe front of the list.", "start": 2963.82, "duration": 1.52 }, { "text": "I get here last.", "start": 2965.34, "duration": 0.84 }, { "text": "I enqueue myself at the\nend of this data structure,", "start": 2966.18, "duration": 3.34 }, { "text": "but you dequeue someone from\nthe beginning of the list.", "start": 2969.52, "duration": 2.63 }, { "text": "By contrast, when we had a stack,\nwhen you push someone onto the stack,", "start": 2972.15, "duration": 3.97 }, { "text": "you pop it off, or him or\nher off first by nature", "start": 2976.12, "duration": 4.5 }, { "text": "of it being a LIFO data structure.", "start": 2980.62, "duration": 2.82 }, { "text": "So, how might we implement a queue?", "start": 2983.44, "duration": 2.7 }, { "text": "It's actually slightly\nmore complicated, 50% more", "start": 2986.14, "duration": 3.59 }, { "text": "pieces of information you need to\nkeep track of, the front of the list.", "start": 2989.73, "duration": 3.25 }, { "text": "But you can still do it in an array.", "start": 2992.98, "duration": 2.76 }, { "text": "So, suppose that we do use an array,\nand let me go ahead and draw this", "start": 2995.74, "duration": 3.07 }, { "text": "as follows.", "start": 2998.81, "duration": 0.97 }, { "text": "Suppose that like hopscotch we\ndraw the queue for an Apple Store", "start": 2999.78, "duration": 6.37 }, { "text": "like an array like this.", "start": 3006.15, "duration": 1.85 }, { "text": "And here is the door of the Apple store,\nso you want to be at location zero,", "start": 3008.0, "duration": 3.96 }, { "text": "ideally.", "start": 3011.96, "duration": 0.5 }, { "text": "1, 2, 3, 4, 5, 6-- so\nthis is how many people", "start": 3012.46, "duration": 3.77 }, { "text": "can fit into our queue in this case.", "start": 3016.23, "duration": 2.64 }, { "text": "So, suppose that Alice wants to buy an\niPhone and she gets to the store first.", "start": 3018.87, "duration": 4.32 }, { "text": "Where should she go to keep things fair?", "start": 3023.19, "duration": 2.76 }, { "text": "This is the queue, so we don't\nwant to put her into the corner,", "start": 3025.95, "duration": 3.304 }, { "text": "so to speak, in our first example.", "start": 3029.254, "duration": 1.416 }, { "text": "We want to put her at\nthe front of the list.", "start": 3030.67, "duration": 1.833 }, { "text": "So, Alice belongs right\nthere, pretty straightforward.", "start": 3032.503, "duration": 3.367 }, { "text": "Now, Bob arrives and he comes\nin slightly after Alice,", "start": 3035.87, "duration": 2.62 }, { "text": "so he gets to get behind Alice in line.", "start": 3038.49, "duration": 2.36 }, { "text": "And so Bob is there, and maybe Charlie\narrives thereafter, and then so forth.", "start": 3040.85, "duration": 3.96 }, { "text": "David maybe comes in fourth and beyond.", "start": 3044.81, "duration": 2.71 }, { "text": "So, that's how people would\nqueue up, so to speak.", "start": 3047.52, "duration": 2.64 }, { "text": "And now, when it's time\nfor Apple to open this door", "start": 3050.16, "duration": 3.25 }, { "text": "and start selling iPhones, what happens?", "start": 3053.41, "duration": 3.26 }, { "text": "We want to take Alice\nout of the list first.", "start": 3056.67, "duration": 2.39 }, { "text": "We want to de-queue Alice.", "start": 3059.06, "duration": 2.18 }, { "text": "So, we need to start\nremembering some information,", "start": 3061.24, "duration": 2.99 }, { "text": "because it's not sufficient\nnow to just remove-- whoops,", "start": 3064.23, "duration": 4.6 }, { "text": "it's not sufficient just\nto remove Alice like this,", "start": 3068.83, "duration": 3.72 }, { "text": "because suppose that we do keep\nadding other people, person", "start": 3072.55, "duration": 3.096 }, { "text": "d, e-- whoops, that's not an f.", "start": 3075.646, "duration": 2.824 }, { "text": "OK, don't know what happened there.", "start": 3078.47, "duration": 2.7 }, { "text": "Person f is here, g, h.", "start": 3081.17, "duration": 3.03 }, { "text": "Suppose that Alice has bought\nher phone and left the store,", "start": 3084.2, "duration": 3.39 }, { "text": "and then Bob does the same.", "start": 3087.59, "duration": 1.96 }, { "text": "He goes ahead and leaves the store,\nand then Charlie leaves the store.", "start": 3089.55, "duration": 4.66 }, { "text": "Where do I put person i who\nmaybe shows up a little late?", "start": 3094.21, "duration": 4.12 }, { "text": "It would seem that I want to put\nthem at the end of the queue, which", "start": 3098.33, "duration": 3.45 }, { "text": "makes good sense, but right now d, e,\nf, g, and h are still in the queue,", "start": 3101.78, "duration": 5.11 }, { "text": "and this is an array I proposed.", "start": 3106.89, "duration": 1.85 }, { "text": "My data structure's an array, so I can't\njust move d to the front of the line", "start": 3108.74, "duration": 5.88 }, { "text": "easily.", "start": 3114.62, "duration": 1.1 }, { "text": "I have to actually\nshift him or move him,", "start": 3115.72, "duration": 2.33 }, { "text": "and this might conjure up some memory\nof our searching and sorting examples", "start": 3118.05, "duration": 3.317 }, { "text": "where when we had our\nhumans on stage, we actually", "start": 3121.367, "duration": 2.083 }, { "text": "had to physically move\npeople like an insertion sort", "start": 3123.45, "duration": 2.46 }, { "text": "to make room for those elements.", "start": 3125.91, "duration": 2.17 }, { "text": "And that's fine.", "start": 3128.08, "duration": 0.73 }, { "text": "We can absolutely say, hey, David,\ncome over here please and person e,", "start": 3128.81, "duration": 4.284 }, { "text": "come over here please, and that's\nobviously what the Apple store does.", "start": 3133.094, "duration": 2.916 }, { "text": "But when you're implementing\nthis idea in memory,", "start": 3136.01, "duration": 2.19 }, { "text": "you can't just ask the numbers\nthemselves or the elements themselves", "start": 3138.2, "duration": 3.55 }, { "text": "to do that moving.", "start": 3141.75, "duration": 1.11 }, { "text": "You need to do it for them, and\nthat's going to cost you time,", "start": 3142.86, "duration": 2.964 }, { "text": "and that's going to be a\nprice that you have to pay.", "start": 3145.824, "duration": 2.166 }, { "text": "But I propose that we\ncan be clever here.", "start": 3147.99, "duration": 3.24 }, { "text": "We do not need to incur the cost of\nmoving d, e, f, g h where Alice, Bob,", "start": 3151.23, "duration": 6.29 }, { "text": "and Charlie previously were.", "start": 3157.52, "duration": 3.12 }, { "text": "Where can we put person i instead?", "start": 3160.64, "duration": 4.01 }, { "text": "I mean, there's obviously\nroom in the line,", "start": 3164.65, "duration": 2.35 }, { "text": "so maybe why don't we\njust put person i here?", "start": 3167.0, "duration": 2.645 }, { "text": "But, again, we don't want to piss off\neveryone who's already in the line.", "start": 3169.645, "duration": 4.135 }, { "text": "So, if this now is an array, we\nhave to be mindful of the fact", "start": 3173.78, "duration": 3.64 }, { "text": "that the front of this list has\nto be remembered separately.", "start": 3177.42, "duration": 5.41 }, { "text": "This data member here front really\nshould store not 0 in perpetuity", "start": 3182.83, "duration": 5.18 }, { "text": "but really 0, 1, 2, 3.", "start": 3188.01, "duration": 2.2 }, { "text": "It should store the\ncurrent front of the list.", "start": 3190.21, "duration": 3.3 }, { "text": "And I need another\nvariable, presumably, called", "start": 3193.51, "duration": 2.63 }, { "text": "size that keeps track of how many\nelements are in the list, which", "start": 3196.14, "duration": 3.19 }, { "text": "in this case is going to be six.", "start": 3199.33, "duration": 2.33 }, { "text": "So with a queue, if I'm\nimplementing it using an array,", "start": 3201.66, "duration": 2.5 }, { "text": "there's some added\ncomplexity if I want to avoid", "start": 3204.16, "duration": 2.62 }, { "text": "the inefficiency of moving\nall of these elements", "start": 3206.78, "duration": 3.022 }, { "text": "and incurring the kind of running times\nwe saw when we talked about searching", "start": 3209.802, "duration": 3.208 }, { "text": "and sorting the other day.", "start": 3213.01, "duration": 1.19 }, { "text": "There's no reason mechanically to move\nd, e, f, g, h anywhere in the array.", "start": 3214.2, "duration": 5.49 }, { "text": "We can in constant time, and maybe\nour old friend the modulo operator", "start": 3219.69, "duration": 3.68 }, { "text": "that you might have\nused in [INAUDIBLE], we", "start": 3223.37, "duration": 2.33 }, { "text": "can just figure out where\ni, and j, and everyone else", "start": 3225.7, "duration": 2.7 }, { "text": "should go so long as we\nkeep track separately", "start": 3228.4, "duration": 2.61 }, { "text": "with a queue of what the\nfront of the list would be.", "start": 3231.01, "duration": 2.57 }, { "text": "And why is this 3?", "start": 3233.58, "duration": 0.98 }, { "text": "Well, if I continue numbering the\narray like this, as we've often done,", "start": 3234.56, "duration": 4.66 }, { "text": "you can now see that d is the head of\nthe list, or the front of the list.", "start": 3239.22, "duration": 4.21 }, { "text": "And so, we should remember\nhis location there as 3.", "start": 3243.43, "duration": 3.96 }, { "text": "But, again, what happens if\nj, k, and then l shows up?", "start": 3247.39, "duration": 4.45 }, { "text": "There is no room for l in this\nworld, not to mention m, n, o.", "start": 3251.84, "duration": 3.76 }, { "text": "So what if we solved\nthis problem as before", "start": 3255.6, "duration": 3.26 }, { "text": "by changing the array from having\nsome fixed capacity to having", "start": 3258.86, "duration": 4.85 }, { "text": "no pre-determined capacity,\njust use a pointer so that we", "start": 3263.71, "duration": 3.76 }, { "text": "can use malloc to dynamically\nallocate a big chunk of memory,", "start": 3267.47, "duration": 3.21 }, { "text": "remember its capacity\nultimately, but also", "start": 3270.68, "duration": 3.32 }, { "text": "remember the front and the\nsize of this data structure?", "start": 3274.0, "duration": 2.87 }, { "text": "So, the same idea there might apply.", "start": 3276.87, "duration": 3.34 }, { "text": "So, at the end of the\nday, what have we done?", "start": 3280.21, "duration": 2.15 }, { "text": "We've taken these new\nbuilding blocks, pointers,", "start": 3282.36, "duration": 4.04 }, { "text": "and this notion of linking\nthings together using pointers", "start": 3286.4, "duration": 2.57 }, { "text": "much like a linked list,\nand we've looked back", "start": 3288.97, "duration": 2.24 }, { "text": "at our data structure called an array.", "start": 3291.21, "duration": 2.12 }, { "text": "And using these now, we can implement\nwhat are generally called abstract data", "start": 3293.33, "duration": 3.69 }, { "text": "types, a queue in a stack\ndoes not have as a low level", "start": 3297.02, "duration": 3.2 }, { "text": "meaning as an array does, which\nis a very technical concept.", "start": 3300.22, "duration": 2.759 }, { "text": "And in a linked list, this\nis a very technical concept.", "start": 3302.979, "duration": 2.291 }, { "text": "It's a node with pointers\nlinking things together.", "start": 3305.27, "duration": 2.21 }, { "text": "A stack is like a stack\nof cafeteria trays,", "start": 3307.48, "duration": 2.521 }, { "text": "or a queue is something like people\nlining up outside of an Apple Store.", "start": 3310.001, "duration": 2.999 }, { "text": "These are abstract data types\nthat can be implemented clearly", "start": 3313.0, "duration": 3.25 }, { "text": "underneath the hood in at least\na couple of different ways.", "start": 3316.25, "duration": 3.09 }, { "text": "You can use an array and\nkeep things simple, a la", "start": 3319.34, "duration": 2.15 }, { "text": "weeks two and beyond in\nthe class, but you're", "start": 3321.49, "duration": 2.35 }, { "text": "going to paint yourself into\na corner by fixing their size,", "start": 3323.84, "duration": 2.51 }, { "text": "as I did a moment ago,\nby declaring this queue", "start": 3326.35, "duration": 2.75 }, { "text": "and before it a stack to\nbe of a fixed capacity.", "start": 3329.1, "duration": 2.71 }, { "text": "But now that we have pointers, and\nmalloc, and dynamic memory allocation,", "start": 3331.81, "duration": 3.35 }, { "text": "and this spirit of linked\nlists, we can change", "start": 3335.16, "duration": 3.06 }, { "text": "that to actually be\nnumbers and actually just", "start": 3338.22, "duration": 2.95 }, { "text": "remember where things\nare underneath the hood.", "start": 3341.17, "duration": 2.46 }, { "text": "And nicely enough, a stack\nin a queue doesn't even", "start": 3343.63, "duration": 2.21 }, { "text": "need me to stitch things\ntogether like a linked list.", "start": 3345.84, "duration": 2.84 }, { "text": "I just need malloc in\norder to allocate those.", "start": 3348.68, "duration": 4.05 }, { "text": "So, let's tie these two\ntopics together if you would", "start": 3352.73, "duration": 2.19 }, { "text": "and compare and contrast them\nby way of a wonderful animation", "start": 3354.92, "duration": 3.28 }, { "text": "that a fellow educator\nmade and posted online", "start": 3358.2, "duration": 2.1 }, { "text": "that we've abbreviated here to give us\na sense of the difference between stacks", "start": 3360.3, "duration": 3.99 }, { "text": "and queues.", "start": 3364.29, "duration": 0.709 }, { "text": "[AUDIO PLAYBACK]", "start": 3364.999, "duration": 0.666 }, { "text": "[MUSIC PLAYING]", "start": 3365.665, "duration": 4.655 }, { "text": "-Once upon a time, there\nwas a guy named Jack.", "start": 3370.32, "duration": 2.91 }, { "text": "When it came to making friends,\nJack did not have the knack.", "start": 3373.23, "duration": 3.36 }, { "text": "So, Jack went to talk to the\nmost popular guy he knew.", "start": 3376.59, "duration": 2.98 }, { "text": "He went to Lou and asked what do I do?", "start": 3379.57, "duration": 2.6 }, { "text": "Lou saw that his friend\nwas really distressed.", "start": 3382.17, "duration": 2.65 }, { "text": "Well, Lou, began just\nlook how you're dressed.", "start": 3384.82, "duration": 2.46 }, { "text": "Don't you have any clothes\nwith a different look?", "start": 3387.28, "duration": 2.73 }, { "text": "Yes, said, Jack.", "start": 3390.01, "duration": 0.97 }, { "text": "I sure do.", "start": 3390.98, "duration": 1.19 }, { "text": "Come to my house and\nI'll show them to you.", "start": 3392.17, "duration": 2.46 }, { "text": "So, they went off to Jack's,\nand Jack showed Lou the box", "start": 3394.63, "duration": 2.96 }, { "text": "where he kept all his shirts,\nand his pants, and his socks Lou", "start": 3397.59, "duration": 3.22 }, { "text": "said I see you have all\nyour clothes in a pile.", "start": 3400.81, "duration": 2.82 }, { "text": "Why don't you wear some\nothers once in a while?", "start": 3403.63, "duration": 2.53 }, { "text": "Jack said, well, when I\nremove clothes and socks,", "start": 3406.16, "duration": 3.17 }, { "text": "I wash them and put them away in\nthe box, then comes the next morning", "start": 3409.33, "duration": 4.0 }, { "text": "and up I hop.", "start": 3413.33, "duration": 1.2 }, { "text": "I go to the box and get\nmy clothes off the top.", "start": 3414.53, "duration": 3.18 }, { "text": "Lou quickly realized\nthe problem with Jack.", "start": 3417.71, "duration": 2.73 }, { "text": "He kept clothes, CDs,\nand books in a stack.", "start": 3420.44, "duration": 2.9 }, { "text": "When he reached something\nto read or to wear", "start": 3423.34, "duration": 2.45 }, { "text": "he chose [? the top ?]\nbook or underwear.", "start": 3425.79, "duration": 2.62 }, { "text": "Then when he was done he\nwould put it right back,", "start": 3428.41, "duration": 2.41 }, { "text": "back it would go on top of the stack.", "start": 3430.82, "duration": 2.49 }, { "text": "I know the solution,\nsaid a triumphant Lou.", "start": 3433.31, "duration": 2.49 }, { "text": "You need to learn to\nstart using a queue.", "start": 3435.8, "duration": 2.59 }, { "text": "Lou took Jack's clothes\nand hung them in a closet,", "start": 3438.39, "duration": 2.81 }, { "text": "and when he had emptied\nthe box he just tossed it.", "start": 3441.2, "duration": 2.84 }, { "text": "Then he said, now, Jack, at the\nend of the day put your clothes", "start": 3444.04, "duration": 3.25 }, { "text": "on the left when you put them away.", "start": 3447.29, "duration": 2.01 }, { "text": "Then tomorrow morning\nwhen you see the sunshine,", "start": 3449.3, "duration": 2.51 }, { "text": "get your clothes from right\nfrom the end of the line.", "start": 3451.81, "duration": 2.96 }, { "text": "Don't you see, said Lou.", "start": 3454.77, "duration": 1.44 }, { "text": "It will be so nice.", "start": 3456.21, "duration": 1.49 }, { "text": "You'll wear everything once\nbefore you wear something twice.", "start": 3457.7, "duration": 3.4 }, { "text": "And with everything in queues\nin his closet and shelf,", "start": 3461.1, "duration": 2.85 }, { "text": "Jack started to feel quite\nsure of himself all thanks", "start": 3463.95, "duration": 3.06 }, { "text": "to Lou and his wonderful queue.", "start": 3467.01, "duration": 4.576 }, { "text": "[END PLAYBACK]", "start": 3471.586, "duration": 0.982 }, { "text": "SPEAKER 1: All right, so let's\ntake a look at another data type,", "start": 3472.568, "duration": 3.712 }, { "text": "this one known as a tree.", "start": 3476.28, "duration": 1.94 }, { "text": "Because now that we have the ability\nto stitch data structures together much", "start": 3478.22, "duration": 4.16 }, { "text": "like a linked list, we\nnow have the ability", "start": 3482.38, "duration": 1.89 }, { "text": "to stitch things together not just left\nto right or top to bottom conceptually,", "start": 3484.27, "duration": 5.78 }, { "text": "but in any number of directions.", "start": 3490.05, "duration": 1.345 }, { "text": "And indeed, there's nothing\nstopping us from having", "start": 3491.395, "duration": 2.125 }, { "text": "one node linked to by way of\nmultiple pointers, multiple nodes.", "start": 3493.52, "duration": 4.41 }, { "text": "So, for instance, this picture here\nfrom a textbook is a tree structure.", "start": 3497.93, "duration": 4.34 }, { "text": "And it's very much like the\nfamily trees that you might", "start": 3502.27, "duration": 2.406 }, { "text": "have drawn in grade school or the like.", "start": 3504.676, "duration": 1.624 }, { "text": "But in this case, you\nhave just one root node,", "start": 3506.3, "duration": 2.91 }, { "text": "the node at the top of the\ndata structure, so to speak,", "start": 3509.21, "duration": 2.53 }, { "text": "from which everything else descends.", "start": 3511.74, "duration": 2.01 }, { "text": "And that node is said to have children.", "start": 3513.75, "duration": 2.26 }, { "text": "For instance, 2 and 3 are children\nof the node number 1 here.", "start": 3516.01, "duration": 4.06 }, { "text": "And then there's other semantics in\nthis world of trees in computer science.", "start": 3520.07, "duration": 3.52 }, { "text": "Much like family trees,\nanything that does not", "start": 3523.59, "duration": 2.53 }, { "text": "have children-- like 5,\n6, and 7, or 8 and 9--", "start": 3526.12, "duration": 2.7 }, { "text": "would be called leaves of the\ntree, because like the leaves", "start": 3528.82, "duration": 2.995 }, { "text": "at the end of the branches,\nthere is nothing beyond them.", "start": 3531.815, "duration": 2.375 }, { "text": "So, nicely enough we borrow\na lot of the language", "start": 3534.19, "duration": 2.53 }, { "text": "from family trees and\nactual trees in order", "start": 3536.72, "duration": 2.75 }, { "text": "to discuss this data\nstructure known as a tree.", "start": 3539.47, "duration": 3.14 }, { "text": "But why in the world would we want\nto lay out data in a tree structure?", "start": 3542.61, "duration": 5.05 }, { "text": "Now we just seem to be\ndoing things because we can,", "start": 3547.66, "duration": 3.06 }, { "text": "it would seem at first glance.", "start": 3550.72, "duration": 1.57 }, { "text": "Because, for instance, suppose we had\nthese numbers-- 22, 33, 44, 55, 66, 77,", "start": 3552.29, "duration": 5.14 }, { "text": "and 88.", "start": 3557.43, "duration": 1.1 }, { "text": "They're clearly sorted.", "start": 3558.53, "duration": 1.1 }, { "text": "And suppose that I wanted to lay\nthese out in a data structure", "start": 3559.63, "duration": 3.27 }, { "text": "and be able to search them\nefficiently, assuming the whole time", "start": 3562.9, "duration": 4.19 }, { "text": "that they are indeed sorted.", "start": 3567.09, "duration": 1.19 }, { "text": "Well, if we wanted to do that, we have\nour old friend arrays from weeks ago.", "start": 3568.28, "duration": 4.26 }, { "text": "And we also have our old algorithm\nfrom Mike Smith, our binary search", "start": 3572.54, "duration": 4.12 }, { "text": "algorithm, divide and conquer.", "start": 3576.66, "duration": 1.53 }, { "text": "And we can find nodes in\nthis data structure super,", "start": 3578.19, "duration": 2.52 }, { "text": "super fast in logarithmic\ntime, big O of log n.", "start": 3580.71, "duration": 4.39 }, { "text": "So, we've solved that problem.", "start": 3585.1, "duration": 1.67 }, { "text": "But it turns out we don't necessarily\nhave to use an array laying out data", "start": 3586.77, "duration": 5.08 }, { "text": "from left to right because, again, one\nof the prices we pay of using arrays", "start": 3591.85, "duration": 5.97 }, { "text": "where as we've realized\ntoday is this finiteness.", "start": 3597.82, "duration": 3.03 }, { "text": "At the end of the day, the\nsize of an array is fixed.", "start": 3600.85, "duration": 2.56 }, { "text": "You have to decide in advance how\nbig your array is going to be.", "start": 3603.41, "duration": 3.35 }, { "text": "So, what if you want to\nadd more numbers to it?", "start": 3606.76, "duration": 1.999 }, { "text": "What if you want to remove\nnumbers for efficiency", "start": 3608.759, "duration": 2.041 }, { "text": "and not waste so much memory?", "start": 3610.8, "duration": 1.46 }, { "text": "You can't really do that with an array.", "start": 3612.26, "duration": 1.67 }, { "text": "You can, but have to\njump through some hoops.", "start": 3613.93, "duration": 2.54 }, { "text": "You have to reallocate the array, as\nwith a function like [? re-alloc ?]", "start": 3616.47, "duration": 3.47 }, { "text": "if you indeed used malloc in\nthe first place to allocate it.", "start": 3619.94, "duration": 2.93 }, { "text": "But then you have to copy the\nold array into the new array,", "start": 3622.87, "duration": 2.65 }, { "text": "so it's all possible.", "start": 3625.52, "duration": 1.29 }, { "text": "Nothing's impossible once you\nhave a keyboard at your disposal,", "start": 3626.81, "duration": 3.96 }, { "text": "but it's a lot of work,\nand it's more time,", "start": 3630.77, "duration": 1.96 }, { "text": "and it's expensive there for\nboth in terms of your time", "start": 3632.73, "duration": 2.3 }, { "text": "and the computer's time.", "start": 3635.03, "duration": 1.7 }, { "text": "But could we achieve\nthe beauty of divide", "start": 3636.73, "duration": 2.91 }, { "text": "and conquer and binary search from\nweek zero without the constraints", "start": 3639.64, "duration": 4.28 }, { "text": "that arrays impose?", "start": 3643.92, "duration": 1.31 }, { "text": "And today, the solution to\nall of our array problems", "start": 3645.23, "duration": 2.61 }, { "text": "seems to be linked lists or\nmore generally pointers so", "start": 3647.84, "duration": 3.33 }, { "text": "that, one, we can dynamically allocate\nmore memory with malloc when we need it", "start": 3651.17, "duration": 3.25 }, { "text": "and then use pointers to\nthread or stitch together", "start": 3654.42, "duration": 2.88 }, { "text": "that node with any existing nodes.", "start": 3657.3, "duration": 2.52 }, { "text": "So, indeed let me propose this\nvariant on a tree structure", "start": 3659.82, "duration": 3.92 }, { "text": "that the world calls binary\nsearch trees or BSTs.", "start": 3663.74, "duration": 2.98 }, { "text": "Binary in this case means two, and this\njust means that every node in this tree", "start": 3666.72, "duration": 4.59 }, { "text": "is going to have 0, 1,\nor 2 maximally children.", "start": 3671.31, "duration": 4.7 }, { "text": "And now, in this case binary search tree\nmeans that for every node in the tree", "start": 3676.01, "duration": 7.35 }, { "text": "it's left child is less than it and\nits right child is greater than it.", "start": 3683.36, "duration": 5.36 }, { "text": "And that's a recursive definition.", "start": 3688.72, "duration": 1.67 }, { "text": "You can look at the root of this\ntree and ask that same question.", "start": 3690.39, "duration": 3.48 }, { "text": "55?", "start": 3693.87, "duration": 0.7 }, { "text": "Is it greater than its left child?", "start": 3694.57, "duration": 1.5 }, { "text": "Yep.", "start": 3696.07, "duration": 0.54 }, { "text": "Is it less than its right child?", "start": 3696.61, "duration": 1.38 }, { "text": "Yep.", "start": 3697.99, "duration": 0.83 }, { "text": "That is the beginning, it would\nseem, of a binary search tree.", "start": 3698.82, "duration": 2.67 }, { "text": "But it's recursive in so far as\nthis is indeed a binary search", "start": 3701.49, "duration": 2.92 }, { "text": "tree if that statement is true.", "start": 3704.41, "duration": 2.08 }, { "text": "Those answers are the same for\nevery other node in the tree.", "start": 3706.49, "duration": 2.88 }, { "text": "33, is its left child smaller?", "start": 3709.37, "duration": 2.16 }, { "text": "Yep.", "start": 3711.53, "duration": 0.5 }, { "text": "Is its right child bigger?", "start": 3712.03, "duration": 1.2 }, { "text": "Yep.", "start": 3713.23, "duration": 0.53 }, { "text": "How about over here, 77?", "start": 3713.76, "duration": 1.16 }, { "text": "Left child smaller?", "start": 3714.92, "duration": 0.95 }, { "text": "Yep.", "start": 3715.87, "duration": 0.5 }, { "text": "Right child bigger?", "start": 3716.37, "duration": 1.16 }, { "text": "Yep, indeed.", "start": 3717.53, "duration": 0.95 }, { "text": "How about the leaves of the tree?", "start": 3718.48, "duration": 2.0 }, { "text": "Is 22 greater than its left child?", "start": 3720.48, "duration": 3.93 }, { "text": "I mean, yeah, there is no child,\nso yes, that's a fair statement.", "start": 3724.41, "duration": 3.4 }, { "text": "It certainly doesn't violate\nour guiding principle.", "start": 3727.81, "duration": 2.66 }, { "text": "Is it less than its right child, if any?", "start": 3730.47, "duration": 2.05 }, { "text": "Yes, there just isn't any.", "start": 3732.52, "duration": 1.82 }, { "text": "And so this is a binary search tree.", "start": 3734.34, "duration": 1.96 }, { "text": "And indeed, if you took a scissors and\nsnipped off any branch of this tree,", "start": 3736.3, "duration": 4.54 }, { "text": "you would have another binary\nsearch tree, albeit smaller.", "start": 3740.84, "duration": 3.98 }, { "text": "But it's recursive and that definition\napplies to every one of the nodes.", "start": 3744.82, "duration": 3.09 }, { "text": "But what's beautiful here now is that\nif we implement this binary search", "start": 3747.91, "duration": 3.88 }, { "text": "tree, similar in spirit to how\nwe implemented linked lists", "start": 3751.79, "duration": 2.89 }, { "text": "using not arrays but using pointers\nand not one pointer but two pointers", "start": 3754.68, "duration": 4.34 }, { "text": "whereby every node in this tree\napparently has up to two pointers--", "start": 3759.02, "duration": 4.42 }, { "text": "let's call them not next but how about\nleft and right just to be intuitive.", "start": 3763.44, "duration": 5.03 }, { "text": "Well, if every node has a\nleft and a right pointer,", "start": 3768.47, "duration": 2.83 }, { "text": "now you can conceptually attach\nyourself to another node over there", "start": 3771.3, "duration": 3.59 }, { "text": "and another node over there,\nand they too can do the same.", "start": 3774.89, "duration": 3.29 }, { "text": "So, we have the syntax already with our\npointers with which to implement this.", "start": 3778.18, "duration": 4.22 }, { "text": "But why would we?", "start": 3782.4, "duration": 1.03 }, { "text": "Well, one, if we're using\npointers now and not an array,", "start": 3783.43, "duration": 2.8 }, { "text": "I can very, very easily allocate\nmore nodes for this tree.", "start": 3786.23, "duration": 3.97 }, { "text": "I can insert 99 or 11 really\neasily, because I just", "start": 3790.2, "duration": 4.26 }, { "text": "called malloc like I did before.", "start": 3794.46, "duration": 1.82 }, { "text": "I put the number 99 or\n11 inside of that node,", "start": 3796.28, "duration": 2.76 }, { "text": "and then I start from\nthe root of the tree,", "start": 3799.04, "duration": 1.88 }, { "text": "much like I start from the first\nelement in the linked list,", "start": 3800.92, "duration": 2.78 }, { "text": "and I just search for its destined\nlocation going left or right", "start": 3803.7, "duration": 3.81 }, { "text": "based on the size of that value.", "start": 3807.51, "duration": 1.62 }, { "text": "And what's nice, too, here is\nnotice how short the tree is.", "start": 3809.13, "duration": 3.85 }, { "text": "This is not a linked list.", "start": 3812.98, "duration": 1.17 }, { "text": "It's not a long list, whether\nvertically or horizontally.", "start": 3814.15, "duration": 3.5 }, { "text": "This is very shallow this tree.", "start": 3817.65, "duration": 2.93 }, { "text": "And indeed I claim that if we've\ngot n elements in this list,", "start": 3820.58, "duration": 4.33 }, { "text": "the height of this tree\nit turns out is log of n.", "start": 3824.91, "duration": 4.04 }, { "text": "So, the height of this tree is\nlog of n, give or take one or so.", "start": 3828.95, "duration": 4.86 }, { "text": "But that's compelling, because\nhow do I search this tree?", "start": 3833.81, "duration": 3.14 }, { "text": "Suppose I am asked-- I'm trying to\nanswer the question is 44 on my list?", "start": 3836.95, "duration": 3.855 }, { "text": "How do I answer that?", "start": 3840.805, "duration": 0.875 }, { "text": "Well, we humans can obviously just look\nback and it's like, yes, 44 is in it.", "start": 3841.68, "duration": 2.97 }, { "text": "It's not how a computer works.", "start": 3844.65, "duration": 1.17 }, { "text": "We have to start from what\nwe're given, which in this case", "start": 3845.82, "duration": 2.416 }, { "text": "is going to be as the arrow\nsuggests a pointer to the tree", "start": 3848.236, "duration": 2.864 }, { "text": "itself, a pointer towards first node.", "start": 3851.1, "duration": 1.77 }, { "text": "And I look is this the number 44?", "start": 3852.87, "duration": 2.36 }, { "text": "Obviously not.", "start": 3855.23, "duration": 1.36 }, { "text": "55 is greater than 44, so I'm\ngoing to go down to the left child", "start": 3856.59, "duration": 3.835 }, { "text": "and ask that same question.", "start": 3860.425, "duration": 1.125 }, { "text": "33, is this 44?", "start": 3861.55, "duration": 1.19 }, { "text": "Obviously not, but it's\nless than it so I'm", "start": 3862.74, "duration": 2.45 }, { "text": "going to go down to the right child.", "start": 3865.19, "duration": 1.56 }, { "text": "Is this 44?", "start": 3866.75, "duration": 0.98 }, { "text": "Yes, and simply by\nlooking at three nodes", "start": 3867.73, "duration": 4.88 }, { "text": "have I whittled this problem\ndown to my yes no answer.", "start": 3872.61, "duration": 3.96 }, { "text": "And indeed, you can think\nof it again with scissors.", "start": 3876.57, "duration": 2.49 }, { "text": "I'm looking at 55 at the\nbeginning of this story.", "start": 3879.06, "duration": 2.61 }, { "text": "Is 44 55?", "start": 3881.67, "duration": 1.07 }, { "text": "No, 44 is less.", "start": 3882.74, "duration": 1.12 }, { "text": "Well, you know what?", "start": 3883.86, "duration": 1.21 }, { "text": "I can effectively snip off\nthe right half of that tree,", "start": 3885.07, "duration": 4.29 }, { "text": "much like I tore that phone book in\nweek zero, throwing half of the problem", "start": 3889.36, "duration": 3.55 }, { "text": "away.", "start": 3892.91, "duration": 0.5 }, { "text": "Here I can throw essentially half of the\ntree away and search only what remains", "start": 3893.41, "duration": 4.41 }, { "text": "and then repeat that process again,\nand again, and again, whittling", "start": 3897.82, "duration": 3.2 }, { "text": "the tree down by half every time.", "start": 3901.02, "duration": 2.3 }, { "text": "So therein lies our\nlogarithmic running time.", "start": 3903.32, "duration": 3.14 }, { "text": "Therein lies the height\nof the tree, so long", "start": 3906.46, "duration": 3.43 }, { "text": "as I am good about\nkeeping the tree balanced.", "start": 3909.89, "duration": 5.88 }, { "text": "There's a danger.", "start": 3915.77, "duration": 1.25 }, { "text": "Suppose that I go ahead and start\nbuilding this tree myself in code", "start": 3917.02, "duration": 6.42 }, { "text": "and I'm a little sloppy\nabout doing that.", "start": 3923.44, "duration": 2.59 }, { "text": "And I go ahead and I insert, for\ninstance, let's say the number 33.", "start": 3926.03, "duration": 7.88 }, { "text": "And it's the first\nnode in my tree, so I'm", "start": 3933.91, "duration": 2.28 }, { "text": "going to put it right\nup here at the top.", "start": 3936.19, "duration": 2.69 }, { "text": "And now suppose that the\nnext number that just happens", "start": 3938.88, "duration": 2.45 }, { "text": "to get inserted into this tree is 44.", "start": 3941.33, "duration": 2.225 }, { "text": "Well, where does it go?", "start": 3943.555, "duration": 1.485 }, { "text": "Well, it has no children\nyet, but it is bigger,", "start": 3945.04, "duration": 2.57 }, { "text": "so it should probably go over here.", "start": 3947.61, "duration": 1.5 }, { "text": "So, yeah, I'll draw 44 there.", "start": 3949.11, "duration": 1.79 }, { "text": "Now, suppose that the\ninputs to this problem", "start": 3950.9, "duration": 2.36 }, { "text": "are such that 55 is inserted next.", "start": 3953.26, "duration": 1.79 }, { "text": "Where does it go?", "start": 3955.05, "duration": 1.19 }, { "text": "All right, 55, it's bigger,\nso it should go over here.", "start": 3956.24, "duration": 4.09 }, { "text": "And then 66 is inserted next.", "start": 3960.33, "duration": 2.2 }, { "text": "All right, it goes over\nhere-- never mind that.", "start": 3962.53, "duration": 6.14 }, { "text": "So, what's happening to\nmy binary search tree?", "start": 3968.67, "duration": 3.342 }, { "text": "Well, first of all, is\nit a binary search tree?", "start": 3972.012, "duration": 1.958 }, { "text": "It is because this node is\nbigger than its left child,", "start": 3973.97, "duration": 4.51 }, { "text": "if any-- there just isn't any--\nand it's less than its right child.", "start": 3978.48, "duration": 3.69 }, { "text": "How about here, 44?", "start": 3982.17, "duration": 1.02 }, { "text": "It's bigger than its left child,\nif any-- because there is none--", "start": 3983.19, "duration": 2.81 }, { "text": "and it's smaller than its right child.", "start": 3986.0, "duration": 1.94 }, { "text": "The same thing is true for 55,\nthe same thing is true for 66.", "start": 3987.94, "duration": 2.71 }, { "text": "So, this is a binary search tree and\nyet somehow what does it look like?", "start": 3990.65, "duration": 5.41 }, { "text": "It looks like a linked list, right?", "start": 3996.06, "duration": 2.27 }, { "text": "It's at a weird angle.", "start": 3998.33, "duration": 1.09 }, { "text": "I've been drawing\neverything horizontally,", "start": 3999.42, "duration": 1.75 }, { "text": "but that's a meaningless\nartistic detail.", "start": 4001.17, "duration": 2.12 }, { "text": "It devolves potentially\ninto a linked list.", "start": 4003.29, "duration": 2.67 }, { "text": "And so, binary search trees if\nthey are balanced, so to speak,", "start": 4005.96, "duration": 3.58 }, { "text": "if they are built in the right order\nor built with the right insertion", "start": 4009.54, "duration": 3.8 }, { "text": "algorithm such that they do\nhave this balanced height,", "start": 4013.34, "duration": 4.0 }, { "text": "this logarithmic height, do afford\nus the same logarithmic running time", "start": 4017.34, "duration": 4.49 }, { "text": "that the phone book example did and\nour binary search of an array did.", "start": 4021.83, "duration": 4.04 }, { "text": "But we have to do a little\nbit more work in order", "start": 4025.87, "duration": 2.47 }, { "text": "to make sure that these\ntrees are balanced.", "start": 4028.34, "duration": 2.05 }, { "text": "And we won't go into detail\nas to the algorithmics", "start": 4030.39, "duration": 2.21 }, { "text": "of keeping the tree balanced.", "start": 4032.6, "duration": 2.07 }, { "text": "But realize, again, there's\ngoing to be this trade-off.", "start": 4034.67, "duration": 2.44 }, { "text": "Yes, you can use a binary search tree or\ntrees more generally to store numbers.", "start": 4037.11, "duration": 4.03 }, { "text": "Yes, they can allow you to achieve that\nsame binary or logarithmic running time", "start": 4041.14, "duration": 4.78 }, { "text": "that we've gotten so\nused to with arrays,", "start": 4045.92, "duration": 1.94 }, { "text": "but they also give us\ndynamism such that we", "start": 4047.86, "duration": 2.34 }, { "text": "can keep adding or even removing nodes.", "start": 4050.2, "duration": 2.39 }, { "text": "But, but, but, but it\nturns out we're going", "start": 4052.59, "duration": 2.58 }, { "text": "to have to think a lot harder about\nhow to keep these things balanced.", "start": 4055.17, "duration": 4.02 }, { "text": "And indeed, in higher\nlevel CS courses, courses", "start": 4059.19, "duration": 2.35 }, { "text": "on data structures and\nalgorithms will you", "start": 4061.54, "duration": 1.75 }, { "text": "explore concepts along\nexactly those lines.", "start": 4063.29, "duration": 2.96 }, { "text": "How would you go about implementing\ninsert and delete into a tree", "start": 4066.25, "duration": 4.34 }, { "text": "so that you do maintain this balance?", "start": 4070.59, "duration": 2.01 }, { "text": "And there is yet more variance\non these kinds of trees", "start": 4072.6, "duration": 2.3 }, { "text": "that you'll encounter accordingly.", "start": 4074.9, "duration": 1.55 }, { "text": "But for our purposes, let's consider\nhow you would implement the tree itself", "start": 4076.45, "duration": 3.59 }, { "text": "independent of how you might\nimplement those actual algorithms.", "start": 4080.04, "duration": 4.27 }, { "text": "Let me propose this type of node.", "start": 4084.31, "duration": 1.74 }, { "text": "Again, notice just the very\ngeneric term in programming", "start": 4086.05, "duration": 2.62 }, { "text": "where it's usually like a container for\none or more other things, and this time", "start": 4088.67, "duration": 4.436 }, { "text": "those things are an\ninteger-- we'll call it n", "start": 4093.106, "duration": 1.874 }, { "text": "but it could be called\nanything-- and two pointers.", "start": 4094.98, "duration": 2.87 }, { "text": "And instead of next, I'm going to just\nby convention call them left and right.", "start": 4097.85, "duration": 4.78 }, { "text": "And as before, notice that I do\nneed to declare struct node up", "start": 4102.63, "duration": 5.13 }, { "text": "here or some word up here.", "start": 4107.76, "duration": 1.769 }, { "text": "But by convention I'm just going to do\ntypedef struct node, because C reads", "start": 4109.529, "duration": 4.431 }, { "text": "things top to bottom, left to right.", "start": 4113.96, "duration": 1.5 }, { "text": "So if I want to refer to\na node inside of a node,", "start": 4115.46, "duration": 3.069 }, { "text": "I need to have that vocabulary, per\nthis first line, even though later on I", "start": 4118.529, "duration": 4.381 }, { "text": "just want to call this\nwhole darn thing a node.", "start": 4122.91, "duration": 3.019 }, { "text": "And so, that's the distinction.", "start": 4125.929, "duration": 1.291 }, { "text": "This actually has the side\neffect of creating a data", "start": 4127.22, "duration": 2.219 }, { "text": "type by two different names.", "start": 4129.439, "duration": 1.166 }, { "text": "One is called struct node, and\nyou can literally in your code", "start": 4130.605, "duration": 2.805 }, { "text": "write struct node something,\nstruct node something.", "start": 4133.41, "duration": 2.44 }, { "text": "It just feels unnecessarily\nverbose, so typedef", "start": 4135.85, "duration": 3.179 }, { "text": "allows you to simplify\nthis as just node, which", "start": 4139.029, "duration": 2.62 }, { "text": "refers to the same structure.", "start": 4141.649, "duration": 1.371 }, { "text": "But this is necessary for this\ninnermost implementation detail.", "start": 4143.02, "duration": 4.459 }, { "text": "So, now that we have the\nability with a structure", "start": 4147.479, "duration": 2.101 }, { "text": "to represent this thing, what\ncan we actually do with it?", "start": 4149.58, "duration": 3.29 }, { "text": "Well, here is where recursion\nfrom a few weeks ago", "start": 4152.87, "duration": 3.88 }, { "text": "actually gets really compelling.", "start": 4156.75, "duration": 1.75 }, { "text": "When we introduced that sigma example\na while ago and talked in the abstract", "start": 4158.5, "duration": 4.41 }, { "text": "about recursion, frankly, it's kind of\nhard to justify it early on, unless you", "start": 4162.91, "duration": 4.35 }, { "text": "actually have a problem that lends\nitself to recursion in a way that", "start": 4167.26, "duration": 3.41 }, { "text": "makes sense to use recursion\nand not just iteration,", "start": 4170.67, "duration": 2.559 }, { "text": "loops-- for loops, while\nloops, do while, and the like.", "start": 4173.229, "duration": 2.811 }, { "text": "And here we actually have a\nperfect incarnation of that.", "start": 4176.04, "duration": 2.739 }, { "text": "What does it mean to search\na binary search tree?", "start": 4178.779, "duration": 2.45 }, { "text": "Well, suppose I'm\nsearching for a number n", "start": 4181.229, "duration": 2.781 }, { "text": "and I'm being given a pointer\nto the root of the tree,", "start": 4184.01, "duration": 2.75 }, { "text": "and I'll call it tree.", "start": 4186.76, "duration": 1.04 }, { "text": "So, just like when I was\nsearching a linked list,", "start": 4187.8, "duration": 1.63 }, { "text": "I'm given two things, the\nnumber I'm looking for", "start": 4189.43, "duration": 1.82 }, { "text": "and a pointer to the first thing in\nthe data structure-- the first thing", "start": 4191.25, "duration": 2.779 }, { "text": "in a linked list or the\nfirst thing in a tree.", "start": 4194.029, "duration": 1.901 }, { "text": "And in this case, we would\ncall that first thing", "start": 4195.93, "duration": 1.999 }, { "text": "in a tree a root, generally speaking.", "start": 4197.929, "duration": 2.521 }, { "text": "So, the first thing I had\nbetter do in my search function", "start": 4200.45, "duration": 2.78 }, { "text": "is check, wait a minute.", "start": 4203.23, "duration": 1.16 }, { "text": "If tree equals equals\nnull, don't do anything.", "start": 4204.39, "duration": 3.88 }, { "text": "Do not risk touching any pointers,\nbecause as you may have gleaned already", "start": 4208.27, "duration": 3.95 }, { "text": "or soon will with some\nof CS50's problems,", "start": 4212.22, "duration": 2.5 }, { "text": "you will cause quite probably\na segmentation fault,", "start": 4214.72, "duration": 4.63 }, { "text": "a memory-related problem in your\nprogram such that it just crashes.", "start": 4219.35, "duration": 3.28 }, { "text": "It literally says segmentation\nfault on this screen", "start": 4222.63, "duration": 2.88 }, { "text": "if you touch memory that you should not.", "start": 4225.51, "duration": 1.71 }, { "text": "And you should not touch null values.", "start": 4227.22, "duration": 2.22 }, { "text": "You should not go to null values.", "start": 4229.44, "duration": 1.76 }, { "text": "You should not do star of any\nvalue that itself might be null.", "start": 4231.2, "duration": 3.89 }, { "text": "And so, if tree equals equals null\nis super, super important here,", "start": 4235.09, "duration": 3.74 }, { "text": "because I want to make\nsure to immediately", "start": 4238.83, "duration": 1.75 }, { "text": "say, well, if you hand me null, that's\nlike handing me no tree whatsoever.", "start": 4240.58, "duration": 3.53 }, { "text": "So, my answer is obviously false.", "start": 4244.11, "duration": 1.72 }, { "text": "N can't be in a non-existent tree.", "start": 4245.83, "duration": 2.84 }, { "text": "But we need that condition\nup top, because the next case", "start": 4248.67, "duration": 3.135 }, { "text": "is [? noticed through ?] the following.", "start": 4251.805, "duration": 1.625 }, { "text": "Else if n-- the value\nwe're looking for-- is less", "start": 4253.43, "duration": 3.95 }, { "text": "than the value of n in\nthis node-- tree, recall,", "start": 4257.38, "duration": 4.39 }, { "text": "doesn't refer to the whole\nthing, per se, in this context.", "start": 4261.77, "duration": 2.54 }, { "text": "It refers to the current\nnode that we've been", "start": 4264.31, "duration": 1.89 }, { "text": "past, which at the beginning of\nthe story is the root of the tree.", "start": 4266.2, "duration": 3.11 }, { "text": "So, if the number n in the root of\nthe tree is greater than the number", "start": 4269.31, "duration": 4.43 }, { "text": "we're looking for, we\nwant to go to the left.", "start": 4273.74, "duration": 3.77 }, { "text": "Else we want to go to the right\nand search the right subtree.", "start": 4277.51, "duration": 5.81 }, { "text": "So, what's the syntax here?", "start": 4283.32, "duration": 1.5 }, { "text": "If the n we're looking\nfor, like 44, is less", "start": 4284.82, "duration": 3.21 }, { "text": "than the value at the current node,\n55, then what do we want to do?", "start": 4288.03, "duration": 5.18 }, { "text": "We want to call search, still\nsearching for the same number n", "start": 4293.21, "duration": 4.06 }, { "text": "but searching on the left subtree.", "start": 4297.27, "duration": 2.85 }, { "text": "And how do you pass in a\npointer to the left tree?", "start": 4300.12, "duration": 3.86 }, { "text": "Well, you have in tree a\npointer to the root node.", "start": 4303.98, "duration": 3.44 }, { "text": "Tree arrow left just means go to my left\nchild and past that value in instead,", "start": 4307.42, "duration": 4.83 }, { "text": "pass its address in instead.", "start": 4312.25, "duration": 1.23 }, { "text": "Meanwhile, if the number\nyou're looking for", "start": 4313.48, "duration": 2.43 }, { "text": "is actually greater than the\nvalue in the current node, search", "start": 4315.91, "duration": 2.75 }, { "text": "the right subtree, else return true.", "start": 4318.66, "duration": 5.12 }, { "text": "Because if the list is not null-- if\nthere is actually a list and the number", "start": 4323.78, "duration": 4.56 }, { "text": "you're looking for is not\nless than the current node", "start": 4328.34, "duration": 2.27 }, { "text": "and it's not greater than the current\nnode, it must be the current node,", "start": 4330.61, "duration": 4.17 }, { "text": "so you can return true.", "start": 4334.78, "duration": 2.39 }, { "text": "But there's one important detail here.", "start": 4337.17, "duration": 2.09 }, { "text": "I didn't just call search.", "start": 4339.26, "duration": 1.96 }, { "text": "I called return search in each\nof these two middle cases.", "start": 4341.22, "duration": 3.75 }, { "text": "Why is that?", "start": 4344.97, "duration": 0.786 }, { "text": "Well, this is where recursion gets\npotentially a little mind bending.", "start": 4345.756, "duration": 2.874 }, { "text": "Recursion is the act of a\nfunction calling itself.", "start": 4348.63, "duration": 3.53 }, { "text": "Now, in and of itself, that sounds bad,\nbecause if a function calls itself,", "start": 4352.16, "duration": 3.63 }, { "text": "why wouldn't it call itself again, and\nagain, and again, and again, and again,", "start": 4355.79, "duration": 4.27 }, { "text": "and just do this\ninfinitely many times such", "start": 4360.06, "duration": 2.06 }, { "text": "that now you get a stack overflow\nwhere all of those frames on the stack", "start": 4362.12, "duration": 3.06 }, { "text": "hit the heap and bad things happen.", "start": 4365.18, "duration": 1.89 }, { "text": "But no, recursion works beautifully\nso long as every time you recurse,", "start": 4367.07, "duration": 5.58 }, { "text": "every time a function calls itself it\ntakes a smaller byte of the problem.", "start": 4372.65, "duration": 6.01 }, { "text": "Or rather, put another\nway, it throws away", "start": 4378.66, "duration": 2.31 }, { "text": "half of the problem, as in this case,\nand looks only at a remaining half.", "start": 4380.97, "duration": 3.042 }, { "text": "Because if you keep shrinking,\nshrinking, shrinking, shrinking", "start": 4384.012, "duration": 2.583 }, { "text": "the problem, you will\neventually hit this base case", "start": 4386.595, "duration": 2.475 }, { "text": "where either there is no more tree\nor you're looking right at the node", "start": 4389.07, "duration": 4.16 }, { "text": "that you want to find.", "start": 4393.23, "duration": 1.27 }, { "text": "And so, by returning\nsearch and tree left,", "start": 4394.5, "duration": 3.52 }, { "text": "or returning search and tree\nright, you're deferring the answer.", "start": 4398.02, "duration": 4.72 }, { "text": "When you, the search\nfunction, are called and asked", "start": 4402.74, "duration": 3.22 }, { "text": "is the number 44 in\nthis tree, you might not", "start": 4405.96, "duration": 2.875 }, { "text": "know because the node you're looking\nat at the beginning of the story", "start": 4408.835, "duration": 2.875 }, { "text": "was again 55.", "start": 4411.71, "duration": 1.57 }, { "text": "But you know who does know?", "start": 4413.28, "duration": 1.21 }, { "text": "I bet my left child will know\nthe answer to that if I just", "start": 4414.49, "duration": 3.05 }, { "text": "ask it by passing it-- passing to\nsearch a pointer to it, my left child,", "start": 4417.54, "duration": 6.98 }, { "text": "and passing in that same number 44.", "start": 4424.52, "duration": 2.31 }, { "text": "So, saying return search is\nlike saying I don't know.", "start": 4426.83, "duration": 3.5 }, { "text": "Ask my left child.", "start": 4430.33, "duration": 1.0 }, { "text": "Or I don't know, ask my right child\nand let me return as my answer", "start": 4431.33, "duration": 4.39 }, { "text": "whatever my child's answer is instead.", "start": 4435.72, "duration": 3.52 }, { "text": "So, you could do this same\nfunction using iteration.", "start": 4439.24, "duration": 4.4 }, { "text": "But you could solve it arguably much\nmore elegantly here using recursion,", "start": 4443.64, "duration": 5.475 }, { "text": "because a data structure like\nthis-- like a binary search tree,", "start": 4449.115, "duration": 2.625 }, { "text": "which again is recursively\ndefined-- each node is conceptually", "start": 4451.74, "duration": 4.15 }, { "text": "identical, if numerically\ndifferent from the others,", "start": 4455.89, "duration": 3.14 }, { "text": "allows us to apply this algorithm,\nthis recursive algorithm", "start": 4459.03, "duration": 4.69 }, { "text": "to that particular data structure.", "start": 4463.72, "duration": 3.264 }, { "text": "Now, let's look at a\nmore concrete incarnation", "start": 4466.984, "duration": 1.916 }, { "text": "of trees that allows us to do something\npretty neat and pretty real world.", "start": 4468.9, "duration": 4.23 }, { "text": "Indeed, this is another problem borne\nof a real world domain of compression.", "start": 4473.13, "duration": 5.02 }, { "text": "We talked a couple weeks\nago about encryption,", "start": 4478.15, "duration": 2.07 }, { "text": "the art of concealing or\nscrambling information.", "start": 4480.22, "duration": 2.33 }, { "text": "Compression, meanwhile, is the art\nof taking something that's this big", "start": 4482.55, "duration": 3.54 }, { "text": "and compressing it to make it smaller,\nideally without losing any information.", "start": 4486.09, "duration": 5.24 }, { "text": "It's pretty easy to take\na 10 page essay that's", "start": 4491.33, "duration": 3.01 }, { "text": "maybe-- that was supposed\nto be a five page essay", "start": 4494.34, "duration": 2.63 }, { "text": "and just remove paragraphs from\nit or remove sentences from it.", "start": 4496.97, "duration": 3.85 }, { "text": "But that changes the meaning of\nthe paper, makes it a worse paper,", "start": 4500.82, "duration": 3.45 }, { "text": "even though you're compressing\nit by making it smaller.", "start": 4504.27, "duration": 2.33 }, { "text": "No, what most students would typically\ndo, if you've written 10 pages", "start": 4506.6, "duration": 2.82 }, { "text": "and it needs to fit into five,\nyou really, really, really", "start": 4509.42, "duration": 2.3 }, { "text": "shrink the font size or\nincrease the margins.", "start": 4511.72, "duration": 1.95 }, { "text": "Or maybe more realistically you\nwrite a five page paper that's", "start": 4513.67, "duration": 2.732 }, { "text": "supposed to be a 10 page paper,\nand so you increase the font size", "start": 4516.402, "duration": 2.708 }, { "text": "or increase the margins so as to\nexpand or decompress the essay.", "start": 4519.11, "duration": 5.86 }, { "text": "So, similarly here, what if\nwe wanted to compress text,", "start": 4524.97, "duration": 4.57 }, { "text": "but we want to do it\nlosslessly in a way that we", "start": 4529.54, "duration": 2.9 }, { "text": "don't lose any information\nby just throwing away", "start": 4532.44, "duration": 2.21 }, { "text": "characters, or paragraphs,\nor pages, but we", "start": 4534.65, "duration": 4.214 }, { "text": "want to use the system with which\nwe're familiar from week zero.", "start": 4538.864, "duration": 2.666 }, { "text": "So ASCII, again, is just this code,\nthis mapping of letters to numbers.", "start": 4541.53, "duration": 3.38 }, { "text": "And so, A is-- capital A is 65\nand that's some pattern of bits,", "start": 4544.91, "duration": 4.07 }, { "text": "but it's some pattern of\n8 bits-- 7 historically,", "start": 4548.98, "duration": 3.13 }, { "text": "but really 8 bits in\npractice So every one", "start": 4552.11, "duration": 2.94 }, { "text": "of the characters in the\nEnglish alphabet, at least here,", "start": 4555.05, "duration": 3.68 }, { "text": "takes up 8 bits.", "start": 4558.73, "duration": 2.16 }, { "text": "Now, that sounds fine.", "start": 4560.89, "duration": 1.1 }, { "text": "That allows us to express as many\nas 256 possible characters, which", "start": 4561.99, "duration": 3.25 }, { "text": "is more than enough for English\ncharacters, plus some punctuation", "start": 4565.24, "duration": 2.74 }, { "text": "and so forth.", "start": 4567.98, "duration": 1.18 }, { "text": "But it seems wasteful.", "start": 4569.16, "duration": 2.23 }, { "text": "I type A, E, and I, maybe\nO and U pretty often.", "start": 4571.39, "duration": 3.24 }, { "text": "I use the values often--\nthe vowels often.", "start": 4574.63, "duration": 3.778 }, { "text": "B and D, I feel like I use those a lot.", "start": 4578.408, "duration": 2.852 }, { "text": "I don't really type Q all\nthat much, Z all that much.", "start": 4581.26, "duration": 4.31 }, { "text": "So, there are certain\nletters that I just", "start": 4585.57, "duration": 1.735 }, { "text": "feel like I don't type them\nthat often, and indeed,", "start": 4587.305, "duration": 2.125 }, { "text": "probably if we analyzed a dictionary,\nwe wouldn't see them as frequently", "start": 4589.43, "duration": 3.24 }, { "text": "as other letters.", "start": 4592.67, "duration": 0.82 }, { "text": "Indeed, if you've ever played\nor watched Wheel of Fortune,", "start": 4593.49, "duration": 2.5 }, { "text": "certainly all the\ncontestants on that show", "start": 4595.99, "duration": 2.04 }, { "text": "know which are the most popular\nletters in English words.", "start": 4598.03, "duration": 3.48 }, { "text": "And it seems silly and\nperhaps inefficient--", "start": 4601.51, "duration": 3.33 }, { "text": "certainly for a computer\nscientist-- that we are not somehow", "start": 4604.84, "duration": 2.65 }, { "text": "embracing the fact that some letters\nare more commonly used than others,", "start": 4607.49, "duration": 4.37 }, { "text": "and yet we are just blindly using\n8 bits, the same amount of memory,", "start": 4611.86, "duration": 3.92 }, { "text": "for every darn letter in our alphabet.", "start": 4615.78, "duration": 1.71 }, { "text": "Why?", "start": 4617.49, "duration": 0.9 }, { "text": "If you keep writing a certain\nletter again and again,", "start": 4618.39, "duration": 2.29 }, { "text": "why not use fewer bits for\nthe more popular letters,", "start": 4620.68, "duration": 3.12 }, { "text": "and more bits for the\nless popular letters", "start": 4623.8, "duration": 2.24 }, { "text": "so that at least you're optimizing\nfor the common case, so to speak?", "start": 4626.04, "duration": 4.61 }, { "text": "Well, it turns out that someone\nnamed Huffman years ago did", "start": 4630.65, "duration": 3.31 }, { "text": "figure this out and introduced what's\ngenerally known as Huffman coding.", "start": 4633.96, "duration": 3.61 }, { "text": "And, at first glance, it's a little\nsimilar in spirit to something", "start": 4637.57, "duration": 2.84 }, { "text": "some of you might have grown up learning\na little something about called Morse", "start": 4640.41, "duration": 3.249 }, { "text": "code, but it's better\nin a couple of ways.", "start": 4643.659, "duration": 2.141 }, { "text": "Morse code typically transmitted with\nelectrical signals or audible signals.", "start": 4645.8, "duration": 5.52 }, { "text": "It has dots and dashes\nwhere a dot is a quick beep", "start": 4651.32, "duration": 3.09 }, { "text": "and a dash is a slightly\nlonger beep, and you", "start": 4654.41, "duration": 2.43 }, { "text": "can use those series of dots and\ndashes, as per this chart here,", "start": 4656.84, "duration": 4.13 }, { "text": "to represent letters of the\nalphabet and some numbers.", "start": 4660.97, "duration": 3.18 }, { "text": "The one problem, though, as efficient\nas this seems-- and then by efficient", "start": 4664.15, "duration": 4.4 }, { "text": "I mean look at E. Mr. Morse realized\nthat is super popular, so he", "start": 4668.55, "duration": 4.14 }, { "text": "used literally the shortest symbol\nfor it, just a dot, a simple blip,", "start": 4672.69, "duration": 3.41 }, { "text": "to represent an E. And,\nmeanwhile, as I kind of imagined,", "start": 4676.1, "duration": 2.99 }, { "text": "Z is not that common,\nso dash, dash, dot,", "start": 4679.09, "duration": 3.47 }, { "text": "dot is longer than just a single dot.", "start": 4682.56, "duration": 2.33 }, { "text": "So Z is probably less popular,\nand that's why we did this.", "start": 4684.89, "duration": 2.59 }, { "text": "And Y may be even less\npopular-- dash, dot, dash--", "start": 4687.48, "duration": 3.437 }, { "text": "I don't know why I'm using this voice.", "start": 4690.917, "duration": 1.583 }, { "text": "But it's longer than E, so we\noptimized for the shorter characters.", "start": 4692.5, "duration": 5.056 }, { "text": "Unfortunately, suppose that you receive\nthe message dot, dot, dot, dot, dot,", "start": 4697.556, "duration": 10.064 }, { "text": "dot, so six dots in a row, and I\ntechnically paused in between them.", "start": 4707.62, "duration": 4.82 }, { "text": "Six dots, what message\ndid I just send you?", "start": 4712.44, "duration": 2.052 }, { "text": " ", "start": 4714.492, "duration": 3.698 }, { "text": "Six dots.", "start": 4718.19, "duration": 0.76 }, { "text": " ", "start": 4718.95, "duration": 5.232 }, { "text": "So, I wanted to say hi, so I said\ndot, dot, dot, dot, which is H,", "start": 4724.182, "duration": 4.068 }, { "text": "and then dot, dot which is I. I\nshould not have paused between them,", "start": 4728.25, "duration": 3.23 }, { "text": "because the whole point of Morse code\nis to do this as quickly as possible,", "start": 4731.48, "duration": 3.125 }, { "text": "even though you probably do want\nto pause to resolve ambiguity,", "start": 4734.605, "duration": 3.045 }, { "text": "and indeed, that's the problem.", "start": 4737.65, "duration": 1.53 }, { "text": "I wanted to send you\nhi, H-I, but maybe I", "start": 4739.18, "duration": 4.03 }, { "text": "just sent you E, E, E,\nE, E, E, six Es in a row,", "start": 4743.21, "duration": 4.59 }, { "text": "because those two were just dots.", "start": 4747.8, "duration": 1.69 }, { "text": "So, in other words, Morse code\nis not immediately decodable", "start": 4749.49, "duration": 2.87 }, { "text": "when you're reading, or hearing,\nor seeing the dots and dashes come", "start": 4752.36, "duration": 3.097 }, { "text": "over the wire, so to speak,\nbecause there's these ambiguities,", "start": 4755.457, "duration": 2.583 }, { "text": "unless this transmitter\ndoes indeed pause,", "start": 4758.04, "duration": 3.47 }, { "text": "as I accidentally did there, to give\nyou a moment to take your breath", "start": 4761.51, "duration": 3.54 }, { "text": "and realize, oh, that was an H. That's\nan I. As opposed to E, E, E, E, E, E.", "start": 4765.05, "duration": 4.95 }, { "text": "So, it's not necessarily the best\nsystem in so far as some letters", "start": 4770.0, "duration": 4.02 }, { "text": "share prefixes with other letters.", "start": 4774.02, "duration": 3.03 }, { "text": "In other words, I, dot dot, has a\ncommon prefix with E. Both of them", "start": 4777.05, "duration": 4.93 }, { "text": "start with a single dot.", "start": 4781.98, "duration": 1.485 }, { "text": "It just so happens that\nI is a little longer,", "start": 4783.465, "duration": 1.875 }, { "text": "and that can lead\npotentially to ambiguity,", "start": 4785.34, "duration": 1.91 }, { "text": "and it certainly means that the\ntransmitter should probably slow down.", "start": 4787.25, "duration": 4.86 }, { "text": "So, the whole system is meant to\nbe super fast, super efficient,", "start": 4792.11, "duration": 2.71 }, { "text": "but you probably should\npause between certain letters", "start": 4794.82, "duration": 3.04 }, { "text": "so that the recipient\ndoesn't get confused as", "start": 4797.86, "duration": 1.98 }, { "text": "to the message you're actually sending.", "start": 4799.84, "duration": 2.11 }, { "text": "Well, thankfully Huffman coding--\nwhich as we'll see in a moment", "start": 4801.95, "duration": 3.25 }, { "text": "is based on trees-- does\nnot have that ambiguity.", "start": 4805.2, "duration": 2.6 }, { "text": "It is a immediately decodable.", "start": 4807.8, "duration": 1.76 }, { "text": "And suppose for the sake of\ndiscussion, as per this example here,", "start": 4809.56, "duration": 3.06 }, { "text": "you just have a whole bunch of\ntext that you want to transmit.", "start": 4812.62, "duration": 1.87 }, { "text": "This is meaningless.", "start": 4814.49, "duration": 0.833 }, { "text": "There's no pattern in these\nAs, and E, B, C, Ds, and Es,", "start": 4815.323, "duration": 2.787 }, { "text": "but if you go through and count them\nup, each these letters-- A, B, C, D, E--", "start": 4818.11, "duration": 4.343 }, { "text": "occur with some frequency in this text.", "start": 4822.453, "duration": 1.734 }, { "text": "So, it's meant to be representative\nof an essay, or a message,", "start": 4824.187, "duration": 2.583 }, { "text": "or whatever that you\nwant to send to someone.", "start": 4826.77, "duration": 1.874 }, { "text": "Indeed, if you count up all of the\nAs, Bs, Cs, Ds, and Es, and divide", "start": 4828.644, "duration": 3.876 }, { "text": "by the total number of\nletters, it turns out", "start": 4832.52, "duration": 1.97 }, { "text": "that 20% of the characters in that\nrandom string are As, 10% are Bs,", "start": 4834.49, "duration": 5.39 }, { "text": "10% are Cs, 15% are\nDs, and 45% are Es, so", "start": 4839.88, "duration": 3.95 }, { "text": "it's roughly consistent\nwith what I'm claiming,", "start": 4843.83, "duration": 2.17 }, { "text": "which is that it E is pretty popular.", "start": 4846.0, "duration": 1.83 }, { "text": "So, intuitively, [? it ?]\nwould be really nice", "start": 4847.83, "duration": 2.95 }, { "text": "if I had an algorithm that came up\nwith some representation of bits", "start": 4850.78, "duration": 5.83 }, { "text": "that's not just 8 bits\nfor every darn letter", "start": 4856.61, "duration": 2.73 }, { "text": "but that is a few bits\nfor the popular letters", "start": 4859.34, "duration": 3.24 }, { "text": "and more bits for the\nless popular letters,", "start": 4862.58, "duration": 1.9 }, { "text": "so I optimize, again, so to\nspeak, for the common case.", "start": 4864.48, "duration": 2.77 }, { "text": "So, by this logic E, hopefully,\nshould have a pretty short encoding", "start": 4867.25, "duration": 4.49 }, { "text": "in binary, and A, and B, and C, and D\nshould have slightly longer encoding,", "start": 4871.74, "duration": 5.69 }, { "text": "so that again if I'm using E a lot I\nwant to send as few bits as possible.", "start": 4877.43, "duration": 3.48 }, { "text": "But I need this algorithm\nto be repeatable.", "start": 4880.91, "duration": 1.829 }, { "text": "I don't want to just arbitrarily\ncome up with something", "start": 4882.739, "duration": 2.291 }, { "text": "and then have to tell you in advance\nthat, hey, we're using this David Malan", "start": 4885.03, "duration": 4.1 }, { "text": "system for binary.", "start": 4889.13, "duration": 1.06 }, { "text": "We want an algorithmic process here.", "start": 4890.19, "duration": 2.11 }, { "text": "And what's nice about trees is that\nit's one way of seeing and solving", "start": 4892.3, "duration": 4.2 }, { "text": "exactly that.", "start": 4896.5, "duration": 1.04 }, { "text": "So, Huffman proposed this.", "start": 4897.54, "duration": 1.67 }, { "text": "If you have a forest of nodes, so to\nspeak, a whole bunch of trees-- each", "start": 4899.21, "duration": 4.48 }, { "text": "of size one, no children-- think of them\nas each having a weight or a frequency.", "start": 4903.69, "duration": 3.9 }, { "text": "So, I've drawn five circles\nhere, per this snippet", "start": 4907.59, "duration": 2.79 }, { "text": "from a popular textbook that has 10%,\n10%, 15%, 20%, 45% equivalently in each", "start": 4910.38, "duration": 7.286 }, { "text": "of those nodes.", "start": 4917.666, "duration": 0.624 }, { "text": "And I've just labeled the\nleaves as B, C, D, A, E,", "start": 4918.29, "duration": 3.225 }, { "text": "deliberately from left to right because\nit will make my tree look prettier,", "start": 4921.515, "duration": 3.125 }, { "text": "but technically the lines could cross\nand it's not a big deal in reality.", "start": 4924.64, "duration": 3.041 }, { "text": "We just need to be consistent.", "start": 4927.681, "duration": 1.419 }, { "text": "So, Huffman proposed this.", "start": 4929.1, "duration": 1.82 }, { "text": "In order to figure out the so-called\nHuffman tree for this particular text,", "start": 4930.92, "duration": 7.39 }, { "text": "in order to figure out what to encode\nit's letters as with zeros and ones,", "start": 4938.31, "duration": 4.05 }, { "text": "go ahead and take the two smallest nodes\nand combine them with a new root node.", "start": 4942.36, "duration": 5.27 }, { "text": "So in other words, B\nand C were both 10%.", "start": 4947.63, "duration": 2.52 }, { "text": "Those are the smallest nodes.", "start": 4950.15, "duration": 1.35 }, { "text": "Let's go ahead and combine\nthem with a new root node", "start": 4951.5, "duration": 2.166 }, { "text": "and add together their weights,\nso 10% plus 10% is 20%.", "start": 4953.666, "duration": 3.384 }, { "text": "And then arbitrarily,\nbut consistently, label", "start": 4957.05, "duration": 3.42 }, { "text": "the left child's edge or arrow as a\n0 and the right arrow's edge as a 1.", "start": 4960.47, "duration": 7.53 }, { "text": "Meanwhile, repeat.", "start": 4968.0, "duration": 1.61 }, { "text": "So, now look for the two smallest nodes.", "start": 4969.61, "duration": 1.99 }, { "text": "And I see a 20%-- so\nignore the children now.", "start": 4971.6, "duration": 3.31 }, { "text": "Only look at the roots of these trees.", "start": 4974.91, "duration": 1.8 }, { "text": "And there's now four trees, one of which\nhas children, three of which don't.", "start": 4976.71, "duration": 5.14 }, { "text": "So, now look at the smallest roots\nnow and you can go left to right here.", "start": 4981.85, "duration": 4.59 }, { "text": "There's a 20%, there's a 15%,\nthere's a 20%, and a 45%.", "start": 4986.44, "duration": 5.3 }, { "text": "So, I'm not sure which one\nto go with, so you just", "start": 4991.74, "duration": 2.395 }, { "text": "have to come up with some\nrule to be consistent.", "start": 4994.135, "duration": 2.0 }, { "text": "I'm going to go with\nthe ones on the left,", "start": 4996.135, "duration": 1.765 }, { "text": "and so I'm going to combine the 20%\nwith the 15%, the 20% on the left.", "start": 4997.9, "duration": 4.38 }, { "text": "Combine their weights.", "start": 5002.28, "duration": 1.14 }, { "text": "That gives me 35% in a new root,\nand again label the left branch 0", "start": 5003.42, "duration": 4.05 }, { "text": "and the right branch a 1.", "start": 5007.47, "duration": 1.78 }, { "text": "Now, it's not ambiguous.", "start": 5009.25, "duration": 1.42 }, { "text": "Let's combine 35% and 20%\nwith a new root that's 55%.", "start": 5010.67, "duration": 4.51 }, { "text": "Call its left branch\n0, its right branch 1.", "start": 5015.18, "duration": 3.93 }, { "text": "And now 55% and 45%, combine\nthose and give us a 1.", "start": 5019.11, "duration": 7.1 }, { "text": "So why did I just do this?", "start": 5026.21, "duration": 2.03 }, { "text": "Well now I have built up the so-called\nHuffman tree for this input text", "start": 5028.24, "duration": 4.45 }, { "text": "and Huffman proposed the following.", "start": 5032.69, "duration": 2.34 }, { "text": "To figure out what\npatterns of zeros and ones", "start": 5035.03, "duration": 2.15 }, { "text": "to use to represent\nA, B, C, D, E, simply", "start": 5037.18, "duration": 3.28 }, { "text": "follow the paths from the\nroot to each of those leaves.", "start": 5040.46, "duration": 4.19 }, { "text": "So what is the encoding for A?", "start": 5044.65, "duration": 2.21 }, { "text": "Start at the root and then\nlook for A-- 0, 1, so 0,", "start": 5046.86, "duration": 4.3 }, { "text": "1 shall be my binary\nencoding for A in this world.", "start": 5051.16, "duration": 3.13 }, { "text": "How about B?", "start": 5054.29, "duration": 1.19 }, { "text": "0, 0, 0, 0 shall be my binary\nencoding for B. How about C?", "start": 5055.48, "duration": 5.762 }, { "text": "0, 0, 0, 1 shall be my\nencoding for C. How about D?", "start": 5061.242, "duration": 6.218 }, { "text": "0, 0, 1.", "start": 5067.46, "duration": 1.46 }, { "text": "And beautifully, how about E?", "start": 5068.92, "duration": 2.292 }, { "text": "1.", "start": 5071.212, "duration": 2.658 }, { "text": "So, to summarize, what\nhas just happened?", "start": 5073.87, "duration": 2.99 }, { "text": "E was the most popular letter, B and\nC, were the least popular letters.", "start": 5076.86, "duration": 3.44 }, { "text": "And if we summarize these,\nyou'll see that, indeed,", "start": 5080.3, "duration": 3.54 }, { "text": "B and C got pretty long encodings,\nbut E got the shortest encoding.", "start": 5083.84, "duration": 5.21 }, { "text": "And it turns out\nmathematically you will now", "start": 5089.05, "duration": 2.88 }, { "text": "have a system for encoding letters\nof the alphabet that is optimal,", "start": 5091.93, "duration": 4.82 }, { "text": "that is you will use\nas few bits as possible", "start": 5096.75, "duration": 3.26 }, { "text": "because you are biasing things\ntoward short representations", "start": 5100.01, "duration": 2.98 }, { "text": "for popular letters, longer\nrepresentations for less", "start": 5102.99, "duration": 2.77 }, { "text": "popular letters.", "start": 5105.76, "duration": 0.87 }, { "text": "And mathematically this gives\nyou the most efficient encoding", "start": 5106.63, "duration": 3.11 }, { "text": "for the original text without\nlosing any information.", "start": 5109.74, "duration": 5.39 }, { "text": "In other words, now if Huffman\nwanted to send a secret message", "start": 5115.13, "duration": 3.17 }, { "text": "to someone in class or over the\ninternet, he and that recipient", "start": 5118.3, "duration": 3.69 }, { "text": "simply have to agree on\nthis scheme in advance", "start": 5121.99, "duration": 2.9 }, { "text": "and then use these encoding\nto transmit those messages.", "start": 5124.89, "duration": 3.58 }, { "text": "Because when someone receives 0, 1 or 0,\n0, 0, 0 or 0, 0, 0, 1 from Mr. Huffman,", "start": 5128.47, "duration": 8.86 }, { "text": "they can use that same\nlook-up table, if you will,", "start": 5137.33, "duration": 2.45 }, { "text": "and say, oh, he just sent me an A\nor, oh, he just sent me a B or C. So,", "start": 5139.78, "duration": 4.0 }, { "text": "you have to know what\ntree Huffman built up.", "start": 5143.78, "duration": 2.49 }, { "text": "And, indeed, what typically\nhappens in actual computers is", "start": 5146.27, "duration": 3.66 }, { "text": "when you use Huffman coding\nto compress some body of text", "start": 5149.93, "duration": 3.51 }, { "text": "like we just have here, you\nstore the compressed text", "start": 5153.44, "duration": 4.59 }, { "text": "by storing your As, Bs, Cs,\nDs, and Es and other letters", "start": 5158.03, "duration": 2.69 }, { "text": "using these new\nencoding, but you somehow", "start": 5160.72, "duration": 2.06 }, { "text": "have to embed in that file in the\ncompressed file the tree itself", "start": 5162.78, "duration": 4.45 }, { "text": "or this cheat sheet of encodings.", "start": 5167.23, "duration": 3.66 }, { "text": "So, with compression-- maybe\nyou're compressing a Microsoft Word", "start": 5170.89, "duration": 3.28 }, { "text": "file, or a dot TXT file,\nor any other type of file,", "start": 5174.17, "duration": 4.03 }, { "text": "you have to store not just the\ncompressed text using these shorter", "start": 5178.2, "duration": 3.88 }, { "text": "representation-- not 8-bit ASCII, but\nthese shorter representations-- but you", "start": 5182.08, "duration": 3.452 }, { "text": "also somewhere, maybe at the beginning\nof the file or at the end of the file,", "start": 5185.532, "duration": 3.208 }, { "text": "somewhere where someone else can find\nit, you need to store this mapping", "start": 5188.74, "duration": 3.0 }, { "text": "or you need to store the tree\nitself in some digital form.", "start": 5191.74, "duration": 4.85 }, { "text": "And so, it's possible\nby this logic that you", "start": 5196.59, "duration": 3.65 }, { "text": "might try to compress\na really small file,", "start": 5200.24, "duration": 3.09 }, { "text": "and that file could\nactually become bigger", "start": 5203.33, "duration": 2.76 }, { "text": "because you're storing a\ntree inside the file to--", "start": 5206.09, "duration": 3.382 }, { "text": "with which to recover\nthe original information.", "start": 5209.472, "duration": 1.958 }, { "text": "Or better yet, most\nalgorithms or most actual", "start": 5211.43, "duration": 2.077 }, { "text": "compression programs will realize,\nwait a minute, if compressing this file", "start": 5213.507, "duration": 3.083 }, { "text": "is actually going to make it bigger,\nlet's just not compress it at all", "start": 5216.59, "duration": 2.916 }, { "text": "and leave it alone untouched.", "start": 5219.506, "duration": 2.444 }, { "text": "So, what if you take a compressed file\nand compress it again, and compress it", "start": 5221.95, "duration": 5.42 }, { "text": "again, and compress it again?", "start": 5227.37, "duration": 2.89 }, { "text": "A dangerous assumption to get into\nis, well, I could just maybe keep", "start": 5230.26, "duration": 3.59 }, { "text": "compressing that video file again,\nand again, and again, and again,", "start": 5233.85, "duration": 3.55 }, { "text": "and I can maybe compress my big\nessay, or my big video file,", "start": 5237.4, "duration": 3.21 }, { "text": "or big music file to just maybe one bit.", "start": 5240.61, "duration": 2.38 }, { "text": "Right?", "start": 5242.99, "duration": 0.5 }, { "text": "That's the logical extreme,\njust keep compressing,", "start": 5243.49, "duration": 1.81 }, { "text": "compressing, compressing, compressing.", "start": 5245.3, "duration": 1.583 }, { "text": "But, of course, that\ncan't possibly make sense,", "start": 5246.883, "duration": 2.587 }, { "text": "because if you compress some file\ndown to just a single bit, 0 or 1,", "start": 5249.47, "duration": 3.39 }, { "text": "you've clearly thrown away information\nand can't possibly recover it all.", "start": 5252.86, "duration": 4.09 }, { "text": "So, at some point, too, you've hit this\nlower bound on the size of the file", "start": 5256.95, "duration": 5.1 }, { "text": "until you need to start throwing\nactual information away.", "start": 5262.05, "duration": 2.69 }, { "text": "At some point, the file just has so\nmuch entropy, appears to be so random,", "start": 5264.74, "duration": 4.55 }, { "text": "there really is no pattern to start\nto leverage to compress it further.", "start": 5269.29, "duration": 3.72 }, { "text": "And so, there generally is some\nmaximum amount of compression", "start": 5273.01, "duration": 3.95 }, { "text": "you can apply to something.", "start": 5276.96, "duration": 1.837 }, { "text": "So, how would we represent this?", "start": 5278.797, "duration": 1.333 }, { "text": "Let's whip out a C struct here.", "start": 5280.13, "duration": 1.42 }, { "text": "So, this time each of the\nnodes in a Huffman tree", "start": 5281.55, "duration": 2.694 }, { "text": "need a little something different.", "start": 5284.244, "duration": 1.416 }, { "text": "They need, at least in the\nleaves, some kind of character", "start": 5285.66, "duration": 2.69 }, { "text": "to remember the symbol.", "start": 5288.35, "duration": 0.96 }, { "text": "Now, technically only the leaves\nneed to know what symbols they are,", "start": 5289.31, "duration": 2.64 }, { "text": "so it's a little redundant\nto have this in every node,", "start": 5291.95, "duration": 2.25 }, { "text": "but we can keep things simple and use\nthe same type of node for everything.", "start": 5294.2, "duration": 3.44 }, { "text": "Float frequency, I could use an integer\nand treat it exactly as a percentage,", "start": 5297.64, "duration": 4.41 }, { "text": "or I can use a float as the nodes\nwere with 0.1 and 0.45 and so forth,", "start": 5302.05, "duration": 4.29 }, { "text": "and I'll call that frequency.", "start": 5306.34, "duration": 1.23 }, { "text": "And then each of those nodes\nneeds a left child potentially", "start": 5307.57, "duration": 2.458 }, { "text": "and a right child potentially.", "start": 5310.028, "duration": 1.292 }, { "text": "And, again, I'll call\nthese things a node.", "start": 5311.32, "duration": 2.58 }, { "text": "So, again, it's getting a little more\ninvolved this node, but it still allows", "start": 5313.9, "duration": 3.37 }, { "text": "me to represent it ultimately in C.", "start": 5317.27, "duration": 6.29 }, { "text": "And now, it's time to pursue lastly\nthe holy grail of data structures,", "start": 5323.56, "duration": 4.51 }, { "text": "if you will.", "start": 5328.07, "duration": 0.91 }, { "text": "Thus far, we've been solving\nproblems, creating new problems,", "start": 5328.98, "duration": 4.9 }, { "text": "trying to solve those again.", "start": 5333.88, "duration": 1.17 }, { "text": "And the problems we've been exploring\nthis week are things like dynamism,", "start": 5335.05, "duration": 3.222 }, { "text": "if we want to be able to grow\nor shrink our data structure.", "start": 5338.272, "duration": 2.458 }, { "text": "Malloc and pointers\ngive us that flexibility", "start": 5340.73, "duration": 2.22 }, { "text": "but might cost us a bit\nmore time, because we", "start": 5342.95, "duration": 2.15 }, { "text": "have to keep things\nsorted differently or we", "start": 5345.1, "duration": 2.53 }, { "text": "have to follow all of those pointers.", "start": 5347.63, "duration": 2.06 }, { "text": "And so, a lot of the algorithms\nwe've been discussing today", "start": 5349.69, "duration": 2.89 }, { "text": "at least have-- like linear time,\nsearching, or inserting, or deleting", "start": 5352.58, "duration": 4.14 }, { "text": "potentially like in a linked list.", "start": 5356.72, "duration": 1.96 }, { "text": "Better still would be\nsomething logarithmic", "start": 5358.68, "duration": 1.87 }, { "text": "like a balanced binary search tree,\nso still preserving that nice binary", "start": 5360.55, "duration": 5.31 }, { "text": "aspect from week zero.", "start": 5365.86, "duration": 1.84 }, { "text": "But the holy grail of a data\nstructure for its operations", "start": 5367.7, "duration": 2.77 }, { "text": "is Big O of 1 so to\nspeak, constant time.", "start": 5370.47, "duration": 3.44 }, { "text": "If you are searching, or inserting,\nor deleting, and somehow changing", "start": 5373.91, "duration": 5.33 }, { "text": "a data structure, wouldn't it be\namazing if every darn operation", "start": 5379.24, "duration": 3.52 }, { "text": "takes just one step, or\nmaybe two steps, or three", "start": 5382.76, "duration": 2.36 }, { "text": "steps but a constant number of steps?", "start": 5385.12, "duration": 2.82 }, { "text": "Now, it might be a little\nnaive for us to expect", "start": 5387.94, "duration": 2.0 }, { "text": "that we can store an arbitrary\namount of data in some fancy way", "start": 5389.94, "duration": 3.47 }, { "text": "that we get constant time,\nbut maybe just maybe if we're", "start": 5393.41, "duration": 3.2 }, { "text": "clever we can get close to that.", "start": 5396.61, "duration": 2.18 }, { "text": "So, let's introduce a step toward that.", "start": 5398.79, "duration": 2.81 }, { "text": "It turns out there exists in this\nworld things called hash tables.", "start": 5401.6, "duration": 3.66 }, { "text": "And a hash table can be\nimplemented in any number of ways,", "start": 5405.26, "duration": 2.75 }, { "text": "but you can think of it\nreally as just an array.", "start": 5408.01, "duration": 2.08 }, { "text": "So, for instance, this might be a way of\nrepresenting a hash table called table,", "start": 5410.09, "duration": 5.442 }, { "text": "whose first location is bracket zero\nand whose last location is bracket", "start": 5415.532, "duration": 2.958 }, { "text": "n minus 1 for however long this is.", "start": 5418.49, "duration": 1.742 }, { "text": "And I just left it as blanks.", "start": 5420.232, "duration": 1.208 }, { "text": "I don't even know what this\nhash table might want to store.", "start": 5421.44, "duration": 1.77 }, { "text": "It could be numbers, it could\nbe names, it could be letters,", "start": 5423.21, "duration": 1.63 }, { "text": "it could be anything we want.", "start": 5424.84, "duration": 1.91 }, { "text": "But hash table has this\nnice theoretical property", "start": 5426.75, "duration": 3.92 }, { "text": "that if well-designed\nand thought through,", "start": 5430.67, "duration": 2.64 }, { "text": "you can maybe just maybe get\nconstant look up time in it.", "start": 5433.31, "duration": 4.52 }, { "text": "And let's do a simple\nexample of a hash table.", "start": 5437.83, "duration": 2.22 }, { "text": "Hash tables are often nicely\nthought of as buckets,", "start": 5440.05, "duration": 2.79 }, { "text": "so we borrowed these from the loading\ndock outside just a little moment ago,", "start": 5442.84, "duration": 3.36 }, { "text": "and we've attached thanks to\nArturo some of these signs to them.", "start": 5446.2, "duration": 5.27 }, { "text": "This is going to be Z, so\nI'll just put this over here.", "start": 5451.47, "duration": 3.97 }, { "text": "This is going to be C, so I'll put\nthis over here, and B here, and A.", "start": 5455.44, "duration": 5.925 }, { "text": "And we thought we might get chased\naway by the folks on the loading dock,", "start": 5461.365, "duration": 4.025 }, { "text": "so we didn't bother getting D\nthrough Y, So we'll just pretend", "start": 5465.39, "duration": 2.98 }, { "text": "that we have 26 such buckets here.", "start": 5468.37, "duration": 2.01 }, { "text": "And suppose that the goal\nat hand is-- I don't know,", "start": 5470.38, "duration": 3.482 }, { "text": "it's like at the end\nof an exam, so we've", "start": 5473.862, "duration": 1.708 }, { "text": "got our old blue books that a\nclass might use for students", "start": 5475.57, "duration": 3.23 }, { "text": "writing essays in some class.", "start": 5478.8, "duration": 1.67 }, { "text": "And it's time for the students\nto come submit their blue books.", "start": 5480.47, "duration": 3.16 }, { "text": "Now, we could just collect them all\nand make a big mess as would generally", "start": 5483.63, "duration": 3.28 }, { "text": "be the case, or we can be a little\nmore methodical to at least make", "start": 5486.91, "duration": 3.24 }, { "text": "our jobs easier.", "start": 5490.15, "duration": 1.384 }, { "text": "Now, at the end of the day, what's going\nto be interesting about hash tables", "start": 5491.534, "duration": 3.166 }, { "text": "is that there's going\nto be this distinction", "start": 5494.7, "duration": 1.833 }, { "text": "between actual benefits and\ntheoretical benefit, or lack thereof.", "start": 5496.533, "duration": 3.317 }, { "text": "So, we'll come to that in just a\nmoment, but here's A, B, C, D, and Z.", "start": 5499.85, "duration": 4.181 }, { "text": "And you know what?", "start": 5504.031, "duration": 0.749 }, { "text": "I just am going to ask the students\nin this class-- there are so", "start": 5504.78, "duration": 1.99 }, { "text": "many people in the room\nafter an exam, I just", "start": 5506.77, "duration": 2.27 }, { "text": "want them to at least make\nmy life 1/26 as difficult", "start": 5509.04, "duration": 4.96 }, { "text": "by putting all the As over there,\nall the Bs here, all the Cs here,", "start": 5514.0, "duration": 3.0 }, { "text": "all the Zs here, so that I don't have\na massive mountain of As through Zs", "start": 5517.0, "duration": 4.66 }, { "text": "that I have to sift\nthrough individually.", "start": 5521.66, "duration": 2.12 }, { "text": "It would just be nice if\nthey do the first pass", "start": 5523.78, "duration": 2.22 }, { "text": "of bucketizing the values based on\nthe first letter in their last name.", "start": 5526.0, "duration": 4.13 }, { "text": "In other words, my hash\nfunction, my algorithm,", "start": 5530.13, "duration": 3.48 }, { "text": "is going to be for each student\nto consider his or her last name,", "start": 5533.61, "duration": 4.09 }, { "text": "look at the first letter they're\nin, and put his or her exam", "start": 5537.7, "duration": 3.22 }, { "text": "in the appropriate bucket.", "start": 5540.92, "duration": 1.38 }, { "text": "So, here is, for instance,\nsomeone with the letter", "start": 5542.3, "duration": 3.34 }, { "text": "C. I'm going to put\nthat blue book in here.", "start": 5545.64, "duration": 2.63 }, { "text": "Here's someone with the letter\nA. That one's going to go here.", "start": 5548.27, "duration": 2.62 }, { "text": "Letter Z?", "start": 5550.89, "duration": 0.746 }, { "text": "This one's going to go over here.", "start": 5551.636, "duration": 1.374 }, { "text": "Letter B?", "start": 5553.01, "duration": 1.11 }, { "text": "This is going to go over here.", "start": 5554.12, "duration": 1.25 }, { "text": "C, and B, and F-- Z, I mean, and all of\n[? the ?] [? letters ?] of the alphabet", "start": 5555.37, "duration": 6.8 }, { "text": "in between.", "start": 5562.17, "duration": 0.78 }, { "text": "So, hashing really has this\nvisual and conceptual equivalence", "start": 5562.95, "duration": 3.774 }, { "text": "of putting something in this bucket,\nputting something in that bucket,", "start": 5566.724, "duration": 2.916 }, { "text": "putting something in this\nother bucket, ultimately", "start": 5569.64, "duration": 2.083 }, { "text": "bucketizing all of your elements.", "start": 5571.723, "duration": 2.147 }, { "text": "And you can think of this,\nfrankly, as just an array,", "start": 5573.87, "duration": 2.21 }, { "text": "but it's not just an\narray with one spot.", "start": 5576.08, "duration": 1.72 }, { "text": "It looks I can stack multiple\nnumbers or multiple blue books inside", "start": 5577.8, "duration": 5.04 }, { "text": "of that array.", "start": 5582.84, "duration": 0.71 }, { "text": "So, we're going to have to come\nback to that, because this clearly", "start": 5583.55, "duration": 1.55 }, { "text": "can't be an array.", "start": 5585.1, "duration": 0.75 }, { "text": "Normally, the array would be filled\nthe moment you put one value in it.", "start": 5585.85, "duration": 3.2 }, { "text": "But this hashing is\nthe interesting part.", "start": 5589.05, "duration": 2.93 }, { "text": "The juicy ingredient today is if I take\ninto account as input what it is I'm", "start": 5591.98, "duration": 4.87 }, { "text": "trying to store, use some piece of that\ninformation to decide where to put it,", "start": 5596.85, "duration": 4.66 }, { "text": "that's an algorithm, because\nI can repeat that process,", "start": 5601.51, "duration": 2.85 }, { "text": "so long as it's not random.", "start": 5604.36, "duration": 1.27 }, { "text": "You go over here, you go over here.", "start": 5605.63, "duration": 1.51 }, { "text": "That's amazing.", "start": 5607.14, "duration": 1.04 }, { "text": "Wow, OK, pushing my luck.", "start": 5608.18, "duration": 2.84 }, { "text": "OK, so I'm not just randomly\nputting things here.", "start": 5611.02, "duration": 2.644 }, { "text": "I'm actually giving some thought\nas to where I'm putting things,", "start": 5613.664, "duration": 2.666 }, { "text": "and that makes the algorithm\ndeterministic, repeatable, predictable", "start": 5616.33, "duration": 4.54 }, { "text": "so that if you insert something\nnow, you can absolutely", "start": 5620.87, "duration": 3.38 }, { "text": "find it if present later.", "start": 5624.25, "duration": 2.37 }, { "text": "Unfortunately, if our\nhash table does look", "start": 5626.62, "duration": 2.03 }, { "text": "like this, just a simple array from\nbracket 0 to bracket n minus 1 dot,", "start": 5628.65, "duration": 4.12 }, { "text": "dot, dot in between,\nand it's just an array", "start": 5632.77, "duration": 2.33 }, { "text": "for integers or an array for strings or\nwhatever, once you put something here,", "start": 5635.1, "duration": 5.59 }, { "text": "or here, or here, that's it.", "start": 5640.69, "duration": 3.14 }, { "text": "There is no more room to\nput another element there", "start": 5643.83, "duration": 2.64 }, { "text": "wide as I might have drawn this table.", "start": 5646.47, "duration": 1.94 }, { "text": "If there's an int there, that's it.", "start": 5648.41, "duration": 1.98 }, { "text": "So, what could you do?", "start": 5650.39, "duration": 2.12 }, { "text": "Suppose that you do have an\narray structure like this,", "start": 5652.51, "duration": 4.06 }, { "text": "and that is unacceptable.", "start": 5656.57, "duration": 3.47 }, { "text": "You have a whole bunch of elements\nhere and this table looks like this,", "start": 5660.04, "duration": 6.96 }, { "text": "and you consider this table like this.", "start": 5667.0, "duration": 3.62 }, { "text": "And maybe it's just where you're\nsupposed to take attendance or put", "start": 5670.62, "duration": 3.04 }, { "text": "people's names.", "start": 5673.66, "duration": 0.914 }, { "text": "So, if you say, oh, Alice is here today.", "start": 5674.574, "duration": 1.666 }, { "text": "Let me go ahead and hash on Alice's\nname and put her where the As should go.", "start": 5676.24, "duration": 4.225 }, { "text": "Oh, Zoe is here, Z-O-E, so\nwe'll put her down there.", "start": 5680.465, "duration": 5.685 }, { "text": "And then who else?", "start": 5686.15, "duration": 1.83 }, { "text": "Alex is here.", "start": 5687.98, "duration": 0.77 }, { "text": "Dammit, Alex, no room for\nyou in our hash table,", "start": 5688.75, "duration": 3.34 }, { "text": "because Alice is already there.", "start": 5692.09, "duration": 1.38 }, { "text": "This is stupid.", "start": 5693.47, "duration": 0.78 }, { "text": "If we have data we want to\ninsert into this data structure,", "start": 5694.25, "duration": 2.47 }, { "text": "it would seem that I have 24 available\nspots into which I could put Alex", "start": 5696.72, "duration": 6.22 }, { "text": "and yet I'm just stubbornly trying\nto put him where only the As belong.", "start": 5702.94, "duration": 3.58 }, { "text": "So, why don't I, in this kind of\nscenario, I need to put Alex in here.", "start": 5706.52, "duration": 3.85 }, { "text": "I clearly have space.", "start": 5710.37, "duration": 0.997 }, { "text": "You know what?", "start": 5711.367, "duration": 0.583 }, { "text": "Let me just probe the array looking\nfor the first available spot.", "start": 5711.95, "duration": 2.91 }, { "text": "OK, Alex, you're just going to go here,\nand if someone else like Erin appears,", "start": 5714.86, "duration": 5.43 }, { "text": "fine.", "start": 5720.29, "duration": 0.5 }, { "text": "You just are going to go over here.", "start": 5720.79, "duration": 2.29 }, { "text": "So, you try to put the letter\nAs where you want them to go,", "start": 5723.08, "duration": 3.77 }, { "text": "but if there's already\nsomeone there, just", "start": 5726.85, "duration": 1.77 }, { "text": "probe deeper into the data structure\nlooking for the first available slot.", "start": 5728.62, "duration": 4.07 }, { "text": "So, this is a general technique in\nprogramming called linear probing", "start": 5732.69, "duration": 3.35 }, { "text": "whereby you have a data structure.", "start": 5736.04, "duration": 1.68 }, { "text": "If you hash to some location like\nthe letter A there's a collision,", "start": 5737.72, "duration": 3.82 }, { "text": "something is there, you probe\nfurther in the data structure just", "start": 5741.54, "duration": 3.0 }, { "text": "looking for some place you can put it.", "start": 5744.54, "duration": 2.0 }, { "text": "So, you get close to constant\ntime decision-making.", "start": 5746.54, "duration": 3.04 }, { "text": "Put A here, put Z here.", "start": 5749.58, "duration": 1.382 }, { "text": "And because this is an array, you have\nrandom access with your square bracket", "start": 5750.962, "duration": 3.208 }, { "text": "notation, but if you have lots of As\nand not too many Zs, or Bs, or Ds,", "start": 5754.17, "duration": 5.02 }, { "text": "it's possible this approach could\ndevolve back into linear time.", "start": 5759.19, "duration": 5.43 }, { "text": "So, in the ideal we have one\nA, one B, one Z, and everything", "start": 5764.62, "duration": 3.45 }, { "text": "in between, that's constant time.", "start": 5768.07, "duration": 1.66 }, { "text": "We have our holy grail, constant\ntime operations for a data structure,", "start": 5769.73, "duration": 4.91 }, { "text": "but not if we want to support\ninsertion of other elements,", "start": 5774.64, "duration": 3.11 }, { "text": "even those that hash\nto the same location.", "start": 5777.75, "duration": 3.0 }, { "text": "So, what's the fix?", "start": 5780.75, "duration": 1.09 }, { "text": "Well, if the problem\nis that we've already", "start": 5781.84, "duration": 1.96 }, { "text": "made room-- we already have used\nthis space for Alice, you know what?", "start": 5783.8, "duration": 3.56 }, { "text": "If we need to put someone else\nhere, why don't we just create", "start": 5787.36, "duration": 3.83 }, { "text": "dynamically some more space?", "start": 5791.19, "duration": 2.89 }, { "text": "We have malloc now.", "start": 5794.08, "duration": 1.13 }, { "text": "We have dynamic memory allocation.", "start": 5795.21, "duration": 1.66 }, { "text": "Why don't we just extend our data\nstructure laterally, horizontally--", "start": 5796.87, "duration": 5.44 }, { "text": "artistically here-- so that, yes,\nyou try to go to that first location.", "start": 5802.31, "duration": 3.86 }, { "text": "But if there's multiple people\nthat are meant to go there,", "start": 5806.17, "duration": 2.79 }, { "text": "multiple values, go ahead\nand just link them together,", "start": 5808.96, "duration": 3.09 }, { "text": "thereby merging the idea of a hash table\nand a linked list with a data structure", "start": 5812.05, "duration": 4.4 }, { "text": "that might look like this.", "start": 5816.45, "duration": 1.43 }, { "text": "So, this is an example, somewhat\narbitrary, of 31 days out of a month.", "start": 5817.88, "duration": 3.659 }, { "text": "And if you actually hash\non people's birth dates,", "start": 5821.539, "duration": 2.041 }, { "text": "as I think this author did, you\ncan think of your hash table", "start": 5823.58, "duration": 4.41 }, { "text": "still as an array.", "start": 5827.99, "duration": 1.51 }, { "text": "But that array does not store\nstrings, it does not store integers.", "start": 5829.5, "duration": 3.13 }, { "text": "It only stores pointers, 31 total\nin this case-- some of which", "start": 5832.63, "duration": 4.15 }, { "text": "might be null, per the\nvertical diagonal slash--", "start": 5836.78, "duration": 3.14 }, { "text": "but those pointers in turn point\nto the beginning of linked lists.", "start": 5839.92, "duration": 4.02 }, { "text": "So, if multiple people were born\non the fourth of some month,", "start": 5843.94, "duration": 2.78 }, { "text": "you would put J. Adams and W. Floyd\nin a linked list at that location.", "start": 5846.72, "duration": 4.22 }, { "text": "If both Aaron, and Alex, and Alice,\nand other students with the names A", "start": 5850.94, "duration": 4.89 }, { "text": "all belong at that first location\nin my previous table, that's fine.", "start": 5855.83, "duration": 4.51 }, { "text": "Just string them together\nwith a linked list.", "start": 5860.34, "duration": 2.79 }, { "text": "Much like with these buckets, at the end\nof the day, I'm still creating piles.", "start": 5863.13, "duration": 4.865 }, { "text": "And at the end of the day, I still have\nto go through them all, ultimately.", "start": 5867.995, "duration": 3.125 }, { "text": "But each of these piles\nis 1/26 the size of it", "start": 5871.12, "duration": 3.63 }, { "text": "would have been if everyone just\ncame up at the end of the exam", "start": 5874.75, "duration": 3.2 }, { "text": "and just piled all their\nbooks in the same pile.", "start": 5877.95, "duration": 2.23 }, { "text": "So, whereas, these algorithms\nat the end of the day", "start": 5880.18, "duration": 2.32 }, { "text": "are still devolving, if you\nwill-- or these data structures", "start": 5882.5, "duration": 3.22 }, { "text": "are devolving, if you will,\ninto linear time operations,", "start": 5885.72, "duration": 2.98 }, { "text": "in the worst case if these things\njust get really long and stringy,", "start": 5888.7, "duration": 3.12 }, { "text": "at least in actuality they might be as\ngood as 1/31 as long or 1/26 as tall.", "start": 5891.82, "duration": 8.32 }, { "text": "And so, now there's this\ndichotomy in this week five", "start": 5900.14, "duration": 3.47 }, { "text": "of asymptotic running time,\nthe theoretical running", "start": 5903.61, "duration": 2.8 }, { "text": "time that we've really been belaboring\nand the actual running time.", "start": 5906.41, "duration": 3.45 }, { "text": "Just because something is n\nsquared does not mean it's bad.", "start": 5909.86, "duration": 2.49 }, { "text": "If there's only a few\nelements, n squared is great.", "start": 5912.35, "duration": 2.124 }, { "text": "It's going to happen super fast\nif your computer is 1 gigahertz,", "start": 5914.474, "duration": 2.836 }, { "text": "or 2 gigahertz, or faster these days.", "start": 5917.31, "duration": 1.799 }, { "text": "N squared in and of itself isn't bad.", "start": 5919.109, "duration": 1.541 }, { "text": "It just gets really bad\nwhen your data gets large.", "start": 5920.65, "duration": 3.1 }, { "text": "But in practice, even n squared divided\nby 2 is actually better than n squared.", "start": 5923.75, "duration": 6.82 }, { "text": "So, a couple weeks ago when I was\nsaying don't worry about the lower order", "start": 5930.57, "duration": 3.85 }, { "text": "terms, the constant terms,\nfocus only on n squared", "start": 5934.42, "duration": 3.53 }, { "text": "and not n or anything you're dividing\nby, that's fine theoretically,", "start": 5937.95, "duration": 3.66 }, { "text": "but in actuality you're going\nto feel that kind of difference.", "start": 5941.61, "duration": 4.27 }, { "text": "So, here's one last data structure\nthat we'll call a trie-- so trie,", "start": 5945.88, "duration": 5.06 }, { "text": "short for retrieval somehow,\nT-R-I-E, but pronounced try.", "start": 5950.94, "duration": 4.39 }, { "text": "And this one is cool\nbecause this now is really", "start": 5955.33, "duration": 4.22 }, { "text": "like a weird offspring of these\ndata structures from today.", "start": 5959.55, "duration": 2.97 }, { "text": "But it's a tree each of\nwhose nodes is in an array.", "start": 5962.52, "duration": 5.09 }, { "text": "And a trie is really good for storing\nwords like words in a dictionary.", "start": 5967.61, "duration": 4.1 }, { "text": "Indeed, one of the problem\nI had for you in CS50", "start": 5971.71, "duration": 2.48 }, { "text": "is going to be to implement a spell\nchecker, which effectively means build", "start": 5974.19, "duration": 3.38 }, { "text": "a dictionary in memory,\nand you'll be challenged", "start": 5977.57, "duration": 2.29 }, { "text": "to spell check words as\nfast as you can, storing", "start": 5979.86, "duration": 2.62 }, { "text": "as many as 100,000 English words\nsomehow in your computer's memory", "start": 5982.48, "duration": 3.009 }, { "text": "and answering questions of the form\nis this a word, is this a word,", "start": 5985.489, "duration": 2.791 }, { "text": "is this a word.", "start": 5988.28, "duration": 1.2 }, { "text": "That's, after all,\nwhat spell checking is.", "start": 5989.48, "duration": 1.98 }, { "text": "So, a trie is kind of interesting in\nthat-- and this is an excerpt of an,", "start": 5991.46, "duration": 5.56 }, { "text": "artist's rendition\nthere of-- the root node", "start": 5997.02, "duration": 2.56 }, { "text": "here represents this-- is this\nrectangle here, and that of course", "start": 5999.58, "duration": 5.11 }, { "text": "looks like an array.", "start": 6004.69, "duration": 1.28 }, { "text": "And notice what's implicit in this.", "start": 6005.97, "duration": 2.91 }, { "text": "If this is location A\nand this is location Z,", "start": 6008.88, "duration": 2.79 }, { "text": "the author here has just\ndecided to only show you", "start": 6011.67, "duration": 2.05 }, { "text": "those letters that matter\nfor the sake of discussion.", "start": 6013.72, "duration": 2.22 }, { "text": "But the fact that the M\nlocation here is not blank", "start": 6015.94, "duration": 4.57 }, { "text": "means there's a pointer there.", "start": 6020.51, "duration": 1.28 }, { "text": "Indeed, what are these arrays?", "start": 6021.79, "duration": 1.249 }, { "text": "They are arrays of\npointers to other nodes.", "start": 6023.039, "duration": 2.711 }, { "text": "So, the fact that M is not null and it\nleads to this node, and notice that A", "start": 6025.75, "duration": 4.52 }, { "text": "is not null and it leads to this node,\nand then this node, and then this node.", "start": 6030.27, "duration": 3.26 }, { "text": "And this is where the artist\nis just taking some liberties.", "start": 6033.53, "duration": 2.04 }, { "text": "This tree would be monstrously\nwide, because all of these arrays", "start": 6035.57, "duration": 2.666 }, { "text": "are so darn wide, so he or she is just\nshowing you the width-- or the element", "start": 6038.236, "duration": 4.734 }, { "text": "that we care about, M, A, X, W, E, L,\nL, and then some special sentinel symbol", "start": 6042.97, "duration": 6.94 }, { "text": "delta, but it could be anything.", "start": 6049.91, "duration": 1.47 }, { "text": "This is null, really.", "start": 6051.38, "duration": 1.57 }, { "text": "This is how using a trie a programmer\ncould store the name Maxwell,", "start": 6052.95, "duration": 6.11 }, { "text": "M-A-X-W-E-L-L, by simply leaving\nlittle bread crumbs, if you will,", "start": 6059.06, "duration": 5.34 }, { "text": "from one node to another such that\neach of those elements in the array is", "start": 6064.4, "duration": 5.08 }, { "text": "a pointer to another array.", "start": 6069.48, "duration": 1.75 }, { "text": "And if you keep following these\npointers, following the bread crumbs", "start": 6071.23, "duration": 3.05 }, { "text": "and you eventually find yourself\nat this special sentinel value--", "start": 6074.28, "duration": 3.61 }, { "text": "and actually, it wouldn't be null, it\nwould be like a Boolean saying true.", "start": 6077.89, "duration": 3.74 }, { "text": "This is a word you can just by storing\na single yes or no at this location way", "start": 6081.63, "duration": 5.76 }, { "text": "down here, implicitly reveal that\nM-A-X-W-E-L was in fact a word.", "start": 6087.39, "duration": 7.995 }, { "text": "Let's follow another.", "start": 6095.385, "duration": 0.875 }, { "text": "So, let's say Turing,\nT-U-R-I-N-G, check, Boolean true.", "start": 6096.26, "duration": 10.88 }, { "text": "Turing is in this dictionary as well.", "start": 6107.14, "duration": 2.42 }, { "text": "So, if there are bread crumbs that\nlead to null, that word is not in here.", "start": 6109.56, "duration": 3.9 }, { "text": "So, apparently there is no\nnames starting with A through L,", "start": 6113.46, "duration": 4.3 }, { "text": "and there is no one after U through\nZ or some of the letters in between,", "start": 6117.76, "duration": 3.99 }, { "text": "because those pointers are\nimplicitly and pictorially null.", "start": 6121.75, "duration": 3.23 }, { "text": "But let's consider, then, what is the\nrunning time of inserting or looking up", "start": 6124.98, "duration": 4.92 }, { "text": "a name and [? in a trie? ?] Thus\nfar, pretty much all of the data", "start": 6129.9, "duration": 4.22 }, { "text": "structures we've talked about\nhave pretty slow running times,", "start": 6134.12, "duration": 3.86 }, { "text": "linear in the worst case.", "start": 6137.98, "duration": 1.84 }, { "text": "So, if we used an array\nto store people's names", "start": 6139.82, "duration": 2.55 }, { "text": "or we used to linked list to store\npeople's names, in the worst case", "start": 6142.37, "duration": 3.152 }, { "text": "we had linear running time, unless\nmaybe we sort things, but even", "start": 6145.522, "duration": 2.708 }, { "text": "then that costs us some time.", "start": 6148.23, "duration": 1.208 }, { "text": "So, linear may be logarithmic\nwas the best we could do.", "start": 6149.438, "duration": 3.192 }, { "text": "And even with a hash\ntable, whereby, maybe we", "start": 6152.63, "duration": 2.75 }, { "text": "store Maxwell at the M\nlocation in our table,", "start": 6155.38, "duration": 2.65 }, { "text": "he might still have a link list of\na whole bunch of other M people.", "start": 6158.03, "duration": 3.49 }, { "text": "That, again, can devolve into\nsomething linear, a linear linked list.", "start": 6161.52, "duration": 3.99 }, { "text": "But what about a hash table?", "start": 6165.51, "duration": 2.13 }, { "text": "To answer the question is Maxwell in\na trie-- sorry, what about to trie?", "start": 6167.64, "duration": 5.14 }, { "text": "To answer the question is\nMaxwell in a trie, what do we do?", "start": 6172.78, "duration": 3.6 }, { "text": "We start at the root and we follow\nthe pointer that represents m,", "start": 6176.38, "duration": 3.96 }, { "text": "and then we follow the pointer there\nthat represents A, then X, W, E, L, L,", "start": 6180.34, "duration": 4.31 }, { "text": "and we look for at the end of that\nseries of steps a true false value.", "start": 6184.65, "duration": 3.73 }, { "text": "And if it's true, yes, Maxwell is here.", "start": 6188.38, "duration": 3.15 }, { "text": "What about Turing?", "start": 6191.53, "duration": 0.89 }, { "text": "Well, we start at the\npointer that represents", "start": 6192.42, "duration": 1.875 }, { "text": "T, then U, R, I, N G, then check.", "start": 6194.295, "duration": 1.865 }, { "text": "Oh, true.", "start": 6196.16, "duration": 1.43 }, { "text": "Turing is in there.", "start": 6197.59, "duration": 1.27 }, { "text": "Let's look for David.", "start": 6198.86, "duration": 1.22 }, { "text": "No, false.", "start": 6200.08, "duration": 1.27 }, { "text": "There's not even a pointer there.", "start": 6201.35, "duration": 1.52 }, { "text": "David is not in this dictionary.", "start": 6202.87, "duration": 2.08 }, { "text": "So, how many steps did that each take?", "start": 6204.95, "duration": 1.95 }, { "text": "To tell whether Maxwell\nwas in the dictionary,", "start": 6206.9, "duration": 2.2 }, { "text": "was M-A-X-W-E-L-L and then look at\nthe Boolean, so that was eight steps.", "start": 6209.1, "duration": 5.51 }, { "text": "And to look up Turing was\nT-U-R-I-N-G. And then that Boolean,", "start": 6214.61, "duration": 4.84 }, { "text": "that was seven steps.", "start": 6219.45, "duration": 2.91 }, { "text": "Those numbers have nothing to do with\nhow many words are already in the trie.", "start": 6222.36, "duration": 5.66 }, { "text": "There might be-- and there's\nonly a couple dozen here--", "start": 6228.02, "duration": 2.94 }, { "text": "there are a dozen or so here-- there\nmight be thousands of actual words", "start": 6230.96, "duration": 3.48 }, { "text": "in this dictionary, but we're still\ngoing to find Alan Turing by way", "start": 6234.44, "duration": 4.15 }, { "text": "of T-U-R-I-N-G Boolean seven steps,\nand M-A-X-W-E-L-L Boolean, eight steps.", "start": 6238.59, "duration": 7.74 }, { "text": "It doesn't matter how many other\ndata elements are in this trie.", "start": 6246.33, "duration": 4.21 }, { "text": "And that's what's\npowerful, because if there", "start": 6250.54, "duration": 2.48 }, { "text": "is an upper bound on the\nnumber of letters in an English", "start": 6253.02, "duration": 2.56 }, { "text": "word-- which is kind of true.", "start": 6255.58, "duration": 2.14 }, { "text": "I've rarely typed words\nthat are longer than I don't", "start": 6257.72, "duration": 2.62 }, { "text": "know 10 characters, 15 characters.", "start": 6260.34, "duration": 1.81 }, { "text": "At some point there\nmight exist these words,", "start": 6262.15, "duration": 2.5 }, { "text": "but no one actually says\nor types these words.", "start": 6264.65, "duration": 2.63 }, { "text": "Those are effectively constants.", "start": 6267.28, "duration": 1.63 }, { "text": "The maximum length of a word in\nEnglish is surely some constant,", "start": 6268.91, "duration": 3.096 }, { "text": "because there is one\nword that's the longest.", "start": 6272.006, "duration": 1.874 }, { "text": "That's a constant value,\nwhich means inserting a name,", "start": 6273.88, "duration": 3.18 }, { "text": "or searching for a name, or\nremoving a name from a trie", "start": 6277.06, "duration": 4.26 }, { "text": "does depend on the length\nof the name, but it does not", "start": 6281.32, "duration": 2.7 }, { "text": "depend on how many pieces of data\nare already in the data structure.", "start": 6284.02, "duration": 3.95 }, { "text": "And as such, it is constant time.", "start": 6287.97, "duration": 4.05 }, { "text": "So, now in C, we have a\nwhole bunch of new syntax", "start": 6292.02, "duration": 2.3 }, { "text": "with which to represent data\nstructures, namely actual structs in C,", "start": 6294.32, "duration": 3.315 }, { "text": "and we have pointers, and\nwe have malloc with which", "start": 6297.635, "duration": 2.125 }, { "text": "we can build more interesting\nshapes, if you will, in memory.", "start": 6299.76, "duration": 2.99 }, { "text": "And we now have a number of abstract\ndata types and actual data structures", "start": 6302.75, "duration": 4.1 }, { "text": "we can build using these ingredients\nwith which we can now solve problems", "start": 6306.85, "duration": 4.142 }, { "text": "that are going to demand all the more\nresources, all the more time, all", "start": 6310.992, "duration": 2.958 }, { "text": "the more space, in which case\nefficiency and good design", "start": 6313.95, "duration": 3.24 }, { "text": "is going to be ever more important.", "start": 6317.19, "duration": 2.3 }, { "text": "All this and more next time.", "start": 6319.49, "duration": 2.34 }, { "text": " ", "start": 6321.83, "duration": 4.394 }, { "text": "AUDIENCE: She thought she\nwas doing the right thing.", "start": 6326.224, "duration": 2.166 }, { "text": " ", "start": 6328.39, "duration": 4.975 }, { "text": "[AUDIO PLAYBACK]", "start": 6333.365, "duration": 2.85 }, { "text": "-Tell me more.", "start": 6336.215, "duration": 2.375 }, { "text": "-David was sure it had to be the\nmuppet, something called muppet mode,", "start": 6338.59, "duration": 6.7 }, { "text": "but the pressure was too much.", "start": 6345.29, "duration": 2.86 }, { "text": "-This is Mario in muppet mode.", "start": 6348.15, "duration": 1.37 }, { "text": "Take 23.", "start": 6349.52, "duration": 0.71 }, { "text": " ", "start": 6350.23, "duration": 4.99 }, { "text": "[HONKING]", "start": 6355.22, "duration": 2.45 }, { "text": " ", "start": 6357.67, "duration": 14.71 }, { "text": "-What's happening?", "start": 6372.38, "duration": 1.048 }, { "text": "I thought this is what\nyou always wanted,", "start": 6373.428, "duration": 2.302 }, { "text": "to star in the walkthrough videos,\nto have all of YouTube's eyes", "start": 6375.73, "duration": 3.52 }, { "text": "watching you.", "start": 6379.25, "duration": 2.5 }, { "text": "[HONKING]", "start": 6381.75, "duration": 0.5 }, { "text": " ", "start": 6382.25, "duration": 5.74 }, { "text": "-Yes, you are.", "start": 6387.99, "duration": 1.96 }, { "text": "You have to be.", "start": 6389.95, "duration": 1.53 }, { "text": "Now stand up straight, tuck in\nyour shirt, look into the camera!", "start": 6391.48, "duration": 3.662 }, { "text": "Take it again from the top.", "start": 6395.142, "duration": 2.238 }, { "text": " ", "start": 6397.38, "duration": 6.178 } ]