1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
1
00:00:00,000 --> 00:00:00,500

2
00:00:00,500 --> 00:00:05,120
[MUSIC PLAYING]

3
00:00:05,120 --> 00:00:12,026

4
00:00:12,026 --> 00:00:12,900
SPEAKER 1: All right.

5
00:00:12,900 --> 00:00:14,600
Everyone welcome back to section.

6
00:00:14,600 --> 00:00:18,660
I hope you all are successfully
recovered from your quiz

7
00:00:18,660 --> 00:00:19,510
from last week.

8
00:00:19,510 --> 00:00:22,564
I know it's a little bit crazy at times.

9
00:00:22,564 --> 00:00:25,230
As I was saying before, if you're
within the standard deviation,

10
00:00:25,230 --> 00:00:28,188
don't really worry about it, especially
for a less comfortable section.

11
00:00:28,188 --> 00:00:30,230
That's about where you should be.

12
00:00:30,230 --> 00:00:32,850
>> If you did great, then awesome.

13
00:00:32,850 --> 00:00:33,650
Kudos to you.

14
00:00:33,650 --> 00:00:36,149
And if you feel like you need
a little bit more help, please

15
00:00:36,149 --> 00:00:38,140
feel free to reach
out to any of the TFs.

16
00:00:38,140 --> 00:00:40,030
We are all here to help.

17
00:00:40,030 --> 00:00:40,960
>> That's why we teach.

18
00:00:40,960 --> 00:00:44,550
That's why I'm here every Monday for you
guys and at office hours on Thursdays.

19
00:00:44,550 --> 00:00:48,130
So please feel free to let me know
if you're worried about anything

20
00:00:48,130 --> 00:00:52,450
or if there's anything on the quiz
that you'd really like to address.

21
00:00:52,450 --> 00:00:56,940
>> So the agenda for today is
all about data structures.

22
00:00:56,940 --> 00:01:01,520
Some of these are just going to be just
to get you familiarized with these.

23
00:01:01,520 --> 00:01:04,870
You may not ever implement
them in this class.

24
00:01:04,870 --> 00:01:08,690
Some of them you will,
like for your speller pset.

25
00:01:08,690 --> 00:01:11,380
>> You'll have your choice
between hash tables and tries.

26
00:01:11,380 --> 00:01:13,680
So we'll definitely be going over those.

27
00:01:13,680 --> 00:01:18,690
It's going to be definitely more of kind
of a high level section today, though,

28
00:01:18,690 --> 00:01:22,630
because there are a lot of them, and if
we went into the implementation details

29
00:01:22,630 --> 00:01:26,490
on all of these, we wouldn't
even get through linked lists

30
00:01:26,490 --> 00:01:28,520
and maybe a little bit of hash tables.

31
00:01:28,520 --> 00:01:31,200
>> So bear with me.

32
00:01:31,200 --> 00:01:33,530
We're not going to be doing
as much coding this time.

33
00:01:33,530 --> 00:01:36,870
If you have any questions about it
or you want to see it implemented

34
00:01:36,870 --> 00:01:39,260
or try it for yourself,
I definitely recommend

35
00:01:39,260 --> 00:01:44,250
going to study.cs50.net, which
has examples of all of these.

36
00:01:44,250 --> 00:01:46,400
It'll have my PowerPoints
with the notes that we

37
00:01:46,400 --> 00:01:50,860
tend to use as well as some programming
exercises, especially for things

38
00:01:50,860 --> 00:01:55,250
like linked lists and binary
trees stacks and cues.

39
00:01:55,250 --> 00:01:59,590
So little more high level, which
might be nice for you guys.

40
00:01:59,590 --> 00:02:01,320
>> So with that, we'll get started.

41
00:02:01,320 --> 00:02:03,060
And also, yes-- quizzes.

42
00:02:03,060 --> 00:02:06,550
I think most of you who are in
my section have your quizzes,

43
00:02:06,550 --> 00:02:12,060
but anyone comes in or some reason you
don't, they're right here in the front.

44
00:02:12,060 --> 00:02:12,740
>> So linked lists.

45
00:02:12,740 --> 00:02:15,650
I know this kind of goes
to back before your quiz.

46
00:02:15,650 --> 00:02:17,940
That was the week before
that we learned about this.

47
00:02:17,940 --> 00:02:21,040
But in this case, we'll just
go a little bit more in depth.

48
00:02:21,040 --> 00:02:25,900
>> So why might we choose a
linked list over an array?

49
00:02:25,900 --> 00:02:27,130
What distinguishes them?

50
00:02:27,130 --> 00:02:27,630
Yes?

51
00:02:27,630 --> 00:02:30,464
>> AUDIENCE: You can expand a linked
list versus an array's fixed size.

52
00:02:30,464 --> 00:02:31,171
SPEAKER 1: Right.

53
00:02:31,171 --> 00:02:33,970
An array has fixed size whereas a
linked list has a variable size.

54
00:02:33,970 --> 00:02:36,970
So if we don't know how
much we want to store,

55
00:02:36,970 --> 00:02:39,880
a linked list gives us a great
way to do that because we can just

56
00:02:39,880 --> 00:02:43,730
add on another node and add on
another node and add on another node.

57
00:02:43,730 --> 00:02:45,750
But what might be a trade-off?

58
00:02:45,750 --> 00:02:49,521
Does anyone remember the trade-off
between arrays and linked lists?

59
00:02:49,521 --> 00:02:50,020
Mmhmm?

60
00:02:50,020 --> 00:02:51,460
>> AUDIENCE: You have to
go through all the way

61
00:02:51,460 --> 00:02:53,738
through the linked list
find an element in a list.

62
00:02:53,738 --> 00:02:55,570
In an array, you can
just find an element.

63
00:02:55,570 --> 00:02:56,278
>> SPEAKER 1: Right.

64
00:02:56,278 --> 00:02:57,120
So with arrays--

65
00:02:57,120 --> 00:02:58,500
>> AUDIENCE: [INAUDIBLE].

66
00:02:58,500 --> 00:03:01,090
>> SPEAKER 1: With arrays, we have
what's called random access.

67
00:03:01,090 --> 00:03:04,820
Means that if we want what is
ever the fifth point of a list

68
00:03:04,820 --> 00:03:07,230
or the fifth point of our
array, we can just grab it.

69
00:03:07,230 --> 00:03:10,440
If it's a linked list, we have
to iterate through, right?

70
00:03:10,440 --> 00:03:14,020
So accessing an element in
an array is constant time,

71
00:03:14,020 --> 00:03:19,530
whereas with a linked list it would
most likely be linear time because maybe

72
00:03:19,530 --> 00:03:21,370
our element is all the way at the end.

73
00:03:21,370 --> 00:03:23,446
We have to search through everything.

74
00:03:23,446 --> 00:03:25,320
So with all these data
structures we're going

75
00:03:25,320 --> 00:03:29,330
to be spending a little more time on,
what are the pluses and negatives.

76
00:03:29,330 --> 00:03:31,480
When might we want to
use one over the other?

77
00:03:31,480 --> 00:03:34,970
And that's kind of the
bigger thing to take away.

78
00:03:34,970 --> 00:03:40,140
>> So we have here the
definition of a node.

79
00:03:40,140 --> 00:03:43,040
It's like one element in
our linked list, right?

80
00:03:43,040 --> 00:03:46,180
So we're all familiar
with our typedef structs,

81
00:03:46,180 --> 00:03:47,980
which we went over in review last time.

82
00:03:47,980 --> 00:03:53,180
It was basically just creating
another data type that we could use.

83
00:03:53,180 --> 00:03:57,930
>> And in this case, it's some node
that will hold some integer in.

84
00:03:57,930 --> 00:04:00,210
And then what's the second part here?

85
00:04:00,210 --> 00:04:03,192

86
00:04:03,192 --> 00:04:05,677
Anyone?

87
00:04:05,677 --> 00:04:06,680
>> AUDIENCE: [INAUDIBLE].

88
00:04:06,680 --> 00:04:07,020
>> SPEAKER 1: Yeah.

89
00:04:07,020 --> 00:04:08,400
It's a pointer to the next node.

90
00:04:08,400 --> 00:04:12,610
So this should actually be up here.

91
00:04:12,610 --> 00:04:18,790
This is a pointer of type
node to the next thing.

92
00:04:18,790 --> 00:04:22,410
And that's what they
encompasses our node.

93
00:04:22,410 --> 00:04:24,060
Cool.

94
00:04:24,060 --> 00:04:29,390
>> All right, so with search, as we were
just saying before hand, if you're

95
00:04:29,390 --> 00:04:31,840
going to search through,
you have to actually iterate

96
00:04:31,840 --> 00:04:33,660
through your linked list.

97
00:04:33,660 --> 00:04:38,530
So if we're looking for the number
9, we would start at our head

98
00:04:38,530 --> 00:04:41,520
and that points us at the beginning
of our linked list, right?

99
00:04:41,520 --> 00:04:44,600
And we say, OK, does this
node contain the number 9?

100
00:04:44,600 --> 00:04:45,690
No?

101
00:04:45,690 --> 00:04:47,500
>> All right, go to the next one.

102
00:04:47,500 --> 00:04:48,312
Follow it.

103
00:04:48,312 --> 00:04:49,520
Does it contain the number 9?

104
00:04:49,520 --> 00:04:50,570
No.

105
00:04:50,570 --> 00:04:51,550
Follow the next one.

106
00:04:51,550 --> 00:04:55,490
>> So we have to actually iterate
through our linked list.

107
00:04:55,490 --> 00:05:00,070
We can't just go directly to where 9 is.

108
00:05:00,070 --> 00:05:05,860
And if you guys actually want to
see some pseudo-code up there.

109
00:05:05,860 --> 00:05:10,420
We have some search function here
that takes in-- what does it take in?

110
00:05:10,420 --> 00:05:13,110

111
00:05:13,110 --> 00:05:14,320
What do you think?

112
00:05:14,320 --> 00:05:15,960
So easy one.

113
00:05:15,960 --> 00:05:17,784
What is this?

114
00:05:17,784 --> 00:05:18,700
AUDIENCE: [INAUDIBLE].

115
00:05:18,700 --> 00:05:20,366
SPEAKER 1: The number we're looking for.

116
00:05:20,366 --> 00:05:20,980
Right?

117
00:05:20,980 --> 00:05:22,875
And what would this correspond to?

118
00:05:22,875 --> 00:05:25,020
It's a pointer to?

119
00:05:25,020 --> 00:05:26,000
>> AUDIENCE: A node.

120
00:05:26,000 --> 00:05:28,980
>> SPEAKER 1: A node to the list
that we're looking at, right?

121
00:05:28,980 --> 00:05:33,700
So we have some nodes are pointer here.

122
00:05:33,700 --> 00:05:37,240
This is a point that's going to
actually iterate through our list.

123
00:05:37,240 --> 00:05:39,630
We set it equal to list
because that's just

124
00:05:39,630 --> 00:05:44,380
setting it equal to the
start of our linked list.

125
00:05:44,380 --> 00:05:50,660
>> And while it's not NULL, while
we still have things in our list,

126
00:05:50,660 --> 00:05:55,580
check to see if that node has
the number we're looking for.

127
00:05:55,580 --> 00:05:57,740
Return true.

128
00:05:57,740 --> 00:06:01,070
Otherwise, update it, right?

129
00:06:01,070 --> 00:06:04,870
>> If it is NULL, we exit our
while loop and return false

130
00:06:04,870 --> 00:06:08,340
because that means we haven't found it.

131
00:06:08,340 --> 00:06:11,048
Does everyone get how that works?

132
00:06:11,048 --> 00:06:11,548
OK.

133
00:06:11,548 --> 00:06:14,940

134
00:06:14,940 --> 00:06:20,260
>> So with insertion, you
have three different ways.

135
00:06:20,260 --> 00:06:25,250
You can prepend, you can append
and you can insert into assorted.

136
00:06:25,250 --> 00:06:28,215
In this case, we're
going to do a prepend.

137
00:06:28,215 --> 00:06:33,380
Does anyone know how those
three cases might differ?

138
00:06:33,380 --> 00:06:36,920
>> So prepend means that you put
it at the front of your list.

139
00:06:36,920 --> 00:06:39,770
So that would mean that no matter
what your node is, no matter

140
00:06:39,770 --> 00:06:43,160
what the value is, you're going
to put it right here in front, OK?

141
00:06:43,160 --> 00:06:45,160
It's going to be the first
element in your list.

142
00:06:45,160 --> 00:06:49,510
>> If you append it, it's going
to go to the back of your list.

143
00:06:49,510 --> 00:06:54,010
And insert into assorted means you're
going to put actually into the place

144
00:06:54,010 --> 00:06:57,700
where it keeps your linked list sorted.

145
00:06:57,700 --> 00:07:00,810
Again, how you use
those and when you use

146
00:07:00,810 --> 00:07:02,530
them will vary depending on your case.

147
00:07:02,530 --> 00:07:05,834

148
00:07:05,834 --> 00:07:07,750
If it doesn't need to
be sorted, prepend tends

149
00:07:07,750 --> 00:07:10,460
to be what most people
use because you don't

150
00:07:10,460 --> 00:07:15,680
have to go through the entire list
to find the end to add it on, right?

151
00:07:15,680 --> 00:07:17,720
You can just stick it right in.

152
00:07:17,720 --> 00:07:21,930
>> So we will go through an
insertion 1 right now.

153
00:07:21,930 --> 00:07:26,360
So one thing that I'm going to
highly recommend on this pset

154
00:07:26,360 --> 00:07:29,820
is to draw things out, as always.

155
00:07:29,820 --> 00:07:35,130
It's very important that you update
your pointers in the correct order

156
00:07:35,130 --> 00:07:38,620
because if you update them
slightly out of order,

157
00:07:38,620 --> 00:07:42,210
you're going to end up
losing parts of your list.

158
00:07:42,210 --> 00:07:49,680
>> So for example, in this case, we're
telling the head to just point to 1.

159
00:07:49,680 --> 00:07:56,070
If we just do that
without saving this 1,

160
00:07:56,070 --> 00:07:58,570
we have no idea what
1 should point to now

161
00:07:58,570 --> 00:08:02,490
because we've lost what
the head pointed to.

162
00:08:02,490 --> 00:08:05,530
So one thing to remember
when you're doing a prepend

163
00:08:05,530 --> 00:08:09,630
is to save what the
head points to first,

164
00:08:09,630 --> 00:08:15,210
then reassign it, and then update
what your new node should point to.

165
00:08:15,210 --> 00:08:20,870

166
00:08:20,870 --> 00:08:22,560
In this case, this is one way to do it.

167
00:08:22,560 --> 00:08:25,440
>> So if we had done it this way
where we just reassigned head,

168
00:08:25,440 --> 00:08:30,320
we lose basically our
entire list, right?

169
00:08:30,320 --> 00:08:38,000
One way to do it is to have 1 point to
next, and then have head point to 1.

170
00:08:38,000 --> 00:08:42,650
Or you can do kind of like the
temporary storage, which I talked about.

171
00:08:42,650 --> 00:08:45,670
>> But reassigning your
pointers in the correct order

172
00:08:45,670 --> 00:08:48,750
is going to be very, very
important for this pset.

173
00:08:48,750 --> 00:08:53,140
Otherwise, you're going to have a hash
table or a try that's just going to be

174
00:08:53,140 --> 00:08:56,014
only part of the words that you
want and then you're-- mmhmm?

175
00:08:56,014 --> 00:08:58,930
AUDIENCE: What was the temporary
storage thing you were talking about?

176
00:08:58,930 --> 00:09:00,305
SPEAKER 1: The temporary storage.

177
00:09:00,305 --> 00:09:02,760
So basically another
way you could do this

178
00:09:02,760 --> 00:09:07,650
is store the head of something, like
store it the temporary variable.

179
00:09:07,650 --> 00:09:11,250
Assign it to 1 and
then update 1 to point

180
00:09:11,250 --> 00:09:13,830
to whatever head used to point to.

181
00:09:13,830 --> 00:09:16,920
This way is obviously
more elegant because you

182
00:09:16,920 --> 00:09:20,770
don't need a temporary value, but
just offering another way to do it.

183
00:09:20,770 --> 00:09:23,999

184
00:09:23,999 --> 00:09:25,790
And we actually do have
some code for this.

185
00:09:25,790 --> 00:09:28,080
So for linked list, we
actually have some code.

186
00:09:28,080 --> 00:09:31,930
So insert here, this is prepending.

187
00:09:31,930 --> 00:09:34,290
So this enters it at the head.

188
00:09:34,290 --> 00:09:38,820
>> So first thing, you need to
create your new node, of course,

189
00:09:38,820 --> 00:09:40,790
and check for NULL.

190
00:09:40,790 --> 00:09:43,250
Always good.

191
00:09:43,250 --> 00:09:47,840
And then you need to assign the values.

192
00:09:47,840 --> 00:09:51,260
Whenever you create a new node, you
don't know what it's pointing to next,

193
00:09:51,260 --> 00:09:54,560
so you want to initialize it to NULL.

194
00:09:54,560 --> 00:09:58,760
If it does end up pointing to something
else, it gets reassigned and it's fine.

195
00:09:58,760 --> 00:10:00,740
If it's the first thing
in the list, it needs

196
00:10:00,740 --> 00:10:04,270
to point to NULL because
that's the end of the list.

197
00:10:04,270 --> 00:10:12,410
>> So then to insert it, we see here we
are assigning the next value of our node

198
00:10:12,410 --> 00:10:17,380
to be whatever head is,
which is what we had here.

199
00:10:17,380 --> 00:10:19,930
That's what we just did.

200
00:10:19,930 --> 00:10:25,820
And then we're assigning head to point
to our new node, because remember,

201
00:10:25,820 --> 00:10:31,090
new is some pointer to a node,
and that's exactly what head is.

202
00:10:31,090 --> 00:10:34,370
That is exactly why we
have this arrow accessor.

203
00:10:34,370 --> 00:10:37,030

204
00:10:37,030 --> 00:10:37,530
Cool?

205
00:10:37,530 --> 00:10:38,130
Mmhmm?

206
00:10:38,130 --> 00:10:41,100
>> AUDIENCE: Do we have to
initialize new next to NULL first,

207
00:10:41,100 --> 00:10:44,240
or can we just initialize it to head?

208
00:10:44,240 --> 00:10:48,210
>> SPEAKER 1: New next
needs to be NULL to start

209
00:10:48,210 --> 00:10:53,760
because you don't know
where it's going to be.

210
00:10:53,760 --> 00:10:56,100
Also, this is kind of
just like a paradigm.

211
00:10:56,100 --> 00:10:59,900
You set it equal to NULL just to make
sure that all your bases are covered

212
00:10:59,900 --> 00:11:04,070
before you do any reassignment so that
you're always guaranteed that it will

213
00:11:04,070 --> 00:11:08,880
be pointing to a specific value
versus like a garbage value.

214
00:11:08,880 --> 00:11:12,210
Because, yeah, we assign
new next automatically,

215
00:11:12,210 --> 00:11:15,420
but it's more just like a
good practice to initialize it

216
00:11:15,420 --> 00:11:19,270
in that way and then reassign.

217
00:11:19,270 --> 00:11:23,420
>> OK, so doubly linked lists now.

218
00:11:23,420 --> 00:11:24,601
What do we think?

219
00:11:24,601 --> 00:11:26,350
What's different with
doubly linked lists?

220
00:11:26,350 --> 00:11:30,750

221
00:11:30,750 --> 00:11:34,300
>> So in our linked lists, we can
only move in one direction, right?

222
00:11:34,300 --> 00:11:35,270
We only have next.

223
00:11:35,270 --> 00:11:36,760
We can only go forward.

224
00:11:36,760 --> 00:11:40,300
>> With a doubly linked list,
we can also move backwards.

225
00:11:40,300 --> 00:11:44,810
So we have not only the
number that we want to store,

226
00:11:44,810 --> 00:11:50,110
we have where it points to next
and where we just came from.

227
00:11:50,110 --> 00:11:52,865
So this allows for
some better traversal.

228
00:11:52,865 --> 00:11:56,620

229
00:11:56,620 --> 00:12:01,240
>> So doubly linked nodes,
very similar, right?

230
00:12:01,240 --> 00:12:05,000
Only difference is now we
have a next and a previous.

231
00:12:05,000 --> 00:12:06,235
It's the only difference.

232
00:12:06,235 --> 00:12:09,570

233
00:12:09,570 --> 00:12:14,790
>> So if we were to prepend or append-- we
don't have any code for this up here--

234
00:12:14,790 --> 00:12:17,830
but if you were to try and
insert it, the important thing

235
00:12:17,830 --> 00:12:19,980
is you need to make
sure you're assigning

236
00:12:19,980 --> 00:12:23,360
both your previous and your
next pointer correctly.

237
00:12:23,360 --> 00:12:29,010
So in this case, you would
not only initialize next,

238
00:12:29,010 --> 00:12:31,820
you initialize previous.

239
00:12:31,820 --> 00:12:36,960
If we're at the head of the list, we
would not only make head equal new,

240
00:12:36,960 --> 00:12:41,750
but our new previous should
point to the head, right?

241
00:12:41,750 --> 00:12:43,380
>> That's the only difference.

242
00:12:43,380 --> 00:12:47,200
And if you want more practice with
these with linked lists, with inserting,

243
00:12:47,200 --> 00:12:49,900
with deleting, with insert
into an assorted list,

244
00:12:49,900 --> 00:12:52,670
please check out study.cs50.net.

245
00:12:52,670 --> 00:12:54,870
There's a bunch of great exercises.

246
00:12:54,870 --> 00:12:55,870
I highly recommend them.

247
00:12:55,870 --> 00:12:59,210
I wish we had time to go through them
but there's a lot of data structures

248
00:12:59,210 --> 00:13:01,530
to get through.

249
00:13:01,530 --> 00:13:02,650
>> OK, so hash tables.

250
00:13:02,650 --> 00:13:07,070
This is probably the most
useful bit for your pset

251
00:13:07,070 --> 00:13:11,090
here because you're going to be
implementing one of these, or a try.

252
00:13:11,090 --> 00:13:12,200
I really like hash tables.

253
00:13:12,200 --> 00:13:13,110
They're pretty cool.

254
00:13:13,110 --> 00:13:17,080
>> So basically what
happens is a hash table

255
00:13:17,080 --> 00:13:22,050
is when we really need speedy
insertion, deletion, and lookup.

256
00:13:22,050 --> 00:13:25,010
Those are the things that we're
prioritizing in a hash table.

257
00:13:25,010 --> 00:13:29,500
They can get pretty big,
but as we'll see with tries,

258
00:13:29,500 --> 00:13:33,040
there are things that are much bigger.

259
00:13:33,040 --> 00:13:38,330
>> But basically, all a hash
table is a hash function

260
00:13:38,330 --> 00:13:47,215
that tells you which bucket to put each
of your data, each of your elements in.

261
00:13:47,215 --> 00:13:51,140
A simple way to think of a hash table
is that it's just buckets of things,

262
00:13:51,140 --> 00:13:51,770
right?

263
00:13:51,770 --> 00:13:59,720
So when you are sorting things by
like the first letter of their name,

264
00:13:59,720 --> 00:14:01,820
that's kind of like a hash table.

265
00:14:01,820 --> 00:14:06,180
>> So if I were to group you guys is
into groups of whoever's name starts

266
00:14:06,180 --> 00:14:11,670
with A over here, or whoever's birthday
is in January, February, March,

267
00:14:11,670 --> 00:14:15,220
whatever, that is effectively
creating a hash table.

268
00:14:15,220 --> 00:14:18,120
It's just creating buckets that
you sort your elements into

269
00:14:18,120 --> 00:14:19,520
so that you can find them easier.

270
00:14:19,520 --> 00:14:22,300
So this way when I need
to find one of you,

271
00:14:22,300 --> 00:14:24,680
I don't have to search
through each of your names.

272
00:14:24,680 --> 00:14:29,490
I can be like, oh, I know that
Danielle's birthday is in--

273
00:14:29,490 --> 00:14:30,240
AUDIENCE: --April.

274
00:14:30,240 --> 00:14:30,948
SPEAKER 1: April.

275
00:14:30,948 --> 00:14:33,120
So I look in my April
bucket, and with any luck,

276
00:14:33,120 --> 00:14:38,270
she'll be the only one in there and
my time was constant in that sense,

277
00:14:38,270 --> 00:14:41,230
whereas if I have to look
through a whole bunch of people,

278
00:14:41,230 --> 00:14:43,090
it's going to take much longer.

279
00:14:43,090 --> 00:14:45,830
So hash tables are really just buckets.

280
00:14:45,830 --> 00:14:48,630
Easy way to think of them.

281
00:14:48,630 --> 00:14:52,930
>> So a very important thing about
a hash table is a hash function.

282
00:14:52,930 --> 00:14:58,140
So the things I just talked about, like
your first letter of your first name

283
00:14:58,140 --> 00:15:01,450
or your birthday month,
these are ideas that

284
00:15:01,450 --> 00:15:03,070
really correlate to a hash function.

285
00:15:03,070 --> 00:15:08,900
It's just a way of deciding which
bucket you're element goes into, OK?

286
00:15:08,900 --> 00:15:14,850
So for this pset, you can look up
pretty much any hash function you want.

287
00:15:14,850 --> 00:15:16,030
>> Doesn't have to be your own.

288
00:15:16,030 --> 00:15:21,140
There are some really cool ones out
there that do all sorts of crazy math.

289
00:15:21,140 --> 00:15:25,170
And if you want to make your
spellchecker super fast,

290
00:15:25,170 --> 00:15:27,620
I would definitely
look into one of those.

291
00:15:27,620 --> 00:15:32,390
>> But there are also the
simple ones, like compute

292
00:15:32,390 --> 00:15:39,010
the sum of the words, like
each letter has a number.

293
00:15:39,010 --> 00:15:39,940
Compute the sum.

294
00:15:39,940 --> 00:15:42,230
That determines the bucket.

295
00:15:42,230 --> 00:15:45,430
They also have the easy ones that
are just like all of the A's here,

296
00:15:45,430 --> 00:15:47,050
all of the B's here.

297
00:15:47,050 --> 00:15:48,920
Any one of those.

298
00:15:48,920 --> 00:15:55,770
>> Basically, it just tells you which
array index your element should go into.

299
00:15:55,770 --> 00:15:58,690
Just deciding the bucket--
it's all a hash function is.

300
00:15:58,690 --> 00:16:04,180
So here we have an example which is
just the first letter of the string

301
00:16:04,180 --> 00:16:05,900
that I was just talking about.

302
00:16:05,900 --> 00:16:11,900
>> So you have some hash that's just the
first letter of your string minus A,

303
00:16:11,900 --> 00:16:16,090
which will give you some
number between 0 and 25.

304
00:16:16,090 --> 00:16:20,790
And what you want to do is
make sure that this represents

305
00:16:20,790 --> 00:16:24,110
the size of your hash table--
how many buckets there are.

306
00:16:24,110 --> 00:16:25,860
With many of these
hash functions, they're

307
00:16:25,860 --> 00:16:31,630
going to be returning values that might
be far above the number of buckets

308
00:16:31,630 --> 00:16:33,610
that you actually have
in your hash table,

309
00:16:33,610 --> 00:16:37,240
so you need to make
sure and mod by those.

310
00:16:37,240 --> 00:16:42,190
Otherwise, it's going to say,
oh, it should be in bucket 5,000

311
00:16:42,190 --> 00:16:46,040
but you only have 30
buckets in your hash table.

312
00:16:46,040 --> 00:16:49,360
And of course, we all know that's
going to result in some crazy errors.

313
00:16:49,360 --> 00:16:52,870
So make sure to mod by the
size of your hash table.

314
00:16:52,870 --> 00:16:58,430

315
00:16:58,430 --> 00:16:58,930
Cool.

316
00:16:58,930 --> 00:17:00,506
So collisions.

317
00:17:00,506 --> 00:17:02,620
Is everyone good so far?

318
00:17:02,620 --> 00:17:03,120
Mmhmm?

319
00:17:03,120 --> 00:17:05,900
>> AUDIENCE: Why would it
return such a massive value?

320
00:17:05,900 --> 00:17:09,210
>> SPEAKER 1: Depending on the algorithm
that your hash function uses.

321
00:17:09,210 --> 00:17:12,270
Some of them will do
crazy multiplication.

322
00:17:12,270 --> 00:17:16,270
And it's all about getting
a even distribution,

323
00:17:16,270 --> 00:17:18,490
so they do some really
crazy things sometimes.

324
00:17:18,490 --> 00:17:20,960
That's all.

325
00:17:20,960 --> 00:17:22,140
Anything else?

326
00:17:22,140 --> 00:17:22,829
OK.

327
00:17:22,829 --> 00:17:24,480
>> So collisions.

328
00:17:24,480 --> 00:17:29,270
Basically, as I said earlier,
in the best case scenario,

329
00:17:29,270 --> 00:17:32,040
any bucket I look into is
going to have one thing,

330
00:17:32,040 --> 00:17:34,160
so I don't have to look at all, right?

331
00:17:34,160 --> 00:17:37,040
I either know it's there or it's
not, and that's what we really want.

332
00:17:37,040 --> 00:17:43,960
But if we have tens of thousands of
data points and less than that number

333
00:17:43,960 --> 00:17:48,700
of buckets, we're going to have
collisions where eventually something

334
00:17:48,700 --> 00:17:54,210
is going to have to end up in a
bucket that already has an element.

335
00:17:54,210 --> 00:17:57,390
>> So the question is, what
do we do in that case?

336
00:17:57,390 --> 00:17:58,480
What do we do?

337
00:17:58,480 --> 00:17:59,300
We already have something there?

338
00:17:59,300 --> 00:18:00,060
Do we just throw it out?

339
00:18:00,060 --> 00:18:00,700
>> No.

340
00:18:00,700 --> 00:18:01,980
We have to keep both of them.

341
00:18:01,980 --> 00:18:06,400
So the way that we
typically do that is what?

342
00:18:06,400 --> 00:18:08,400
What is the data structure
we just talked about?

343
00:18:08,400 --> 00:18:09,316
AUDIENCE: Linked list.

344
00:18:09,316 --> 00:18:10,500
SPEAKER 1: A linked list.

345
00:18:10,500 --> 00:18:16,640
So now, instead of each of these
buckets just having one element,

346
00:18:16,640 --> 00:18:24,020
it's going to contain a linked list of
the elements that were hashed into it.

347
00:18:24,020 --> 00:18:27,588
OK, does everyone kind of get that idea?

348
00:18:27,588 --> 00:18:30,546
Because we couldn't have an array
because we don't know how many things

349
00:18:30,546 --> 00:18:31,730
are going to be in there.

350
00:18:31,730 --> 00:18:36,540
A linked list allows us to
have just the exact number that

351
00:18:36,540 --> 00:18:38,465
are hashed into that bucket, right?

352
00:18:38,465 --> 00:18:42,260

353
00:18:42,260 --> 00:18:50,500
>> So linear probing is
basically this idea--

354
00:18:50,500 --> 00:18:52,300
it's one way to deal with a collision.

355
00:18:52,300 --> 00:18:58,010
What you can do is if, in this
case, berry was hashed into 1

356
00:18:58,010 --> 00:19:01,130
and we already have
something there, you just

357
00:19:01,130 --> 00:19:04,840
keep going down until
you find an empty slot.

358
00:19:04,840 --> 00:19:06,370
That's one way to handle it.

359
00:19:06,370 --> 00:19:09,020
The other way to handle
it is with what we just

360
00:19:09,020 --> 00:19:12,280
called-- the linked
list is called chaining.

361
00:19:12,280 --> 00:19:20,510
>> So this idea works if
your hash table you think

362
00:19:20,510 --> 00:19:24,150
is much larger than
your data set or if you

363
00:19:24,150 --> 00:19:28,870
want to try and minimize chaining
until it's absolutely necessary.

364
00:19:28,870 --> 00:19:34,050
So one thing is linear
probing obviously means

365
00:19:34,050 --> 00:19:37,290
that your hash function
isn't quite as useful

366
00:19:37,290 --> 00:19:42,200
because you're going to end up using
your hash function, getting to a point,

367
00:19:42,200 --> 00:19:46,400
you linear probe down to
some place that is available.

368
00:19:46,400 --> 00:19:49,670
But now, of course, anything
else that ends up there,

369
00:19:49,670 --> 00:19:52,050
you're going to have to
search even further down.

370
00:19:52,050 --> 00:19:55,650
>> And there's a lot more
search expense that

371
00:19:55,650 --> 00:19:59,820
goes into inputting an element
in your hash table now, right?

372
00:19:59,820 --> 00:20:05,640
And now when you go and try and find
berry again, you're going to hash it,

373
00:20:05,640 --> 00:20:07,742
and it's going to say,
oh, look in bucket 1,

374
00:20:07,742 --> 00:20:09,700
and it's not going to be
in bucket 1, so you're

375
00:20:09,700 --> 00:20:11,970
going to have to traverse
through the rest of these.

376
00:20:11,970 --> 00:20:17,720
So it's sometimes useful,
but in most cases,

377
00:20:17,720 --> 00:20:22,660
we're going to say that
chaining is what you want to do.

378
00:20:22,660 --> 00:20:25,520
>> So we talked about this earlier.

379
00:20:25,520 --> 00:20:27,812
I got a little ahead of myself.

380
00:20:27,812 --> 00:20:33,560
But chaining is basically that
each bucket in your hash table

381
00:20:33,560 --> 00:20:36,120
is just a linked list.

382
00:20:36,120 --> 00:20:39,660
>> So another way, or more technical
way, to think of a hash table

383
00:20:39,660 --> 00:20:44,490
is that it's just an array
of linked lists, which

384
00:20:44,490 --> 00:20:49,330
when you're writing your dictionary
and you're trying to load it,

385
00:20:49,330 --> 00:20:52,070
thinking of it as an
array of linked lists

386
00:20:52,070 --> 00:20:54,390
will make it much easier
for you to initialize.

387
00:20:54,390 --> 00:20:57,680
>> AUDIENCE: So hash table
has a predetermined size,

388
00:20:57,680 --> 00:20:58,980
like a [INAUDIBLE] of buckets?

389
00:20:58,980 --> 00:20:59,220
>> SPEAKER 1: Right.

390
00:20:59,220 --> 00:21:01,655
So it has a set number of
buckets that you determine--

391
00:21:01,655 --> 00:21:03,530
which you guys should
feel free to play with.

392
00:21:03,530 --> 00:21:05,269
It can be pretty cool
to see what happens

393
00:21:05,269 --> 00:21:06,810
as you change your number of buckets.

394
00:21:06,810 --> 00:21:09,410

395
00:21:09,410 --> 00:21:11,510
But yeah, it has a
set number of buckets.

396
00:21:11,510 --> 00:21:15,360
What allows you to fit as
many elements as you need

397
00:21:15,360 --> 00:21:19,350
is this separate chaining where you
have linked lists in each bucket.

398
00:21:19,350 --> 00:21:22,850
That means your hash table
will be exactly the size

399
00:21:22,850 --> 00:21:25,440
that you need it to be, right?

400
00:21:25,440 --> 00:21:27,358
That's the whole point of linked lists.

401
00:21:27,358 --> 00:21:30,850

402
00:21:30,850 --> 00:21:32,480
Cool.

403
00:21:32,480 --> 00:21:38,780
>> So everyone OK there?

404
00:21:38,780 --> 00:21:39,801
All right.

405
00:21:39,801 --> 00:21:40,300
Ah.

406
00:21:40,300 --> 00:21:41,860
What just happened?

407
00:21:41,860 --> 00:21:42,960
Really now.

408
00:21:42,960 --> 00:21:45,250
Guess somebody's killing me.

409
00:21:45,250 --> 00:21:52,060
>> OK we're going to go into
tries, which are a little crazy.

410
00:21:52,060 --> 00:21:53,140
I like hash tables.

411
00:21:53,140 --> 00:21:54,460
I think they're really cool.

412
00:21:54,460 --> 00:21:56,710
Tries are cool, too.

413
00:21:56,710 --> 00:21:59,590
>> So does anyone remember what a try is?

414
00:21:59,590 --> 00:22:01,740
You should have gone over
it briefly in lecture?

415
00:22:01,740 --> 00:22:04,570

416
00:22:04,570 --> 00:22:06,377
Do you remember kind of how it works?

417
00:22:06,377 --> 00:22:08,460
AUDIENCE: I'm just nodding
that we did go over it.

418
00:22:08,460 --> 00:22:09,626
SPEAKER 1: We do go over it.

419
00:22:09,626 --> 00:22:13,100
OK, we're really going to go
over it now is what we're saying.

420
00:22:13,100 --> 00:22:14,860
>> AUDIENCE: That's for a retrieval tree.

421
00:22:14,860 --> 00:22:15,280
>> SPEAKER 1: Yeah.

422
00:22:15,280 --> 00:22:16,196
It's a retrieval tree.

423
00:22:16,196 --> 00:22:16,960
Awesome.

424
00:22:16,960 --> 00:22:23,610
So one thing to notice here is that we
are looking at individual characters

425
00:22:23,610 --> 00:22:24,480
here, right?

426
00:22:24,480 --> 00:22:29,710
>> So before with our hash function, we
were looking at the words as a whole,

427
00:22:29,710 --> 00:22:32,270
and now we're looking more
at the characters, right?

428
00:22:32,270 --> 00:22:38,380
So we have Maxwell over here and Mendel.

429
00:22:38,380 --> 00:22:47,840
So basically a try-- a way to think
about this is that every level here

430
00:22:47,840 --> 00:22:49,000
is an array of letters.

431
00:22:49,000 --> 00:22:53,310

432
00:22:53,310 --> 00:22:55,790
So this is your root node here, right?

433
00:22:55,790 --> 00:23:01,980
This has all the characters of the
alphabet for the start of every word.

434
00:23:01,980 --> 00:23:06,480
>> And what you want to do is
say, OK, we have some M word.

435
00:23:06,480 --> 00:23:10,590
We're going to look for Maxwell, so
we go to M. And M points to a whole

436
00:23:10,590 --> 00:23:14,800
other a array where every
word, as long as there

437
00:23:14,800 --> 00:23:17,044
is a word that has A
as the second letter,

438
00:23:17,044 --> 00:23:19,460
as long as there's a word that
has B as the second letter,

439
00:23:19,460 --> 00:23:24,630
it will have a pointer
going to some next array.

440
00:23:24,630 --> 00:23:29,290
>> There's probably not a
word that MP something,

441
00:23:29,290 --> 00:23:32,980
so at the P position in this
array, it would just be NULL.

442
00:23:32,980 --> 00:23:38,840
It would say, OK, there is no word
that has M followed by a P, OK?

443
00:23:38,840 --> 00:23:43,100
So if we think about it, each
one of these smaller things

444
00:23:43,100 --> 00:23:47,990
is actually one of these
large arrays from A through Z.

445
00:23:47,990 --> 00:23:55,064
So what might be one of the things
that is kind of a drawback of a try?

446
00:23:55,064 --> 00:23:56,500
>> AUDIENCE: A lot of memory.

447
00:23:56,500 --> 00:23:59,940
>> SPEAKER 1: It's a ton of memory, right?

448
00:23:59,940 --> 00:24:08,750
Each one of these blocks here
represents 26 spaces, 26 element array.

449
00:24:08,750 --> 00:24:13,680
So tries get incredibly space heavy.

450
00:24:13,680 --> 00:24:17,100
>> But they are very fast.

451
00:24:17,100 --> 00:24:22,540
So incredibly fast but
really space inefficient.

452
00:24:22,540 --> 00:24:24,810
Kind of have to figure
out which one you want.

453
00:24:24,810 --> 00:24:29,470
These are really cool for your pset,
but they do take up a lot of memory,

454
00:24:29,470 --> 00:24:30,290
so you trade off.

455
00:24:30,290 --> 00:24:31,480
Yeah?

456
00:24:31,480 --> 00:24:34,300
>> AUDIENCE: Would it be possible
to set up a try and then

457
00:24:34,300 --> 00:24:37,967
once you have all the
data in it that you need--

458
00:24:37,967 --> 00:24:39,550
I don't know if that would make sense.

459
00:24:39,550 --> 00:24:42,200
I was getting rid of all the
NULL characters, but then

460
00:24:42,200 --> 00:24:42,910
you wouldn't be able to index them--

461
00:24:42,910 --> 00:24:43,275
>> SPEAKER 1: You still need them.

462
00:24:43,275 --> 00:24:44,854
>> AUDIENCE: -- the same way each time.

463
00:24:44,854 --> 00:24:45,520
SPEAKER 1: Yeah.

464
00:24:45,520 --> 00:24:50,460
You need the NULL characters to let
you know if there's not a word there.

465
00:24:50,460 --> 00:24:52,040
Ben did you have something you want?

466
00:24:52,040 --> 00:24:52,540
OK.

467
00:24:52,540 --> 00:24:54,581
All right, so we're going
to go a little bit more

468
00:24:54,581 --> 00:24:58,920
into the technical detail behind
a try and work through an example.

469
00:24:58,920 --> 00:25:01,490
>> OK, so this is the same thing.

470
00:25:01,490 --> 00:25:06,290
Whereas in a linked list, our main
kind of-- what's the word I want?--

471
00:25:06,290 --> 00:25:08,350
like building block was a node.

472
00:25:08,350 --> 00:25:12,280
In a try, we also have a node,
but it's defined differently.

473
00:25:12,280 --> 00:25:17,000
>> So we have some bool that
represents whether a word actually

474
00:25:17,000 --> 00:25:23,530
exists at this location, and then
we have some array here-- or rather,

475
00:25:23,530 --> 00:25:27,840
this is a pointer to an
array of 27 characters.

476
00:25:27,840 --> 00:25:33,339
And this is for, in this case, this
27-- I'm sure all of you are like, wait,

477
00:25:33,339 --> 00:25:34,880
there are 26 letters in the alphabet.

478
00:25:34,880 --> 00:25:36,010
Why do we have 27?

479
00:25:36,010 --> 00:25:37,870
>> So depending on the
way you implement this,

480
00:25:37,870 --> 00:25:43,240
this is from a pset that
allowed for apostrophes.

481
00:25:43,240 --> 00:25:46,010
So that's why the extra one.

482
00:25:46,010 --> 00:25:50,500
You'll also have in some
cases the null terminator

483
00:25:50,500 --> 00:25:53,230
is included as one of the
characters that it's allowed to be,

484
00:25:53,230 --> 00:25:56,120
and that's how they check to
see if it's the end of the word.

485
00:25:56,120 --> 00:26:01,340
If you're interested, check out
Kevin's video on study.cs50,

486
00:26:01,340 --> 00:26:04,790
as well as Wikipedia has
some good resources there.

487
00:26:04,790 --> 00:26:09,000
>> But we're going to go through just kind
of how you might work through a try

488
00:26:09,000 --> 00:26:11,010
if you're given one.

489
00:26:11,010 --> 00:26:16,230
So we have a super simple one here that
has the words "bat" and "zoom" in them.

490
00:26:16,230 --> 00:26:18,920
And as we see up here,
this little space here

491
00:26:18,920 --> 00:26:22,560
represents our bool that
says, yes, this is a word.

492
00:26:22,560 --> 00:26:27,060
And then this has our
arrays of characters, right?

493
00:26:27,060 --> 00:26:33,480
>> So we are going to go through
finding "bat" in this try.

494
00:26:33,480 --> 00:26:38,340
So begin at the top, right?

495
00:26:38,340 --> 00:26:46,290
And we know that b corresponds to
the second index, the second element

496
00:26:46,290 --> 00:26:47,840
in this array, because a and b.

497
00:26:47,840 --> 00:26:51,340
So approximately the second one.

498
00:26:51,340 --> 00:26:58,820
>> And it says, OK, cool, follow that into
the next array, because if we remember,

499
00:26:58,820 --> 00:27:02,160
it's not that each of these
actually contains the element.

500
00:27:02,160 --> 00:27:07,110
Each one of these arrays
contains a pointer, right?

501
00:27:07,110 --> 00:27:10,030
It's an important distinction to make.

502
00:27:10,030 --> 00:27:13,450
>> I know this is going to be-- tries are
really hard to get on the first time,

503
00:27:13,450 --> 00:27:15,241
so even if this is the
second or third time

504
00:27:15,241 --> 00:27:18,370
and it's still kind
of seeming difficult,

505
00:27:18,370 --> 00:27:21,199
I promise if you go watch
the short again tomorrow,

506
00:27:21,199 --> 00:27:22,740
it'll probably make a lot more sense.

507
00:27:22,740 --> 00:27:23,890
It takes a lot to digest.

508
00:27:23,890 --> 00:27:27,800
I still sometimes am
like, wait, what is a try?

509
00:27:27,800 --> 00:27:29,080
How do I use this?

510
00:27:29,080 --> 00:27:33,880
>> So we have b in this case,
which is our second index.

511
00:27:33,880 --> 00:27:40,240
If we had, say, c or
d or any other letter,

512
00:27:40,240 --> 00:27:45,810
we need to map that back to the index
of our array that that corresponds to.

513
00:27:45,810 --> 00:27:56,930
So we would take like rchar and we just
subtract off a to map it into 0 to 25.

514
00:27:56,930 --> 00:27:58,728
Everybody good how we
map our characters?

515
00:27:58,728 --> 00:28:00,440
OK.

516
00:28:00,440 --> 00:28:05,980
>> So we go to the second one and we
see that, yes, it is not to NULL.

517
00:28:05,980 --> 00:28:07,780
We can move on to this next array.

518
00:28:07,780 --> 00:28:12,300
So we go on to this next array here.

519
00:28:12,300 --> 00:28:15,500
>> And we say, OK, now we
need to see if a is here.

520
00:28:15,500 --> 00:28:18,590
Is A null or does it
actually move forward?

521
00:28:18,590 --> 00:28:21,880
So a actually moves
forward in this array.

522
00:28:21,880 --> 00:28:24,570
And we say, OK, t is our last letter.

523
00:28:24,570 --> 00:28:27,580
So we go to the t at the index.

524
00:28:27,580 --> 00:28:30,120
And then we move forward
because there's another one.

525
00:28:30,120 --> 00:28:38,340
And this one says basically that, yes,
it says that there is a word here--

526
00:28:38,340 --> 00:28:41,750
that if you follow this
path, you have arrived

527
00:28:41,750 --> 00:28:43,210
at a word, which we know is "bat."

528
00:28:43,210 --> 00:28:43,800
Yes?

529
00:28:43,800 --> 00:28:46,770
>> AUDIENCE: Is it standard to have that
as index 0 and then have a sort at 1

530
00:28:46,770 --> 00:28:47,660
or to have at the end?

531
00:28:47,660 --> 00:28:48,243
>> SPEAKER 1: No.

532
00:28:48,243 --> 00:28:55,360
So if we look back at our
declaration here, it's a bool,

533
00:28:55,360 --> 00:28:59,490
so it's its own element in your node.

534
00:28:59,490 --> 00:29:03,331
So it's not part of the array.

535
00:29:03,331 --> 00:29:03,830
Cool.

536
00:29:03,830 --> 00:29:08,370
So when we finish our word and we're
at this array, what we want to do

537
00:29:08,370 --> 00:29:12,807
is do a check for is this a word.

538
00:29:12,807 --> 00:29:14,390
And in this case, it would return yes.

539
00:29:14,390 --> 00:29:17,220

540
00:29:17,220 --> 00:29:24,090
>> So on that note, we know that "zoo"--
we know as humans that "zoo" is a word,

541
00:29:24,090 --> 00:29:24,820
right?

542
00:29:24,820 --> 00:29:28,990
But are try here would
say, no, it's not.

543
00:29:28,990 --> 00:29:33,980
And it would say that because we
haven't designated it as a word here.

544
00:29:33,980 --> 00:29:40,440
Even though we can traverse
through to this array,

545
00:29:40,440 --> 00:29:43,890
this try would say that, no,
zoo isn't in your dictionary

546
00:29:43,890 --> 00:29:47,070
because we haven't
designated it as such.

547
00:29:47,070 --> 00:29:52,870
>> So one way to do that--
oh, sorry, this one.

548
00:29:52,870 --> 00:29:59,450
So in this case, "zoo" is not
a word, but it is in our try.

549
00:29:59,450 --> 00:30:05,690
But in this one, say we want it
introduce the word "bath," what happens

550
00:30:05,690 --> 00:30:08,260
is we follow through-- b, a, t.

551
00:30:08,260 --> 00:30:11,820
We're in this array, and
we go to search for h.

552
00:30:11,820 --> 00:30:15,220
>> In this case, when we
look at the pointer at h,

553
00:30:15,220 --> 00:30:17,890
it's pointing to NULL, OK?

554
00:30:17,890 --> 00:30:20,780
So unless it's explicitly
pointing to another array,

555
00:30:20,780 --> 00:30:25,000
you assume that all the pointers
in this array are pointing to null.

556
00:30:25,000 --> 00:30:28,270
So in this case, h is pointing
to null so we can't do anything,

557
00:30:28,270 --> 00:30:31,540
so it would also return
false, "bath" is not in here.

558
00:30:31,540 --> 00:30:34,102

559
00:30:34,102 --> 00:30:35,810
So now we're actually
going to go through

560
00:30:35,810 --> 00:30:39,790
how would we actually say
that "zoo" is in our try.

561
00:30:39,790 --> 00:30:42,920
How do we insert "zoo" into our try?

562
00:30:42,920 --> 00:30:47,810
So in the same way that we started with
our linked list, we start at the root.

563
00:30:47,810 --> 00:30:50,600
When in doubt, start at
the root of these things.

564
00:30:50,600 --> 00:30:53,330
>> And we'll say, OK, z.

565
00:30:53,330 --> 00:30:55,650
z exists in this, and it does.

566
00:30:55,650 --> 00:30:58,370
So you're moving on to
your next array, OK?

567
00:30:58,370 --> 00:31:01,482
And then on the next one,
we say, OK, does o exist?

568
00:31:01,482 --> 00:31:03,000
It does.

569
00:31:03,000 --> 00:31:04,330
This again.

570
00:31:04,330 --> 00:31:08,670
>> And so on our next one, we've said,
OK, "zoo" already exists here.

571
00:31:08,670 --> 00:31:12,440
All we need to do is set this equal
to true, that there is a word there.

572
00:31:12,440 --> 00:31:15,260
If you had followed everything
up to before that point,

573
00:31:15,260 --> 00:31:17,030
it's a word, so just
set it equal to such.

574
00:31:17,030 --> 00:31:17,530
Yes?

575
00:31:17,530 --> 00:31:22,550
>> AUDIENCE: So then does that
mean that "ba" is a word also?

576
00:31:22,550 --> 00:31:24,120
>> SPEAKER 1: No.

577
00:31:24,120 --> 00:31:28,870
So in this case, "ba" we would get
here, we would say is it a word,

578
00:31:28,870 --> 00:31:31,590
and it would still be no.

579
00:31:31,590 --> 00:31:32,822
OK?

580
00:31:32,822 --> 00:31:33,740
Mmhmm?

581
00:31:33,740 --> 00:31:36,360
>> AUDIENCE: So once you is it a
word and you say yes, then it

582
00:31:36,360 --> 00:31:38,380
will contain to go to m?

583
00:31:38,380 --> 00:31:42,260
>> SPEAKER 1: So this has to do
with-- you're loading this in.

584
00:31:42,260 --> 00:31:43,640
You say "zoo" is a word.

585
00:31:43,640 --> 00:31:47,020
When you go to check--
like, say you want to say,

586
00:31:47,020 --> 00:31:49,400
does "zoo" exist in this dictionary?

587
00:31:49,400 --> 00:31:54,200
You're only going to search for "zoo,"
and then check to see if it's a word.

588
00:31:54,200 --> 00:31:57,291
You're never going to move
through to m because that's not

589
00:31:57,291 --> 00:31:58,290
what you're looking for.

590
00:31:58,290 --> 00:32:02,690

591
00:32:02,690 --> 00:32:08,070
>> So if we actually wanted to
add "bath" into this try,

592
00:32:08,070 --> 00:32:11,390
we would do the same thing
as we did with "zoo,"

593
00:32:11,390 --> 00:32:15,380
except we would see that when we
try and get to h, it doesn't exist.

594
00:32:15,380 --> 00:32:20,090
So you can think of this as trying
to add a new node into a linked list,

595
00:32:20,090 --> 00:32:27,210
so we would need to add another
one of these arrays, like so.

596
00:32:27,210 --> 00:32:35,670
And then what we do is we just set the h
element of this array pointing to this.

597
00:32:35,670 --> 00:32:39,430
>> And then what would we want to do here?

598
00:32:39,430 --> 00:32:43,110
Add it equal to true
because it's a word.

599
00:32:43,110 --> 00:32:46,350

600
00:32:46,350 --> 00:32:48,150
Cool.

601
00:32:48,150 --> 00:32:48,700
I know.

602
00:32:48,700 --> 00:32:51,170
Tries are not the most exciting.

603
00:32:51,170 --> 00:32:54,250
Trust me, I know.

604
00:32:54,250 --> 00:32:58,040
>> So one thing to realize with tries,
I said, they're very efficient.

605
00:32:58,040 --> 00:33:00,080
So we've seen they
take up a ton of space.

606
00:33:00,080 --> 00:33:01,370
They're kind of confusing.

607
00:33:01,370 --> 00:33:03,367
So why would we ever use these?

608
00:33:03,367 --> 00:33:05,450
We use these because they're
incredibly efficient.

609
00:33:05,450 --> 00:33:08,130
>> So if you're ever looking
up a word, you are only

610
00:33:08,130 --> 00:33:10,450
bounded by the length of the word.

611
00:33:10,450 --> 00:33:15,210
So if you're looking for a
word that is of length five,

612
00:33:15,210 --> 00:33:20,940
you're only ever going to have to
make at most five comparisons, OK?

613
00:33:20,940 --> 00:33:25,780
So it makes it basically a constant.

614
00:33:25,780 --> 00:33:29,150
Like insertion and lookup
are basically constant time.

615
00:33:29,150 --> 00:33:33,750
>> So if you can ever get
something in constant time,

616
00:33:33,750 --> 00:33:35,150
that's as good as it gets.

617
00:33:35,150 --> 00:33:37,990
You can't get better than
constant time for these things.

618
00:33:37,990 --> 00:33:43,150
So that is one of the
huge pluses of tries.

619
00:33:43,150 --> 00:33:46,780
>> But it is a lot of space.

620
00:33:46,780 --> 00:33:50,380
So you kind of have to decide
what's more important to you.

621
00:33:50,380 --> 00:33:54,700
And on today's computers, the
space that a try may take up

622
00:33:54,700 --> 00:33:57,740
maybe doesn't affect
you that much, but maybe

623
00:33:57,740 --> 00:34:01,350
you're dealing with something
that has far, far more things,

624
00:34:01,350 --> 00:34:02,810
and a try just isn't reasonable.

625
00:34:02,810 --> 00:34:03,000
Yes?

626
00:34:03,000 --> 00:34:05,610
>> AUDIENCE: Wait, so you have 26
letters in every single one?

627
00:34:05,610 --> 00:34:07,440
>> SPEAKER 1: Mmhmm.

628
00:34:07,440 --> 00:34:08,570
Yeah, you have 26.

629
00:34:08,570 --> 00:34:16,984
You have some is word marker and then
you have 26 pointers in every one.

630
00:34:16,984 --> 00:34:17,775
And they're point--

631
00:34:17,775 --> 00:34:20,280
>> AUDIENCE: And every 26,
do they each have 26?

632
00:34:20,280 --> 00:34:21,500
>> SPEAKER 1: Yes.

633
00:34:21,500 --> 00:34:27,460
And that's why, as you can
see, it expands quite rapidly.

634
00:34:27,460 --> 00:34:28,130
All right.

635
00:34:28,130 --> 00:34:32,524
So we're going to get into trees, which
I feel like is easier and will probably

636
00:34:32,524 --> 00:34:36,150
be a nice little reprieve
from tries there.

637
00:34:36,150 --> 00:34:39,620
So hopefully most of you
have seen a tree before.

638
00:34:39,620 --> 00:34:41,820
Not like the pretty
ones outside, which I

639
00:34:41,820 --> 00:34:44,340
don't know if anyone
went outdoors recently.

640
00:34:44,340 --> 00:34:49,230
I went apple picking this weekend,
and oh my gosh, it was beautiful.

641
00:34:49,230 --> 00:34:52,250
I didn't know leaves
could look that pretty.

642
00:34:52,250 --> 00:34:53,610
>> So this is just a tree, right?

643
00:34:53,610 --> 00:34:56,790
It's just some node, and it
points to a bunch of other nodes.

644
00:34:56,790 --> 00:34:59,570
As you see here, this is
kind of a recurring theme.

645
00:34:59,570 --> 00:35:03,720
Nodes pointing to nodes is kind of
the essence of many data structures.

646
00:35:03,720 --> 00:35:06,670
It just depends on how we
have them point to each other

647
00:35:06,670 --> 00:35:08,600
and how we traverse
through them and how we

648
00:35:08,600 --> 00:35:14,500
insert things that determines
their different characteristics.

649
00:35:14,500 --> 00:35:17,600
>> So just some terminology,
which I've used before.

650
00:35:17,600 --> 00:35:20,010
So root is whatever is at the very top.

651
00:35:20,010 --> 00:35:21,200
it's where we always start.

652
00:35:21,200 --> 00:35:23,610
You can think of it as the head also.

653
00:35:23,610 --> 00:35:28,750
But for trees, we tend to
refer to it as the root.

654
00:35:28,750 --> 00:35:32,820
>> Anything at the bottom here--
at the very, very bottom--

655
00:35:32,820 --> 00:35:34,500
are considered leaves.

656
00:35:34,500 --> 00:35:37,210
So it goes along with the
whole tree thing, right?

657
00:35:37,210 --> 00:35:39,860
Leaves are at the edges of your tree.

658
00:35:39,860 --> 00:35:45,820
>> And then we also have a couple of
terms to talk about nodes in relation

659
00:35:45,820 --> 00:35:46,680
to each other.

660
00:35:46,680 --> 00:35:49,700
So we have parent,
children, and siblings.

661
00:35:49,700 --> 00:35:56,260
So in this case, 3 is the
parent of 5, 6, and 7.

662
00:35:56,260 --> 00:36:00,370
So the parent is whatever is
one step above whatever you're

663
00:36:00,370 --> 00:36:02,940
referring to, so just
like a family tree.

664
00:36:02,940 --> 00:36:07,090
Hopefully, this is all a little
bit more intuitive than the tries.

665
00:36:07,090 --> 00:36:10,970
>> Siblings are any that have
the same parent, right?

666
00:36:10,970 --> 00:36:13,470
They're on the same level here.

667
00:36:13,470 --> 00:36:16,960
And then, as I was
saying, children are just

668
00:36:16,960 --> 00:36:22,630
whatever is one step below
the node in question, OK?

669
00:36:22,630 --> 00:36:23,470
Cool.

670
00:36:23,470 --> 00:36:25,610
So a binary tree.

671
00:36:25,610 --> 00:36:31,450
Can anyone hazard a guess on one of
the characteristics of the binary tree?

672
00:36:31,450 --> 00:36:32,770
>> AUDIENCE: Max two leaves.

673
00:36:32,770 --> 00:36:33,478
>> SPEAKER 1: Right.

674
00:36:33,478 --> 00:36:34,640
So max of two leaves.

675
00:36:34,640 --> 00:36:39,730
So in this one before, we had this one
that had three, but in a binary tree,

676
00:36:39,730 --> 00:36:45,000
you have a max of two
children per parent, right?

677
00:36:45,000 --> 00:36:46,970
There's another
interesting characteristic.

678
00:36:46,970 --> 00:36:51,550
Does anyone know that?

679
00:36:51,550 --> 00:36:52,620
Binary tree.

680
00:36:52,620 --> 00:37:00,350
>> So a binary tree will have everything
on the-- this one isn't sorted--

681
00:37:00,350 --> 00:37:05,320
but in a sorted binary tree,
everything on the right

682
00:37:05,320 --> 00:37:08,530
is greater than the parent,
and everything on the left

683
00:37:08,530 --> 00:37:10,035
is less than the parent.

684
00:37:10,035 --> 00:37:15,690
And that has been a quiz
question before, so good to know.

685
00:37:15,690 --> 00:37:19,500
So the way we define this,
again, we have another node.

686
00:37:19,500 --> 00:37:21,880
This looks very similar to what?

687
00:37:21,880 --> 00:37:28,336

688
00:37:28,336 --> 00:37:28,836
Doubly

689
00:37:28,836 --> 00:37:29,320
>> AUDIENCE: Linked lists

690
00:37:29,320 --> 00:37:31,100
>> SPEAKER 1: A double linked list, right?

691
00:37:31,100 --> 00:37:33,690
So if we replace this
with previous and next,

692
00:37:33,690 --> 00:37:35,670
this would be a doubly linked list.

693
00:37:35,670 --> 00:37:40,125
But in this case, we actually
have left and right and that's it.

694
00:37:40,125 --> 00:37:41,500
Otherwise, it's exactly the same.

695
00:37:41,500 --> 00:37:43,374
We still have the element
you're looking for,

696
00:37:43,374 --> 00:37:45,988
and you just have two pointers
going to whatever's next.

697
00:37:45,988 --> 00:37:49,210

698
00:37:49,210 --> 00:37:51,870
Yeah, so binary search tree.

699
00:37:51,870 --> 00:37:57,665
If we notice, everything on the
right here is greater than--

700
00:37:57,665 --> 00:37:59,850
or everything immediately
to the right here

701
00:37:59,850 --> 00:38:02,840
is greater than, everything
here is less than.

702
00:38:02,840 --> 00:38:06,980

703
00:38:06,980 --> 00:38:14,000
>> So if we were to search through, it
should look very close to binary search

704
00:38:14,000 --> 00:38:14,910
here, right?

705
00:38:14,910 --> 00:38:17,640
Except instead of looking
at half the array,

706
00:38:17,640 --> 00:38:21,720
we are just looking at either the left
side or the right side of the tree.

707
00:38:21,720 --> 00:38:24,850
So it gets a little simpler, I think.

708
00:38:24,850 --> 00:38:29,300
>> So if your root is NULL,
obviously it's just false.

709
00:38:29,300 --> 00:38:33,470
And if it's there, obviously it's true.

710
00:38:33,470 --> 00:38:35,320
If it's less than, we search the left.

711
00:38:35,320 --> 00:38:37,070
If it's greater than,
we search the right.

712
00:38:37,070 --> 00:38:39,890
It's exactly like binary search,
just a different data structure

713
00:38:39,890 --> 00:38:40,600
that we're using.

714
00:38:40,600 --> 00:38:42,790
Instead of an array,
it's just a binary tree.

715
00:38:42,790 --> 00:38:45,820

716
00:38:45,820 --> 00:38:48,090
>> OK, stacks.

717
00:38:48,090 --> 00:38:51,550
And also, it looks like we
might have a little bit of time.

718
00:38:51,550 --> 00:38:54,460
If we do, I'm happy to go
over any of this again.

719
00:38:54,460 --> 00:38:56,856
OK, so stacks.

720
00:38:56,856 --> 00:39:02,695
Does anyone remember what stacks--
any characteristics of a stack?

721
00:39:02,695 --> 00:39:05,550

722
00:39:05,550 --> 00:39:10,400
>> OK, so most of us, I think,
eat in the dining halls--

723
00:39:10,400 --> 00:39:13,100
as much as we may not like to.

724
00:39:13,100 --> 00:39:16,900
But obviously, you can think of a stack
literally just as a stack of trays

725
00:39:16,900 --> 00:39:18,460
or a stack of things.

726
00:39:18,460 --> 00:39:21,820
And what's important
to realize is that it's

727
00:39:21,820 --> 00:39:26,850
something-- the characteristic
that we call it by-- is LIFO.

728
00:39:26,850 --> 00:39:28,450
Does anyone know what that stands for?

729
00:39:28,450 --> 00:39:29,070
Mmhmm?

730
00:39:29,070 --> 00:39:30,650
>> AUDIENCE: Last in, first out.

731
00:39:30,650 --> 00:39:32,250
>> SPEAKER 1: Right, last in, first out.

732
00:39:32,250 --> 00:39:36,585
So if we know, if we're stacking things
up, the easiest thing to grab off--

733
00:39:36,585 --> 00:39:39,570
and maybe the only thing we can grab
off if our stack is big enough--

734
00:39:39,570 --> 00:39:40,850
is that top element.

735
00:39:40,850 --> 00:39:43,460
So whatever was put on
last-- as we see here,

736
00:39:43,460 --> 00:39:46,370
whatever was pushed
on most recently-- is

737
00:39:46,370 --> 00:39:51,160
going to be the first
thing that we pop off, OK?

738
00:39:51,160 --> 00:39:56,324
>> So what we have here is
another typedef struct.

739
00:39:56,324 --> 00:39:58,740
This is really just like a
crash course in data structure,

740
00:39:58,740 --> 00:40:01,650
so there's a lot thrown at you guys.

741
00:40:01,650 --> 00:40:02,540
I know.

742
00:40:02,540 --> 00:40:04,970
So yet another struct.

743
00:40:04,970 --> 00:40:06,740
Yay for structures.

744
00:40:06,740 --> 00:40:16,660
>> And in this case, it's some pointer
to an array that has some capacity.

745
00:40:16,660 --> 00:40:20,830
So this represents our stack
here, like our actual array

746
00:40:20,830 --> 00:40:22,520
that's holding our elements.

747
00:40:22,520 --> 00:40:24,850
And then here we have some size.

748
00:40:24,850 --> 00:40:31,170
>> And typically, you want to keep
track of how big your stack is

749
00:40:31,170 --> 00:40:36,180
because what it's going to allow
you to do is if you know the size,

750
00:40:36,180 --> 00:40:39,170
it allows you to say,
OK, am I at capacity?

751
00:40:39,170 --> 00:40:40,570
Can I add anything more?

752
00:40:40,570 --> 00:40:44,650
And it also tells you
where the top of your stack

753
00:40:44,650 --> 00:40:48,180
is so you know what you
can actually take off.

754
00:40:48,180 --> 00:40:51,760
And that's actually going to
be a little more clear here.

755
00:40:51,760 --> 00:40:57,350
>> So for push, one thing, if you
were ever to implement push,

756
00:40:57,350 --> 00:41:01,330
as I was just saying, your
stack has a limited size, right?

757
00:41:01,330 --> 00:41:03,990
Our array had some capacity.

758
00:41:03,990 --> 00:41:04,910
It's an array.

759
00:41:04,910 --> 00:41:08,930
It's a fixed size, so we need to
make sure that we're not putting more

760
00:41:08,930 --> 00:41:11,950
into our array than we
actually have space for.

761
00:41:11,950 --> 00:41:16,900
>> So when you're creating a push
function, first thing you do is say, OK,

762
00:41:16,900 --> 00:41:18,570
do I have space in my stack?

763
00:41:18,570 --> 00:41:23,330
Because if I don't, sorry,
I can't store your element.

764
00:41:23,330 --> 00:41:28,980
If I do, then you want to store
it at the top of the stack, right?

765
00:41:28,980 --> 00:41:31,325
>> And this is why we have
to keep track of our size.

766
00:41:31,325 --> 00:41:35,290
If we don't keep track of our size,
we don't know where to put it.

767
00:41:35,290 --> 00:41:39,035
We don't know how many things
are in our array already.

768
00:41:39,035 --> 00:41:41,410
Like obviously there are ways
that maybe you could do it.

769
00:41:41,410 --> 00:41:44,610
You could initialize everything to NULL
and then check for the latest NULL,

770
00:41:44,610 --> 00:41:47,950
but a much easier thing is just
to say, OK, keep track of size.

771
00:41:47,950 --> 00:41:51,840
Like I know I have four elements
in my array, so the next thing

772
00:41:51,840 --> 00:41:55,930
that we put on, we're
going to store at index 4.

773
00:41:55,930 --> 00:42:00,940
And then, of course, this means that
you've successfully pushed something

774
00:42:00,940 --> 00:42:03,320
onto your stack, you
want to increase the size

775
00:42:03,320 --> 00:42:08,880
so that you know where you are so
that you can push more things on.

776
00:42:08,880 --> 00:42:12,730
>> So if we are trying to pop
something off the stack,

777
00:42:12,730 --> 00:42:16,072
what might be the first thing
that we want to check for?

778
00:42:16,072 --> 00:42:18,030
You're trying to take
something off your stack.

779
00:42:18,030 --> 00:42:21,710

780
00:42:21,710 --> 00:42:24,781
Are you sure there's
something in your stack?

781
00:42:24,781 --> 00:42:25,280
No.

782
00:42:25,280 --> 00:42:26,894
So what might we want to check?

783
00:42:26,894 --> 00:42:27,810
>> AUDIENCE: [INAUDIBLE].

784
00:42:27,810 --> 00:42:29,880
SPEAKER 1: Check for the size?

785
00:42:29,880 --> 00:42:31,840
Size.

786
00:42:31,840 --> 00:42:38,520
So we want to check to see if
our size is greater than 0, OK?

787
00:42:38,520 --> 00:42:44,970
And if it is, then we want to decrease
our size by 0 and return that.

788
00:42:44,970 --> 00:42:45,840
Why?

789
00:42:45,840 --> 00:42:49,950
>> In the first one we were
pushing, we pushed it

790
00:42:49,950 --> 00:42:52,460
onto size and then updated size.

791
00:42:52,460 --> 00:42:57,850
In this case, we're decrementing size
and then taking it off, plucking it

792
00:42:57,850 --> 00:42:58,952
from our array.

793
00:42:58,952 --> 00:42:59,826
Why might we do that?

794
00:42:59,826 --> 00:43:04,800

795
00:43:04,800 --> 00:43:11,811
So if I have one thing on my stack,
what would be my size at that point?

796
00:43:11,811 --> 00:43:13,140
1.

797
00:43:13,140 --> 00:43:15,180
>> And where is element 1 stored?

798
00:43:15,180 --> 00:43:17,621
At what index?

799
00:43:17,621 --> 00:43:18,120
AUDIENCE: 0.

800
00:43:18,120 --> 00:43:19,060
SPEAKER 1: 0.

801
00:43:19,060 --> 00:43:22,800
So in this case, we
always need to make sure--

802
00:43:22,800 --> 00:43:27,630
instead of returning
size minus 1, because we

803
00:43:27,630 --> 00:43:31,730
know that our element is
going to be stored at 1 less

804
00:43:31,730 --> 00:43:34,705
whatever our size is, this
just takes care of it.

805
00:43:34,705 --> 00:43:36,080
It's a slightly more elegant way.

806
00:43:36,080 --> 00:43:41,220
And we just decrement our
size and then return size.

807
00:43:41,220 --> 00:43:42,330
Mmhmm?

808
00:43:42,330 --> 00:43:45,300
>> AUDIENCE: I guess just in general,
why would this data structure

809
00:43:45,300 --> 00:43:47,800
be beneficial?

810
00:43:47,800 --> 00:43:50,660
>> SPEAKER 1: It depends on your context.

811
00:43:50,660 --> 00:43:57,420
So for some of the theory,
if you're working with-- OK,

812
00:43:57,420 --> 00:44:02,750
let me see if there is a beneficial one
that is beneficial to more than outside

813
00:44:02,750 --> 00:44:05,420
of CS.

814
00:44:05,420 --> 00:44:15,780
With stacks, any time you need
to keep track of something that

815
00:44:15,780 --> 00:44:20,456
is the most recently added is when
you're going to want to use a stack.

816
00:44:20,456 --> 00:44:24,770
>> And I can't think of a good
example of that right now.

817
00:44:24,770 --> 00:44:29,955
But whenever the most recent
thing is most important to you,

818
00:44:29,955 --> 00:44:31,705
that's when a stack
is going to be useful.

819
00:44:31,705 --> 00:44:35,797

820
00:44:35,797 --> 00:44:39,330
I'm trying to think if
there's a good one for this.

821
00:44:39,330 --> 00:44:43,720
If I think of a good example in the next
20 minutes, I will definitely tell you.

822
00:44:43,720 --> 00:44:49,455
>> But overall, if there's anything,
like I said most, where most recent

823
00:44:49,455 --> 00:44:52,470
is most important, that's
where a stack comes into play.

824
00:44:52,470 --> 00:44:58,860
Whereas queues are kind of the opposite.

825
00:44:58,860 --> 00:44:59,870
And all the little dogs.

826
00:44:59,870 --> 00:45:00,890
Isn't this great, right?

827
00:45:00,890 --> 00:45:03,299
I feel like I should
just have a bunny video

828
00:45:03,299 --> 00:45:05,090
right in the middle of
section for you guys

829
00:45:05,090 --> 00:45:08,870
because this is an intense section.

830
00:45:08,870 --> 00:45:10,480
>> So a queue.

831
00:45:10,480 --> 00:45:12,710
Basically a queue is like a line.

832
00:45:12,710 --> 00:45:15,780
You guys I'm sure use this everyday,
just like in our dining halls.

833
00:45:15,780 --> 00:45:18,160
So we have to go in
and get our trays, I'm

834
00:45:18,160 --> 00:45:21,260
sure you have to wait in line
to swipe or get your food.

835
00:45:21,260 --> 00:45:24,650
>> So the difference here
is that this is FIFO.

836
00:45:24,650 --> 00:45:30,090
So if LIFO was last in, first
out, FIFO is first in, first out.

837
00:45:30,090 --> 00:45:33,400
So this is where whatever you put
on first is your most important.

838
00:45:33,400 --> 00:45:35,540
So if you were waiting
in a line-- can you

839
00:45:35,540 --> 00:45:39,130
imagine if you went to
go get the new iPhone

840
00:45:39,130 --> 00:45:42,800
and it was a stack where the
last person in line got it first,

841
00:45:42,800 --> 00:45:44,160
people would kill each other.

842
00:45:44,160 --> 00:45:49,800
>> So FIFO, we're all very familiar
with in the real world here,

843
00:45:49,800 --> 00:45:54,930
and it all has to do with actually
kind of recreating this whole line

844
00:45:54,930 --> 00:45:56,900
and queuing structure.

845
00:45:56,900 --> 00:46:02,390
So whereas with the stack,
we have push and pop.

846
00:46:02,390 --> 00:46:06,440
With a queue, we have
enqueue and dequeue.

847
00:46:06,440 --> 00:46:10,910
So enqueue basically means
put it onto the back,

848
00:46:10,910 --> 00:46:13,680
and dequeue means take
off from the front.

849
00:46:13,680 --> 00:46:18,680
So our data structure is a
little bit more complicated.

850
00:46:18,680 --> 00:46:21,060
We have a second thing to keep track of.

851
00:46:21,060 --> 00:46:25,950
>> So without the head, this
is exactly a stack, right?

852
00:46:25,950 --> 00:46:27,900
This is the same structure as a stack.

853
00:46:27,900 --> 00:46:32,480
The only thing different now is we
have this head, which what do you think

854
00:46:32,480 --> 00:46:34,272
is going to keep track of?

855
00:46:34,272 --> 00:46:35,510
>> AUDIENCE: The first one.

856
00:46:35,510 --> 00:46:38,685
>> SPEAKER 1: Right, the
first thing that we put in.

857
00:46:38,685 --> 00:46:41,130
The head of our queue.

858
00:46:41,130 --> 00:46:42,240
Whoever's first in line.

859
00:46:42,240 --> 00:46:45,300

860
00:46:45,300 --> 00:46:49,420
All right, so if we do enqueue.

861
00:46:49,420 --> 00:46:52,720

862
00:46:52,720 --> 00:46:55,920
Again, with any of
these data structures,

863
00:46:55,920 --> 00:46:59,760
since we're dealing with an array,
we need to check if we have space.

864
00:46:59,760 --> 00:47:03,290
>> This is kind of like me telling
you guys, if you open a file,

865
00:47:03,290 --> 00:47:04,760
you need to check for null.

866
00:47:04,760 --> 00:47:08,330
With any of these stacks
and queues, you need

867
00:47:08,330 --> 00:47:13,420
to see if there's space because we're
dealing with a fixed size array,

868
00:47:13,420 --> 00:47:16,030
as we see here-- 0, 1 all up to 5.

869
00:47:16,030 --> 00:47:20,690
So what we do in that case is check
to see if we still have space.

870
00:47:20,690 --> 00:47:23,110
Is our size less than capacity?

871
00:47:23,110 --> 00:47:28,480
>> If so, we need to store it at
the tail and we update our size.

872
00:47:28,480 --> 00:47:30,250
So what might the tail be in this case?

873
00:47:30,250 --> 00:47:32,360
It's not explicitly written out.

874
00:47:32,360 --> 00:47:33,380
How would we store it?

875
00:47:33,380 --> 00:47:34,928
What would the tail be?

876
00:47:34,928 --> 00:47:38,600

877
00:47:38,600 --> 00:47:40,190
>> So let's walk through this example.

878
00:47:40,190 --> 00:47:44,590
So this is an array of size 6, right?

879
00:47:44,590 --> 00:47:49,220
And we have right now, our size is 5.

880
00:47:49,220 --> 00:47:55,240
And when we put it in, it's going
to go into the fifth index, right?

881
00:47:55,240 --> 00:47:57,030
So store at tail.

882
00:47:57,030 --> 00:48:05,600
>> Another way to write tail would just
be our array at index of size, right?

883
00:48:05,600 --> 00:48:07,560
This is size 5.

884
00:48:07,560 --> 00:48:11,490
Next thing is going to go into 5.

885
00:48:11,490 --> 00:48:12,296
Cool?

886
00:48:12,296 --> 00:48:13,290
OK.

887
00:48:13,290 --> 00:48:16,350
It gets slightly more complicated
when we start messing with the head.

888
00:48:16,350 --> 00:48:17,060
Yes?

889
00:48:17,060 --> 00:48:20,090
>> AUDIENCE: Does that mean that we
would have declared an array that

890
00:48:20,090 --> 00:48:23,880
was five elements long and
then we're adding onto it?

891
00:48:23,880 --> 00:48:24,730
>> SPEAKER 1: No.

892
00:48:24,730 --> 00:48:27,560
So in this case, this is a stack.

893
00:48:27,560 --> 00:48:31,760
This would be declared
as an array of size 6.

894
00:48:31,760 --> 00:48:37,120
And in this case, we
just have one space left.

895
00:48:37,120 --> 00:48:42,720
>> OK, so one thing is in this
case, if our head is at 0,

896
00:48:42,720 --> 00:48:45,270
then we just can add it at size.

897
00:48:45,270 --> 00:48:51,020
But it gets a little trickier
because actually, they

898
00:48:51,020 --> 00:48:52,840
don't have a slide
for this, so I'm going

899
00:48:52,840 --> 00:48:56,670
to draw one because it's not
quite that simple once you

900
00:48:56,670 --> 00:48:59,230
start getting rid of things.

901
00:48:59,230 --> 00:49:03,920
So whereas with a stack
you only ever have

902
00:49:03,920 --> 00:49:08,920
to worry about what the size is
when you're adding something on,

903
00:49:08,920 --> 00:49:15,710
with a queue you also need to make
sure that your head is accounted for,

904
00:49:15,710 --> 00:49:20,760
because a cool thing about queues
is that if you're not at capacity,

905
00:49:20,760 --> 00:49:23,040
you can actually make it wrap around.

906
00:49:23,040 --> 00:49:28,810
>> OK, so one thing-- oh,
this is terrible chalk.

907
00:49:28,810 --> 00:49:31,815
One thing to consider is the case.

908
00:49:31,815 --> 00:49:35,514

909
00:49:35,514 --> 00:49:37,140
We'll just do five.

910
00:49:37,140 --> 00:49:41,810
OK, so we're going to
say the head is here.

911
00:49:41,810 --> 00:49:46,140
This is 0, 1, 2, 3, 4.

912
00:49:46,140 --> 00:49:54,210
>> The head's there, and
please have things in them.

913
00:49:54,210 --> 00:49:58,340
And we want to add something in, right?

914
00:49:58,340 --> 00:50:01,170
So the thing that we need to
know is that the head is always

915
00:50:01,170 --> 00:50:05,620
going to move this way and
then loop back around, OK?

916
00:50:05,620 --> 00:50:10,190
>> So this queue has space, right?

917
00:50:10,190 --> 00:50:13,950
It has space in the very beginning,
kind of the opposite of this.

918
00:50:13,950 --> 00:50:17,920
So what we need to do is we
need to calculate the tail.

919
00:50:17,920 --> 00:50:20,530
If you know that your
head hasn't moved, tail

920
00:50:20,530 --> 00:50:24,630
is just your array at
the index of the size.

921
00:50:24,630 --> 00:50:30,000
>> But in reality, if you're using a queue,
your head is probably being updated.

922
00:50:30,000 --> 00:50:33,890
So what you need to do is
actually calculate the tail.

923
00:50:33,890 --> 00:50:39,990
So what we do is this formula
here, which I'm going to let you

924
00:50:39,990 --> 00:50:42,680
guys think about, and
then we'll talk about it.

925
00:50:42,680 --> 00:50:49,567

926
00:50:49,567 --> 00:50:50,400
So this is capacity.

927
00:50:50,400 --> 00:50:55,890

928
00:50:55,890 --> 00:50:59,660
>> So this will actually
give you a way to do it.

929
00:50:59,660 --> 00:51:03,205

930
00:51:03,205 --> 00:51:04,330
Because in this case, what?

931
00:51:04,330 --> 00:51:09,205
Our head is at 1, our size is 4.

932
00:51:09,205 --> 00:51:11,760

933
00:51:11,760 --> 00:51:18,490
If we mod that by 5, we get 0,
which is where we should input it.

934
00:51:18,490 --> 00:51:23,320

935
00:51:23,320 --> 00:51:26,080
>> So then in the next case,
if we were to do this,

936
00:51:26,080 --> 00:51:33,390
we say, OK, let's dequeue something.

937
00:51:33,390 --> 00:51:34,390
We dequeue this.

938
00:51:34,390 --> 00:51:37,740
We take out this element, right?

939
00:51:37,740 --> 00:51:47,930
>> And now our head is pointing here,
and we want to add in another thing.

940
00:51:47,930 --> 00:51:52,470
This is basically the
back of our line, right?

941
00:51:52,470 --> 00:51:55,450
Queues can wrap around the array.

942
00:51:55,450 --> 00:51:57,310
That's one of the main differences.

943
00:51:57,310 --> 00:51:58,780
Stacks, you can't do this.

944
00:51:58,780 --> 00:52:01,140
>> With queues, you can
because all that matters

945
00:52:01,140 --> 00:52:03,940
is that you know what
was most recently added.

946
00:52:03,940 --> 00:52:10,650
Since everything is going to be added in
this leftward direction, in this case,

947
00:52:10,650 --> 00:52:16,480
and then wrap around, you can
continue putting in new elements

948
00:52:16,480 --> 00:52:18,830
at the front of the array
because it's not really

949
00:52:18,830 --> 00:52:20,640
the front of the array anymore.

950
00:52:20,640 --> 00:52:26,320
You can think of the beginning of the
array as where your head actually is.

951
00:52:26,320 --> 00:52:29,710
>> So this formula is how
you calculate your tail.

952
00:52:29,710 --> 00:52:32,780

953
00:52:32,780 --> 00:52:35,610
Does that makes sense?

954
00:52:35,610 --> 00:52:36,110
OK.

955
00:52:36,110 --> 00:52:39,400

956
00:52:39,400 --> 00:52:44,040
OK, dequeue, and then
you guys have 10 minutes

957
00:52:44,040 --> 00:52:48,840
to ask me any clarifying questions
you want, because I know it's crazy.

958
00:52:48,840 --> 00:52:51,980
>> All right, so in the same way--
I don't know if you guys noticed,

959
00:52:51,980 --> 00:52:53,450
but CS is all about patterns.

960
00:52:53,450 --> 00:52:57,370
Things are pretty much the
same, just with tiny tweaks.

961
00:52:57,370 --> 00:52:58,950
So same thing here.

962
00:52:58,950 --> 00:53:04,040
We need to check to see if we actually
have something in our queue, right?

963
00:53:04,040 --> 00:53:05,960
Say, OK, is our size greater than 0?

964
00:53:05,960 --> 00:53:06,730
Cool.

965
00:53:06,730 --> 00:53:10,690
>> If we do, then we move our head, which
is what I just demonstrated here.

966
00:53:10,690 --> 00:53:13,870
We update our head to be one more.

967
00:53:13,870 --> 00:53:18,390
And then we decrement our
size and return the element.

968
00:53:18,390 --> 00:53:21,000

969
00:53:21,000 --> 00:53:26,250
>> There is much more concrete
code on study.cs50.net,

970
00:53:26,250 --> 00:53:29,440
and I highly recommend going
through it if you have time,

971
00:53:29,440 --> 00:53:30,980
even if it's just a pseudo-code.

972
00:53:30,980 --> 00:53:35,980
And if you guys want to talk through
that with me one on one, please let me

973
00:53:35,980 --> 00:53:37,500
know.

974
00:53:37,500 --> 00:53:38,770
I'd be happy to.

975
00:53:38,770 --> 00:53:42,720
Data structures, if
you take CS 124, you'll

976
00:53:42,720 --> 00:53:47,830
know that data structures get very
fun and this is just beginning.

977
00:53:47,830 --> 00:53:50,350
>> So I know it's hard.

978
00:53:50,350 --> 00:53:51,300
It's OK.

979
00:53:51,300 --> 00:53:52,410
We struggle.

980
00:53:52,410 --> 00:53:53,630
I still do.

981
00:53:53,630 --> 00:53:56,660
So don't worry too much about it.

982
00:53:56,660 --> 00:54:02,390
>> But that is basically your
crash course in data structures.

983
00:54:02,390 --> 00:54:03,400
I know it's a lot.

984
00:54:03,400 --> 00:54:06,860
Is there anything that we
would like to go over again?

985
00:54:06,860 --> 00:54:09,400
Anything we want to talk through?

986
00:54:09,400 --> 00:54:10,060
Yes?

987
00:54:10,060 --> 00:54:16,525
>> AUDIENCE: For that example, so
the new tail is at 0 over that?

988
00:54:16,525 --> 00:54:17,150
SPEAKER 1: Yes.

989
00:54:17,150 --> 00:54:18,230
AUDIENCE: OK.

990
00:54:18,230 --> 00:54:24,220
So then going through,
you'd have 1 plus 4 or--

991
00:54:24,220 --> 00:54:27,671
>> SPEAKER 1: So you were saying,
when we want to go do this again?

992
00:54:27,671 --> 00:54:28,296
AUDIENCE: Yeah.

993
00:54:28,296 --> 00:54:38,290
So if you were figuring out-- where are
you calculating the tail from in that?

994
00:54:38,290 --> 00:54:44,260
>> SPEAKER 1: So the tail
was in-- I changed this.

995
00:54:44,260 --> 00:54:52,010
So in this example here, this was
the array we're looking at, OK?

996
00:54:52,010 --> 00:54:54,670
So we have things in 1, 2, 3, and 4.

997
00:54:54,670 --> 00:55:05,850
So we have our head is equal to 1 at
this point, and our size is equal to 4

998
00:55:05,850 --> 00:55:07,050
at this point, right?

999
00:55:07,050 --> 00:55:08,960
>> You all agree that's the case?

1000
00:55:08,960 --> 00:55:14,620
So we do the head plus the size, which
gives us 5, and then we mod by 5.

1001
00:55:14,620 --> 00:55:20,690
We get 0, which tells us that 0 is
where is our tail, where we have space.

1002
00:55:20,690 --> 00:55:22,010
>> AUDIENCE: What's a cap?

1003
00:55:22,010 --> 00:55:23,520
>> SPEAKER 1: The capacity.

1004
00:55:23,520 --> 00:55:24,020
Sorry.

1005
00:55:24,020 --> 00:55:29,640
So that is the size of your array.

1006
00:55:29,640 --> 00:55:35,210

1007
00:55:35,210 --> 00:55:36,047
Yes?

1008
00:55:36,047 --> 00:55:39,210
>> AUDIENCE: [INAUDIBLE] before
we return the element?

1009
00:55:39,210 --> 00:55:46,270
>> SPEAKER 1: So we move the
head or return the moment?

1010
00:55:46,270 --> 00:55:52,680
So if we move one, decrement the size?

1011
00:55:52,680 --> 00:55:54,150
Hold on.

1012
00:55:54,150 --> 00:55:55,770
I definitely forgot another.

1013
00:55:55,770 --> 00:56:00,646

1014
00:56:00,646 --> 00:56:01,990
Never mind.

1015
00:56:01,990 --> 00:56:04,980
There is not another formula.

1016
00:56:04,980 --> 00:56:09,980
Yeah, you would want to return
the head and then move it back.

1017
00:56:09,980 --> 00:56:13,270
>> AUDIENCE: OK, because At this
point, the head was at 0,

1018
00:56:13,270 --> 00:56:18,452
and then you want to return
index 0 and then make head 1?

1019
00:56:18,452 --> 00:56:19,870
>> SPEAKER 1: Right.

1020
00:56:19,870 --> 00:56:22,820
I think there's another
formula kind of like this.

1021
00:56:22,820 --> 00:56:26,970
I don't have it on the top my head as
I don't want to give you the wrong one.

1022
00:56:26,970 --> 00:56:35,470
But I think it's perfectly valid to
say, OK, store this element-- whatever

1023
00:56:35,470 --> 00:56:40,759
head's element is-- decrement your
size, move your head over, and return

1024
00:56:40,759 --> 00:56:41,800
whatever that element is.

1025
00:56:41,800 --> 00:56:44,760
That's perfectly valid.

1026
00:56:44,760 --> 00:56:45,260
OK.

1027
00:56:45,260 --> 00:56:48,360

1028
00:56:48,360 --> 00:56:53,560
I feel like this is not
like the most-- you're not

1029
00:56:53,560 --> 00:56:55,740
going to walk out of here
like, yes, I know tries.

1030
00:56:55,740 --> 00:56:56,880
I got it all.

1031
00:56:56,880 --> 00:56:57,670
That's OK.

1032
00:56:57,670 --> 00:57:00,200
I promise.

1033
00:57:00,200 --> 00:57:05,240
But data structures are something that
it takes a lot of time to get used to.

1034
00:57:05,240 --> 00:57:10,010
Probably one of the hardest
things, I think, in the course.

1035
00:57:10,010 --> 00:57:15,330
>> So it definitely takes
repetition and looking at-- I

1036
00:57:15,330 --> 00:57:20,050
didn't really know linked lists
until I did far too much with them,

1037
00:57:20,050 --> 00:57:22,550
in the same way that I didn't
really understand pointers

1038
00:57:22,550 --> 00:57:27,040
until I've had to teach it for two
years and do my own psets with it.

1039
00:57:27,040 --> 00:57:28,990
It takes a lot of reiteration and time.

1040
00:57:28,990 --> 00:57:32,600
And eventually, it will kind of click.

1041
00:57:32,600 --> 00:57:36,320
>> But in the meantime, if you have kind
of a high level understanding of what

1042
00:57:36,320 --> 00:57:39,321
these do, their pros
and cons-- which is what

1043
00:57:39,321 --> 00:57:41,820
we really tend to emphasize,
especially in the intro course.

1044
00:57:41,820 --> 00:57:45,511
Like, why would we use
a try over an array?

1045
00:57:45,511 --> 00:57:48,010
Like, what are the positives
and negatives of each of those?

1046
00:57:48,010 --> 00:57:51,610
>> And understanding the trade-offs
between each of these structures

1047
00:57:51,610 --> 00:57:54,910
is what's much more important right now.

1048
00:57:54,910 --> 00:57:58,140
There may be one crazy
question or two that's

1049
00:57:58,140 --> 00:58:03,710
going to ask you to implement push or
implement pop or enqueue and dequeue.

1050
00:58:03,710 --> 00:58:07,340
But for the most part, having that
higher level understanding and more

1051
00:58:07,340 --> 00:58:09,710
of an intuitive grasp is
more important than actually

1052
00:58:09,710 --> 00:58:11,250
being able to implement it.

1053
00:58:11,250 --> 00:58:14,880
>> It'd be really awesome if all of you
could go out and go implement a try,

1054
00:58:14,880 --> 00:58:19,720
but we understand it's not necessarily
the most reasonable thing right now.

1055
00:58:19,720 --> 00:58:23,370
But you can in your pset, if you want
to, and then you'll get practice,

1056
00:58:23,370 --> 00:58:27,200
and then maybe you'll
really understand it.

1057
00:58:27,200 --> 00:58:27,940
Yes?

1058
00:58:27,940 --> 00:58:30,440
>> AUDIENCE: OK, so which ones are
we meant to use in the pset?

1059
00:58:30,440 --> 00:58:31,916
Do I need to use one of them?

1060
00:58:31,916 --> 00:58:32,540
SPEAKER 1: Yes.

1061
00:58:32,540 --> 00:58:34,199
So you have your choice.

1062
00:58:34,199 --> 00:58:36,740
I guess in this case, we can
talk about the pset a little bit

1063
00:58:36,740 --> 00:58:40,480
because I ran through these.

1064
00:58:40,480 --> 00:58:47,779
So in your pset, you have your
choice of tries or hash tables.

1065
00:58:47,779 --> 00:58:49,570
Some people will try
and use bloom filters,

1066
00:58:49,570 --> 00:58:51,840
but those technically are not correct.

1067
00:58:51,840 --> 00:58:55,804
Because of their probabilistic nature,
they give false positives sometimes.

1068
00:58:55,804 --> 00:58:57,095
They're cool look into, though.

1069
00:58:57,095 --> 00:58:59,030
Highly recommend looking
at them at least.

1070
00:58:59,030 --> 00:59:03,260
But you have your choice
between a hash table and a try.

1071
00:59:03,260 --> 00:59:06,660
And that's going to be where
you load in your dictionary.

1072
00:59:06,660 --> 00:59:09,230
>> And you'll need to choose
your hash function,

1073
00:59:09,230 --> 00:59:13,420
you'll need to choose how many
buckets you have, and it will vary.

1074
00:59:13,420 --> 00:59:17,440
Like if you have more buckets,
maybe it'll run faster.

1075
00:59:17,440 --> 00:59:22,790
But maybe you're wasting a
lot of space that way, though.

1076
00:59:22,790 --> 00:59:26,320
You have to figure it out.

1077
00:59:26,320 --> 00:59:27,140
Mmhmm?

1078
00:59:27,140 --> 00:59:29,875
>> AUDIENCE: You said before that
we can use other hash functions,

1079
00:59:29,875 --> 00:59:31,750
that we don't have to
create a hash function?

1080
00:59:31,750 --> 00:59:32,666
>> SPEAKER 1: Yes, right.

1081
00:59:32,666 --> 00:59:38,150
So literally for your hash function,
like google "hash function"

1082
00:59:38,150 --> 00:59:40,770
and look for some cool ones.

1083
00:59:40,770 --> 00:59:43,250
You are not expected to build
your own hash functions.

1084
00:59:43,250 --> 00:59:46,100
People spend their
theses on these things.

1085
00:59:46,100 --> 00:59:50,250
>> So don't worry about building your own.

1086
00:59:50,250 --> 00:59:53,350
Find one online to start with.

1087
00:59:53,350 --> 00:59:56,120
Some of them you have to
manipulate a little bit

1088
00:59:56,120 --> 00:59:59,430
to make sure return types match up
and whatnot, so in the beginning,

1089
00:59:59,430 --> 01:00:02,420
I would recommend using something
really easy that maybe just

1090
01:00:02,420 --> 01:00:04,680
hashes on the first letter.

1091
01:00:04,680 --> 01:00:08,760
And then once you have that working,
incorporating a cooler hash function.

1092
01:00:08,760 --> 01:00:09,260
Mmhmm?

1093
01:00:09,260 --> 01:00:13,020
>> AUDIENCE: Would a try be or
efficient but just harder to, like--

1094
01:00:13,020 --> 01:00:15,880
>> SPEAKER 1: So a try, I think,
is intuitively hard to implement

1095
01:00:15,880 --> 01:00:18,310
but is very fast.

1096
01:00:18,310 --> 01:00:20,620
However, takes up more space.

1097
01:00:20,620 --> 01:00:25,270
Again, you can optimize both of those in
different ways and there are ways to--

1098
01:00:25,270 --> 01:00:26,770
AUDIENCE: How are we graded on this?

1099
01:00:26,770 --> 01:00:27,540
Does it matter--

1100
01:00:27,540 --> 01:00:29,164
>> SPEAKER 1: So you're graded normal way.

1101
01:00:29,164 --> 01:00:31,330
You're going to be graded on design.

1102
01:00:31,330 --> 01:00:36,020
Whichever way you do, you want to
make sure it's as elegant as it can be

1103
01:00:36,020 --> 01:00:38,610
and as efficient as it can be.

1104
01:00:38,610 --> 01:00:41,950
But if you choose a try or hash
table, as long as it works,

1105
01:00:41,950 --> 01:00:45,350
we're happy with that.

1106
01:00:45,350 --> 01:00:48,370
And if you use something that hashes
on the first letter, that's fine,

1107
01:00:48,370 --> 01:00:51,410
like maybe like design-wise.

1108
01:00:51,410 --> 01:00:53,410
We're also reaching the
point in this semester--

1109
01:00:53,410 --> 01:00:55,340
I don't know if you
guys noticed-- if you're

1110
01:00:55,340 --> 01:00:58,780
pset grades decline a little bit
because of design and whatnot,

1111
01:00:58,780 --> 01:00:59,900
that's perfectly fine.

1112
01:00:59,900 --> 01:01:02,960
It's getting to a point where your
programs are getting more complicated.

1113
01:01:02,960 --> 01:01:04,830
There are more places
you can improve on.

1114
01:01:04,830 --> 01:01:06,370
>> So it's perfectly normal.

1115
01:01:06,370 --> 01:01:08,810
It's not that you're
doing worse on your pset.

1116
01:01:08,810 --> 01:01:11,885
It's just we're being harder on you now.

1117
01:01:11,885 --> 01:01:13,732
So everyone's feeling it.

1118
01:01:13,732 --> 01:01:14,940
I just graded all your psets.

1119
01:01:14,940 --> 01:01:16,490
I know everyone is feeling it.

1120
01:01:16,490 --> 01:01:19,600
>> So don't be worried about that.

1121
01:01:19,600 --> 01:01:23,580
And if you have any questions about
prior psets or ways you can improve,

1122
01:01:23,580 --> 01:01:27,760
I try and comment the specific
places, but sometimes it's late

1123
01:01:27,760 --> 01:01:30,840
and I get tired.

1124
01:01:30,840 --> 01:01:34,885
Are there any other things
about data structures?

1125
01:01:34,885 --> 01:01:37,510
I'm sure you guys don't really
want to talk about them anymore,

1126
01:01:37,510 --> 01:01:42,650
but if there are, I'm happy to
go over them, as well as anything

1127
01:01:42,650 --> 01:01:45,580
from lecture this past
week or last week.

1128
01:01:45,580 --> 01:01:51,580
>> I know last week was all review, so
we may have skipped over some review

1129
01:01:51,580 --> 01:01:54,190
from lecture.

1130
01:01:54,190 --> 01:01:58,230
Any other questions I can answer?

1131
01:01:58,230 --> 01:01:59,350
OK, all right.

1132
01:01:59,350 --> 01:02:02,400
Well, you guys get out 15 minutes early.

1133
01:02:02,400 --> 01:02:08,370
>> I hope this was semi-helpful at least,
and I will see you guys next week,

1134
01:02:08,370 --> 01:02:12,150
or Thursday office hours.

1135
01:02:12,150 --> 01:02:15,285
Are there requests for snacks
for next week, it's the thing?

1136
01:02:15,285 --> 01:02:17,459
Because I forgot candy today.

1137
01:02:17,459 --> 01:02:19,750
And I brought candy last
week, but it was Columbus Day,

1138
01:02:19,750 --> 01:02:25,400
so there were like six people who
had four bags of candy to themselves.

1139
01:02:25,400 --> 01:02:28,820
I can bring Starbursts
again if you like.

1140
01:02:28,820 --> 01:02:29,580
Starbursts?

1141
01:02:29,580 --> 01:02:32,250
OK, sounds good.

1142
01:02:32,250 --> 01:02:35,050
Have a great day, guys.

1143
01:02:35,050 --> 01:02:39,510