File size: 470,648 Bytes
08c8a6d | 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 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305 18306 18307 18308 18309 18310 18311 18312 18313 18314 18315 18316 18317 18318 18319 18320 18321 18322 18323 18324 18325 18326 18327 18328 18329 18330 18331 18332 18333 18334 18335 18336 18337 18338 18339 18340 18341 18342 18343 18344 18345 18346 18347 18348 18349 18350 18351 18352 18353 18354 18355 18356 18357 18358 18359 18360 18361 18362 18363 18364 18365 18366 18367 18368 18369 18370 18371 18372 18373 18374 18375 18376 18377 18378 18379 18380 18381 18382 18383 18384 18385 18386 18387 18388 18389 18390 18391 18392 18393 18394 18395 18396 18397 18398 18399 18400 18401 18402 18403 18404 18405 18406 18407 18408 18409 18410 18411 18412 18413 18414 18415 18416 18417 18418 18419 18420 18421 18422 18423 18424 18425 18426 18427 18428 18429 18430 18431 18432 18433 18434 18435 18436 18437 18438 18439 18440 18441 18442 18443 18444 18445 18446 18447 18448 18449 18450 18451 18452 18453 18454 18455 18456 18457 18458 18459 18460 18461 18462 18463 18464 18465 18466 18467 18468 18469 18470 18471 18472 18473 18474 18475 18476 18477 18478 18479 18480 18481 18482 18483 18484 18485 18486 18487 18488 18489 18490 18491 18492 18493 18494 18495 18496 18497 18498 18499 18500 18501 18502 18503 18504 18505 18506 18507 18508 18509 18510 18511 18512 18513 18514 18515 18516 18517 18518 18519 18520 18521 18522 18523 18524 18525 18526 18527 18528 18529 18530 18531 18532 18533 18534 18535 18536 18537 18538 18539 18540 18541 18542 18543 18544 18545 18546 18547 18548 18549 18550 18551 18552 18553 18554 18555 18556 18557 18558 18559 18560 18561 18562 18563 18564 18565 18566 18567 18568 18569 18570 18571 18572 18573 18574 18575 18576 18577 18578 18579 18580 18581 18582 18583 18584 18585 18586 18587 18588 18589 18590 18591 18592 18593 18594 18595 18596 18597 18598 18599 18600 18601 18602 18603 18604 18605 18606 18607 18608 18609 18610 18611 18612 18613 18614 18615 18616 18617 18618 18619 18620 18621 18622 18623 18624 18625 18626 18627 18628 18629 18630 18631 18632 18633 18634 18635 18636 18637 18638 18639 18640 18641 18642 18643 18644 18645 18646 18647 18648 18649 18650 18651 18652 18653 18654 18655 18656 18657 18658 18659 18660 18661 18662 18663 18664 18665 18666 18667 18668 18669 18670 18671 18672 18673 18674 18675 18676 18677 18678 18679 18680 18681 18682 18683 18684 18685 18686 18687 18688 18689 18690 18691 18692 18693 18694 18695 18696 18697 18698 18699 18700 18701 18702 18703 18704 18705 18706 18707 18708 18709 18710 18711 18712 18713 18714 18715 18716 18717 18718 18719 18720 18721 18722 18723 18724 18725 18726 18727 18728 18729 18730 18731 18732 18733 18734 18735 18736 18737 18738 18739 18740 18741 18742 18743 18744 18745 18746 18747 18748 18749 18750 18751 18752 18753 18754 18755 18756 18757 18758 18759 18760 18761 18762 18763 18764 18765 18766 18767 18768 18769 18770 18771 18772 18773 18774 18775 18776 18777 18778 18779 18780 18781 18782 18783 18784 18785 18786 18787 18788 18789 18790 18791 18792 18793 18794 18795 18796 18797 18798 18799 18800 18801 18802 18803 18804 18805 18806 18807 18808 18809 18810 18811 18812 18813 18814 18815 18816 18817 18818 18819 18820 18821 18822 18823 18824 18825 18826 18827 18828 18829 18830 18831 18832 18833 18834 18835 18836 18837 18838 18839 18840 18841 18842 18843 18844 18845 18846 18847 18848 18849 18850 18851 18852 18853 18854 18855 18856 18857 18858 18859 18860 18861 18862 18863 18864 18865 18866 18867 18868 18869 18870 18871 18872 18873 18874 18875 18876 18877 18878 18879 18880 18881 18882 18883 18884 18885 18886 18887 18888 18889 18890 18891 18892 18893 18894 18895 18896 18897 18898 18899 18900 18901 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 18912 18913 18914 18915 18916 18917 18918 18919 18920 18921 18922 18923 18924 18925 18926 18927 18928 18929 18930 18931 18932 18933 18934 18935 18936 18937 18938 18939 18940 18941 18942 18943 18944 18945 18946 18947 18948 18949 18950 18951 18952 18953 18954 18955 18956 18957 18958 18959 18960 18961 18962 18963 18964 18965 18966 18967 18968 18969 18970 18971 18972 18973 18974 18975 18976 18977 18978 18979 18980 18981 18982 18983 18984 18985 18986 18987 18988 18989 18990 18991 18992 18993 18994 18995 18996 18997 18998 18999 19000 19001 19002 19003 19004 19005 19006 19007 19008 19009 19010 19011 19012 19013 19014 19015 19016 19017 19018 19019 19020 19021 19022 19023 19024 19025 19026 19027 19028 19029 19030 19031 19032 19033 19034 19035 19036 19037 19038 19039 19040 19041 19042 19043 19044 19045 19046 19047 19048 19049 19050 19051 19052 19053 19054 19055 19056 19057 19058 19059 19060 19061 19062 19063 19064 19065 19066 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090 19091 19092 19093 19094 19095 19096 19097 19098 19099 19100 19101 19102 19103 19104 19105 19106 19107 19108 19109 19110 19111 19112 19113 19114 19115 19116 19117 19118 19119 19120 19121 19122 19123 19124 19125 19126 19127 19128 19129 19130 19131 19132 19133 19134 19135 19136 19137 19138 19139 19140 19141 19142 19143 19144 19145 19146 19147 19148 19149 19150 19151 19152 19153 19154 19155 19156 19157 19158 19159 19160 19161 19162 19163 19164 19165 19166 19167 19168 19169 19170 19171 19172 19173 19174 19175 19176 19177 19178 19179 19180 19181 19182 19183 19184 19185 19186 19187 19188 19189 19190 19191 19192 19193 19194 19195 19196 19197 19198 19199 19200 19201 19202 19203 19204 19205 19206 19207 19208 19209 19210 19211 19212 19213 19214 19215 19216 19217 19218 19219 19220 19221 19222 19223 19224 19225 19226 19227 19228 19229 19230 19231 19232 19233 19234 19235 19236 19237 19238 19239 19240 19241 19242 19243 19244 19245 19246 19247 19248 19249 19250 19251 19252 19253 19254 19255 19256 19257 19258 19259 19260 19261 19262 19263 19264 19265 19266 19267 19268 19269 19270 19271 19272 19273 19274 19275 19276 19277 19278 19279 19280 19281 19282 19283 19284 19285 19286 19287 19288 19289 19290 19291 19292 19293 19294 19295 19296 19297 19298 19299 19300 19301 19302 19303 19304 19305 19306 19307 19308 19309 19310 19311 19312 19313 19314 19315 19316 19317 19318 19319 19320 19321 19322 19323 19324 19325 19326 19327 19328 19329 19330 19331 19332 19333 19334 19335 19336 19337 19338 19339 19340 19341 19342 19343 19344 19345 19346 19347 19348 19349 19350 19351 19352 19353 19354 19355 19356 19357 19358 19359 19360 19361 19362 19363 19364 19365 19366 19367 19368 19369 19370 19371 19372 19373 19374 19375 19376 19377 19378 19379 19380 19381 19382 19383 19384 19385 19386 19387 19388 19389 19390 19391 19392 19393 19394 19395 19396 19397 19398 19399 19400 19401 19402 19403 19404 19405 19406 19407 19408 19409 19410 19411 19412 19413 19414 19415 19416 19417 19418 19419 19420 19421 19422 19423 19424 19425 19426 19427 19428 19429 19430 19431 19432 19433 19434 19435 19436 19437 19438 19439 19440 19441 19442 19443 19444 19445 19446 19447 19448 19449 19450 19451 19452 19453 19454 19455 19456 19457 19458 19459 19460 19461 19462 19463 19464 19465 19466 19467 19468 19469 19470 19471 19472 19473 19474 19475 19476 19477 19478 19479 19480 19481 19482 19483 19484 19485 19486 19487 19488 19489 19490 19491 19492 19493 19494 19495 19496 19497 19498 19499 19500 19501 19502 19503 19504 19505 19506 19507 19508 19509 19510 19511 19512 19513 19514 19515 19516 19517 19518 19519 19520 19521 19522 19523 19524 19525 19526 19527 19528 19529 19530 19531 19532 19533 19534 19535 19536 19537 19538 19539 19540 19541 19542 19543 19544 19545 19546 19547 19548 19549 19550 19551 19552 19553 19554 19555 19556 19557 19558 19559 19560 19561 19562 19563 19564 19565 19566 19567 19568 19569 19570 19571 19572 19573 19574 19575 19576 19577 19578 19579 19580 19581 19582 19583 19584 19585 19586 19587 19588 19589 19590 19591 19592 19593 19594 19595 19596 19597 19598 19599 19600 19601 19602 19603 19604 19605 19606 19607 19608 19609 19610 19611 19612 19613 19614 19615 19616 19617 19618 19619 19620 19621 19622 19623 19624 19625 19626 19627 19628 19629 19630 19631 19632 19633 19634 19635 19636 19637 19638 19639 19640 19641 19642 19643 19644 19645 19646 19647 19648 19649 19650 19651 19652 19653 19654 19655 19656 19657 19658 19659 19660 19661 19662 19663 19664 19665 19666 19667 19668 19669 19670 19671 19672 19673 19674 19675 19676 19677 19678 19679 19680 19681 19682 19683 19684 19685 19686 19687 19688 19689 19690 19691 19692 19693 19694 19695 19696 19697 19698 19699 19700 19701 19702 19703 19704 19705 19706 19707 19708 19709 19710 19711 19712 19713 19714 19715 19716 19717 19718 19719 19720 19721 19722 19723 19724 19725 19726 19727 19728 19729 19730 19731 19732 19733 19734 19735 19736 19737 19738 19739 19740 19741 19742 19743 19744 19745 19746 19747 19748 19749 19750 19751 19752 19753 19754 19755 19756 19757 19758 19759 19760 19761 19762 19763 19764 19765 19766 19767 19768 19769 19770 19771 19772 19773 19774 19775 19776 19777 19778 19779 19780 19781 19782 19783 19784 19785 19786 19787 19788 19789 19790 19791 19792 19793 19794 19795 19796 19797 19798 19799 19800 19801 19802 19803 19804 19805 19806 19807 19808 19809 19810 19811 19812 19813 19814 19815 19816 19817 19818 19819 19820 19821 19822 19823 19824 19825 19826 19827 19828 19829 19830 19831 19832 19833 19834 19835 19836 19837 19838 19839 19840 19841 19842 19843 19844 19845 19846 19847 19848 19849 19850 19851 19852 19853 19854 19855 19856 19857 19858 19859 19860 19861 19862 19863 19864 19865 19866 19867 19868 19869 19870 19871 19872 19873 19874 19875 19876 19877 19878 19879 19880 19881 19882 19883 19884 19885 19886 19887 19888 19889 19890 19891 19892 19893 19894 19895 19896 19897 19898 19899 19900 19901 19902 19903 19904 19905 19906 19907 19908 19909 19910 19911 19912 19913 19914 19915 19916 19917 19918 19919 19920 19921 19922 19923 19924 19925 19926 19927 19928 19929 19930 19931 19932 19933 19934 19935 19936 19937 19938 19939 19940 19941 19942 19943 19944 19945 19946 19947 19948 19949 19950 19951 19952 19953 19954 19955 19956 19957 19958 19959 19960 19961 19962 19963 19964 19965 19966 19967 19968 19969 19970 19971 19972 19973 19974 19975 19976 19977 19978 19979 19980 19981 19982 19983 19984 19985 19986 19987 19988 19989 19990 19991 19992 19993 19994 19995 19996 19997 19998 19999 20000 20001 20002 20003 20004 20005 20006 20007 20008 20009 20010 20011 20012 20013 20014 20015 20016 20017 20018 20019 20020 20021 20022 20023 20024 20025 20026 20027 20028 20029 20030 20031 20032 20033 20034 20035 20036 20037 20038 20039 20040 20041 20042 20043 20044 20045 20046 20047 20048 20049 20050 20051 20052 20053 20054 20055 20056 20057 20058 20059 20060 20061 20062 20063 20064 20065 20066 20067 20068 20069 20070 20071 20072 20073 20074 20075 20076 20077 20078 20079 20080 20081 20082 20083 20084 20085 20086 20087 20088 20089 20090 20091 20092 20093 20094 20095 20096 20097 20098 20099 20100 20101 20102 20103 20104 20105 20106 20107 20108 20109 20110 20111 20112 20113 20114 20115 20116 20117 20118 20119 20120 20121 20122 20123 20124 20125 20126 20127 20128 20129 20130 20131 20132 20133 20134 20135 20136 20137 20138 20139 20140 20141 20142 20143 20144 20145 20146 20147 20148 20149 20150 20151 20152 20153 20154 20155 20156 20157 20158 20159 20160 20161 20162 20163 20164 20165 20166 20167 20168 20169 20170 20171 20172 20173 20174 20175 20176 20177 20178 20179 20180 20181 20182 20183 20184 20185 20186 20187 20188 20189 20190 20191 20192 20193 20194 20195 20196 20197 20198 20199 20200 20201 20202 20203 20204 20205 20206 20207 20208 20209 20210 20211 20212 20213 20214 20215 20216 20217 20218 20219 20220 20221 20222 20223 20224 20225 20226 20227 20228 20229 20230 20231 20232 20233 20234 20235 20236 20237 20238 20239 20240 20241 20242 20243 20244 20245 20246 20247 20248 20249 20250 20251 20252 20253 20254 20255 20256 20257 20258 20259 20260 20261 20262 20263 20264 20265 20266 20267 20268 20269 20270 20271 20272 20273 20274 20275 20276 20277 20278 20279 20280 20281 20282 20283 20284 20285 20286 20287 20288 20289 20290 20291 20292 20293 20294 20295 20296 20297 20298 20299 20300 20301 20302 20303 20304 20305 20306 20307 20308 20309 20310 20311 20312 20313 20314 20315 20316 20317 20318 20319 20320 20321 20322 20323 20324 20325 20326 20327 20328 20329 20330 20331 20332 20333 20334 20335 20336 20337 20338 20339 20340 20341 20342 20343 20344 20345 20346 20347 20348 20349 20350 20351 20352 20353 20354 20355 20356 20357 20358 20359 20360 20361 20362 20363 20364 20365 20366 20367 20368 20369 20370 20371 20372 20373 20374 20375 20376 20377 20378 20379 20380 20381 20382 20383 20384 20385 20386 20387 20388 20389 20390 20391 | Open Data Structures (in Java)
Edition 0.1G
Pat Morin
Contents
Acknowledgments
Why This Book?
ix
xi
1 Introduction
1.1 The Need for Efficiency . . . . . . . . . . . . . . . . . . . . .
1.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2.1 The Queue, Stack, and Deque Interfaces
. . . . . . .
1.2.2 The List Interface: Linear Sequences . . . . . . . . .
1.2.3 The USet Interface: Unordered Sets . . . . . . . . . .
1.2.4 The SSet Interface: Sorted Sets . . . . . . . . . . . .
1.3 Mathematical Background . . . . . . . . . . . . . . . . . . .
1
2
4
5
6
8
9
9
1.3.1 Exponentials and Logarithms . . . . . . . . . . . . . 10
1.3.2 Factorials . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.3 Asymptotic Notation . . . . . . . . . . . . . . . . . . 12
1.3.4 Randomization and Probability . . . . . . . . . . . . 15
1.4 The Model of Computation . . . . . . . . . . . . . . . . . . . 18
1.5 Correctness, Time Complexity, and Space Complexity . . . 19
. . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.6 Code Samples
1.7 List of Data Structures
. . . . . . . . . . . . . . . . . . . . . 22
1.8 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 26
2 Array-Based Lists
29
2.1 ArrayStack: Fast Stack Operations Using an Array . . . . . 30
2.1.1 The Basics . . . . . . . . . . . . . . . . . . . . . . . . 30
2.1.2 Growing and Shrinking . . . . . . . . . . . . . . . . . 33
2.1.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 35
Contents
2.2 FastArrayStack: An Optimized ArrayStack . . . . . . . . . 35
2.3 ArrayQueue: An Array-Based Queue . . . . . . . . . . . . . 36
2.3.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4 ArrayDeque: Fast Deque Operations Using an Array . . . . 40
2.4.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.5 DualArrayDeque: Building a Deque from Two Stacks . . . . 43
2.5.1 Balancing . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.6 RootishArrayStack: A Space-Efficient Array Stack . . . . . 49
2.6.1 Analysis of Growing and Shrinking . . . . . . . . . . 54
2.6.2 Space Usage . . . . . . . . . . . . . . . . . . . . . . . 54
2.6.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.6.4 Computing Square Roots . . . . . . . . . . . . . . . . 56
2.7 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 59
3 Linked Lists
3.1 SLList: A Singly-Linked List
63
. . . . . . . . . . . . . . . . . 63
3.1.1 Queue Operations . . . . . . . . . . . . . . . . . . . . 65
3.1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.2 DLList: A Doubly-Linked List . . . . . . . . . . . . . . . . . 67
3.2.1 Adding and Removing . . . . . . . . . . . . . . . . . 69
3.2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.3 SEList: A Space-Efficient Linked List . . . . . . . . . . . . . 71
. . . . . . . . . . . . . . . . . . 72
3.3.1 Space Requirements
3.3.2 Finding Elements . . . . . . . . . . . . . . . . . . . . 73
3.3.3 Adding an Element . . . . . . . . . . . . . . . . . . . 74
3.3.4 Removing an Element
. . . . . . . . . . . . . . . . . 77
3.3.5 Amortized Analysis of Spreading and Gathering . . 79
3.3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.4 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 82
4 Skiplists
87
4.1 The Basic Structure . . . . . . . . . . . . . . . . . . . . . . . 87
4.2 SkiplistSSet: An Efficient SSet . . . . . . . . . . . . . . . 90
4.2.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.3 SkiplistList: An Efficient Random-Access List . . . . . . 93
iv
4.3.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.4 Analysis of Skiplists . . . . . . . . . . . . . . . . . . . . . . . 98
4.5 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 102
5 Hash Tables
107
5.1 ChainedHashTable: Hashing with Chaining . . . . . . . . . 107
5.1.1 Multiplicative Hashing . . . . . . . . . . . . . . . . . 110
5.1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.2 LinearHashTable: Linear Probing . . . . . . . . . . . . . . . 114
5.2.1 Analysis of Linear Probing . . . . . . . . . . . . . . . 118
5.2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.2.3 Tabulation Hashing . . . . . . . . . . . . . . . . . . . 121
5.3 Hash Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.3.1 Hash Codes for Primitive Data Types . . . . . . . . . 123
5.3.2 Hash Codes for Compound Objects . . . . . . . . . . 123
5.3.3 Hash Codes for Arrays and Strings . . . . . . . . . . 126
5.4 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 128
6 Binary Trees
133
6.1 BinaryTree: A Basic Binary Tree . . . . . . . . . . . . . . . 135
6.1.1 Recursive Algorithms . . . . . . . . . . . . . . . . . . 136
6.1.2 Traversing Binary Trees . . . . . . . . . . . . . . . . . 136
6.2 BinarySearchTree: An Unbalanced Binary Search Tree . . 140
6.2.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . 140
6.2.2 Addition . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.2.3 Removal
. . . . . . . . . . . . . . . . . . . . . . . . . 144
6.2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.3 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 147
7 Random Binary Search Trees
153
7.1 Random Binary Search Trees . . . . . . . . . . . . . . . . . . 153
7.1.1 Proof of Lemma 7.1 . . . . . . . . . . . . . . . . . . . 156
7.1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 158
7.2 Treap: A Randomized Binary Search Tree . . . . . . . . . . 159
7.2.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 166
7.3 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 168
v
Contents
8 Scapegoat Trees
173
8.1 ScapegoatTree: A Binary Search Tree with Partial Rebuild-
ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
8.1.1 Analysis of Correctness and Running-Time . . . . . 178
8.1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 180
8.2 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 181
9 Red-Black Trees
185
9.1 2-4 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
9.1.1 Adding a Leaf . . . . . . . . . . . . . . . . . . . . . . 187
. . . . . . . . . . . . . . . . . . . . 187
9.1.2 Removing a Leaf
9.2 RedBlackTree: A Simulated 2-4 Tree . . . . . . . . . . . . . 190
9.2.1 Red-Black Trees and 2-4 Trees . . . . . . . . . . . . . 190
9.2.2 Left-Leaning Red-Black Trees . . . . . . . . . . . . . 194
9.2.3 Addition . . . . . . . . . . . . . . . . . . . . . . . . . 196
. . . . . . . . . . . . . . . . . . . . . . . . . 199
9.2.4 Removal
9.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
9.4 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 206
10 Heaps
211
10.1 BinaryHeap: An Implicit Binary Tree . . . . . . . . . . . . . 211
10.1.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 217
10.2 MeldableHeap: A Randomized Meldable Heap . . . . . . . 217
10.2.1 Analysis of merge(h1, h2) . . . . . . . . . . . . . . . . 220
10.2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 221
10.3 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 222
11 Sorting Algorithms
225
11.1 Comparison-Based Sorting . . . . . . . . . . . . . . . . . . . 226
11.1.1 Merge-Sort . . . . . . . . . . . . . . . . . . . . . . . . 226
. . . . . . . . . . . . . . . . . . . . . . . . 230
11.1.2 Quicksort
11.1.3 Heap-sort
. . . . . . . . . . . . . . . . . . . . . . . . 233
11.1.4 A Lower-Bound for Comparison-Based Sorting . . . 235
. . . . . . . . . . . . . . . . . 238
11.2.1 Counting Sort . . . . . . . . . . . . . . . . . . . . . . 239
11.2.2 Radix-Sort . . . . . . . . . . . . . . . . . . . . . . . . 241
11.2 Counting Sort and Radix Sort
vi
11.3 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 243
12 Graphs
247
12.1 AdjacencyMatrix: Representing a Graph by a Matrix . . . . 249
12.2 AdjacencyLists: A Graph as a Collection of Lists . . . . . . 252
. . . . . . . . . . . . . . . . . . . . . . . . . 256
12.3 Graph Traversal
12.3.1 Breadth-First Search . . . . . . . . . . . . . . . . . . 256
12.3.2 Depth-First Search . . . . . . . . . . . . . . . . . . . 258
12.4 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 261
13 Data Structures for Integers
265
13.1 BinaryTrie: A digital search tree . . . . . . . . . . . . . . . 266
13.2 XFastTrie: Searching in Doubly-Logarithmic Time . . . . . 272
13.3 YFastTrie: A Doubly-Logarithmic Time SSet . . . . . . . . 275
13.4 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 280
14 External Memory Searching
283
14.1 The Block Store . . . . . . . . . . . . . . . . . . . . . . . . . 285
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
14.2 B-Trees
14.2.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . 288
14.2.2 Addition . . . . . . . . . . . . . . . . . . . . . . . . . 290
14.2.3 Removal
. . . . . . . . . . . . . . . . . . . . . . . . . 295
14.2.4 Amortized Analysis of B-Trees . . . . . . . . . . . . . 301
14.3 Discussion and Exercises . . . . . . . . . . . . . . . . . . . . 304
Bibliography
Index
309
317
vii
Acknowledgments
I am grateful to Nima Hoda, who spent a summer tirelessly proofread-
ing many of the chapters in this book; to the students in the Fall 2011
offering of COMP2402/2002, who put up with the first draft of this book
and spotted many typographic, grammatical, and factual errors; and to
Morgan Tunzelmann at Athabasca University Press, for patiently editing
several near-final drafts.
ix
Why This Book?
There are plenty of books that teach introductory data structures. Some
of them are very good. Most of them cost money, and the vast majority
of computer science undergraduate students will shell out at least some
cash on a data structures book.
Several free data structures books are available online. Some are very
good, but most of them are getting old. The majority of these books be-
came free when their authors and/or publishers decided to stop updat-
ing them. Updating these books is usually not possible, for two reasons:
(1) The copyright belongs to the author and/or publisher, either of whom
may not allow it. (2) The source code for these books is often not avail-
able. That is, the Word, WordPerfect, FrameMaker, or LATEX source for
the book is not available, and even the version of the software that han-
dles this source may not be available.
The goal of this project is to free undergraduate computer science stu-
dents from having to pay for an introductory data structures book. I have
decided to implement this goal by treating this book like an Open Source
software project. The LATEX source, Java source, and build scripts for the
book are available to download from the author’s website1 and also, more
importantly, on a reliable source code management site.2
The source code available there is released under a Creative Commons
Attribution license, meaning that anyone is free to share: to copy, dis-
tribute and transmit the work; and to remix: to adapt the work, including
the right to make commercial use of the work. The only condition on
these rights is attribution: you must acknowledge that the derived work
contains code and/or text from opendatastructures.org.
1http://opendatastructures.org
2https://github.com/patmorin/ods
xi
Why This Book?
Anyone can contribute corrections/fixes using the git source-code
management system. Anyone can also fork the book’s sources to develop
a separate version (for example, in another programming language). My
hope is that, by doing things this way, this book will continue to be a use-
ful textbook long after my interest in the project, or my pulse, (whichever
comes first) has waned.
xii
Chapter 1
Introduction
Every computer science curriculum in the world includes a course on data
structures and algorithms. Data structures are that important; they im-
prove our quality of life and even save lives on a regular basis. Many
multi-million and several multi-billion dollar companies have been built
around data structures.
How can this be? If we stop to think about it, we realize that we inter-
act with data structures constantly.
• Open a file: File system data structures are used to locate the parts
of that file on disk so they can be retrieved. This isn’t easy; disks
contain hundreds of millions of blocks. The contents of your file
could be stored on any one of them.
• Look up a contact on your phone: A data structure is used to look
up a phone number in your contact list based on partial information
even before you finish dialing/typing. This isn’t easy; your phone
may contain information about a lot of people—everyone you have
ever contacted via phone or email—and your phone doesn’t have a
very fast processor or a lot of memory.
• Log in to your favourite social network: The network servers use
your login information to look up your account information. This
isn’t easy; the most popular social networks have hundreds of mil-
lions of active users.
• Do a web search: The search engine uses data structures to find the
web pages containing your search terms. This isn’t easy; there are
1
§1.1
Introduction
over 8.5 billion web pages on the Internet and each page contains a
lot of potential search terms.
• Phone emergency services (9-1-1): The emergency services network
looks up your phone number in a data structure that maps phone
numbers to addresses so that police cars, ambulances, or fire trucks
can be sent there without delay. This is important; the person mak-
ing the call may not be able to provide the exact address they are
calling from and a delay can mean the difference between life or
death.
1.1 The Need for Efficiency
In the next section, we look at the operations supported by the most com-
monly used data structures. Anyone with a bit of programming experi-
ence will see that these operations are not hard to implement correctly.
We can store the data in an array or a linked list and each operation can
be implemented by iterating over all the elements of the array or list and
possibly adding or removing an element.
This kind of implementation is easy, but not very efficient. Does this
really matter? Computers are becoming faster and faster. Maybe the ob-
vious implementation is good enough. Let’s do some rough calculations
to find out.
Number of operations:
Imagine an application with a moderately-sized
data set, say of one million (106), items. It is reasonable, in most appli-
cations, to assume that the application will want to look up each item
at least once. This means we can expect to do at least one million (106)
searches in this data. If each of these 106 searches inspects each of the
106 items, this gives a total of 106
106 = 1012 (one thousand billion)
inspections.
×
Processor speeds: At the time of writing, even a very fast desktop com-
puter can not do more than one billion (109) operations per second.1 This
1Computer speeds are at most a few gigahertz (billions of cycles per second), and each
operation typically takes a few cycles.
2
The Need for Efficiency
§1.1
means that this application will take at least 1012/109 = 1000 seconds, or
roughly 16 minutes and 40 seconds. Sixteen minutes is an eon in com-
puter time, but a person might be willing to put up with it (if he or she
were headed out for a coffee break).
Bigger data sets: Now consider a company like Google, that indexes
over 8.5 billion web pages. By our calculations, doing any kind of query
over this data would take at least 8.5 seconds. We already know that this
isn’t the case; web searches complete in much less than 8.5 seconds, and
they do much more complicated queries than just asking if a particular
page is in their list of indexed pages. At the time of writing, Google re-
ceives approximately 4, 500 queries per second, meaning that they would
require at least 4, 500
8.5 = 38, 250 very fast servers just to keep up.
×
The solution: These examples tell us that the obvious implementations
of data structures do not scale well when the number of items, n, in the
data structure and the number of operations, m, performed on the data
structure are both large. In these cases, the time (measured in, say, ma-
chine instructions) is roughly n
m.
The solution, of course, is to carefully organize data within the data
structure so that not every operation requires every data item to be in-
spected. Although it sounds impossible at first, we will see data struc-
tures where a search requires looking at only two items on average, in-
dependent of the number of items stored in the data structure. In our
billion instruction per second computer it takes only 0.000000002 sec-
onds to search in a data structure containing a billion items (or a trillion,
or a quadrillion, or even a quintillion items).
×
We will also see implementations of data structures that keep the
items in sorted order, where the number of items inspected during an
operation grows very slowly as a function of the number of items in the
data structure. For example, we can maintain a sorted set of one billion
items while inspecting at most 60 items during any operation. In our bil-
lion instruction per second computer, these operations take 0.00000006
seconds each.
The remainder of this chapter briefly reviews some of the main con-
cepts used throughout the rest of the book. Section 1.2 describes the in-
3
§1.2
Introduction
terfaces implemented by all of the data structures described in this book
and should be considered required reading. The remaining sections dis-
cuss:
• some mathematical review including exponentials, logarithms, fac-
torials, asymptotic (big-Oh) notation, probability, and randomiza-
tion;
• the model of computation;
• correctness, running time, and space;
• an overview of the rest of the chapters; and
• the sample code and typesetting conventions.
A reader with or without a background in these areas can easily skip them
now and come back to them later if necessary.
1.2 Interfaces
When discussing data structures, it is important to understand the dif-
ference between a data structure’s interface and its implementation. An
interface describes what a data structure does, while an implementation
describes how the data structure does it.
An interface, sometimes also called an abstract data type, defines the
set of operations supported by a data structure and the semantics, or
meaning, of those operations. An interface tells us nothing about how
the data structure implements these operations; it only provides a list of
supported operations along with specifications about what types of argu-
ments each operation accepts and the value returned by each operation.
A data structure implementation, on the other hand, includes the inter-
nal representation of the data structure as well as the definitions of the
algorithms that implement the operations supported by the data struc-
ture. Thus, there can be many implementations of a single interface. For
example, in Chapter 2, we will see implementations of the List interface
using arrays and in Chapter 3 we will see implementations of the List
interface using pointer-based data structures. Each implements the same
interface, List, but in different ways.
4
Interfaces
§1.2
Figure 1.1: A FIFO Queue.
1.2.1 The Queue, Stack, and Deque Interfaces
The Queue interface represents a collection of elements to which we can
add elements and remove the next element. More precisely, the opera-
tions supported by the Queue interface are
• add(x): add the value x to the Queue
• remove(): remove the next (previously added) value, y, from the
Queue and return y
Notice that the remove() operation takes no argument. The Queue’s queue-
ing discipline decides which element should be removed. There are many
possible queueing disciplines, the most common of which include FIFO,
priority, and LIFO.
A FIFO (first-in-first-out) Queue, which is illustrated in Figure 1.1, re-
moves items in the same order they were added, much in the same way
a queue (or line-up) works when checking out at a cash register in a gro-
cery store. This is the most common kind of Queue so the qualifier FIFO
is often omitted. In other texts, the add(x) and remove() operations on a
FIFO Queue are often called enqueue(x) and dequeue(), respectively.
A priority Queue, illustrated in Figure 1.2, always removes the small-
est element from the Queue, breaking ties arbitrarily. This is similar to the
way in which patients are triaged in a hospital emergency room. As pa-
tients arrive they are evaluated and then placed in a waiting room. When
a doctor becomes available he or she first treats the patient with the most
life-threatening condition. The remove() operation on a priority Queue is
usually called deleteMin() in other texts.
A very common queueing discipline is the LIFO (last-in-first-out) dis-
cipline, illustrated in Figure 1.3.
In a LIFO Queue, the most recently
added element is the next one removed. This is best visualized in terms
of a stack of plates; plates are placed on the top of the stack and also
5
x···add(x)/enqueue(x)remove()/dequeue()§1.2
Introduction
Figure 1.2: A priority Queue.
Figure 1.3: A stack.
removed from the top of the stack. This structure is so common that it
gets its own name: Stack. Often, when discussing a Stack, the names
of add(x) and remove() are changed to push(x) and pop(); this is to avoid
confusing the LIFO and FIFO queueing disciplines.
A Deque is a generalization of both the FIFO Queue and LIFO Queue
(Stack). A Deque represents a sequence of elements, with a front and a
back. Elements can be added at the front of the sequence or the back of
the sequence. The names of the Deque operations are self-explanatory:
addFirst(x), removeFirst(), addLast(x), and removeLast(). It is worth
noting that a Stack can be implemented using only addFirst(x) and
removeFirst() while a FIFO Queue can be implemented using addLast(x)
and removeFirst().
1.2.2 The List Interface: Linear Sequences
This book will talk very little about the FIFO Queue, Stack, or Deque in-
terfaces. This is because these interfaces are subsumed by the List inter-
face. A List, illustrated in Figure 1.4, represents a sequence, x0, . . . , xn
1,
−
6
16add(x)remove()/deleteMin()x6133···remove()/pop()add(x)/push(x)xInterfaces
§1.2
Figure 1.4: A List represents a sequence indexed by 0, 1, 2, . . . , n
a call to get(2) would return the value c.
−
1. In this List
of values. The List interface includes the following operations:
1. size(): return n, the length of the list
2. get(i): return the value xi
3. set(i, x): set the value of xi equal to x
4. add(i, x): add x at position i, displacing xi, . . . , xn
1;
Set xj+1 = xj , for all j
n
1, . . . , i
}
−
∈ {
−
, increment n, and set xi = x
5. remove(i) remove the value xi, displacing xi+1, . . . , xn
and decrement n
Set xj = xj+1, for all j
i, . . . , n
2
1;
−
∈ {
−
}
Notice that these operations are easily sufficient to implement the Deque
interface:
addFirst(x)
removeFirst()
addLast(x)
removeLast()
⇒
⇒
⇒
⇒
add(0, x)
remove(0)
add(size(), x)
remove(size()
1)
−
Although we will normally not discuss the Stack, Deque and FIFO
Queue interfaces in subsequent chapters, the terms Stack and Deque are
sometimes used in the names of data structures that implement the List
interface. When this happens, it highlights the fact that these data struc-
tures can be used to implement the Stack or Deque interface very effi-
ciently. For example, the ArrayDeque class is an implementation of the
List interface that implements all the Deque operations in constant time
per operation.
7
e4567n−1···fbkca0123bcd···§1.2
Introduction
1.2.3 The USet Interface: Unordered Sets
The USet interface represents an unordered set of unique elements, which
mimics a mathematical set. A USet contains n distinct elements; no ele-
ment appears more than once; the elements are in no specific order. A
USet supports the following operations:
1. size(): return the number, n, of elements in the set
2. add(x): add the element x to the set if not already present;
Add x to the set provided that there is no element y in the set such
that x equals y. Return true if x was added to the set and false
otherwise.
3. remove(x): remove x from the set;
Find an element y in the set such that x equals y and remove y.
Return y, or null if no such element exists.
4. find(x): find x in the set if it exists;
Find an element y in the set such that y equals x. Return y, or null
if no such element exists.
These definitions are a bit fussy about distinguishing x, the element
we are removing or finding, from y, the element we may remove or find.
This is because x and y might actually be distinct objects that are never-
theless treated as equal.2 Such a distinction is useful because it allows for
the creation of dictionaries or maps that map keys onto values.
To create a dictionary/map, one forms compound objects called Pairs,
each of which contains a key and a value. Two Pairs are treated as equal
if their keys are equal. If we store some pair (k, v) in a USet and then
later call the find(x) method using the pair x = (k, null) the result will be
y = (k, v). In other words, it is possible to recover the value, v, given only
the key, k.
2In Java, this is done by overriding the class’s equals(y) and hashCode() methods.
8
Mathematical Background
§1.3
1.2.4 The SSet Interface: Sorted Sets
The SSet interface represents a sorted set of elements. An SSet stores
elements from some total order, so that any two elements x and y can
be compared. In code examples, this will be done with a method called
compare(x, y) in which
compare(x, y)
< 0 if x < y
> 0 if x > y
= 0 if x = y
An SSet supports the size(), add(x), and remove(x) methods with exactly
the same semantics as in the USet interface. The difference between a
USet and an SSet is in the find(x) method:
4. find(x): locate x in the sorted set;
Find the smallest element y in the set such that y
null if no such element exists.
≥
x. Return y or
This version of the find(x) operation is sometimes referred to as a
successor search. It differs in a fundamental way from USet.find(x) since
it returns a meaningful result even when there is no element equal to x
in the set.
The distinction between the USet and SSet find(x) operations is very
important and often missed. The extra functionality provided by an SSet
usually comes with a price that includes both a larger running time and a
higher implementation complexity. For example, most of the SSet imple-
mentations discussed in this book all have find(x) operations with run-
ning times that are logarithmic in the size of the set. On the other hand,
the implementation of a USet as a ChainedHashTable in Chapter 5 has
a find(x) operation that runs in constant expected time. When choosing
which of these structures to use, one should always use a USet unless the
extra functionality offered by an SSet is truly needed.
1.3 Mathematical Background
In this section, we review some mathematical notations and tools used
throughout this book, including logarithms, big-Oh notation, and proba-
9
§1.3
Introduction
bility theory. This review will be brief and is not intended as an introduc-
tion. Readers who feel they are missing this background are encouraged
to read, and do exercises from, the appropriate sections of the very good
(and free) textbook on mathematics for computer science [50].
1.3.1 Exponentials and Logarithms
The expression bx denotes the number b raised to the power of x. If x is
a positive integer, then this is just the value of b multiplied by itself x
1
times:
−
bx = b
b
×
b
.
× · · · ×
x
x. When x = 0, bx = 1. When b is not
When x is a negative integer, bx = 1/b−
(cid:123)(cid:122)
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
an integer, we can still define exponentiation in terms of the exponential
function ex (see below), which is itself defined in terms of the exponential
series, but this is best left to a calculus text.
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
(cid:124)
(cid:125)
In this book, the expression logb k denotes the base-b logarithm of k.
That is, the unique value x that satisfies
bx = k .
Most of the logarithms in this book are base 2 (binary logarithms). For
these, we omit the base, so that log k is shorthand for log2 k.
An informal, but useful, way to think about logarithms is to think of
logb k as the number of times we have to divide k by b before the result
is less than or equal to 1. For example, when one does binary search,
each comparison reduces the number of possible answers by a factor of 2.
This is repeated until there is at most one possible answer. Therefore, the
number of comparison done by binary search when there are initially at
most n + 1 possible answers is at most
.
log2(n + 1)
(cid:101)
(cid:100)
Another logarithm that comes up several times in this book is the nat-
ural logarithm. Here we use the notation ln k to denote loge k, where e —
Euler’s constant — is given by
1 +
e = lim
n
→∞ (cid:18)
1
n
n
(cid:19)
≈
2.71828 .
10
Mathematical Background
§1.3
The natural logarithm comes up frequently because it is the value of a
particularly common integral:
k
1
(cid:90)
1/x dx = ln k .
Two of the most common manipulations we do with logarithms are re-
moving them from an exponent:
and changing the base of a logarithm:
blogb k = k
logb k =
loga k
loga b
.
For example, we can use these two manipulations to compare the natural
and binary logarithms
ln k =
log k
log e
=
log k
(ln e)/(ln 2)
= (ln 2)(log k)
≈
0.693147 log k .
1.3.2 Factorials
In one or two places in this book, the factorial function is used. For a non-
negative integer n, the notation n! (pronounced “n factorial”) is defined
to mean
n! = 1
2
3
n .
·
Factorials appear because n! counts the number of distinct permutations,
i.e., orderings, of n distinct elements. For the special case n = 0, 0! is
defined as 1.
· · · · ·
·
The quantity n! can be approximated using Stirling’s Approximation:
where
n! = √2πn
eα(n)
,
n
e
(cid:18)
n
(cid:19)
1
12n + 1
< α(n) <
1
12n
.
Stirling’s Approximation also approximates ln(n!):
ln(n!) = n ln n
n +
−
1
2
ln(2πn) + α(n)
11
§1.3
Introduction
(In fact, Stirling’s Approximation is most easily proven by approximating
ln(n!) = ln 1 + ln 2 +
+ ln n by the integral
ln n dn = n ln n
n + 1.)
Related to the factorial function are the binomial coefficients. For a
de-
, the notation
(cid:82)
non-negative integer n and an integer k
notes:
0, . . . , n
}
∈ {
n
k
n
1
· · ·
−
n
k
=
n!
k!(n
k)!
.
(cid:0)
(cid:1)
(cid:32)
The binomial coefficient
(pronounced “n choose k”) counts the num-
ber of subsets of an n element set that have size k, i.e., the number of ways
of choosing k distinct integers from the set
n
k
−
(cid:33)
(cid:1)
(cid:0)
.
1, . . . , n
}
{
1.3.3 Asymptotic Notation
When analyzing data structures in this book, we want to talk about the
running times of various operations. The exact running times will, of
course, vary from computer to computer and even from run to run on an
individual computer. When we talk about the running time of an opera-
tion we are referring to the number of computer instructions performed
during the operation. Even for simple code, this quantity can be diffi-
cult to compute exactly. Therefore, instead of analyzing running times
exactly, we will use the so-called big-Oh notation: For a function f (n),
O(f (n)) denotes a set of functions,
O(f (n)) =
g(n) : there exists c > 0, and n0 such that
g(n)
c
·
≤
(cid:40)
f (n) for all n
n0
≥
.
(cid:41)
Thinking graphically, this set consists of the functions g(n) where c
starts to dominate g(n) when n is sufficiently large.
·
f (n)
We generally use asymptotic notation to simplify functions. For exam-
200 we can write O(n log n). This is proven
ple, in place of 5n log n + 8n
as follows:
−
5n log n + 8n
200
−
5n log n + 8n
5n log n + 8n log n
for n
13n log n .
≤
≤
≤
2 (so that log n
1)
≥
≥
This demonstrates that the function f (n) = 5n log n + 8n
O(n log n) using the constants c = 13 and n0 = 2.
−
200 is in the set
12
Mathematical Background
§1.3
A number of useful shortcuts can be applied when using asymptotic
notation. First:
for any c1 < c2. Second: For any constants a, b, c > 0,
O(nc1)
⊂
O(nc2) ,
O(a)
⊂
O(log n)
O(nb)
⊂
⊂
O(cn) .
These inclusion relations can be multiplied by any positive value, and
they still hold. For example, multiplying by n yields:
O(n)
⊂
O(n log n)
⊂
O(n1+b)
⊂
O(ncn) .
∈
Continuing in a long and distinguished tradition, we will abuse this
notation by writing things like f1(n) = O(f (n)) when what we really mean
is f1(n)
O(f (n)). We will also make statements like “the running time
of this operation is O(f (n))” when this statement should be “the running
time of this operation is a member of O(f (n)).” These shortcuts are mainly
to avoid awkward language and to make it easier to use asymptotic nota-
tion within strings of equations.
A particularly strange example of this occurs when we write state-
ments like
T (n) = 2 log n + O(1) .
Again, this would be more correctly written as
T (n)
≤
2 log n + [some member of O(1)] .
The expression O(1) also brings up another issue. Since there is no
variable in this expression, it may not be clear which variable is getting
arbitrarily large. Without context, there is no way to tell. In the example
above, since the only variable in the rest of the equation is n, we can
assume that this should be read as T (n) = 2 log n+O(f (n)), where f (n) = 1.
Big-Oh notation is not new or unique to computer science. It was used
by the number theorist Paul Bachmann as early as 1894, and is immensely
useful for describing the running times of computer algorithms. Consider
the following piece of code:
13
§1.3
Introduction
void snippet() {
for (int i = 0; i < n; i++)
Simple
a[i] = i;
}
One execution of this method involves
• 1 assignment (int i = 0),
• n + 1 comparisons (i < n),
• n increments (i + +),
• n array offset calculations (a[i]), and
• n indirect assignments (a[i] = i).
So we could write this running time as
T (n) = a + b(n + 1) + cn + dn + en ,
where a, b, c, d, and e are constants that depend on the machine running
the code and represent the time to perform assignments, comparisons,
increment operations, array offset calculations, and indirect assignments,
respectively. However, if this expression represents the running time of
two lines of code, then clearly this kind of analysis will not be tractable
to complicated code or algorithms. Using big-Oh notation, the running
time can be simplified to
T (n) = O(n) .
Not only is this more compact, but it also gives nearly as much informa-
tion. The fact that the running time depends on the constants a, b, c, d,
and e in the above example means that, in general, it will not be possible
to compare two running times to know which is faster without knowing
the values of these constants. Even if we make the effort to determine
these constants (say, through timing tests), then our conclusion will only
be valid for the machine we run our tests on.
Big-Oh notation allows us to reason at a much higher level, making it
possible to analyze more complicated functions. If two algorithms have
14
Mathematical Background
§1.3
the same big-Oh running time, then we won’t know which is faster, and
there may not be a clear winner. One may be faster on one machine,
and the other may be faster on a different machine. However, if the two
algorithms have demonstrably different big-Oh running times, then we
can be certain that the one with the smaller running time will be faster
for large enough values of n.
An example of how big-Oh notation allows us to compare two differ-
ent functions is shown in Figure 1.5, which compares the rate of growth
of f1(n) = 15n versus f2(n) = 2n log n. It might be that f1(n) is the run-
ning time of a complicated linear time algorithm while f2(n) is the run-
ning time of a considerably simpler algorithm based on the divide-and-
conquer paradigm. This illustrates that, although f1(n) is greater than
f2(n) for small values of n, the opposite is true for large values of n. Even-
tually f1(n) wins out, by an increasingly wide margin. Analysis using
big-Oh notation told us that this would happen, since O(n)
O(n log n).
In a few cases, we will use asymptotic notation on functions with more
than one variable. There seems to be no standard for this, but for our
purposes, the following definition is sufficient:
⊂
O(f (n1, . . . , nk)) =
g(n1, . . . , nk) : there exists c > 0, and z such that
g(n1, . . . , nk)
f (n1, . . . , nk)
for all n1, . . . , nk such that g(n1, . . . , nk)
≤
c
·
≥
This definition captures the situation we really care about: when the ar-
guments n1, . . . , nk make g take on large values. This definition also agrees
with the univariate definition of O(f (n)) when f (n) is an increasing func-
tion of n. The reader should be warned that, although this works for our
purposes, other texts may treat multivariate functions and asymptotic
notation differently.
.
z
1.3.4 Randomization and Probability
Some of the data structures presented in this book are randomized; they
make random choices that are independent of the data being stored in
them or the operations being performed on them. For this reason, per-
forming the same set of operations more than once using these structures
could result in different running times. When analyzing these data struc-
15
§1.3
Introduction
)
n
(
f
1600
1400
1200
1000
800
600
400
200
0
300000
250000
200000
)
n
(
f
150000
100000
50000
0
10
20
30
40
50
n
60
70
80
90
100
15n
2n log n
0
1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
n
15n
2n log n
Figure 1.5: Plots of 15n versus 2n log n.
16
Mathematical Background
§1.3
tures we are interested in their average or expected running times.
Formally, the running time of an operation on a randomized data
structure is a random variable, and we want to study its expected value.
For a discrete random variable X taking on values in some countable uni-
verse U , the expected value of X, denoted by E[X], is given by the formula
E[X] =
U
(cid:88)x
∈
x
X = x
Pr
{
}
·
.
{E}
denotes the probability that the event
Here Pr
occurs. In all of the
examples in this book, these probabilities are only with respect to the ran-
dom choices made by the randomized data structure; there is no assump-
tion that the data stored in the structure, nor the sequence of operations
performed on the data structure, is random.
E
One of the most important properties of expected values is linearity of
expectation. For any two random variables X and Y ,
E[X + Y ] = E[X] + E[Y ] .
More generally, for any random variables X1, . . . , Xk,
k
k
E
Xk
(cid:88)i=1
=
E[Xi] .
(cid:88)i=1
Linearity of expectation allows us to break down complicated random
variables (like the left hand sides of the above equations) into sums of
simpler random variables (the right hand sides).
A useful trick, that we will use repeatedly, is defining indicator ran-
dom variables. These binary variables are useful when we want to count
something and are best illustrated by an example. Suppose we toss a fair
coin k times and we want to know the expected number of times the coin
turns up as heads. Intuitively, we know the answer is k/2, but if we try to
prove it using the definition of expected value, we get
E[X] =
=
k
(cid:88)i=0
k
(cid:88)i=0
i
Pr
{
·
X = i
}
i
·
k
i
(cid:32)
/2k
(cid:33)
17
§1.4
Introduction
k
1
−
k
= k
·
(cid:88)i=0 (cid:32)
= k/2 .
−
i
1
/2k
(cid:33)
This requires that we know enough to calculate that Pr
and that we know the binomial identities i
= k
{
and
1
k
k
i
−
i
X = i
}
k
i=0
=
k
i
/2k,
k
i
= 2k.
(cid:0)
(cid:1)
Using indicator variables and linearity of expectation makes things
much easier. For each i
1, . . . , k
(cid:1)
, define the indicator random variable
}
(cid:0)
(cid:0)
(cid:1)
(cid:0)
(cid:1)
(cid:80)
Ii =
∈ {
1 if the ith coin toss is heads
0 otherwise.
E[Ii] = (1/2)1 + (1/2)0 = 1/2 .
Then
Now, X =
k
i=1 Ii, so
(cid:80)
E[X] = E
k
Ii
(cid:88)i=1
E[Ii]
1/2
k
(cid:88)i=1
k
=
=
(cid:88)i=1
= k/2 .
This is a bit more long-winded, but doesn’t require that we know any
magical identities or compute any non-trivial probabilities. Even better,
it agrees with the intuition that we expect half the coins to turn up as
heads precisely because each individual coin turns up as heads with a
probability of 1/2.
1.4 The Model of Computation
In this book, we will analyze the theoretical running times of operations
on the data structures we study. To do this precisely, we need a mathemat-
ical model of computation. For this, we use the w-bit word-RAM model.
18
Correctness, Time Complexity, and Space Complexity
§1.5
RAM stands for Random Access Machine. In this model, we have access
to a random access memory consisting of cells, each of which stores a w-
bit word. This implies that a memory cell can represent, for example, any
integer in the set
0, . . . , 2w
.
{
1
}
−
In the word-RAM model, basic operations on words take constant
, /, %), comparisons
∗
), and bitwise boolean operations (bitwise-AND, OR, and
time. This includes arithmetic operations (+,
(<, >, =,
≥
exclusive-OR).
,
−
≤
,
Any cell can be read or written in constant time. A computer’s mem-
ory is managed by a memory management system from which we can
allocate or deallocate a block of memory of any size we would like. Allo-
cating a block of memory of size k takes O(k) time and returns a reference
(a pointer) to the newly-allocated memory block. This reference is small
enough to be represented by a single word.
The word-size w is a very important parameter of this model. The only
log n, where n
assumption we will make about w is the lower-bound w
is the number of elements stored in any of our data structures. This is a
fairly modest assumption, since otherwise a word is not even big enough
to count the number of elements stored in the data structure.
≥
Space is measured in words, so that when we talk about the amount of
space used by a data structure, we are referring to the number of words of
memory used by the structure. All of our data structures store values of
a generic type T, and we assume an element of type T occupies one word
of memory. (In reality, we are storing references to objects of type T, and
these references occupy only one word of memory.)
The w-bit word-RAM model is a fairly close match for the (32-bit) Java
Virtual Machine (JVM) when w = 32. The data structures presented in
this book don’t use any special tricks that are not implementable on the
JVM and most other architectures.
1.5 Correctness, Time Complexity, and Space Complexity
When studying the performance of a data structure, there are three things
that matter most:
19
§1.5
Introduction
Correctness: The data structure should correctly implement its inter-
face.
Time complexity: The running times of operations on the data structure
should be as small as possible.
Space complexity: The data structure should use as little memory as
possible.
In this introductory text, we will take correctness as a given; we won’t
consider data structures that give incorrect answers to queries or don’t
perform updates properly. We will, however, see data structures that
make an extra effort to keep space usage to a minimum. This won’t usu-
ally affect the (asymptotic) running times of operations, but can make the
data structures a little slower in practice.
When studying running times in the context of data structures we
tend to come across three different kinds of running time guarantees:
Worst-case running times: These are the strongest kind of running time
guarantees. If a data structure operation has a worst-case running
time of f (n), then one of these operations never takes longer than
f (n) time.
Amortized running times: If we say that the amortized running time of
an operation in a data structure is f (n), then this means that the
cost of a typical operation is at most f (n). More precisely, if a data
structure has an amortized running time of f (n), then a sequence
of m operations takes at most mf (n) time. Some individual opera-
tions may take more than f (n) time but the average, over the entire
sequence of operations, is at most f (n).
Expected running times: If we say that the expected running time of an
operation on a data structure is f (n), this means that the actual run-
ning time is a random variable (see Section 1.3.4) and the expected
value of this random variable is at most f (n). The randomization
here is with respect to random choices made by the data structure.
To understand the difference between worst-case, amortized, and ex-
pected running times, it helps to consider a financial example. Consider
the cost of buying a house:
20
Correctness, Time Complexity, and Space Complexity
§1.5
Worst-case versus amortized cost: Suppose that a home costs $120 000.
In order to buy this home, we might get a 120 month (10 year) mortgage
with monthly payments of $1 200 per month. In this case, the worst-case
monthly cost of paying this mortgage is $1 200 per month.
If we have enough cash on hand, we might choose to buy the house
outright, with one payment of $120 000. In this case, over a period of 10
years, the amortized monthly cost of buying this house is
$120 000/120 months = $1 000 per month .
This is much less than the $1 200 per month we would have to pay if we
took out a mortgage.
Worst-case versus expected cost: Next, consider the issue of fire insur-
ance on our $120 000 home. By studying hundreds of thousands of cases,
insurance companies have determined that the expected amount of fire
damage caused to a home like ours is $10 per month. This is a very small
number, since most homes never have fires, a few homes may have some
small fires that cause a bit of smoke damage, and a tiny number of homes
burn right to their foundations. Based on this information, the insurance
company charges $15 per month for fire insurance.
Now it’s decision time. Should we pay the $15 worst-case monthly cost
for fire insurance, or should we gamble and self-insure at an expected cost
of $10 per month? Clearly, the $10 per month costs less in expectation,
but we have to be able to accept the possibility that the actual cost may be
much higher. In the unlikely event that the entire house burns down, the
actual cost will be $120 000.
These financial examples also offer insight into why we sometimes set-
tle for an amortized or expected running time over a worst-case running
time. It is often possible to get a lower expected or amortized running
time than a worst-case running time. At the very least, it is very often
possible to get a much simpler data structure if one is willing to settle for
amortized or expected running times.
21
§1.6
Introduction
1.6 Code Samples
The code samples in this book are written in the Java programming lan-
guage. However, to make the book accessible to readers not familiar with
all of Java’s constructs and keywords, the code samples have been sim-
plified. For example, a reader won’t find any of the keywords public,
protected, private, or static. A reader also won’t find much discus-
sion about class hierarchies. Which interfaces a particular class imple-
ments or which class it extends, if relevant to the discussion, should be
clear from the accompanying text.
These conventions should make the code samples understandable by
anyone with a background in any of the languages from the ALGOL tradi-
tion, including B, C, C++, C#, Objective-C, D, Java, JavaScript, and so on.
Readers who want the full details of all implementations are encouraged
to look at the Java source code that accompanies this book.
This book mixes mathematical analyses of running times with Java
source code for the algorithms being analyzed. This means that some
equations contain variables also found in the source code. These vari-
ables are typeset consistently, both within the source code and within
equations. The most common such variable is the variable n that, without
exception, always refers to the number of items currently stored in the
data structure.
1.7 List of Data Structures
Tables 1.1 and 1.2 summarize the performance of data structures in this
book that implement each of the interfaces, List, USet, and SSet, de-
scribed in Section 1.2. Figure 1.6 shows the dependencies between vari-
ous chapters in this book. A dashed arrow indicates only a weak depen-
dency, in which only a small part of the chapter depends on a previous
chapter or only the main results of the previous chapter.
22
List of Data Structures
§1.7
List implementations
get(i)/set(i, x)
ArrayStack
O(1)
ArrayDeque
O(1)
DualArrayDeque
O(1)
RootishArrayStack O(1)
DLList
SEList
SkiplistList
O(1 + min
O(1 + min
O(log n)E
i, n
i, n
−
−
{
{
add(i, x)/remove(i)
i)A
O(1 + n
−
O(1 + min
O(1 + min
O(1 + n
−
i, n
)
O(1 + min
{
/b) O(b + min
i, n
{
O(log n)E
§ 2.1
§ 2.4
§ 2.5
§ 2.6
i
§ 3.2
)
}
/b)A § 3.3
i
}
§ 4.3
{
{
i)A
i, n
i, n
i
}
i
}
)A
)A
−
−
−
−
i
}
i
}
USet implementations
find(x)
O(1)E
O(1)E
ChainedHashTable
LinearHashTable
A Denotes an amortized running time.
E Denotes an expected running time.
add(x)/remove(x)
O(1)A,E
O(1)A,E
§ 5.1
§ 5.2
Table 1.1: Summary of List and USet implementations.
23
§1.7
Introduction
SSet implementations
find(x)
O(log n)E
SkiplistSSet
O(log n)E
Treap
ScapegoatTree O(log n)
O(log n)
RedBlackTree
BinaryTrieI
O(w)
O(log w)A,E O(w)A,E
XFastTrieI
O(log w)A,E O(log w)A,E
YFastTrieI
O(log n)
BTree
BTreeX
O(logB n)
add(x)/remove(x)
O(log n)E
O(log n)E
O(log n)A
O(log n)
O(w)
O(B + log n)A
O(logB n)
§ 4.2
§ 7.2
§ 8.1
§ 9.2
§ 13.1
§ 13.2
§ 13.3
§ 14.2
§ 14.2
(Priority) Queue implementations
findMin()
O(1)
O(1)
add(x)/remove()
O(log n)A
O(log n)E
BinaryHeap
MeldableHeap
I This structure can only store w-bit integer data.
X This denotes the running time in the external-memory
§ 10.1
§ 10.2
model; see Chapter 14.
Table 1.2: Summary of SSet and priority Queue implementations.
24
List of Data Structures
§1.7
Figure 1.6: The dependencies between chapters in this book.
25
11.1.2.Quicksort1.Introduction6.Binarytrees3.Linkedlists3.3Space-efficientlinkedlists2.Array-basedlists7.Randombinarysearchtrees9.Red-blacktrees10.Heaps12.Graphs13.Datastructuresforintegers8.Scapegoattrees11.Sortingalgorithms11.1.3.Heapsort4.Skiplists5.Hashtables14.External-memorysearching§1.8
Introduction
1.8 Discussion and Exercises
The List, USet, and SSet interfaces described in Section 1.2 are influ-
enced by the Java Collections Framework [54]. These are essentially sim-
plified versions of the List, Set, Map, SortedSet, and SortedMap inter-
faces found in the Java Collections Framework. The accompanying source
code includes wrapper classes for making USet and SSet implementa-
tions into Set, Map, SortedSet, and SortedMap implementations.
For a superb (and free) treatment of the mathematics discussed in this
chapter, including asymptotic notation, logarithms, factorials, Stirling’s
approximation, basic probability, and lots more, see the textbook by Ley-
man, Leighton, and Meyer [50]. For a gentle calculus text that includes
formal definitions of exponentials and logarithms, see the (freely avail-
able) classic text by Thompson [73].
For more information on basic probability, especially as it relates to
computer science, see the textbook by Ross [65]. Another good reference,
which covers both asymptotic notation and probability, is the textbook by
Graham, Knuth, and Patashnik [37].
Readers wanting to brush up on their Java programming can find
many Java tutorials online [56].
Exercise 1.1. This exercise is designed to help familiarize the reader with
choosing the right data structure for the right problem. If implemented,
the parts of this exercise should be done by making use of an implemen-
tation of the relevant interface (Stack, Queue, Deque, USet, or SSet) pro-
vided by the Java Collections Framework.
Solve the following problems by reading a text file one line at a time
and performing operations on each line in the appropriate data struc-
ture(s). Your implementations should be fast enough that even files con-
taining a million lines can be processed in a few seconds.
1. Read the input one line at a time and then write the lines out in
reverse order, so that the last input line is printed first, then the
second last input line, and so on.
2. Read the first 50 lines of input and then write them out in reverse
order. Read the next 50 lines and then write them out in reverse
26
Discussion and Exercises
§1.8
order. Do this until there are no more lines left to read, at which
point any remaining lines should be output in reverse order.
In other words, your output will start with the 50th line, then the
49th, then the 48th, and so on down to the first line. This will be
followed by the 100th line, followed by the 99th, and so on down to
the 51st line. And so on.
Your code should never have to store more than 50 lines at any given
time.
3. Read the input one line at a time. At any point after reading the
first 42 lines, if some line is blank (i.e., a string of length 0), then
output the line that occured 42 lines prior to that one. For example,
if Line 242 is blank, then your program should output line 200.
This program should be implemented so that it never stores more
than 43 lines of the input at any given time.
4. Read the input one line at a time and write each line to the output
if it is not a duplicate of some previous input line. Take special care
so that a file with a lot of duplicate lines does not use more memory
than what is required for the number of unique lines.
5. Read the input one line at a time and write each line to the output
only if you have already read this line before. (The end result is that
you remove the first occurrence of each line.) Take special care so
that a file with a lot of duplicate lines does not use more memory
than what is required for the number of unique lines.
6. Read the entire input one line at a time. Then output all lines sorted
by length, with the shortest lines first. In the case where two lines
have the same length, resolve their order using the usual “sorted
order.” Duplicate lines should be printed only once.
7. Do the same as the previous question except that duplicate lines
should be printed the same number of times that they appear in the
input.
8. Read the entire input one line at a time and then output the even
numbered lines (starting with the first line, line 0) followed by the
odd-numbered lines.
27
§1.8
Introduction
9. Read the entire input one line at a time and randomly permute the
lines before outputting them. To be clear: You should not modify
the contents of any line. Instead, the same collection of lines should
be printed, but in a random order.
Exercise 1.2. A Dyck word is a sequence of +1’s and -1’s with the property
that the sum of any prefix of the sequence is never negative. For example,
+1,
1, +1 is not a Dyck word since
1, +1,
the prefix +1
1 < 0. Describe any relationship between Dyck words
and Stack push(x) and pop() operations.
1 is a Dyck word, but +1,
1,
−
−
−
−
−
−
1
Exercise 1.3. A matched string is a sequence of
, (, ), [, and ] characters
,
{
” is a matched string, but
that are properly matched. For example, “
()[]
}}
this “
is matched with a ]. Show how to
{
use a stack so that, given a string of length n, you can determine if it is a
matched string in O(n) time.
” is not, since the second
()]
{{
{{
}
}
Exercise 1.4. Suppose you have a Stack, s, that supports only the push(x)
and pop() operations. Show how, using only a FIFO Queue, q, you can
reverse the order of all elements in s.
Exercise 1.5. Using a USet, implement a Bag. A Bag is like a USet—it sup-
ports the add(x), remove(x) and find(x) methods—but it allows duplicate
elements to be stored. The find(x) operation in a Bag returns some ele-
ment (if any) that is equal to x. In addition, a Bag supports the findAll(x)
operation that returns a list of all elements in the Bag that are equal to x.
Exercise 1.6. From scratch, write and test implementations of the List,
USet and SSet interfaces. These do not have to be efficient. They can
be used later to test the correctness and performance of more efficient
implementations. (The easiest way to do this is to store the elements in
an array.)
Exercise 1.7. Work to improve the performance of your implementations
from the previous question using any tricks you can think of. Experiment
and think about how you could improve the performance of add(i, x) and
remove(i) in your List implementation. Think about how you could im-
prove the performance of the find(x) operation in your USet and SSet
implementations. This exercise is designed to give you a feel for how
difficult it can be to obtain efficient implementations of these interfaces.
28
Chapter 2
Array-Based Lists
In this chapter, we will study implementations of the List and Queue in-
terfaces where the underlying data is stored in an array, called the backing
array. The following table summarizes the running times of operations
for the data structures presented in this chapter:
get(i)/set(i, x)
ArrayStack
O(1)
ArrayDeque
O(1)
DualArrayDeque
O(1)
RootishArrayStack O(1)
i)
add(i, x)/remove(i)
O(n
−
O(min
{
O(min
{
i)
O(n
i, n
i, n
i
}
i
}
−
−
)
)
−
Data structures that work by storing data in a single array have many
advantages and limitations in common:
• Arrays offer constant time access to any value in the array. This is
what allows get(i) and set(i, x) to run in constant time.
• Arrays are not very dynamic. Adding or removing an element near
the middle of a list means that a large number of elements in the
array need to be shifted to make room for the newly added element
or to fill in the gap created by the deleted element. This is why the
operations add(i, x) and remove(i) have running times that depend
on n and i.
• Arrays cannot expand or shrink. When the number of elements in
the data structure exceeds the size of the backing array, a new array
29
§2.1
Array-Based Lists
needs to be allocated and the data from the old array needs to be
copied into the new array. This is an expensive operation.
The third point is important. The running times cited in the table above
do not include the cost associated with growing and shrinking the back-
ing array. We will see that, if carefully managed, the cost of growing and
shrinking the backing array does not add much to the cost of an aver-
age operation. More precisely, if we start with an empty data structure,
and perform any sequence of m add(i, x) or remove(i) operations, then
the total cost of growing and shrinking the backing array, over the entire
sequence of m operations is O(m). Although some individual operations
are more expensive, the amortized cost, when amortized over all m oper-
ations, is only O(1) per operation.
2.1 ArrayStack: Fast Stack Operations Using an Array
An ArrayStack implements the list interface using an array a, called the
backing array. The list element with index i is stored in a[i]. At most
times, a is larger than strictly necessary, so an integer n is used to keep
track of the number of elements actually stored in a. In this way, the list
elements are stored in a[0],. . . ,a[n
1] and, at all times, a.length
n.
−
≥
ArrayStack
T[] a;
int n;
int size() {
return n;
}
2.1.1 The Basics
Accessing and modifying the elements of an ArrayStack using get(i) and
set(i, x) is trivial. After performing any necessary bounds-checking we
simply return or set, respectively, a[i].
T get(int i) {
ArrayStack
30
ArrayStack: Fast Stack Operations Using an Array
§2.1
return a[i];
}
T set(int i, T x) {
T y = a[i];
a[i] = x;
return y;
}
The operations of adding and removing elements from an ArrayStack
are illustrated in Figure 2.1. To implement the add(i, x) operation, we first
check if a is already full. If so, we call the method resize() to increase
the size of a. How resize() is implemented will be discussed later. For
now, it is sufficient to know that, after a call to resize(), we can be sure
that a.length > n. With this out of the way, we now shift the elements
a[i], . . . , a[n
1] right by one position to make room for x, set a[i] equal to
x, and increment n.
−
ArrayStack
void add(int i, T x) {
if (n + 1 > a.length) resize();
for (int j = n; j > i; j--)
a[j] = a[j-1];
a[i] = x;
n++;
}
If we ignore the cost of the potential call to resize(), then the cost of
the add(i, x) operation is proportional to the number of elements we have
to shift to make room for x. Therefore the cost of this operation (ignoring
the cost of resizing a) is O(n
i).
Implementing the remove(i) operation is similar. We shift the ele-
ments a[i + 1], . . . , a[n
1] left by one position (overwriting a[i]) and de-
crease the value of n. After doing this, we check if n is getting much
3n. If so, then we call
smaller than a.length by checking if a.length
resize() to reduce the size of a.
≥
−
−
T remove(int i) {
T x = a[i];
ArrayStack
31
§2.1
Array-Based Lists
Figure 2.1: A sequence of add(i, x) and remove(i) operations on an ArrayStack.
Arrows denote elements being copied. Operations that result in a call to resize()
are marked with an asterisk.
32
add(2,e)add(5,r)add(5,e)∗brederbrederbredeerbreeerbrerebreebreeremove(4)remove(4)remove(4)∗01234567891011breiset(2,i)bredebredArrayStack: Fast Stack Operations Using an Array
§2.1
for (int j = i; j < n-1; j++)
a[j] = a[j+1];
n--;
if (a.length >= 3*n) resize();
return x;
}
If we ignore the cost of the resize() method, the cost of a remove(i)
operation is proportional to the number of elements we shift, which is
O(n
i).
−
2.1.2 Growing and Shrinking
The resize() method is fairly straightforward; it allocates a new array b
whose size is 2n and copies the n elements of a into the first n positions in
b, and then sets a to b. Thus, after a call to resize(), a.length = 2n.
ArrayStack
void resize() {
T[] b = newArray(max(n*2,1));
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
a = b;
}
Analyzing the actual cost of the resize() operation is easy. It allocates
an array b of size 2n and copies the n elements of a into b. This takes O(n)
time.
The running time analysis from the previous section ignored the cost
of calls to resize(). In this section we analyze this cost using a technique
known as amortized analysis. This technique does not try to determine the
cost of resizing during each individual add(i, x) and remove(i) operation.
Instead, it considers the cost of all calls to resize() during a sequence of
m calls to add(i, x) or remove(i). In particular, we will show:
Lemma 2.1. If an empty ArrayStack is created and any sequence of m
≥
1 calls to add(i, x) and remove(i) are performed, then the total time spent
during all calls to resize() is O(m).
33
§2.1
Array-Based Lists
Proof. We will show that any time resize() is called, the number of calls
to add or remove since the last call to resize() is at least n/2
1. Therefore,
if ni denotes the value of n during the ith call to resize() and r denotes
the number of calls to resize(), then the total number of calls to add(i, x)
or remove(i) is at least
−
which is equivalent to
r
(cid:88)i=1
(ni/2
1)
−
≤
m ,
r
(cid:88)i=1
2m + 2r .
ni ≤
On the other hand, the total time spent during all calls to resize() is
r
(cid:88)i=1
O(ni)
≤
O(m + r) = O(m) ,
since r is not more than m. All that remains is to show that the number
of calls to add(i, x) or remove(i) between the (i
1)th and the ith call to
resize() is at least ni/2.
−
There are two cases to consider. In the first case, resize() is being
called by add(i, x) because the backing array a is full, i.e., a.length = n =
ni. Consider the previous call to resize(): after this previous call, the
size of a was a.length, but the number of elements stored in a was at
most a.length/2 = ni/2. But now the number of elements stored in a is
ni = a.length, so there must have been at least ni/2 calls to add(i, x) since
the previous call to resize().
The second case occurs when resize() is being called by remove(i)
3n = 3ni. Again, after the previous call to resize()
1.1 Now
a.length/3 elements stored in a. Therefore, the number of
because a.length
the number of elements stored in a was at least a.length/2
there are ni ≤
≥
−
1The
a.length = 1.
−
1 in this formula accounts for the special case that occurs when n = 0 and
34
FastArrayStack: An Optimized ArrayStack
§2.2
remove(i) operations since the last call to resize() is at least
R
a.length/2
≥
= a.length/6
a.length/3
1
1
−
−
−
= (a.length/3)/2
1
−
ni/2
1 .
−
≥
In either case, the number of calls to add(i, x) or remove(i) that occur
1)th call to resize() and the ith call to resize() is at least
between the (i
ni/2
1, as required to complete the proof.
−
−
2.1.3 Summary
The following theorem summarizes the performance of an ArrayStack:
Theorem 2.1. An ArrayStack implements the List interface. Ignoring the
cost of calls to resize(), an ArrayStack supports the operations
• get(i) and set(i, x) in O(1) time per operation; and
• add(i, x) and remove(i) in O(1 + n
i) time per operation.
−
Furthermore, beginning with an empty ArrayStack and performing any se-
quence of m add(i, x) and remove(i) operations results in a total of O(m) time
spent during all calls to resize().
The ArrayStack is an efficient way to implement a Stack. In particu-
1), in
lar, we can implement push(x) as add(n, x) and pop() as remove(n
which case these operations will run in O(1) amortized time.
−
2.2 FastArrayStack: An Optimized ArrayStack
Much of the work done by an ArrayStack involves shifting (by add(i, x)
and remove(i)) and copying (by resize()) of data. In the implementa-
tions shown above, this was done using for loops. It turns out that many
programming environments have specific functions that are very efficient
at copying and moving blocks of data. In the C programming language,
there are the memcpy(d, s, n) and memmove(d, s, n) functions. In the C++
35
§2.3
Array-Based Lists
language there is the std :: copy(a0, a1, b) algorithm. In Java there is the
System.arraycopy(s, i, d, j, n) method.
FastArrayStack
void resize() {
T[] b = newArray(max(2*n,1));
System.arraycopy(a, 0, b, 0, n);
a = b;
}
void add(int i, T x) {
if (n + 1 > a.length) resize();
System.arraycopy(a, i, a, i+1, n-i);
a[i] = x;
n++;
}
T remove(int i) {
T x = a[i];
System.arraycopy(a, i+1, a, i, n-i-1);
n--;
if (a.length >= 3*n) resize();
return x;
}
These functions are usually highly optimized and may even use spe-
cial machine instructions that can do this copying much faster than we
could by using a for loop. Although using these functions does not
asymptotically decrease the running times, it can still be a worthwhile
optimization.
In the Java implementations here, the use of the native System.arraycopy(s, i, d, j, n)
resulted in speedups of a factor between 2 and 3, depending on the types
of operations performed. Your mileage may vary.
2.3 ArrayQueue: An Array-Based Queue
In this section, we present the ArrayQueue data structure, which imple-
ments a FIFO (first-in-first-out) queue; elements are removed (using the
remove() operation) from the queue in the same order they are added (us-
ing the add(x) operation).
36
ArrayQueue: An Array-Based Queue
§2.3
Notice that an ArrayStack is a poor choice for an implementation of
a FIFO queue. It is not a good choice because we must choose one end of
the list upon which to add elements and then remove elements from the
other end. One of the two operations must work on the head of the list,
which involves calling add(i, x) or remove(i) with a value of i = 0. This
gives a running time proportional to n.
To obtain an efficient array-based implementation of a queue, we first
notice that the problem would be easy if we had an infinite array a. We
could maintain one index j that keeps track of the next element to remove
and an integer n that counts the number of elements in the queue. The
queue elements would always be stored in
a[j], a[j + 1], . . . , a[j + n
1] .
−
Initially, both j and n would be set to 0. To add an element, we would
place it in a[j + n] and increment n. To remove an element, we would
remove it from a[j], increment j, and decrement n.
Of course, the problem with this solution is that it requires an infinite
array. An ArrayQueue simulates this by using a finite array a and modular
arithmetic. This is the kind of arithmetic used when we are talking about
the time of day. For example 10:00 plus five hours gives 3:00. Formally,
we say that
10 + 5 = 15
≡
3 (mod 12) .
We read the latter part of this equation as “15 is congruent to 3 modulo
12.” We can also treat mod as a binary operator, so that
15 mod 12 = 3 .
More generally, for an integer a and positive integer m, a mod m is the
unique integer r
such that a = r + km for some integer k.
Less formally, the value r is the remainder we get when we divide a by
m.
In many programming languages, including Java, the mod operator
is represented using the % symbol.2
0, . . . , m
1
}
∈ {
−
2This is sometimes referred to as the brain-dead mod operator, since it does not correctly
implement the mathematical mod operator when the first argument is negative.
37
§2.3
Array-Based Lists
Modular arithmetic is useful for simulating an infinite array, since
i mod a.length always gives a value in the range 0, . . . , a.length
1. Us-
ing modular arithmetic we can store the queue elements at array locations
−
a[j%a.length], a[(j + 1)%a.length], . . . , a[(j + n
1)%a.length] .
−
−
This treats the array a like a circular array in which array indices larger
than a.length
1 “wrap around” to the beginning of the array.
The only remaining thing to worry about is taking care that the num-
ber of elements in the ArrayQueue does not exceed the size of a.
ArrayQueue
T[] a;
int j;
int n;
A sequence of add(x) and remove() operations on an ArrayQueue is
illustrated in Figure 2.2. To implement add(x), we first check if a is full
and, if necessary, call resize() to increase the size of a. Next, we store x
in a[(j + n)%a.length] and increment n.
ArrayQueue
boolean add(T x) {
if (n + 1 > a.length) resize();
a[(j+n) % a.length] = x;
n++;
return true;
}
To implement remove(), we first store a[j] so that we can return it
later. Next, we decrement n and increment j (modulo a.length) by set-
ting j = (j + 1) mod a.length. Finally, we return the stored value of a[j].
If necessary, we may call resize() to decrease the size of a.
T remove() {
ArrayQueue
if (n == 0) throw new NoSuchElementException();
T x = a[j];
j = (j + 1) % a.length;
38
ArrayQueue: An Array-Based Queue
§2.3
Figure 2.2: A sequence of add(x) and remove(i) operations on an ArrayQueue.
Arrows denote elements being copied. Operations that result in a call to resize()
are marked with an asterisk.
39
abbcaadd(d)add(e)remove()ebcadebcdefbcdbcefdadd(g)add(h)∗01234567891011cdefbcdgadd(f)gbcefdghcefdghremove()j=2,n=3j=2,n=4j=2,n=5j=3,n=4j=3,n=5j=3,n=6j=0,n=7j=1,n=6j=0,n=6§2.4
Array-Based Lists
n--;
if (a.length >= 3*n) resize();
return x;
}
Finally, the resize() operation is very similar to the resize() opera-
tion of ArrayStack. It allocates a new array, b, of size 2n and copies
a[j], a[(j + 1)%a.length], . . . , a[(j + n
1)%a.length]
−
onto
and sets j = 0.
b[0], b[1], . . . , b[n
1]
−
ArrayQueue
void resize() {
T[] b = newArray(max(1,n*2));
for (int k = 0; k < n; k++)
b[k] = a[(j+k) % a.length];
a = b;
j = 0;
}
2.3.1 Summary
The following theorem summarizes the performance of the ArrayQueue
data structure:
Theorem 2.2. An ArrayQueue implements the (FIFO) Queue interface. Ig-
noring the cost of calls to resize(), an ArrayQueue supports the operations
add(x) and remove() in O(1) time per operation. Furthermore, beginning with
an empty ArrayQueue, any sequence of m add(i, x) and remove(i) operations
results in a total of O(m) time spent during all calls to resize().
2.4 ArrayDeque: Fast Deque Operations Using an Array
The ArrayQueue from the previous section is a data structure for rep-
resenting a sequence that allows us to efficiently add to one end of the
40
ArrayDeque: Fast Deque Operations Using an Array
§2.4
sequence and remove from the other end. The ArrayDeque data structure
allows for efficient addition and removal at both ends. This structure im-
plements the List interface by using the same circular array technique
used to represent an ArrayQueue.
ArrayDeque
T[] a;
int j;
int n;
The get(i) and set(i, x) operations on an ArrayDeque are straightfor-
ward. They get or set the array element a[(j + i) mod a.length].
ArrayDeque
T get(int i) {
return a[(j+i)%a.length];
}
T set(int i, T x) {
T y = a[(j+i)%a.length];
a[(j+i)%a.length] = x;
return y;
}
The implementation of add(i, x) is a little more interesting. As usual,
we first check if a is full and, if necessary, call resize() to resize a. Re-
member that we want this operation to be fast when i is small (close
to 0) or when i is large (close to n). Therefore, we check if i < n/2. If
1] left by one position. Otherwise
so, we shift the elements a[0], . . . , a[i
(i
1] right by one position. See
Figure 2.3 for an illustration of add(i, x) and remove(x) operations on an
ArrayDeque.
n/2), we shift the elements a[i], . . . , a[n
≥
−
−
void add(int i, T x) {
ArrayDeque
if (n+1 > a.length) resize();
if (i < n/2) { // shift a[0],..,a[i-1] left one position
j = (j == 0) ? a.length - 1 : j - 1; //(j-1)mod a.length
for (int k = 0; k <= i-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
41
§2.4
Array-Based Lists
Figure 2.3: A sequence of add(i, x) and remove(i) operations on an ArrayDeque.
Arrows denote elements being copied.
} else { // shift a[i],..,a[n-1] right one position
for (int k = n; k > i; k--)
a[(j+k)%a.length] = a[(j+k-1)%a.length];
}
a[(j+i)%a.length] = x;
n++;
}
i
}
−
{
By doing the shifting in this way, we guarantee that add(i, x) never
i
elements. Thus, the running time
has to shift more than min
}
of the add(i, x) operation (ignoring the cost of a resize() operation) is
O(1 + min
i, n
i, n
−
).
{
The implementation of the remove(i) operation is similar. It either
1] right by one position or shifts the ele-
shifts elements a[0], . . . , a[i
ments a[i + 1], . . . , a[n
1] left by one position depending on whether i <
n/2. Again, this means that remove(i) never spends more than O(1 +
min
−
−
i
i, n
{
−
) time to shift elements.
}
T remove(int i) {
ArrayDeque
T x = a[(j+i)%a.length];
if (i < n/2) {
// shift a[0],..,[i-1] right one position
for (int k = i; k > 0; k--)
42
01234567891011bcefdghremove(2)j=0,n=8aabefdghj=1,n=7add(4,x)abexdfgj=1,n=8hadd(3,y)bdexyfgj=0,n=9haadd(4,z)dyexzfgj=11,n=10hbaDualArrayDeque: Building a Deque from Two Stacks
§2.5
a[(j+k)%a.length] = a[(j+k-1)%a.length];
j = (j + 1) % a.length;
} else { // shift a[i+1],..,a[n-1] left one position
for (int k = i; k < n-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
}
n--;
if (3*n < a.length) resize();
return x;
}
2.4.1 Summary
The following theorem summarizes the performance of the ArrayDeque
data structure:
Theorem 2.3. An ArrayDeque implements the List interface. Ignoring the
cost of calls to resize(), an ArrayDeque supports the operations
• get(i) and set(i, x) in O(1) time per operation; and
• add(i, x) and remove(i) in O(1 + min
i, n
{
i
}
−
) time per operation.
Furthermore, beginning with an empty ArrayDeque, performing any sequence
of m add(i, x) and remove(i) operations results in a total of O(m) time spent
during all calls to resize().
2.5 DualArrayDeque: Building a Deque from Two Stacks
Next, we present a data structure, the DualArrayDeque that achieves the
same performance bounds as an ArrayDeque by using two ArrayStacks.
Although the asymptotic performance of the DualArrayDeque is no bet-
ter than that of the ArrayDeque, it is still worth studying, since it offers a
good example of how to make a sophisticated data structure by combin-
ing two simpler data structures.
A DualArrayDeque represents a list using two ArrayStacks. Recall
that an ArrayStack is fast when the operations on it modify elements
43
§2.5
Array-Based Lists
near the end. A DualArrayDeque places two ArrayStacks, called front
and back, back-to-back so that operations are fast at either end.
List<T> front;
List<T> back;
DualArrayDeque
A DualArrayDeque does not explicitly store the number, n, of ele-
ments it contains. It doesn’t need to, since it contains n = front.size() +
back.size() elements. Nevertheless, when analyzing the DualArrayDeque
we will still use n to denote the number of elements it contains.
int size() {
return front.size() + back.size();
DualArrayDeque
}
The front ArrayStack stores the list elements that whose indices are
1, but stores them in reverse order. The back Array-
0, . . . , front.size()
Stack contains list elements with indices in front.size(), . . . , size()
1 in
the normal order. In this way, get(i) and set(i, x) translate into appro-
priate calls to get(i) or set(i, x) on either front or back, which take O(1)
time per operation.
−
−
DualArrayDeque
T get(int i) {
if (i < front.size()) {
return front.get(front.size()-i-1);
} else {
return back.get(i-front.size());
}
}
T set(int i, T x) {
if (i < front.size()) {
return front.set(front.size()-i-1, x);
} else {
return back.set(i-front.size(), x);
}
}
44
DualArrayDeque: Building a Deque from Two Stacks
§2.5
Figure 2.4: A sequence of add(i, x) and remove(i) operations on a DualArray-
Deque. Arrows denote elements being copied. Operations that result in a rebal-
ancing by balance() are marked with an asterisk.
Note that if an index i < front.size(), then it corresponds to the ele-
1, since the elements of front
i
ment of front at position front.size()
are stored in reverse order.
−
−
Adding and removing elements from a DualArrayDeque is illustrated
in Figure 2.4. The add(i, x) operation manipulates either front or back,
as appropriate:
DualArrayDeque
void add(int i, T x) {
if (i < front.size()) {
front.add(front.size()-i, x);
} else {
back.add(i-front.size(), x);
}
balance();
}
The add(i, x) method performs rebalancing of the two ArrayStacks
front and back, by calling the balance() method. The implementation
45
01234bcdfrontaback01234add(3,x)bcxadadd(4,y)bcxaydremove(0)∗bcxydbcxyd§2.5
Array-Based Lists
of balance() is described below, but for now it is sufficient to know that
balance() ensures that, unless size() < 2, front.size() and back.size()
do not differ by more than a factor of 3. In particular, 3
back.size() and 3
front.size().
front.size()
back.size()
≥
·
Next we analyze the cost of add(i, x), ignoring the cost of calls to
balance(). If i < front.size(), then add(i, x) gets implemented by the
call to front.add(front.size()
1, x). Since front is an ArrayStack,
the cost of this is
−
−
i
·
≥
O(front.size()
(front.size()
i
−
−
−
1) + 1) = O(i + 1) .
(2.1)
On the other hand, if i
as back.add(i
≥
−
front.size(), x). The cost of this is
front.size(), then add(i, x) gets implemented
O(back.size()
(i
−
−
front.size()) + 1) = O(n
i + 1) .
(2.2)
−
Notice that the first case (2.1) occurs when i < n/4. The second case
i < 3n/4, we cannot be sure
(2.2) occurs when i
whether the operation affects front or back, but in either case, the op-
eration takes O(n) = O(i) = O(n
i > n/4.
Summarizing the situation, we have
3n/4. When n/4
i) time, since i
n/4 and n
≤
≥
≥
−
−
Running time of add(i, x)
O(1 + i)
O(n)
O(1 + n
−
i)
if i < n/4
if n/4
if i
≤
3n/4
≥
i < 3n/4
≤
Thus, the running time of add(i, x), if we ignore the cost of the call to
balance(), is O(1 + min
i, n
The remove(i) operation and its analysis resemble the add(i, x) oper-
i
).
}
−
{
ation and analysis.
DualArrayDeque
T remove(int i) {
T x;
if (i < front.size()) {
x = front.remove(front.size()-i-1);
} else {
x = back.remove(i-front.size());
}
balance();
46
DualArrayDeque: Building a Deque from Two Stacks
§2.5
return x;
}
2.5.1 Balancing
Finally, we turn to the balance() operation performed by add(i, x) and
remove(i). This operation ensures that neither front nor back becomes
too big (or too small). It ensures that, unless there are fewer than two
elements, each of front and back contain at least n/4 elements. If this
is not the case, then it moves elements between them so that front and
back contain exactly
elements, respectively.
elements and
n/2
n/2
(cid:99)
(cid:98)
(cid:100)
(cid:101)
void balance() {
DualArrayDeque
int n = size();
if (3*front.size() < back.size()) {
int s = n/2 - front.size();
List<T> l1 = newStack();
List<T> l2 = newStack();
l1.addAll(back.subList(0,s));
Collections.reverse(l1);
l1.addAll(front);
l2.addAll(back.subList(s, back.size()));
front = l1;
back = l2;
} else if (3*back.size() < front.size()) {
int s = front.size() - n/2;
List<T> l1 = newStack();
List<T> l2 = newStack();
l1.addAll(front.subList(s, front.size()));
l2.addAll(front.subList(0, s));
Collections.reverse(l2);
l2.addAll(back);
front = l1;
back = l2;
}
}
Here there is little to analyze. If the balance() operation does rebal-
47
§2.5
Array-Based Lists
ancing, then it moves O(n) elements and this takes O(n) time. This is bad,
since balance() is called with each call to add(i, x) and remove(i). How-
ever, the following lemma shows that, on average, balance() only spends
a constant amount of time per operation.
Lemma 2.2. If an empty DualArrayDeque is created and any sequence of
1 calls to add(i, x) and remove(i) are performed, then the total time
m
spent during all calls to balance() is O(m).
≥
Proof. We will show that, if balance() is forced to shift elements, then
the number of add(i, x) and remove(i) operations since the last time any
elements were shifted by balance() is at least n/2
1. As in the proof
of Lemma 2.1, this is sufficient to prove that the total time spent by
balance() is O(m).
−
We will perform our analysis using a technique knows as the potential
method. Define the potential, Φ, of the DualArrayDeque as the difference
in size between front and back:
Φ =
front.size()
|
−
back.size()
|
.
The interesting thing about this potential is that a call to add(i, x) or
remove(i) that does not do any balancing can increase the potential by
at most 1.
Observe that, immediately after a call to balance() that shifts ele-
ments, the potential, Φ0, is at most 1, since
Φ0 =
n/2
|(cid:98)
(cid:99) − (cid:100)
n/2
(cid:101)| ≤
1 .
Consider the situation immediately before a call to balance() that
shifts elements and suppose, without loss of generality, that balance()
is shifting elements because 3front.size() < back.size(). Notice that, in
this case,
n = front.size() + back.size()
< back.size()/3 + back.size()
=
4
3
back.size()
48
RootishArrayStack: A Space-Efficient Array Stack
§2.6
Furthermore, the potential at this point in time is
Φ1 = back.size()
> back.size()
−
front.size()
back.size()/3
−
back.size()
=
2
3
2
3 ×
= n/2
>
3
4
n
Therefore, the number of calls to add(i, x) or remove(i) since the last time
balance() shifted elements is at least Φ1 −
1. This completes
the proof.
Φ0 > n/2
−
2.5.2 Summary
The following theorem summarizes the properties of a DualArrayDeque:
Theorem 2.4. A DualArrayDeque implements the List interface. Ignoring
the cost of calls to resize() and balance(), a DualArrayDeque supports the
operations
• get(i) and set(i, x) in O(1) time per operation; and
• add(i, x) and remove(i) in O(1 + min
i, n
{
i
}
−
) time per operation.
Furthermore, beginning with an empty DualArrayDeque, any sequence of m
add(i, x) and remove(i) operations results in a total of O(m) time spent dur-
ing all calls to resize() and balance().
2.6 RootishArrayStack: A Space-Efficient Array Stack
One of the drawbacks of all previous data structures in this chapter is
that, because they store their data in one or two arrays and they avoid
resizing these arrays too often, the arrays frequently are not very full. For
example, immediately after a resize() operation on an ArrayStack, the
backing array a is only half full. Even worse, there are times when only
one third of a contains data.
49
§2.6
Array-Based Lists
Figure 2.5: A sequence of add(i, x) and remove(i) operations on a RootishArray-
Stack. Arrows denote elements being copied.
In this section, we discuss the RootishArrayStack data structure, that
addresses the problem of wasted space. The RootishArrayStack stores
n elements using O(√n) arrays. In these arrays, at most O(√n) array lo-
cations are unused at any time. All remaining array locations are used
to store data. Therefore, these data structures waste at most O(√n) space
when storing n elements.
A RootishArrayStack stores its elements in a list of r arrays called
1. See Figure 2.5. Block b contains
blocks that are numbered 0, 1, . . . , r
b + 1 elements. Therefore, all r blocks contain a total of
−
· · ·
elements. The above formula can be obtained as shown in Figure 2.6.
1 + 2 + 3 +
+ r = r(r + 1)/2
List<T[]> blocks;
int n;
RootishArrayStack
As we might expect, the elements of the list are laid out in order
within the blocks. The list element with index 0 is stored in block 0,
50
0blocksabcdefgh1234567891011121314abxcdefgadd(2,x)hremove(1)axcdefghaxcdefgaxcdefremove(7)remove(6)RootishArrayStack: A Space-Efficient Array Stack
§2.6
+r. The number of shaded
Figure 2.6: The number of white squares is 1+2+3+
squares is the same. Together the white and shaded squares make a rectangle
consisting of r(r + 1) squares.
· · ·
elements with list indices 1 and 2 are stored in block 1, elements with list
indices 3, 4, and 5 are stored in block 2, and so on. The main problem
we have to address is that of determining, given an index i, which block
contains i as well as the index corresponding to i within that block.
Determining the index of i within its block turns out to be easy. If
1 is
index i is in block b, then the number of elements in blocks 0, . . . , b
b(b + 1)/2. Therefore, i is stored at location
−
j = i
−
b(b + 1)/2
within block b. Somewhat more challenging is the problem of determin-
ing the value of b. The number of elements that have indices less than or
equal to i is i + 1. On the other hand, the number of elements in blocks
0, . . . , b is (b + 1)(b + 2)/2. Therefore, b is the smallest integer such that
(b + 1)(b + 2)/2
i + 1 .
≥
We can rewrite this equation as
b2 + 3b
2i
−
≥
0 .
3 + √9 + 8i)/2 and b = (
The corresponding quadratic equation b2 + 3b
2i = 0 has two solutions:
√9 + 8i)/2. The second solution makes
b = (
no sense in our application since it always gives a negative value. There-
3 + √9 + 8i)/2. In general, this solution
fore, we obtain the solution b = (
−
−
−
−
3
−
51
r...............r+1§2.6
Array-Based Lists
is not an integer, but going back to our inequality, we want the smallest
integer b such that b
3 + √9 + 8i)/2. This is simply
(
−
≥
b =
(
−
(cid:108)
3 + √9 + 8i)/2
.
(cid:109)
RootishArrayStack
int i2b(int i) {
double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0;
int b = (int)Math.ceil(db);
return b;
}
With this out of the way, the get(i) and set(i, x) methods are straight-
forward. We first compute the appropriate block b and the appropriate
index j within the block and then perform the appropriate operation:
RootishArrayStack
T get(int i) {
int b = i2b(i);
int j = i - b*(b+1)/2;
return blocks.get(b)[j];
}
T set(int i, T x) {
int b = i2b(i);
int j = i - b*(b+1)/2;
T y = blocks.get(b)[j];
blocks.get(b)[j] = x;
return y;
}
If we use any of the data structures in this chapter for representing
the blocks list, then get(i) and set(i, x) will each run in constant time.
The add(i, x) method will, by now, look familiar. We first check to see
if our data structure is full, by checking if the number of blocks, r, is such
that r(r + 1)/2 = n. If so, we call grow() to add another block. With this
done, we shift elements with indices i, . . . , n
1 to the right by one position
to make room for the new element with index i:
−
52
RootishArrayStack: A Space-Efficient Array Stack
§2.6
RootishArrayStack
void add(int i, T x) {
int r = blocks.size();
if (r*(r+1)/2 < n + 1) grow();
n++;
for (int j = n-1; j > i; j--)
set(j, get(j-1));
set(i, x);
}
The grow() method does what we expect. It adds a new block:
void grow() {
blocks.add(newArray(blocks.size()+1));
RootishArrayStack
}
Ignoring the cost of the grow() operation, the cost of an add(i, x) oper-
i), just
ation is dominated by the cost of shifting and is therefore O(1 + n
like an ArrayStack.
−
The remove(i) operation is similar to add(i, x). It shifts the elements
with indices i + 1, . . . , n left by one position and then, if there is more than
one empty block, it calls the shrink() method to remove all but one of the
unused blocks:
RootishArrayStack
T remove(int i) {
T x = get(i);
for (int j = i; j < n-1; j++)
set(j, get(j+1));
n--;
int r = blocks.size();
if ((r-2)*(r-1)/2 >= n)
return x;
}
shrink();
void shrink() {
int r = blocks.size();
RootishArrayStack
53
§2.6
Array-Based Lists
while (r > 0 && (r-2)*(r-1)/2 >= n) {
blocks.remove(blocks.size()-1);
r--;
}
}
Once again, ignoring the cost of the shrink() operation, the cost of a
remove(i) operation is dominated by the cost of shifting and is therefore
O(n
i).
−
2.6.1 Analysis of Growing and Shrinking
The above analysis of add(i, x) and remove(i) does not account for the
cost of grow() and shrink(). Note that, unlike the ArrayStack.resize()
operation, grow() and shrink() do not copy any data. They only allocate
or free an array of size r. In some environments, this takes only constant
time, while in others, it may require time proportional to r.
We note that, immediately after a call to grow() or shrink(), the situ-
ation is clear. The final block is completely empty, and all other blocks
are completely full. Another call to grow() or shrink() will not happen
until at least r
1 elements have been added or removed. Therefore, even
if grow() and shrink() take O(r) time, this cost can be amortized over at
least r
1 add(i, x) or remove(i) operations, so that the amortized cost of
grow() and shrink() is O(1) per operation.
−
−
2.6.2 Space Usage
Next, we analyze the amount of extra space used by a RootishArray-
Stack. In particular, we want to count any space used by a Rootish-
ArrayStack that is not an array element currently used to hold a list ele-
ment. We call all such space wasted space.
The remove(i) operation ensures that a RootishArrayStack never has
more than two blocks that are not completely full. The number of blocks,
r, used by a RootishArrayStack that stores n elements therefore satisfies
2)(r
(r
−
1)
−
≤
n .
54
RootishArrayStack: A Space-Efficient Array Stack
§2.6
Again, using the quadratic equation on this gives
(3 + √1 + 4n)/2 = O(√n) .
r
≤
1, so the space wasted by these
The last two blocks have sizes r and r
two blocks is at most 2r
1 = O(√n). If we store the blocks in (for example)
an ArrayStack, then the amount of space wasted by the List that stores
those r blocks is also O(r) = O(√n). The other space needed for storing n
and other accounting information is O(1). Therefore, the total amount of
wasted space in a RootishArrayStack is O(√n).
−
−
Next, we argue that this space usage is optimal for any data structure
that starts out empty and can support the addition of one item at a time.
More precisely, we will show that, at some point during the addition of
n items, the data structure is wasting an amount of space at least in √n
(though it may be only wasted for a moment).
Suppose we start with an empty data structure and we add n items one
at a time. At the end of this process, all n items are stored in the structure
and distributed among a collection of r memory blocks. If r
√n, then
the data structure must be using r pointers (or references) to keep track
of these r blocks, and these pointers are wasted space. On the other hand,
if r < √n then, by the pigeonhole principle, some block must have a size
of at least n/r > √n. Consider the moment at which this block was first
allocated. Immediately after it was allocated, this block was empty, and
was therefore wasting √n space. Therefore, at some point in time during
the insertion of n elements, the data structure was wasting √n space.
≥
2.6.3 Summary
The following theorem summarizes our discussion of the RootishArray-
Stack data structure:
Theorem 2.5. A RootishArrayStack implements the List interface. Ignor-
ing the cost of calls to grow() and shrink(), a RootishArrayStack supports
the operations
• get(i) and set(i, x) in O(1) time per operation; and
• add(i, x) and remove(i) in O(1 + n
i) time per operation.
−
55
§2.6
Array-Based Lists
Furthermore, beginning with an empty RootishArrayStack, any sequence
of m add(i, x) and remove(i) operations results in a total of O(m) time spent
during all calls to grow() and shrink().
The space (measured in words)3 used by a RootishArrayStack that stores
n elements is n + O(√n).
2.6.4 Computing Square Roots
A reader who has had some exposure to models of computation may no-
tice that the RootishArrayStack, as described above, does not fit into the
usual word-RAM model of computation (Section 1.4) because it requires
taking square roots. The square root operation is generally not consid-
ered a basic operation and is therefore not usually part of the word-RAM
model.
In this section, we show that the square root operation can be imple-
mented efficiently. In particular, we show that for any integer x
0, . . . , n
,
}
can be computed in constant-time, after O(√n) preprocessing that
√x
(cid:98)
(cid:99)
creates two arrays of length O(√n). The following lemma shows that we
can reduce the problem of computing the square root of x to the square
root of a related value x(cid:48).
∈ {
Lemma 2.3. Let x
1 and let x(cid:48) = x
a, where 0
−
a
√x. Then √x(cid:48) ≥
≤
≤
√x
1.
−
≥
Proof. It suffices to show that
Square both sides of this inequality to get
√x
x
−
≥
√x
−
1 .
(cid:113)
√x
x
−
x
−
≥
2√x + 1
and gather terms to get
which is clearly true for any x
√x
1
≥
1.
≥
3Recall Section 1.4 for a discussion of how memory is measured.
56
RootishArrayStack: A Space-Efficient Array Stack
§2.6
(cid:98)
log x
(cid:99)
Start by restricting the problem a little, and assume that 2r
so that
resentation. We can take x(cid:48) = x
ditions of Lemma 2.3, so √x
r/2
lower-order
x < 2r+1,
= r, i.e., x is an integer having r + 1 bits in its binary rep-
r/2
(cid:99)). Now, x(cid:48) satisfies the con-
1. Furthermore, x(cid:48) has all of its
(x mod 2(cid:98)
−
√x(cid:48) ≤
−
bits equal to 0, so there are only
≤
(cid:98)
(cid:99)
2r+1
r/2
(cid:99)
−(cid:98)
4
2r/2
4√x
·
possible values of x(cid:48). This means that we can use an array, sqrttab, that
for each possible value of x(cid:48). A little more pre-
stores the value of
cisely, we have
√x(cid:48)(cid:99)
≤
≤
(cid:98)
sqrttab[i] =
i2(cid:98)
r/2
(cid:99)
.
. Stated another way, the array entry s = sqrttab[x>>
(cid:98)
(cid:23)
In this way, sqrttab[i] is within 2 of √x for all x
1
}
equal to
√x
(cid:99)
(cid:98)
1, or
by incrementing s until (s + 1)2 > x.
(cid:99) −
] is either
(cid:99)
2. From s we can determine the value of
(cid:99), . . . , (i + 1)2(cid:98)
√x
(cid:98)
√x
(cid:98)
√x
(cid:98)
r/2
i2(cid:98)
(cid:22)(cid:112)
(cid:99) −
(cid:99) −
,
(cid:99)
∈ {
r/2
r/2
int sqrt(int x, int r) {
FastSqrt
int s = sqrtab[x>>r/2];
while ((s+1)*(s+1) <= x) s++; // executes at most twice
return s;
}
2r, . . . , 2r+1
1
∈ {
Now, this only works for x
and sqrttab is a special
log x
table that only works for a particular value of r =
. To overcome
(cid:99)
(cid:98)
different sqrttab arrays, one for each pos-
this, we could compute
sible value of
. The sizes of these tables form an exponential se-
quence whose largest value is at most 4√n, so the total size of all tables is
O(√n).
log x
(cid:99)
(cid:98)
log n
(cid:99)
−
(cid:98)
}
However, it turns out that more than one sqrttab array is unneces-
sary; we only need one sqrttab array for the value r =
log n
. Any value
(cid:99)
(cid:98)
r(cid:48) and using
x with log x = r(cid:48) < r can be upgraded by multiplying x by 2r
−
the equation
r(cid:48))/2√x .
−
2r, . . . , 2r+1
The quantity 2r
so we can look up
{
its square root in sqrttab. The following code implements this idea to
r(cid:48) x is in the range
−
r(cid:48) x = 2(r
−
√2r
1
}
−
57
§2.6
Array-Based Lists
compute
using an array, sqrttab, of size 216.
√x
(cid:98)
(cid:99)
for all non-negative integers x in the range
0, . . . , 230
{
1
}
−
int sqrt(int x) {
FastSqrt
int rp = log(x);
int upgrade = ((r-rp)/2) * 2;
int xp = x << upgrade;
int s = sqrtab[xp>>(r/2)] >> (upgrade/2);
while ((s+1)*(s+1) <= x) s++; // executes at most twice
return s;
// xp has r or r-1 bits
}
log x
(cid:99)
(cid:98)
log x
(cid:99)
(cid:98)
Something we have taken for granted thus far is the question of how
to compute r(cid:48) =
. Again, this is a problem that can be solved with
an array, logtab, of size 2r/2. In this case, the code is particularly simple,
since
is just the index of the most significant 1 bit in the binary
representation of x. This means that, for x > 2r/2, we can right-shift the
bits of x by r/2 positions before using it as an index into logtab. The
following code does this using an array logtab of size 216 to compute
log x
(cid:99)
(cid:98)
for all x in the range
1, . . . , 232
{
1
}
−
.
FastSqrt
int log(int x) {
if (x >= halfint)
return 16 + logtab[x>>>16];
return logtab[x];
}
Finally, for completeness, we include the following code that initial-
izes logtab and sqrttab:
FastSqrt
void inittabs() {
sqrtab = new int[1<<(r/2)];
logtab = new int[1<<(r/2)];
for (int d = 0; d < r/2; d++)
Arrays.fill(logtab, 1<<d, 2<<d, d);
int s = 1<<(r/4);
// sqrt(2ˆ(r/2))
58
Discussion and Exercises
§2.7
for (int i = 0; i < 1<<(r/2); i++) {
if ((s+1)*(s+1) <= i << (r/2)) s++; // sqrt increases
sqrtab[i] = s;
}
}
To summarize, the computations done by the i2b(i) method can be
implemented in constant time on the word-RAM using O(√n) extra mem-
ory to store the sqrttab and logtab arrays. These arrays can be rebuilt
when n increases or decreases by a factor of two, and the cost of this re-
building can be amortized over the number of add(i, x) and remove(i)
operations that caused the change in n in the same way that the cost of
resize() is analyzed in the ArrayStack implementation.
2.7 Discussion and Exercises
Most of the data structures described in this chapter are folklore. They
can be found in implementations dating back over 30 years. For example,
implementations of stacks, queues, and deques, which generalize eas-
ily to the ArrayStack, ArrayQueue and ArrayDeque structures described
here, are discussed by Knuth [46, Section 2.2.2].
Brodnik et al. [13] seem to have been the first to describe the Rootish-
ArrayStack and prove a √n lower-bound like that in Section 2.6.2. They
also present a different structure that uses a more sophisticated choice
of block sizes in order to avoid computing square roots in the i2b(i)
method. Within their scheme, the block containing i is block
log(i + 1)
,
(cid:99)
which is simply the index of the leading 1 bit in the binary representation
of i + 1. Some computer architectures provide an instruction for comput-
ing the index of the leading 1-bit in an integer. In Java, the Integer class
provides a method numberOfLeadingZeros(i) from which one can easily
compute
(cid:98)
.
A structure related to the RootishArrayStack is the two-level tiered-
vector of Goodrich and Kloss [35]. This structure supports the get(i, x)
and set(i, x) operations in constant time and add(i, x) and remove(i) in
O(√n) time. These running times are similar to what can be achieved with
the more careful implementation of a RootishArrayStack discussed in
log(i + 1)
(cid:99)
(cid:98)
59
§2.7
Array-Based Lists
Exercise 2.11.
Exercise 2.1. In the ArrayStack implementation, after the first call to
remove(i), the backing array, a, contains n + 1 non-null values despite
the fact that the ArrayStack only contains n elements. Where is the extra
non-null value? Discuss any consequences this non-null value might
have on the Java Runtime Environment’s memory manager.
Exercise 2.2. The List method addAll(i, c) inserts all elements of the
Collection c into the list at position i. (The add(i, x) method is a special
case where c =
.) Explain why, for the data structures in this chapter,
it is not efficient to implement addAll(i, c) by repeated calls to add(i, x).
Design and implement a more efficient implementation.
x
}
{
Exercise 2.3. Design and implement a RandomQueue. This is an imple-
mentation of the Queue interface in which the remove() operation removes
an element that is chosen uniformly at random among all the elements
currently in the queue. (Think of a RandomQueue as a bag in which we
can add elements or reach in and blindly remove some random element.)
The add(x) and remove() operations in a RandomQueue should run in con-
stant time per operation.
Exercise 2.4. Design and implement a Treque (triple-ended queue). This
is a List implementation in which get(i) and set(i, x) run in constant
time and add(i, x) and remove(i) run in time
O(1 + min
i,
n/2
i
) .
i, n
{
−
In other words, modifications are fast if they are near either end or near
the middle of the list.
−
|}
|
Exercise 2.5. Implement a method rotate(a, r) that “rotates” the array a
0, . . . , a.length
so that a[i] moves to a[(i + r) mod a.length], for all i
.
}
Exercise 2.6. Implement a method rotate(r) that “rotates” a List so that
list item i becomes list item (i + r) mod n. When run on an ArrayDeque,
or a DualArrayDeque, rotate(r) should run in O(1 + min
∈ {
r, n
{
−
r
) time.
}
Exercise 2.7. Modify the ArrayDeque implementation so that the shift-
ing done by add(i, x), remove(i), and resize() is done using the faster
System.arraycopy(s, i, d, j, n) method.
60
Discussion and Exercises
§2.7
Exercise 2.8. Modify the ArrayDeque implementation so that it does not
use the % operator (which is expensive on some systems).
Instead, it
should make use of the fact that, if a.length is a power of 2, then
k%a.length = k&(a.length
1) .
−
(Here, & is the bitwise-and operator.)
Exercise 2.9. Design and implement a variant of ArrayDeque that does
not do any modular arithmetic at all. Instead, all the data sits in a con-
secutive block, in order, inside an array. When the data overruns the
beginning or the end of this array, a modified rebuild() operation is per-
formed. The amortized cost of all operations should be the same as in an
ArrayDeque.
Hint: Getting this to work is really all about how you implement the
rebuild() operation. You would like rebuild() to put the data structure
into a state where the data cannot run off either end until at least n/2
operations have been performed.
Test the performance of your implementation against the ArrayDeque.
Optimize your implementation (by using System.arraycopy(a, i, b, i, n))
and see if you can get it to outperform the ArrayDeque implementation.
Exercise 2.10. Design and implement a version of a RootishArrayStack
that has only O(√n) wasted space, but that can perform add(i, x) and
remove(i, x) operations in O(1 + min
i, n
{
Exercise 2.11. Design and implement a version of a RootishArrayStack
that has only O(√n) wasted space, but that can perform add(i, x) and
remove(i, x) operations in O(1 + min
) time. (For an idea on how
{
to do this, see Section 3.3.)
) time.
}
√n, n
i
}
−
−
i
Exercise 2.12. Design and implement a version of a RootishArrayStack
that has only O(√n) wasted space, but that can perform add(i, x) and
remove(i, x) operations in O(1 + min
) time. (See Section 3.3
for ideas on how to achieve this.)
i, √n, n
{
i
}
−
Exercise 2.13. Design and implement a CubishArrayStack. This three
level structure implements the List interface using O(n2/3) wasted space.
In this structure, get(i) and set(i, x) take constant time; while add(i, x)
and remove(i) take O(n1/3) amortized time.
61
Chapter 3
Linked Lists
In this chapter, we continue to study implementations of the List inter-
face, this time using pointer-based data structures rather than arrays. The
structures in this chapter are made up of nodes that contain the list items.
Using references (pointers), the nodes are linked together into a sequence.
We first study singly-linked lists, which can implement Stack and (FIFO)
Queue operations in constant time per operation and then move on to
doubly-linked lists, which can implement Deque operations in constant
time.
Linked lists have advantages and disadvantages when compared to
array-based implementations of the List interface. The primary disad-
vantage is that we lose the ability to access any element using get(i) or
set(i, x) in constant time. Instead, we have to walk through the list, one
element at a time, until we reach the ith element. The primary advantage
is that they are more dynamic: Given a reference to any list node u, we
can delete u or insert a node adjacent to u in constant time. This is true
no matter where u is in the list.
3.1 SLList: A Singly-Linked List
An SLList (singly-linked list) is a sequence of Nodes. Each node u stores
a data value u.x and a reference u.next to the next node in the sequence.
For the last node w in the sequence, w.next = null
63
§3.1
Linked Lists
Figure 3.1: A sequence of Queue (add(x) and remove()) and Stack (push(x) and
pop()) operations on an SLList.
SLList
class Node {
T x;
Node next;
}
For efficiency, an SLList uses variables head and tail to keep track
of the first and last node in the sequence, as well as an integer n to keep
track of the length of the sequence:
Node head;
Node tail;
int n;
SLList
A sequence of Stack and Queue operations on an SLList is illustrated
in Figure 3.1.
An SLList can efficiently implement the Stack operations push() and
pop() by adding and removing elements at the head of the sequence. The
push() operation simply creates a new node u with data value x, sets
u.next to the old head of the list and makes u the new head of the list.
Finally, it increments n since the size of the SLList has increased by one:
64
abcdeheadtailabcdeadd(x)xheadtailbcderemove()xheadtailcdepop()xheadtailycdepush(y)xheadtailSLList: A Singly-Linked List
§3.1
SLList
T push(T x) {
Node u = new Node();
u.x = x;
u.next = head;
head = u;
if (n == 0)
tail = u;
n++;
return x;
}
The pop() operation, after checking that the SLList is not empty, re-
moves the head by setting head = head.next and decrementing n. A spe-
cial case occurs when the last element is being removed, in which case
tail is set to null:
SLList
T pop() {
if (n == 0) return null;
T x = head.x;
head = head.next;
if (--n == 0) tail = null;
return x;
}
Clearly, both the push(x) and pop() operations run in O(1) time.
3.1.1 Queue Operations
An SLList can also implement the FIFO queue operations add(x) and
remove() in constant time. Removals are done from the head of the list,
and are identical to the pop() operation:
SLList
T remove() {
if (n == 0) return null;
T x = head.x;
head = head.next;
65
§3.1
Linked Lists
if (--n == 0) tail = null;
return x;
}
Additions, on the other hand, are done at the tail of the list. In most
cases, this is done by setting tail.next = u, where u is the newly created
node that contains x. However, a special case occurs when n = 0, in which
case tail = head = null. In this case, both tail and head are set to u.
SLList
boolean add(T x) {
Node u = new Node();
u.x = x;
if (n == 0) {
head = u;
} else {
tail.next = u;
}
tail = u;
n++;
return true;
}
Clearly, both add(x) and remove() take constant time.
3.1.2 Summary
The following theorem summarizes the performance of an SLList:
Theorem 3.1. An SLList implements the Stack and (FIFO) Queue inter-
faces. The push(x), pop(), add(x) and remove() operations run in O(1) time
per operation.
An SLList nearly implements the full set of Deque operations. The
only missing operation is removing from the tail of an SLList. Removing
from the tail of an SLList is difficult because it requires updating the
value of tail so that it points to the node w that precedes tail in the
SLList; this is the node w such that w.next = tail. Unfortunately, the
only way to get to w is by traversing the SLList starting at head and taking
n
2 steps.
−
66
DLList: A Doubly-Linked List
§3.2
Figure 3.2: A DLList containing a,b,c,d,e.
3.2 DLList: A Doubly-Linked List
A DLList (doubly-linked list) is very similar to an SLList except that each
node u in a DLList has references to both the node u.next that follows it
and the node u.prev that precedes it.
DLList
class Node {
T x;
Node prev, next;
}
When implementing an SLList, we saw that there were always several
special cases to worry about. For example, removing the last element
from an SLList or adding an element to an empty SLList requires care
In a DLList, the
to ensure that head and tail are correctly updated.
number of these special cases increases considerably. Perhaps the cleanest
way to take care of all these special cases in a DLList is to introduce a
dummy node. This is a node that does not contain any data, but acts as a
placeholder so that there are no special nodes; every node has both a next
and a prev, with dummy acting as the node that follows the last node in the
list and that precedes the first node in the list. In this way, the nodes of
the list are (doubly-)linked into a cycle, as illustrated in Figure 3.2.
int n;
Node dummy;
DLList() {
dummy = new Node();
DLList
67
abcdedummy§3.2
Linked Lists
dummy.next = dummy;
dummy.prev = dummy;
n = 0;
}
Finding the node with a particular index in a DLList is easy; we can
either start at the head of the list (dummy.next) and work forward, or start
at the tail of the list (dummy.prev) and work backward. This allows us to
reach the ith node in O(1 + min
i, n
{
i
) time:
−
}
DLList
Node getNode(int i) {
Node p = null;
if (i < n / 2) {
p = dummy.next;
for (int j = 0; j < i; j++)
p = p.next;
} else {
p = dummy;
for (int j = n; j > i; j--)
p = p.prev;
}
return p;
}
The get(i) and set(i, x) operations are now also easy. We first find
the ith node and then get or set its x value:
DLList
T get(int i) {
return getNode(i).x;
}
T set(int i, T x) {
Node u = getNode(i);
T y = u.x;
u.x = x;
return y;
}
The running time of these operations is dominated by the time it takes
to find the ith node, and is therefore O(1 + min
i, n
{
i
).
}
−
68
DLList: A Doubly-Linked List
§3.2
Figure 3.3: Adding the node u before the node w in a DLList.
3.2.1 Adding and Removing
If we have a reference to a node w in a DLList and we want to insert a node
u before w, then this is just a matter of setting u.next = w, u.prev = w.prev,
and then adjusting u.prev.next and u.next.prev. (See Figure 3.3.) Thanks
to the dummy node, there is no need to worry about w.prev or w.next not
existing.
Node addBefore(Node w, T x) {
DLList
Node u = new Node();
u.x = x;
u.prev = w.prev;
u.next = w;
u.next.prev = u;
u.prev.next = u;
n++;
return u;
}
Now, the list operation add(i, x) is trivial to implement. We find the
ith node in the DLList and insert a new node u that contains x just before
it.
void add(int i, T x) {
addBefore(getNode(i), x);
}
DLList
69
wu······u.nextu.prev§3.2
Linked Lists
The only non-constant part of the running time of add(i, x) is the time
it takes to find the ith node (using getNode(i)). Thus, add(i, x) runs in
O(1 + min
i, n
i
) time.
}
−
{
Removing a node w from a DLList is easy. We only need to adjust
pointers at w.next and w.prev so that they skip over w. Again, the use of
the dummy node eliminates the need to consider any special cases:
DLList
void remove(Node w) {
w.prev.next = w.next;
w.next.prev = w.prev;
n--;
}
Now the remove(i) operation is trivial. We find the node with index i
and remove it:
DLList
T remove(int i) {
Node w = getNode(i);
remove(w);
return w.x;
}
Again, the only expensive part of this operation is finding the ith node
using getNode(i), so remove(i) runs in O(1 + min
i, n
{
i
}
−
) time.
3.2.2 Summary
The following theorem summarizes the performance of a DLList:
Theorem 3.2. A DLList implements the List interface. In this implementa-
tion, the get(i), set(i, x), add(i, x) and remove(i) operations run in O(1 +
min
) time per operation.
i, n
{
i
}
−
It is worth noting that, if we ignore the cost of the getNode(i) opera-
tion, then all operations on a DLList take constant time. Thus, the only
expensive part of operations on a DLList is finding the relevant node.
70
SEList: A Space-Efficient Linked List
§3.3
Once we have the relevant node, adding, removing, or accessing the data
at that node takes only constant time.
This is in sharp contrast to the array-based List implementations
of Chapter 2; in those implementations, the relevant array item can be
found in constant time. However, addition or removal requires shifting
elements in the array and, in general, takes non-constant time.
For this reason, linked list structures are well-suited to applications
where references to list nodes can be obtained through external means.
An example of this is the LinkedHashSet data structure found in the Java
Collections Framework, in which a set of items is stored in a doubly-
linked list and the nodes of the doubly-linked list are stored in a hash ta-
ble (discussed in Chapter 5). When elements are removed from a Linked-
HashSet, the hash table is used to find the relevant list node in constant
time and then the list node is deleted (also in constant time).
3.3 SEList: A Space-Efficient Linked List
One of the drawbacks of linked lists (besides the time it takes to access
elements that are deep within the list) is their space usage. Each node in
a DLList requires an additional two references to the next and previous
nodes in the list. Two of the fields in a Node are dedicated to maintaining
the list, and only one of the fields is for storing data!
An SEList (space-efficient list) reduces this wasted space using a sim-
ple idea: Rather than store individual elements in a DLList, we store a
block (array) containing several items. More precisely, an SEList is pa-
rameterized by a block size b. Each individual node in an SEList stores a
block that can hold up to b + 1 elements.
For reasons that will become clear later, it will be helpful if we can
do Deque operations on each block. The data structure that we choose for
this is a BDeque (bounded deque), derived from the ArrayDeque structure
described in Section 2.4. The BDeque differs from the ArrayDeque in one
small way: When a new BDeque is created, the size of the backing array a
is fixed at b + 1 and never grows or shrinks. The important property of a
BDeque is that it allows for the addition or removal of elements at either
the front or back in constant time. This will be useful as elements are
71
§3.3
Linked Lists
shifted from one block to another.
class BDeque extends ArrayDeque<T> {
SEList
BDeque() {
super(SEList.this.type());
a = newArray(b+1);
}
void resize() { }
}
An SEList is then a doubly-linked list of blocks:
SEList
SEList
class Node {
BDeque d;
Node prev, next;
}
int n;
Node dummy;
3.3.1 Space Requirements
An SEList places very tight restrictions on the number of elements in a
block: Unless a block is the last block, then that block contains at least
b
1 and at most b + 1 elements. This means that, if an SEList contains n
elements, then it has at most
−
n/(b
−
1) + 1 = O(n/b)
blocks. The BDeque for each block contains an array of length b + 1 but,
for every block except the last, at most a constant amount of space is
wasted in this array. The remaining memory used by a block is also con-
stant. This means that the wasted space in an SEList is only O(b + n/b).
By choosing a value of b within a constant factor of √n, we can make
the space-overhead of an SEList approach the √n lower bound given in
Section 2.6.2.
72
SEList: A Space-Efficient Linked List
§3.3
3.3.2 Finding Elements
The first challenge we face with an SEList is finding the list item with a
given index i. Note that the location of an element consists of two parts:
1. The node u that contains the block that contains the element with
index i; and
2. the index j of the element within its block.
SEList
class Location {
Node u;
int j;
Location(Node u, int j) {
this.u = u;
this.j = j;
}
}
To find the block that contains a particular element, we proceed the
same way as we do in a DLList. We either start at the front of the list and
traverse in the forward direction, or at the back of the list and traverse
backwards until we reach the node we want. The only difference is that,
each time we move from one node to the next, we skip over a whole block
of elements.
SEList
Location getLocation(int i) {
if (i < n/2) {
Node u = dummy.next;
while (i >= u.d.size()) {
i -= u.d.size();
u = u.next;
}
return new Location(u, i);
} else {
Node u = dummy;
int idx = n;
73
§3.3
Linked Lists
while (i < idx) {
u = u.prev;
idx -= u.d.size();
}
return new Location(u, i-idx);
}
}
−
1 elements, so each step in our search gets us b
Remember that, with the exception of at most one block, each block
contains at least b
1
elements closer to the element we are looking for. If we are searching
forward, this means that we reach the node we want after O(1 + i/b)
If we search backwards, then we reach the node we want after
steps.
i)/b) steps. The algorithm takes the smaller of these two quan-
O(1 + (n
tities depending on the value of i, so the time to locate the item with
index i is O(1 + min
/b).
i, n
−
−
Once we know how to locate the item with index i, the get(i) and
set(i, x) operations translate into getting or setting a particular index in
the correct block:
i
}
−
{
SEList
T get(int i) {
Location l = getLocation(i);
return l.u.d.get(l.j);
}
T set(int i, T x) {
Location l = getLocation(i);
T y = l.u.d.get(l.j);
l.u.d.set(l.j,x);
return y;
}
The running times of these operations are dominated by the time it
takes to locate the item, so they also run in O(1 + min
i, n
{
i
}
−
/b) time.
3.3.3 Adding an Element
Adding elements to an SEList is a little more complicated. Before consid-
ering the general case, we consider the easier operation, add(x), in which
74
SEList: A Space-Efficient Linked List
§3.3
x is added to the end of the list. If the last block is full (or does not exist
because there are no blocks yet), then we first allocate a new block and
append it to the list of blocks. Now that we are sure that the last block
exists and is not full, we append x to the last block.
boolean add(T x) {
SEList
Node last = dummy.prev;
if (last == dummy || last.d.size() == b+1) {
last = addBefore(dummy);
}
last.d.add(x);
n++;
return true;
}
Things get more complicated when we add to the interior of the list
using add(i, x). We first locate i to get the node u whose block contains
the ith list item. The problem is that we want to insert x into u’s block,
but we have to be prepared for the case where u’s block already contains
b + 1 elements, so that it is full and there is no room for x.
Let u0, u1, u2, . . . denote u, u.next, u.next.next, and so on. We explore
u0, u1, u2, . . . looking for a node that can provide space for x. Three cases
can occur during our space exploration (see Figure 3.4):
1. We quickly (in r + 1
b steps) find a node ur whose block is not full.
In this case, we perform r shifts of an element from one block into
the next, so that the free space in ur becomes a free space in u0. We
can then insert x into u0’s block.
≤
2. We quickly (in r +1
b steps) run off the end of the list of blocks. In
this case, we add a new empty block to the end of the list of blocks
and proceed as in the first case.
≤
3. After b steps we do not find any block that is not full. In this case,
u0, . . . , ub
1 is a sequence of b blocks that each contain b+1 elements.
We insert a new block ub at the end of this sequence and spread the
original b(b + 1) elements so that each block of u0, . . . , ub contains
−
75
§3.3
Linked Lists
Figure 3.4: The three cases that occur during the addition of an item x in the
interior of an SEList. (This SEList has block size b = 3.)
exactly b elements. Now u0’s block contains only b elements so it
has room for us to insert x.
SEList
void add(int i, T x) {
if (i == n) {
add(x);
return;
}
Location l = getLocation(i);
Node u = l.u;
int r = 0;
while (r < b && u != dummy && u.d.size() == b+1) {
u = u.next;
r++;
}
if (r == b) {
spread(l.u);
u = l.u;
}
// b blocks each with b+1 elements
76
abcdefghij······axbcdefghi······jabcdefgh···axbcdefgh···abcdefghij······axbcdefgh···ikl···jklSEList: A Space-Efficient Linked List
§3.3
if (u == dummy) { // ran off the end - add new node
u = addBefore(u);
}
while (u != l.u) { // work backwards, shifting elements
u.d.add(0, u.prev.d.remove(u.prev.d.size()-1));
u = u.prev;
}
u.d.add(l.j, x);
n++;
}
The running time of the add(i, x) operation depends on which of the
three cases above occurs. Cases 1 and 2 involve examining and shifting
elements through at most b blocks and take O(b) time. Case 3 involves
calling the spread(u) method, which moves b(b + 1) elements and takes
O(b2) time. If we ignore the cost of Case 3 (which we will account for
later with amortization) this means that the total running time to locate
i, n
i and perform the insertion of x is O(b + min
{
/b).
i
}
−
3.3.4 Removing an Element
Removing an element from an SEList is similar to adding an element.
We first locate the node u that contains the element with index i. Now,
we have to be prepared for the case where we cannot remove an element
from u without causing u’s block to become smaller than b
1.
Again, let u0, u1, u2, . . . denote u, u.next, u.next.next, and so on. We
examine u0, u1, u2, . . . in order to look for a node from which we can bor-
row an element to make the size of u0’s block at least b
1. There are three
cases to consider (see Figure 3.5):
−
−
≤
1. We quickly (in r + 1
b steps) find a node whose block contains
more than b
1 elements. In this case, we perform r shifts of an
element from one block into the previous one, so that the extra ele-
ment in ur becomes an extra element in u0. We can then remove the
appropriate element from u0’s block.
−
2. We quickly (in r + 1
b steps) run off the end of the list of blocks.
In this case, ur is the last block, and there is no need for ur ’s block
≤
77
§3.3
Linked Lists
Figure 3.5: The three cases that occur during the removal of an item x in the
interior of an SEList. (This SEList has block size b = 3.)
to contain at least b
1 elements. Therefore, we proceed as above,
borrowing an element from ur to make an extra element in u0. If
this causes ur ’s block to become empty, then we remove it.
−
In this case, u0, . . . , ub
3. After b steps, we do not find any block containing more than b
1
1 is a sequence of b blocks that
elements.
−
1 elements. We gather these b(b
each contain b
1) elements into
1 blocks contains exactly b el-
u0, . . . , ub
ements and we remove ub
1, which is now empty. Now u0’s block
contains b elements and we can then remove the appropriate ele-
ment from it.
2 so that each of these b
−
−
−
−
−
−
SEList
T remove(int i) {
Location l = getLocation(i);
T y = l.u.d.get(l.j);
Node u = l.u;
int r = 0;
78
abcdef······acdefg······gabcdef···acdef···abcdef······acde······fSEList: A Space-Efficient Linked List
§3.3
while (r < b && u != dummy && u.d.size() == b-1) {
u = u.next;
r++;
}
if (r == b) { // b blocks each with b-1 elements
gather(l.u);
}
u = l.u;
u.d.remove(l.j);
while (u.d.size() < b-1 && u.next != dummy) {
u.d.add(u.next.d.remove(0));
u = u.next;
}
if (u.d.isEmpty()) remove(u);
n--;
return y;
}
Like the add(i, x) operation, the running time of the remove(i) opera-
/b) if we ignore the cost of the gather(u) method
tion is O(b + min
i, n
−
that occurs in Case 3.
{
i
}
3.3.5 Amortized Analysis of Spreading and Gathering
Next, we consider the cost of the gather(u) and spread(u) methods that
may be executed by the add(i, x) and remove(i) methods. For the sake of
completeness, here they are:
void spread(Node u) {
Node w = u;
for (int j = 0; j < b; j++) {
SEList
w = w.next;
}
w = addBefore(w);
while (w != u) {
while (w.d.size() < b)
w.d.add(0,w.prev.d.remove(w.prev.d.size()-1));
w = w.prev;
}
79
§3.3
}
Linked Lists
SEList
void gather(Node u) {
Node w = u;
for (int j = 0; j < b-1; j++) {
while (w.d.size() < b)
w.d.add(w.next.d.remove(0));
w = w.next;
}
remove(w);
}
The running time of each of these methods is dominated by the two
nested loops. Both the inner and outer loops execute at most b + 1 times,
so the total running time of each of these methods is O((b + 1)2) = O(b2).
However, the following lemma shows that these methods execute on at
most one out of every b calls to add(i, x) or remove(i).
Lemma 3.1. If an empty SEList is created and any sequence of m
1 calls
to add(i, x) and remove(i) is performed, then the total time spent during all
calls to spread() and gather() is O(bm).
≥
Proof. We will use the potential method of amortized analysis. We say
that a node u is fragile if u’s block does not contain b elements (so that u is
either the last node, or contains b
1 or b + 1 elements). Any node whose
block contains b elements is rugged. Define the potential of an SEList
as the number of fragile nodes it contains. We will consider only the
add(i, x) operation and its relation to the number of calls to spread(u).
The analysis of remove(i) and gather(u) is identical.
−
Notice that, if Case 1 occurs during the add(i, x) method, then only
one node, ur has the size of its block changed. Therefore, at most one
node, namely ur , goes from being rugged to being fragile. If Case 2 occurs,
then a new node is created, and this node is fragile, but no other node
changes size, so the number of fragile nodes increases by one. Thus, in
either Case 1 or Case 2 the potential of the SEList increases by at most
one.
80
SEList: A Space-Efficient Linked List
§3.3
Finally, if Case 3 occurs, it is because u0, . . . , ub
1 are all fragile nodes.
−
Then spread(u0) is called and these b fragile nodes are replaced with b+1
rugged nodes. Finally, x is added to u0’s block, making u0 fragile. In total
the potential decreases by b
1.
In summary, the potential starts at 0 (there are no nodes in the list).
Each time Case 1 or Case 2 occurs, the potential increases by at most 1.
Each time Case 3 occurs, the potential decreases by b
1. The poten-
tial (which counts the number of fragile nodes) is never less than 0. We
conclude that, for every occurrence of Case 3, there are at least b
1 oc-
currences of Case 1 or Case 2. Thus, for every call to spread(u) there are
at least b calls to add(i, x). This completes the proof.
−
−
−
3.3.6 Summary
The following theorem summarizes the performance of the SEList data
structure:
Theorem 3.3. An SEList implements the List interface. Ignoring the cost
of calls to spread(u) and gather(u), an SEList with block size b supports the
operations
• get(i) and set(i, x) in O(1 + min
i, n
{
• add(i, x) and remove(i) in O(b + min
{
i
−
}
i, n
/b) time per operation; and
/b) time per operation.
i
}
−
Furthermore, beginning with an empty SEList, any sequence of m add(i, x)
and remove(i) operations results in a total of O(bm) time spent during all
calls to spread(u) and gather(u).
The space (measured in words)1 used by an SEList that stores n elements
is n + O(b + n/b).
The SEList is a trade-off between an ArrayList and a DLList where
the relative mix of these two structures depends on the block size b. At
the extreme b = 2, each SEList node stores at most three values, which
is not much different than a DLList. At the other extreme, b > n, all
the elements are stored in a single array, just like in an ArrayList. In
between these two extremes lies a trade-off between the time it takes to
1Recall Section 1.4 for a discussion of how memory is measured.
81
§3.4
Linked Lists
add or remove a list item and the time it takes to locate a particular list
item.
3.4 Discussion and Exercises
Both singly-linked and doubly-linked lists are established techniques,
having been used in programs for over 40 years. They are discussed,
for example, by Knuth [46, Sections 2.2.3–2.2.5]. Even the SEList data
structure seems to be a well-known data structures exercise. The SEList
is sometimes referred to as an unrolled linked list [69].
Another way to save space in a doubly-linked list is to use so-called
XOR-lists. In an XOR-list, each node, u, contains only one pointer, called
u.nextprev, that holds the bitwise exclusive-or of u.prev and u.next. The
list itself needs to store two pointers, one to the dummy node and one to
dummy.next (the first node, or dummy if the list is empty). This technique
uses the fact that, if we have pointers to u and u.prev, then we can extract
u.next using the formula
u.next = u.prevˆu.nextprev .
(Here ˆ computes the bitwise exclusive-or of its two arguments.) This
technique complicates the code a little and is not possible in some lan-
guages, like Java and Python, that have garbage collection but gives a
doubly-linked list implementation that requires only one pointer per node.
See Sinha’s magazine article [70] for a detailed discussion of XOR-lists.
Exercise 3.1. Why is it not possible to use a dummy node in an SLList
to avoid all the special cases that occur in the operations push(x), pop(),
add(x), and remove()?
Exercise 3.2. Design and implement an SLList method, secondLast(),
that returns the second-last element of an SLList. Do this without using
the member variable, n, that keeps track of the size of the list.
Exercise 3.3. Implement the List operations get(i), set(i, x), add(i, x)
and remove(i) on an SLList. Each of these operations should run in O(1+
i) time.
82
Discussion and Exercises
§3.4
Exercise 3.4. Design and implement an SLList method, reverse() that
reverses the order of elements in an SLList. This method should run in
O(n) time, should not use recursion, should not use any secondary data
structures, and should not create any new nodes.
Exercise 3.5. Design and implement SLList and DLList methods called
checkSize(). These methods walk through the list and count the number
of nodes to see if this matches the value, n, stored in the list. These meth-
ods return nothing, but throw an exception if the size they compute does
not match the value of n.
Exercise 3.6. Try to recreate the code for the addBefore(w) operation that
creates a node, u, and adds it in a DLList just before the node w. Do not
refer to this chapter. Even if your code does not exactly match the code
given in this book it may still be correct. Test it and see if it works.
The next few exercises involve performing manipulations on DLLists.
You should complete them without allocating any new nodes or tempo-
rary arrays. They can all be done only by changing the prev and next
values of existing nodes.
Exercise 3.7. Write a DLList method isPalindrome() that returns true
if the list is a palindrome, i.e., the element at position i is equal to the
element at position n
. Your code should run
in O(n) time.
1 for all i
0, . . . , n
1
}
∈ {
−
−
−
i
Exercise 3.8. Implement a method rotate(r) that “rotates” a DLList so
that list item i becomes list item (i + r) mod n. This method should run
in O(1 + min
) time and should not modify any nodes in the list.
r, n
r
}
−
{
Exercise 3.9. Write a method, truncate(i), that truncates a DLList at
position i. After executing this method, the size of the list will be i and
it should contain only the elements at indices 0, . . . , i
1. The return value
is another DLList that contains the elements at indices i, . . . , n
1. This
i, n
method should run in O(min
{
i
}
Exercise 3.10. Write a DLList method, absorb(l2), that takes as an ar-
gument a DLList, l2, empties it and appends its contents, in order, to
the receiver. For example, if l1 contains a, b, c and l2 contains d, e, f ,
) time.
−
−
−
83
§3.4
Linked Lists
then after calling l1.absorb(l2), l1 will contain a, b, c, d, e, f and l2 will
be empty.
Exercise 3.11. Write a method deal() that removes all the elements with
odd-numbered indices from a DLList and return a DLList containing
these elements. For example, if l1, contains the elements a, b, c, d, e, f ,
then after calling l1.deal(), l1 should contain a, c, e and a list containing
b, d, f should be returned.
Exercise 3.12. Write a method, reverse(), that reverses the order of ele-
ments in a DLList.
Exercise 3.13. This exercise walks you through an implementation of the
merge-sort algorithm for sorting a DLList, as discussed in Section 11.1.1.
In your implementation, perform comparisons between elements using
the compareTo(x) method so that the resulting implementation can sort
any DLList containing elements that implement the Comparable inter-
face.
1. Write a DLList method called takeFirst(l2). This method takes
the first node from l2 and appends it to the the receiving list. This
is equivalent to add(size(), l2.remove(0)), except that it should not
create a new node.
2. Write a DLList static method, merge(l1, l2), that takes two sorted
lists l1 and l2, merges them, and returns a new sorted list contain-
ing the result. This causes l1 and l2 to be emptied in the proces.
For example, if l1 contains a, c, d and l2 contains b, e, f , then this
method returns a new list containing a, b, c, d, e, f .
3. Write a DLList method sort() that sorts the elements contained in
the list using the merge sort algorithm. This recursive algorithm
works in the following way:
(a) If the list contains 0 or 1 elements then there is nothing to do.
Otherwise,
(b) Using the truncate(size()/2) method, split the list into two
lists of approximately equal length, l1 and l2;
(c) Recursively sort l1;
84
Discussion and Exercises
§3.4
(d) Recursively sort l2; and, finally,
(e) Merge l1 and l2 into a single sorted list.
The next few exercises are more advanced and require a clear under-
standing of what happens to the minimum value stored in a Stack or
Queue as items are added and removed.
Exercise 3.14. Design and implement a MinStack data structure that can
store comparable elements and supports the stack operations push(x),
pop(), and size(), as well as the min() operation, which returns the mini-
mum value currently stored in the data structure. All operations should
run in constant time.
Exercise 3.15. Design and implement a MinQueue data structure that can
store comparable elements and supports the queue operations add(x),
remove(), and size(), as well as the min() operation, which returns the
minimum value currently stored in the data structure. All operations
should run in constant amortized time.
Exercise 3.16. Design and implement a MinDeque data structure that
can store comparable elements and supports all the deque operations
addFirst(x), addLast(x) removeFirst(), removeLast() and size(), and
the min() operation, which returns the minimum value currently stored
in the data structure. All operations should run in constant amortized
time.
The next exercises are designed to test the reader’s understanding of
the implementation and analysis of the space-efficient SEList:
Exercise 3.17. Prove that, if an SEList is used like a Stack (so that the
add(size(), x)
only modifications to the SEList are done using push(x)
and pop()
1)), then these operations run in constant
amortized time, independent of the value of b.
remove(size()
≡
≡
−
Exercise 3.18. Design and implement of a version of an SEList that sup-
ports all the Deque operations in constant amortized time per operation,
independent of the value of b.
Exercise 3.19. Explain how to use the bitwise exclusive-or operator, ˆ, to
swap the values of two int variables without using a third variable.
85
Chapter 4
Skiplists
In this chapter, we discuss a beautiful data structure: the skiplist, which
has a variety of applications. Using a skiplist we can implement a List
that has O(log n) time implementations of get(i), set(i, x), add(i, x), and
remove(i). We can also implement an SSet in which all operations run in
O(log n) expected time.
The efficiency of skiplists relies on their use of randomization. When
a new element is added to a skiplist, the skiplist uses random coin tosses
to determine the height of the new element. The performance of skiplists
is expressed in terms of expected running times and path lengths. This
expectation is taken over the random coin tosses used by the skiplist. In
the implementation, the random coin tosses used by a skiplist are simu-
lated using a pseudo-random number (or bit) generator.
4.1 The Basic Structure
Conceptually, a skiplist is a sequence of singly-linked lists L0, . . . , Lh. Each
list Lr contains a subset of the items in Lr
1. We start with the input list
L0 that contains n items and construct L1 from L0, L2 from L1, and so on.
The items in Lr are obtained by tossing a coin for each element, x, in Lr
1
−
and including x in Lr if the coin turns up as heads. This process ends
when we create a list Lr that is empty. An example of a skiplist is shown
in Figure 4.1.
−
For an element, x, in a skiplist, we call the height of x the largest value
87
§4.1
Skiplists
Figure 4.1: A skiplist containing seven elements.
r such that x appears in Lr . Thus, for example, elements that only appear
in L0 have height 0. If we spend a few moments thinking about it, we
notice that the height of x corresponds to the following experiment: Toss
a coin repeatedly until it comes up as tails. How many times did it come
up as heads? The answer, not surprisingly, is that the expected height of
a node is 1. (We expect to toss the coin twice before getting tails, but we
don’t count the last toss.) The height of a skiplist is the height of its tallest
node.
At the head of every list is a special node, called the sentinel, that acts
as a dummy node for the list. The key property of skiplists is that there is
a short path, called the search path, from the sentinel in Lh to every node
in L0. Remembering how to construct a search path for a node, u, is easy
(see Figure 4.2) : Start at the top left corner of your skiplist (the sentinel
in Lh) and always go right unless that would overshoot u, in which case
you should take a step down into the list below.
More precisely, to construct the search path for the node u in L0, we
start at the sentinel, w, in Lh. Next, we examine w.next. If w.next contains
an item that appears before u in L0, then we set w = w.next. Otherwise,
we move down and continue the search at the occurrence of w in the list
1. We continue this way until we reach the predecessor of u in L0.
Lh
−
The following result, which we will prove in Section 4.4, shows that
the search path is quite short:
Lemma 4.1. The expected length of the search path for any node, u, in L0 is
at most 2 log n + O(1) = O(log n).
A space-efficient way to implement a skiplist is to define a Node, u,
88
0123456sentinelL0L1L2L3L4L5The Basic Structure
§4.1
Figure 4.2: The search path for the node containing 4 in a skiplist.
as consisting of a data value, x, and an array, next, of pointers, where
u.next[i] points to u’s successor in the list Li. In this way, the data, x, in
a node is referenced only once, even though x may appear in several lists.
SkiplistSSet
class Node<T> {
T x;
Node<T>[] next;
Node(T ix, int h) {
x = ix;
next = Array.newInstance(Node.class, h+1);
}
int height() {
return next.length - 1;
}
}
The next two sections of this chapter discuss two different applica-
tions of skiplists. In each of these applications, L0 stores the main struc-
ture (a list of elements or a sorted set of elements). The primary difference
between these structures is in how a search path is navigated; in partic-
ular, they differ in how they decide if a search path should go down into
Lr
1 or go right within Lr .
−
89
0123456sentinelL0L1L2L3L4L5§4.2
Skiplists
4.2 SkiplistSSet: An Efficient SSet
A SkiplistSSet uses a skiplist structure to implement the SSet interface.
When used in this way, the list L0 stores the elements of the SSet in sorted
order. The find(x) method works by following the search path for the
smallest value y such that y
x:
≥
SkiplistSSet
Node<T> findPredNode(T x) {
Node<T> u = sentinel;
int r = h;
while (r >= 0) {
while (u.next[r] != null && compare(u.next[r].x,x) < 0)
// go right in list r
// go down into list r-1
u = u.next[r];
r--;
}
return u;
}
T find(T x) {
Node<T> u = findPredNode(x);
return u.next[0] == null ? null : u.next[0].x;
}
Following the search path for y is easy: when situated at some node, u,
in Lr, we look right to u.next[r].x. If x > u.next[r].x, then we take a step
1. Each step (right
to the right in Lr; otherwise, we move down into Lr
−
or down) in this search takes only constant time; thus, by Lemma 4.1, the
expected running time of find(x) is O(log n).
Before we can add an element to a SkipListSSet, we need a method
to simulate tossing coins to determine the height, k, of a new node. We do
so by picking a random integer, z, and counting the number of trailing 1s
in the binary representation of z:1
int pickHeight() {
int z = rand.nextInt();
SkiplistSSet
1This method does not exactly replicate the coin-tossing experiment since the value of k
will always be less than the number of bits in an int. However, this will have negligible im-
pact unless the number of elements in the structure is much greater than 232 = 4294967296.
90
SkiplistSSet: An Efficient SSet
§4.2
int k = 0;
int m = 1;
while ((z & m) != 0) {
k++;
m <<= 1;
}
return k;
}
To implement the add(x) method in a SkiplistSSet we search for x
and then splice x into a few lists L0,. . . ,Lk, where k is selected using the
pickHeight() method. The easiest way to do this is to use an array, stack,
that keeps track of the nodes at which the search path goes down from
1. More precisely, stack[r] is the node in Lr where
some list Lr into Lr
−
the search path proceeded down into Lr
1. The nodes that we modify to
insert x are precisely the nodes stack[0], . . . , stack[k]. The following code
implements this algorithm for add(x):
−
SkiplistSSet
boolean add(T x) {
Node<T> u = sentinel;
int r = h;
int comp = 0;
while (r >= 0) {
while (u.next[r] != null
&& (comp = compare(u.next[r].x,x)) < 0)
u = u.next[r];
if (u.next[r] != null && comp == 0) return false;
stack[r--] = u;
// going down, store u
}
Node<T> w = new Node<T>(x, pickHeight());
while (h < w.height())
stack[++h] = sentinel;
// height increased
for (int i = 0; i < w.next.length; i++) {
w.next[i] = stack[i].next[i];
stack[i].next[i] = w;
}
n++;
return true;
}
91
§4.2
Skiplists
Figure 4.3: Adding the node containing 3.5 to a skiplist. The nodes stored in
stack are highlighted.
Removing an element, x, is done in a similar way, except that there is
no need for stack to keep track of the search path. The removal can be
done as we are following the search path. We search for x and each time
the search moves downward from a node u, we check if u.next.x = x and
if so, we splice u out of the list:
SkiplistSSet
boolean remove(T x) {
boolean removed = false;
Node<T> u = sentinel;
int r = h;
int comp = 0;
while (r >= 0) {
while (u.next[r] != null
&& (comp = compare(u.next[r].x, x)) < 0) {
u = u.next[r];
}
if (u.next[r] != null && comp == 0) {
removed = true;
u.next[r] = u.next[r].next[r];
if (u == sentinel && u.next[r] == null)
h--; // height has gone down
}
r--;
}
if (removed) n--;
return removed;
}
92
0123456sentinel3.5add(3.5)SkiplistList: An Efficient Random-Access List
§4.3
Figure 4.4: Removing the node containing 3 from a skiplist.
4.2.1 Summary
The following theorem summarizes the performance of skiplists when
used to implement sorted sets:
Theorem 4.1. SkiplistSSet implements the SSet interface. A SkiplistS-
Set supports the operations add(x), remove(x), and find(x) in O(log n) ex-
pected time per operation.
4.3 SkiplistList: An Efficient Random-Access List
A SkiplistList implements the List interface using a skiplist structure.
In a SkiplistList, L0 contains the elements of the list in the order in
which they appear in the list. As in a SkiplistSSet, elements can be
added, removed, and accessed in O(log n) time.
For this to be possible, we need a way to follow the search path for the
ith element in L0. The easiest way to do this is to define the notion of the
length of an edge in some list, Lr. We define the length of every edge in
L0 as 1. The length of an edge, e, in Lr, r > 0, is defined as the sum of the
lengths of the edges below e in Lr
1. Equivalently, the length of e is the
−
number of edges in L0 below e. See Figure 4.5 for an example of a skiplist
with the lengths of its edges shown. Since the edges of skiplists are stored
in arrays, the lengths can be stored the same way:
class Node {
SkiplistList
93
012456sentinelremove(3)3§4.3
Skiplists
Figure 4.5: The lengths of the edges in a skiplist.
T x;
Node[] next;
int[] length;
Node(T ix, int h) {
x = ix;
next = Array.newInstance(Node.class, h+1);
length = new int[h+1];
}
int height() {
return next.length - 1;
}
}
The useful property of this definition of length is that, if we are cur-
rently at a node that is at position j in L0 and we follow an edge of length
(cid:96), then we move to a node whose position, in L0, is j + (cid:96). In this way,
while following a search path, we can keep track of the position, j, of the
current node in L0. When at a node, u, in Lr, we go right if j plus the
length of the edge u.next[r] is less than i. Otherwise, we go down into
Lr
1.
−
SkiplistList
Node findPred(int i) {
Node u = sentinel;
int r = h;
int j = -1;
while (r >= 0) {
// index of the current node in list 0
while (u.next[r] != null && j + u.length[r] < i) {
j += u.length[r];
94
0123456sentinelL0L1L2L3L4L51111111311111133255SkiplistList: An Efficient Random-Access List
§4.3
u = u.next[r];
}
r--;
}
return u;
}
SkiplistList
T get(int i) {
return findPred(i).next[0].x;
}
T set(int i, T x) {
Node u = findPred(i).next[0];
T y = u.x;
u.x = x;
return y;
}
Since the hardest part of the operations get(i) and set(i, x) is finding
the ith node in L0, these operations run in O(log n) time.
Adding an element to a SkiplistList at a position, i, is fairly simple.
Unlike in a SkiplistSSet, we are sure that a new node will actually be
added, so we can do the addition at the same time as we search for the
new node’s location. We first pick the height, k, of the newly inserted
node, w, and then follow the search path for i. Any time the search path
moves down from Lr with r
k, we splice w into Lr. The only extra care
needed is to ensure that the lengths of edges are updated properly. See
Figure 4.6.
≤
Note that, each time the search path goes down at a node, u, in Lr,
the length of the edge u.next[r] increases by one, since we are adding an
element below that edge at position i. Splicing the node w between two
nodes, u and z, works as shown in Figure 4.7. While following the search
path we are already keeping track of the position, j, of u in L0. Therefore,
we know that the length of the edge from u to w is i
j. We can also
deduce the length of the edge from w to z from the length, (cid:96), of the edge
from u to z. Therefore, we can splice in w and update the lengths of the
edges in constant time.
−
95
§4.3
Skiplists
Figure 4.6: Adding an element to a SkiplistList.
Figure 4.7: Updating the lengths of edges while splicing a node w into a skiplist.
This sounds more complicated than it is, for the code is actually quite
simple:
SkiplistList
void add(int i, T x) {
Node w = new Node(x, pickHeight());
if (w.height() > h)
h = w.height();
add(i, w);
}
SkiplistList
Node add(int i, Node w) {
Node u = sentinel;
int k = w.height();
int r = h;
int j = -1; // index of u
while (r >= 0) {
96
0123456sentinelxadd(4,x)111112111133356561111112112123211j‘ujuwii−j‘+1−(i−j)‘+1zzSkiplistList: An Efficient Random-Access List
§4.3
Figure 4.8: Removing an element from a SkiplistList.
while (u.next[r] != null && j+u.length[r] < i) {
j += u.length[r];
u = u.next[r];
}
u.length[r]++; // accounts for new node in list 0
if (r <= k) {
w.next[r] = u.next[r];
u.next[r] = w;
w.length[r] = u.length[r] - (i - j);
u.length[r] = i - j;
}
r--;
}
n++;
return u;
}
By now, the implementation of the remove(i) operation in a Skip-
listList should be obvious. We follow the search path for the node at
position i. Each time the search path takes a step down from a node, u,
at level r we decrement the length of the edge leaving u at that level. We
also check if u.next[r] is the element of rank i and, if so, splice it out of
the list at that level. An example is shown in Figure 4.8.
SkiplistList
T remove(int i) {
T x = null;
Node u = sentinel;
int r = h;
97
012456sentinelL0L1L2L3L4L51111131133215454remove(3)3111111111§4.4
Skiplists
int j = -1; // index of node u
while (r >= 0) {
while (u.next[r] != null && j+u.length[r] < i) {
j += u.length[r];
u = u.next[r];
}
u.length[r]--; // for the node we are removing
if (j + u.length[r] + 1 == i && u.next[r] != null) {
x = u.next[r].x;
u.length[r] += u.next[r].length[r];
u.next[r] = u.next[r].next[r];
if (u == sentinel && u.next[r] == null)
h--;
}
r--;
}
n--;
return x;
}
4.3.1 Summary
The following theorem summarizes the performance of the Skiplist-
List data structure:
Theorem 4.2. A SkiplistList implements the List interface. A Skip-
listList supports the operations get(i), set(i, x), add(i, x), and remove(i)
in O(log n) expected time per operation.
4.4 Analysis of Skiplists
In this section, we analyze the expected height, size, and length of the
search path in a skiplist. This section requires a background in basic
probability. Several proofs are based on the following basic observation
about coin tosses.
Lemma 4.2. Let T be the number of times a fair coin is tossed up to and
including the first time the coin comes up heads. Then E[T ] = 2.
98
Analysis of Skiplists
§4.4
Proof. Suppose we stop tossing the coin the first time it comes up heads.
Define the indicator variable
0 if the coin is tossed less than i times
1 if the coin is tossed i or more times
Ii =
(cid:40)
Note that Ii = 1 if and only if the first i
1 coin tosses are tails, so E[Ii] =
1. Observe that T , the total number of coin tosses, can
= 1/2i
Ii = 1
Pr
−
}
{
be written as T =
∞i=1 Ii. Therefore,
−
(cid:80)
E[T ] = E
∞
Ii
(cid:88)i=1
E [Ii]
1/2i
1
−
∞
(cid:88)i=1
∞
=
=
(cid:88)i=1
= 1 + 1/2 + 1/4 + 1/8 +
= 2 .
· · ·
The next two lemmata tell us that skiplists have linear size:
Lemma 4.3. The expected number of nodes in a skiplist containing n ele-
ments, not including occurrences of the sentinel, is 2n.
Proof. The probability that any particular element, x, is included in list
Lr is 1/2r, so the expected number of nodes in Lr is n/2r.2 Therefore, the
total expected number of nodes in all lists is
n/2r = n(1 + 1/2 + 1/4 + 1/8 +
) = 2n .
· · ·
∞
r=0
(cid:88)
Lemma 4.4. The expected height of a skiplist containing n elements is at most
log n + 2.
Proof. For each r
∈ {
1, 2, 3, . . . ,
, define the indicator random variable
∞}
0 if Lr is empty
1 if Lr is non-empty
Ir =
(cid:40)
2See Section 1.3.4 to see how this is derived using indicator variables and linearity of
expectation.
99
§4.4
Skiplists
The height, h, of the skiplist is then given by
∞
h =
Ir .
(cid:88)r=1
Note that Ir is never more than the length,
Therefore, we have
E[Ir]
E[
Lr
|
≤
E[h] = E
∞
Ir
, of Lr, so
Lr
|
|
] = n/2r .
|
∞
(cid:88)r=1
E[Ir]
r=1
(cid:88)
log n
(cid:98)
(cid:99)
E[Ir] +
∞
E[Ir]
+1
(cid:99)
log n
(cid:88)r=
(cid:98)
∞
n/2r
(cid:99)
1 +
r=1
(cid:88)
log n
(cid:98)
r=1
(cid:88)
log n
(cid:88)r=
+1
(cid:98)
(cid:99)
∞
1/2r
log n +
r=0
(cid:88)
= log n + 2 .
=
=
≤
≤
Lemma 4.5. The expected number of nodes in a skiplist containing n ele-
ments, including all occurrences of the sentinel, is 2n + O(log n).
Proof. By Lemma 4.3, the expected number of nodes, not including the
sentinel, is 2n. The number of occurrences of the sentinel is equal to
the height, h, of the skiplist so, by Lemma 4.4 the expected number of
occurrences of the sentinel is at most log n + 2 = O(log n).
Lemma 4.6. The expected length of a search path in a skiplist is at most
2 log n + O(1).
Proof. The easiest way to see this is to consider the reverse search path for
a node, x. This path starts at the predecessor of x in L0. At any point in
100
Analysis of Skiplists
§4.4
time, if the path can go up a level, then it does. If it cannot go up a level
then it goes left. Thinking about this for a few moments will convince
us that the reverse search path for x is identical to the search path for x,
except that it is reversed.
The number of nodes that the reverse search path visits at a particular
level, r, is related to the following experiment: Toss a coin. If the coin
comes up as heads, then move up and stop. Otherwise, move left and
repeat the experiment. The number of coin tosses before the heads rep-
resents the number of steps to the left that a reverse search path takes at
a particular level.3 Lemma 4.2 tells us that the expected number of coin
tosses before the first heads is 1.
Let Sr denote the number of steps the forward search path takes at
1. Further-
, since we can’t take more steps in Lr than the length of Lr,
level r that go to the right. We have just argued that E[Sr]
more, Sr
so
≤ |
Lr
≤
|
E[Sr]
E[
Lr
|
We can now finish as in the proof of Lemma 4.4. Let S be the length of
the search path for some node, u, in a skiplist, and let h be the height of
the skiplist. Then
≤
] = n/2r .
|
E[S] = E
h +
∞
Sr
= E[h] +
r=0
(cid:88)
∞
E[Sr]
r=0
(cid:88)
log n
(cid:98)
(cid:99)
r=0
(cid:88)
log n
(cid:98)
(cid:99)
r=0
(cid:88)
log n
(cid:98)
(cid:99)
E[Sr] +
∞
E[Sr]
r=
(cid:98)
log n
(cid:88)
(cid:99)
+1
∞
n/2r
+1
(cid:99)
log n
(cid:88)r=
(cid:98)
∞
1/2r
1 +
1 +
r=0
(cid:88)
r=0
(cid:88)
= E[h] +
E[h] +
E[h] +
≤
≤
3Note that this might overcount the number of steps to the left, since the experiment
should end either at the first heads or when the search path reaches the sentinel, whichever
comes first. This is not a problem since the lemma is only stating an upper bound.
101
§4.5
Skiplists
log n
(cid:99)
(cid:98)
E[h] +
1 +
∞
1/2r
r=0
(cid:88)
E[h] + log n + 3
r=0
(cid:88)
2 log n + 5 .
≤
≤
≤
The following theorem summarizes the results in this section:
Theorem 4.3. A skiplist containing n elements has expected size O(n) and
the expected length of the search path for any particular element is at most
2 log n + O(1).
4.5 Discussion and Exercises
Skiplists were introduced by Pugh [62] who also presented a number of
applications and extensions of skiplists [61]. Since then they have been
studied extensively. Several researchers have done very precise analyses
of the expected length and variance of the length of the search path for
the ith element in a skiplist [45, 44, 58]. Deterministic versions [53], bi-
ased versions [8, 26], and self-adjusting versions [12] of skiplists have all
been developed. Skiplist implementations have been written for various
languages and frameworks and have been used in open-source database
systems [71, 63]. A variant of skiplists is used in the HP-UX operating
system kernel’s process management structures [42]. Skiplists are even
part of the Java 1.6 API [55].
Exercise 4.1. Illustrate the search paths for 2.5 and 5.5 on the skiplist in
Figure 4.1.
Exercise 4.2. Illustrate the addition of the values 0.5 (with a height of 1)
and then 3.5 (with a height of 2) to the skiplist in Figure 4.1.
Exercise 4.3. Illustrate the removal of the values 1 and then 3 from the
skiplist in Figure 4.1.
Exercise 4.4. Illustrate the execution of remove(2) on the SkiplistList
in Figure 4.5.
102
Discussion and Exercises
§4.5
Exercise 4.5. Illustrate the execution of add(3, x) on the SkiplistList in
Figure 4.5. Assume that pickHeight() selects a height of 4 for the newly
created node.
Exercise 4.6. Show that, during an add(x) or a remove(x) operation, the
expected number of pointers in a SkiplistSet that get changed is con-
stant.
Exercise 4.7. Suppose that, instead of promoting an element from Li
1
−
into Li based on a coin toss, we promote it with some probability p, 0 <
p < 1.
1. Show that, with this modification, the expected length of a search
path is at most (1/p) log1/p n + O(1).
2. What is the value of p that minimizes the preceding expression?
3. What is the expected height of the skiplist?
4. What is the expected number of nodes in the skiplist?
Exercise 4.8. The find(x) method in a SkiplistSet sometimes performs
redundant comparisons; these occur when x is compared to the same value
more than once. They can occur when, for some node, u, u.next[r] =
u.next[r
1]. Show how these redundant comparisons happen and mod-
ify find(x) so that they are avoided. Analyze the expected number of
comparisons done by your modified find(x) method.
−
Exercise 4.9. Design and implement a version of a skiplist that imple-
ments the SSet interface, but also allows fast access to elements by rank.
That is, it also supports the function get(i), which returns the element
whose rank is i in O(log n) expected time. (The rank of an element x in
an SSet is the number of elements in the SSet that are less than x.)
Exercise 4.10. A finger in a skiplist is an array that stores the sequence
of nodes on a search path at which the search path goes down. (The vari-
able stack in the add(x) code on page 91 is a finger; the shaded nodes in
Figure 4.3 show the contents of the finger.) One can think of a finger as
pointing out the path to a node in the lowest list, L0.
103
§4.5
Skiplists
A finger search implements the find(x) operation using a finger, by
walking up the list using the finger until reaching a node u such that
u.x < x and u.next = null or u.next.x > x and then performing a normal
search for x starting from u.
It is possible to prove that the expected
number of steps required for a finger search is O(1 + log r), where r is the
number values in L0 between x and the value pointed to by the finger.
Implement a subclass of Skiplist called SkiplistWithFinger that
implements find(x) operations using an internal finger. This subclass
stores a finger, which is then used so that every find(x) operation is im-
plemented as a finger search. During each find(x) operation the finger is
updated so that each find(x) operation uses, as a starting point, a finger
that points to the result of the previous find(x) operation.
Exercise 4.11. Write a method, truncate(i), that truncates a Skiplist-
List at position i. After the execution of this method, the size of the
list is i and it contains only the elements at indices 0, . . . , i
1. The re-
turn value is another SkiplistList that contains the elements at indices
i, . . . , n
1. This method should run in O(log n) time.
−
−
Exercise 4.12. Write a SkiplistList method, absorb(l2), that takes as
an argument a SkiplistList, l2, empties it and appends its contents, in
order, to the receiver. For example, if l1 contains a, b, c and l2 contains
d, e, f , then after calling l1.absorb(l2), l1 will contain a, b, c, d, e, f and l2
will be empty. This method should run in O(log n) time.
Exercise 4.13. Using the ideas from the space-efficient list, SEList, de-
sign and implement a space-efficient SSet, SESSet. To do this, store
the data, in order, in an SEList, and store the blocks of this SEList in
an SSet. If the original SSet implementation uses O(n) space to store
n elements, then the SESSet will use enough space for n elements plus
O(n/b + b) wasted space.
Exercise 4.14. Using an SSet as your underlying structure, design and
implement an application that reads a (large) text file and allows you to
search, interactively, for any substring contained in the text. As the user
types their query, a matching part of the text (if any) should appear as a
result.
104
Discussion and Exercises
§4.5
Hint 1: Every substring is a prefix of some suffix, so it suffices to store all
suffixes of the text file.
Hint 2: Any suffix can be represented compactly as a single integer indi-
cating where the suffix begins in the text.
Test your application on some large texts, such as some of the books
available at Project Gutenberg [1]. If done correctly, your applications
will be very responsive; there should be no noticeable lag between typing
keystrokes and seeing the results.
Exercise 4.15. (This exercise should be done after reading about binary
search trees, in Section 6.2.) Compare skiplists with binary search trees
in the following ways:
1. Explain how removing some edges of a skiplist leads to a structure
that looks like a binary tree and is similar to a binary search tree.
2. Skiplists and binary search trees each use about the same number
of pointers (2 per node). Skiplists make better use of those pointers,
though. Explain why.
105
Chapter 5
Hash Tables
Hash tables are an efficient method of storing a small number, n, of inte-
. The term hash table includes a
gers from a large range U =
broad range of data structures. The first part of this chapter focuses on
two of the most common implementations of hash tables: hashing with
chaining and linear probing.
0, . . . , 2w
−
1
}
{
Very often hash tables store types of data that are not integers. In this
case, an integer hash code is associated with each data item and is used in
the hash table. The second part of this chapter discusses how such hash
codes are generated.
Some of the methods used in this chapter require random choices of
integers in some specific range. In the code samples, some of these “ran-
dom” integers are hard-coded constants. These constants were obtained
using random bits generated from atmospheric noise.
5.1 ChainedHashTable: Hashing with Chaining
A ChainedHashTable data structure uses hashing with chaining to store
data as an array, t, of lists. An integer, n, keeps track of the total number
of items in all lists (see Figure 5.1):
List<T>[] t;
int n;
ChainedHashTable
107
§5.1
Hash Tables
Figure 5.1: An example of a ChainedHashTable with n = 14 and t.length = 16.
In this example hash(x) = 6
The hash value of a data item x, denoted hash(x) is a value in the range
. All items with hash value i are stored in the list at
}
0, . . . , t.length
{
t[i]. To ensure that lists don’t get too long, we maintain the invariant
−
1
t.length
n
≤
≤
so that the average number of elements stored in one of these lists is
n/t.length
1.
To add an element, x, to the hash table, we first check if the length of
t needs to be increased and, if so, we grow t. With this out of the way
we hash x to get an integer, i, in the range
, and we
append x to the list t[i]:
0, . . . , t.length
1
}
−
{
ChainedHashTable
boolean add(T x) {
if (find(x) != null) return false;
if (n+1 > t.length) resize();
t[hash(x)].add(x);
n++;
return true;
}
Growing the table, if necessary, involves doubling the length of t and
reinserting all elements into the new table. This strategy is exactly the
same as the one used in the implementation of ArrayStack and the same
result applies: The cost of growing is only constant when amortized over
a sequence of insertions (see Lemma 2.1 on page 33).
108
bdcixhjgafme‘k0123456789101112131415tChainedHashTable: Hashing with Chaining
§5.1
Besides growing, the only other work done when adding a new value
x to a ChainedHashTable involves appending x to the list t[hash(x)]. For
any of the list implementations described in Chapters 2 or 3, this takes
only constant time.
To remove an element, x, from the hash table, we iterate over the list
t[hash(x)] until we find x so that we can remove it:
ChainedHashTable
T remove(T x) {
Iterator<T> it = t[hash(x)].iterator();
while (it.hasNext()) {
T y = it.next();
if (y.equals(x)) {
it.remove();
n--;
return y;
}
}
return null;
}
This takes O(nhash(x)) time, where ni denotes the length of the list
stored at t[i].
Searching for the element x in a hash table is similar. We perform a
linear search on the list t[hash(x)]:
ChainedHashTable
T find(Object x) {
for (T y : t[hash(x)])
if (y.equals(x))
return y;
return null;
}
Again, this takes time proportional to the length of the list t[hash(x)].
The performance of a hash table depends critically on the choice of
the hash function. A good hash function will spread the elements evenly
among the t.length lists, so that the expected size of the list t[hash(x)] is
O(n/t.length) = O(1). On the other hand, a bad hash function will hash
109
§5.1
Hash Tables
all values (including x) to the same table location, in which case the size
of the list t[hash(x)] will be n. In the next section we describe a good hash
function.
5.1.1 Multiplicative Hashing
Multiplicative hashing is an efficient method of generating hash values
based on modular arithmetic (discussed in Section 2.3) and integer divi-
sion. It uses the div operator, which calculates the integral part of a quo-
0
tient, while discarding the remainder. Formally, for any integers a
and b
1, a div b =
a/b
≥
In multiplicative hashing, we use a hash table of size 2d for some in-
.
(cid:99)
(cid:98)
≥
teger d (called the dimension). The formula for hashing an integer x
0, . . . , 2w
{
1
}
is
−
∈
hash(x) = ((z
x) mod 2w) div 2w
{
1
−
Here, z is a randomly chosen odd integer in
. This hash func-
}
tion can be realized very efficiently by observing that, by default, oper-
ations on integers are already done modulo 2w where w is the number of
bits in an integer.1 (See Figure 5.2.) Furthermore, integer division by 2w
d
−
d bits in a binary representa-
is equivalent to dropping the rightmost w
tion (which is implemented by shifting the bits right by w
d using the
>>> operator). In this way, the code that implements the above formula is
simpler than the formula itself:
−
−
·
d .
−
1, . . . , 2w
ChainedHashTable
int hash(Object x) {
return (z * x.hashCode()) >>> (w-d);
}
The following lemma, whose proof is deferred until later in this sec-
tion, shows that multiplicative hashing does a good job of avoiding colli-
sions:
hash(x) = hash(y)
Lemma 5.1. Let x and y be any two values in
Pr
{
1This is true for most programming languages including C, C#, C++, and Java. Notable
exceptions are Python and Ruby, in which the result of a fixed-length w-bit integer operation
that overflows is upgraded to a variable-length representation.
2/2d.
with x (cid:44) y. Then
0, . . . , 2w
{
} ≤
−
1
}
110
ChainedHashTable: Hashing with Chaining
§5.1
2w (4294967296)
z (4102541685)
x (42)
x
z
·
x) mod 2w
(z
·
x) mod 2w) div 2w
((z
·
d
−
100000000000000000000000000000000
11110100100001111101000101110101
00000000000000000000000000101010
10100000011110010010000101110100110010
00011110010010000101110100110010
00011110
Figure 5.2: The operation of the multiplicative hash function with w = 32 and
d = 8.
With Lemma 5.1, the performance of remove(x), and find(x) are easy
to analyze:
Lemma 5.2. For any data value x, the expected length of the list t[hash(x)]
is at most nx + 2, where nx is the number of occurrences of x in the hash table.
Proof. Let S be the (multi-)set of elements stored in the hash table that
are not equal to x. For an element y
S, define the indicator variable
∈
1 if hash(x) = hash(y)
0 otherwise
Iy =
(cid:40)
and notice that, by Lemma 5.1, E[Iy]
≤
length of the list t[hash(x)] is given by
2/2d = 2/t.length. The expected
E [t[hash(x)].size()] = E
nx +
Iy
= nx +
y
S
(cid:88)
∈
E[Iy]
nx +
2/t.length
y
S
(cid:88)
∈
y
S
(cid:88)
∈
nx +
2/n
y
S
(cid:88)
∈
nx + (n
−
nx + 2 ,
nx)2/n
as required.
≤
≤
≤
≤
111
§5.1
Hash Tables
Now, we want to prove Lemma 5.1, but first we need a result from
number theory. In the following proof, we use the notation (br , . . . , b0)2
r
i=0 bi2i, where each bi is a bit, either 0 or 1. In other words,
to denote
(br , . . . , b0)2 is the integer whose binary representation is given by br , . . . , b0.
We use (cid:63) to denote a bit of unknown value.
(cid:80)
Lemma 5.3. Let S be the set of odd integers in
1
−
be any two elements in S. Then there is exactly one value z
zq mod 2w = i.
{
1, . . . , 2w
; let q and i
}
S such that
∈
Proof. Since the number of choices for z and i is the same, it is sufficient
S that satisfies zq mod 2w = i.
to prove that there is at most one value z
Suppose, for the sake of contradiction, that there are two such values
∈
z and z(cid:48), with z > z(cid:48). Then
zq mod 2w = z(cid:48)q mod 2w = i
So
But this means that
z(cid:48))q mod 2w = 0
(z
−
−
for some integer k. Thinking in terms of binary numbers, we have
(z
z(cid:48))q = k2w
(5.1)
z(cid:48))q = k
(z
−
·
(1, 0, . . . , 0
)2 ,
w
so that the w trailing bits in the binary representation of (z
0’s.
(cid:125)
(cid:123)(cid:122)
(cid:124)
(cid:32)
(cid:32)
−
z(cid:48))q are all
Furthermore k (cid:44) 0, since q (cid:44) 0 and z
trailing 0’s in its binary representation:
z(cid:48)
−
(cid:44) 0. Since q is odd, it has no
q = ((cid:63), . . . , (cid:63), 1)2 .
Since
−
sentation:
z
|
z(cid:48)|
< 2w, z
−
z(cid:48) has fewer than w trailing 0’s in its binary repre-
z(cid:48) = ((cid:63), . . . , (cid:63), 1, 0, . . . , 0
)2 .
z
−
<w
(cid:125)
(cid:123)(cid:122)
(cid:124)
(cid:32)
(cid:32)
112
ChainedHashTable: Hashing with Chaining
§5.1
Therefore, the product (z
representation:
−
z(cid:48))q has fewer than w trailing 0’s in its binary
z(cid:48))q = ((cid:63),
(z
−
· · ·
, (cid:63), 1, 0, . . . , 0
)2 .
<w
Therefore (z
pleting the proof.
−
z(cid:48))q cannot satisfy (5.1), yielding a contradiction and com-
(cid:125)
(cid:123)(cid:122)
(cid:124)
(cid:32)
(cid:32)
The utility of Lemma 5.3 comes from the following observation: If z is
chosen uniformly at random from S, then zt is uniformly distributed over
S. In the following proof, it helps to think of the binary representation of
z, which consists of w
1 random bits followed by a 1.
−
Proof of Lemma 5.1. First we note that the condition hash(x) = hash(y) is
equivalent to the statement “the highest-order d bits of zx mod 2w and the
highest-order d bits of zy mod 2w are the same.” A necessary condition of
that statement is that the highest-order d bits in the binary representation
of z(x
y) mod 2w are either all 0’s or all 1’s. That is,
−
when zx mod 2w > zy mod 2w or
y) mod 2w = (0, . . . , 0
, (cid:63), . . . , (cid:63)
)2
z(x
−
(cid:125)
(cid:123)(cid:122)
(cid:124)
(cid:32)
(cid:32)
y) mod 2w = (1, . . . , 1
d
w
d
−
(cid:123)(cid:122)
(cid:125)
(cid:124)
(cid:32)
(cid:32)
, (cid:63), . . . , (cid:63)
)2 .
z(x
−
(5.2)
(5.3)
d
w
d
when zx mod 2w < zy mod 2w. Therefore, we only have to bound the
(cid:124)
(cid:125)
(cid:123)(cid:122)
(cid:32)
(cid:32)
y) mod 2w looks like (5.2) or (5.3).
probability that z(x
−
(cid:123)(cid:122)
(cid:125)
(cid:124)
(cid:32)
(cid:32)
y) mod 2w = q2r for some
0. By Lemma 5.3, the binary representation of zq mod 2w has
Let q be the unique odd integer such that (x
−
−
integer r
w
≥
−
1 random bits, followed by a 1:
zq mod 2w = (bw
1, . . . , b1
, 1)2
−
w
1
−
y) mod 2w = zq2r mod 2w has
Therefore, the binary representation of z(x
(cid:123)(cid:122)
(cid:125)
−
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
w
1 random bits, followed by a 1, followed by r 0’s:
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
(cid:124)
r
−
−
y) mod 2w = zq2r mod 2w = (bw
z(x
−
1, . . . , b1
, 1, 0, 0, . . . , 0
)2
r
−
−
w
1
r
−
−
(cid:123)(cid:122)
(cid:125)
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)(cid:32)
(cid:124)
r
(cid:124)
(cid:123)(cid:122)
(cid:125)
(cid:32)(cid:32)(cid:32)(cid:32)
(cid:32)(cid:32)(cid:32)(cid:32)
113
§5.2
Hash Tables
−
d, then the d higher order bits
−
y) mod 2w contain both 0’s and 1’s, so the probability that z(x
We can now finish the proof: If r > w
of z(x
−
y) mod 2w looks like (5.2) or (5.3) is 0. If r = w
d, then the probabil-
ity of looking like (5.2) is 0, but the probability of looking like (5.3) is
1 = 2/2d (since we must have b1, . . . , bd
1/2d
1 = 1, . . . , 1). If r < w
d, then
−
−
−
d = 1, . . . , 1. The
d = 0, . . . , 0 or bw
we must have bw
1, . . . , bw
−
−
probability of each of these cases is 1/2d and they are mutually exclu-
sive, so the probability of either of these cases is 2/2d. This completes the
proof.
1, . . . , bw
−
r
−
r
−
r
−
−
−
−
r
5.1.2 Summary
The following theorem summarizes the performance of a ChainedHash-
Table data structure:
Theorem 5.1. A ChainedHashTable implements the USet interface. Ignor-
ing the cost of calls to grow(), a ChainedHashTable supports the operations
add(x), remove(x), and find(x) in O(1) expected time per operation.
Furthermore, beginning with an empty ChainedHashTable, any sequence
of m add(x) and remove(x) operations results in a total of O(m) time spent
during all calls to grow().
5.2 LinearHashTable: Linear Probing
The ChainedHashTable data structure uses an array of lists, where the
ith list stores all elements x such that hash(x) = i. An alternative, called
open addressing is to store the elements directly in an array, t, with each
array location in t storing at most one value. This approach is taken by
the LinearHashTable described in this section. In some places, this data
structure is described as open addressing with linear probing.
The main idea behind a LinearHashTable is that we would, ideally,
like to store the element x with hash value i = hash(x) in the table loca-
tion t[i]. If we cannot do this (because some element is already stored
there) then we try to store it at location t[(i + 1) mod t.length]; if that’s
not possible, then we try t[(i + 2) mod t.length], and so on, until we find
a place for x.
114
LinearHashTable: Linear Probing
§5.2
There are three types of entries stored in t:
1. data values: actual values in the USet that we are representing;
2. null values: at array locations where no data has ever been stored;
and
3. del values: at array locations where data was once stored but that
has since been deleted.
In addition to the counter, n, that keeps track of the number of elements
in the LinearHashTable, a counter, q, keeps track of the number of ele-
ments of Types 1 and 3. That is, q is equal to n plus the number of del
values in t. To make this work efficiently, we need t to be considerably
larger than q, so that there are lots of null values in t. The operations on
2q.
a LinearHashTable therefore maintain the invariant that t.length
To summarize, a LinearHashTable contains an array, t, that stores
data elements, and integers n and q that keep track of the number of
data elements and non-null values of t, respectively. Because many hash
functions only work for table sizes that are a power of 2, we also keep an
integer d and maintain the invariant that t.length = 2d.
≥
LinearHashTable
T[] t;
int n;
int d;
int q;
// the table
// the size
// t.length = 2ˆd
// number of non-null entries in t
The find(x) operation in a LinearHashTable is simple. We start at
array entry t[i] where i = hash(x) and search entries t[i], t[(i + 1) mod
t.length], t[(i + 2) mod t.length], and so on, until we find an index i(cid:48)
such that, either, t[i(cid:48)] = x, or t[i(cid:48)] = null. In the former case we return
t[i(cid:48)]. In the latter case, we conclude that x is not contained in the hash
table and return null.
LinearHashTable
T find(T x) {
int i = hash(x);
while (t[i] != null) {
115
§5.2
Hash Tables
if (t[i] != del && x.equals(t[i])) return t[i];
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
return null;
}
The add(x) operation is also fairly easy to implement. After checking
that x is not already stored in the table (using find(x)), we search t[i],
t[(i+1) mod t.length], t[(i+2) mod t.length], and so on, until we find a
null or del and store x at that location, increment n, and q, if appropriate.
boolean add(T x) {
LinearHashTable
if (find(x) != null) return false;
if (2*(q+1) > t.length) resize(); // max 50% occupancy
int i = hash(x);
while (t[i] != null && t[i] != del)
i = (i == t.length-1) ? 0 : i + 1; // increment i
if (t[i] == null) q++;
n++;
t[i] = x;
return true;
}
By now, the implementation of the remove(x) operation should be ob-
vious. We search t[i], t[(i + 1) mod t.length], t[(i + 2) mod t.length],
and so on until we find an index i(cid:48) such that t[i(cid:48)] = x or t[i(cid:48)] = null.
In the former case, we set t[i(cid:48)] = del and return true. In the latter case
we conclude that x was not stored in the table (and therefore cannot be
deleted) and return false.
LinearHashTable
T remove(T x) {
int i = hash(x);
while (t[i] != null) {
T y = t[i];
if (y != del && x.equals(y)) {
t[i] = del;
n--;
116
LinearHashTable: Linear Probing
§5.2
if (8*n < t.length) resize(); // min 12.5% occupancy
return y;
}
i = (i == t.length-1) ? 0 : i + 1;
// increment i
}
return null;
}
The correctness of the find(x), add(x), and remove(x) methods is easy
to verify, though it relies on the use of del values. Notice that none of
these operations ever sets a non-null entry to null. Therefore, when we
reach an index i(cid:48) such that t[i(cid:48)] = null, this is a proof that the element, x,
that we are searching for is not stored in the table; t[i(cid:48)] has always been
null, so there is no reason that a previous add(x) operation would have
proceeded beyond index i(cid:48).
The resize() method is called by add(x) when the number of non-
null entries exceeds t.length/2 or by remove(x) when the number of
data entries is less than t.length/8. The resize() method works like the
resize() methods in other array-based data structures. We find the small-
est non-negative integer d such that 2d
3n. We reallocate the array t so
that it has size 2d, and then we insert all the elements in the old version
of t into the newly-resized copy of t. While doing this, we reset q equal
to n since the newly-allocated t contains no del values.
≥
void resize() {
LinearHashTable
d = 1;
while ((1<<d) < 3*n) d++;
T[] told = t;
t = newArray(1<<d);
q = n;
// insert everything from told
for (int k = 0; k < told.length; k++) {
if (told[k] != null && told[k] != del) {
int i = hash(told[k]);
while (t[i] != null)
i = (i == t.length-1) ? 0 : i + 1;
t[i] = told[k];
}
117
§5.2
}
}
Hash Tables
5.2.1 Analysis of Linear Probing
Notice that each operation, add(x), remove(x), or find(x), finishes as soon
as (or before) it discovers the first null entry in t. The intuition behind
the analysis of linear probing is that, since at least half the elements in t
are equal to null, an operation should not take long to complete because
it will very quickly come across a null entry. We shouldn’t rely too heav-
ily on this intuition, though, because it would lead us to (the incorrect)
conclusion that the expected number of locations in t examined by an
operation is at most 2.
0, . . . , t.length
For the rest of this section, we will assume that all hash values are
independently and uniformly distributed in
. This is
}
not a realistic assumption, but it will make it possible for us to analyze
linear probing. Later in this section we will describe a method, called
tabulation hashing, that produces a hash function that is “good enough”
for linear probing. We will also assume that all indices into the positions
of t are taken modulo t.length, so that t[i] is really a shorthand for
t[i mod t.length].
−
1
{
−
1] are non-null and t[i
We say that a run of length k that starts at i occurs when all the table en-
1] = t[i+k] = null.
tries t[i], t[i + 1], . . . , t[i+k
The number of non-null elements of t is exactly q and the add(x) method
ensures that, at all times, q
t.length/2. There are q elements x1, . . . , xq
that have been inserted into t since the last rebuild() operation. By our
assumption, each of these has a hash value, hash(xj ), that is uniform and
independent of the rest. With this setup, we can prove the main lemma
required to analyze linear probing.
≤
−
Lemma 5.4. Fix a value i
run of length k starts at i is O(ck) for some constant 0 < c < 1.
0, . . . , t.length
1
}
∈ {
−
. Then the probability that a
Proof. If a run of length k starts at i, then there are exactly k elements xj
such that hash(xj )
. The probability that this occurs is
}
i, . . . , i + k
∈ {
−
1
118
LinearHashTable: Linear Probing
§5.2
exactly
pk =
k
q
k
k
t.length
t.length
−
t.length
q
k
−
k
,
−
(cid:32)
(cid:33) (cid:32)
(cid:32)
since, for each choice of k elements, these k elements must hash to one of
the k locations and the remaining q
k elements must hash to the other
t.length
k table locations.2
−
(cid:33)
(cid:33)
In the following derivation we will cheat a little and replace r! with
(r/e)r . Stirling’s Approximation (Section 1.3.2) shows that this is only a
factor of O(√r) from the truth. This is just done to make the derivation
simpler; Exercise 5.4 asks the reader to redo the calculation more rigor-
ously using Stirling’s Approximation in its entirety.
The value of pk is maximized when t.length is minimum, and the
data structure maintains the invariant that t.length
2q, so
≥
[Stirling’s approximation]
q
k
−
(cid:33)
q
k
−
(cid:33)
pk ≤
q
k
(cid:32)
(cid:33) (cid:32)
k
2q
k
(cid:33)
(cid:32)
2q
−
2q
q
k
−
k
(cid:33)
k
q
k
−
k
2q
−
2q
(cid:33)
k
k
2q
−
2q
2q
−
2q
k
(cid:32)
(cid:33)
k
2q
k
2q
(cid:32)
(cid:33)
k
(cid:33)
q
(cid:32)
k
−
k
2q
(cid:33) (cid:32)
kkk
−
(cid:33) (cid:32)
(cid:33) (cid:32)
kkk
−
q(2q
2q(q
k)
k)
−
−
(cid:33)
k
2(q
−
(q
(q
q!
k)!k!
−
qq
k)q
−
qkqq
k
−
k)q
k
(q
−
qk
2qk
1
2
1
2
k
(cid:33)
(cid:32)
(2q
2(q
(cid:32)
(cid:19)
k
1 +
(cid:32)
k
(cid:33)
.
(cid:19)
√e
2
=
≈
=
=
=
=
≤
(cid:32)
(cid:32)
(cid:32)
(cid:32)
(cid:18)
(cid:18)
(cid:32)
k)
−
k)
(cid:33)
−
q
k
−
q
k
−
k)
(cid:33)
(In the last step, we use the inequality (1 + 1/x)x
x > 0.) Since √e/2 < 0.824360636 < 1, this completes the proof.
≤
e, which holds for all
2Note that pk is greater than the probability that a run of length k starts at i, since the
definition of pk does not include the requirement t[i
1] = t[i + k] = null.
−
119
.
}
}
§5.2
Hash Tables
Using Lemma 5.4 to prove upper-bounds on the expected running
time of find(x), add(x), and remove(x) is now fairly straightforward. Con-
sider the simplest case, where we execute find(x) for some value x that
has never been stored in the LinearHashTable. In this case, i = hash(x)
is a random value in
independent of the contents of
t. If i is part of a run of length k, then the time it takes to execute the
find(x) operation is at most O(1 + k). Thus, the expected running time
can be upper-bounded by
0, . . . , t.length
1
}
−
{
1
t.length
(cid:18)
t.length
∞
(cid:19)
(cid:88)i=1
(cid:88)k=0
k Pr
{
i is part of a run of length k
O
1 +
Note that each run of length k contributes to the inner sum k times for a
total contribution of k2, so the above sum can be rewritten as
i starts a run of length k
{
O
1 +
1
t.length
(cid:18)
(cid:19)
t.length
∞
k2 Pr
(cid:88)i=1
(cid:88)k=0
t.length
1
t.length
1 +
(cid:18)
∞
1 +
k2pk
(cid:19)
(cid:88)i=1
(cid:88)k=0
∞
k2pk
O
≤
= O
= O
1 +
= O(1) .
O(ck)
(cid:88)k=0
∞
(cid:88)k=0
k2
·
O(ck)
The last step in this derivation comes from the fact that
is an exponentially decreasing series.3 Therefore, we conclude that the
expected running time of the find(x) operation for a value x that is not
contained in a LinearHashTable is O(1).
∞k=0 k2
(cid:80)
·
If we ignore the cost of the resize() operation, then the above analysis
gives us all we need to analyze the cost of operations on a LinearHash-
Table.
3In the terminology of many calculus texts, this sum passes the ratio test: There exists a
(k+1)2ck+1
k2ck
positive integer k0 such that, for all k
< 1.
k0,
≥
120
LinearHashTable: Linear Probing
§5.2
First of all, the analysis of find(x) given above applies to the add(x)
operation when x is not contained in the table. To analyze the find(x)
operation when x is contained in the table, we need only note that this
is the same as the cost of the add(x) operation that previously added x to
the table. Finally, the cost of a remove(x) operation is the same as the cost
of a find(x) operation.
In summary, if we ignore the cost of calls to resize(), all operations on
a LinearHashTable run in O(1) expected time. Accounting for the cost of
resize can be done using the same type of amortized analysis performed
for the ArrayStack data structure in Section 2.1.
5.2.2 Summary
The following theorem summarizes the performance of the LinearHash-
Table data structure:
Theorem 5.2. A LinearHashTable implements the USet interface. Ignor-
ing the cost of calls to resize(), a LinearHashTable supports the operations
add(x), remove(x), and find(x) in O(1) expected time per operation.
Furthermore, beginning with an empty LinearHashTable, any sequence
of m add(x) and remove(x) operations results in a total of O(m) time spent
during all calls to resize().
5.2.3 Tabulation Hashing
While analyzing the LinearHashTable structure, we made a very strong
assumption: That for any set of elements,
, the hash values
}
hash(x1), . . . , hash(xn) are independently and uniformly distributed over
. One way to achieve this is to store a giant
the set
}
array, tab, of length 2w, where each entry is a random w-bit integer, inde-
pendent of all the other entries. In this way, we could implement hash(x)
by extracting a d-bit integer from tab[x.hashCode()]:
0, . . . , t.length
x1, . . . , xn
−
1
{
{
int idealHash(T x) {
return tab[x.hashCode() >>> w-d];
LinearHashTable
}
121
§5.3
Hash Tables
Unfortunately, storing an array of size 2w is prohibitive in terms of
memory usage. The approach used by tabulation hashing is to, instead,
treat w-bit integers as being comprised of w/r integers, each having only r
bits. In this way, tabulation hashing only needs w/r arrays each of length
2r. All the entries in these arrays are independent random w-bit integers.
To obtain the value of hash(x) we split x.hashCode() up into w/r r-bit
integers and use these as indices into these arrays. We then combine all
these values with the bitwise exclusive-or operator to obtain hash(x). The
following code shows how this works when w = 32 and r = 4:
LinearHashTable
int hash(T x) {
int h = x.hashCode();
return (tab[0][h&0xff]
ˆ tab[1][(h>>>8)&0xff]
ˆ tab[2][(h>>>16)&0xff]
ˆ tab[3][(h>>>24)&0xff])
>>> (w-d);
}
In this case, tab is a two-dimensional array with four columns and
232/4 = 256 rows.
{
1
0, . . . , 2d
One can easily verify that, for any x, hash(x) is uniformly distributed
over
. With a little work, one can even verify that any pair
of values have independent hash values. This implies tabulation hashing
could be used in place of multiplicative hashing for the ChainedHash-
Table implementation.
−
}
However, it is not true that any set of n distinct values gives a set of n
independent hash values. Nevertheless, when tabulation hashing is used,
the bound of Theorem 5.2 still holds. References for this are provided at
the end of this chapter.
5.3 Hash Codes
The hash tables discussed in the previous section are used to associate
data with integer keys consisting of w bits. In many cases, we have keys
122
Hash Codes
§5.3
that are not integers. They may be strings, objects, arrays, or other com-
pound structures. To use hash tables for these types of data, we must
map these data types to w-bit hash codes. Hash code mappings should
have the following properties:
1. If x and y are equal, then x.hashCode() and y.hashCode() are equal.
2. If x and y are not equal, then the probability that x.hashCode() =
y.hashCode() should be small (close to 1/2w).
The first property ensures that if we store x in a hash table and later
look up a value y equal to x, then we will find x—as we should. The sec-
ond property minimizes the loss from converting our objects to integers.
It ensures that unequal objects usually have different hash codes and so
are likely to be stored at different locations in our hash table.
5.3.1 Hash Codes for Primitive Data Types
Small primitive data types like char, byte, int, and float are usually
easy to find hash codes for. These data types always have a binary rep-
resentation and this binary representation usually consists of w or fewer
bits. (For example, in Java, byte is an 8-bit type and float is a 32-bit
type.) In these cases, we just treat these bits as the representation of an
integer in the range
. If two values are different, they get
}
different hash codes. If they are the same, they get the same hash code.
0, . . . , 2w
−
1
{
A few primitive data types are made up of more than w bits, usually
cw bits for some constant integer c.
(Java’s long and double types are
examples of this with c = 2.) These data types can be treated as compound
objects made of c parts, as described in the next section.
5.3.2 Hash Codes for Compound Objects
For a compound object, we want to create a hash code by combining the
individual hash codes of the object’s constituent parts. This is not as easy
as it sounds. Although one can find many hacks for this (for example,
combining the hash codes with bitwise exclusive-or operations), many of
these hacks turn out to be easy to foil (see Exercises 5.7–5.9). However,
123
§5.3
Hash Tables
if one is willing to do arithmetic with 2w bits of precision, then there are
simple and robust methods available. Suppose we have an object made
1. Then we
up of several parts P0, . . . , Pr
−
can choose mutually independent random w-bit integers z0, . . . , zr
1 and a
−
random 2w-bit odd integer z and compute a hash code for our object with
1 whose hash codes are x0, . . . , xr
−
h(x0, . . . , xr
1) =
−
mod 22w
div 2w .
zixi
r
1
−
(cid:88)i=0
z
Note that this hash code has a final step (multiplying by z and dividing by
2w) that uses the multiplicative hash function from Section 5.1.1 to take
the 2w-bit intermediate result and reduce it to a w-bit final result. Here
is an example of this method applied to a simple compound object with
three parts x0, x1, and x2:
int hashCode() {
Point3D
// random numbers from rand.org
long[] z = {0x2058cc50L, 0xcb19137eL, 0x2cb6b6fdL};
long zz = 0xbea0107e5067d19dL;
// convert (unsigned) hashcodes to long
long h0 = x0.hashCode() & ((1L<<32)-1);
long h1 = x1.hashCode() & ((1L<<32)-1);
long h2 = x2.hashCode() & ((1L<<32)-1);
return (int)(((z[0]*h0 + z[1]*h1 + z[2]*h2)*zz)
>>> 32);
}
The following theorem shows that, in addition to being straightfor-
ward to implement, this method is provably good:
Theorem 5.3. Let x0, . . . , xr
gers in
Then
0, . . . , 2w
−
1
}
{
1 and y0, . . . , yr
−
and assume xi
(cid:44) yi for at least one index i
1 each be sequences of w bit inte-
−
1
.
}
0, . . . , r
∈ {
−
h(x0, . . . , xr
Pr
{
−
1) = h(y0, . . . , yr
3/2w .
1)
−
} ≤
124
Hash Codes
§5.3
Proof. We will first ignore the final multiplicative hashing step and see
how that step contributes later. Define:
h(cid:48)(x0, . . . , xr
1) =
−
zj xj
mod 22w .
r
1
−
(cid:88)j=0
−
yi) mod 22w = t
zi(xi −
Suppose that h(cid:48)(x0, . . . , xr
1) = h(cid:48)(y0, . . . , yr
−
1). We can rewrite this as:
where
t =
i
1
−
(cid:88)j=0
r
1
−
xj ) +
zj (yj −
zj (yj −
xj )
(cid:88)j=i+1
If we assume, without loss of generality that xi > yi, then (5.4) becomes
(5.4)
mod 22w
(5.5)
yi) = t ,
zi(xi −
yi) is at most 2w
1. By assumption, xi −
−
−
since each of zi and (xi −
1, so their product is at
(cid:44) 0, so (5.5) has
2w+1 + 1 < 22w
most 22w
−
at most one solution in zi. Therefore, since zi and t are independent
(z0, . . . , zr
1 are mutually independent), the probability that we select zi
−
so that h(cid:48)(x0, . . . , xr
yi
1) = h(cid:48)(y0, . . . , yr
−
1) is at most 1/2w.
−
The final step of the hash function is to apply multiplicative hashing
1) to a w-bit final re-
1), then
to reduce our 2w-bit intermediate result h(cid:48)(x0, . . . , xr
sult h(x0, . . . , xr
h(x0, . . . , xr
Pr
{
To summarize,
1). By Theorem 5.3, if h(cid:48)(x0, . . . , xr
−
1) = h(y0, . . . , yr
1)
−
−
1) (cid:44) h(cid:48)(y0, . . . , yr
−
2/2w.
} ≤
−
−
Pr
(cid:40)
= Pr
h(x0, . . . , xr
1)
−
= h(y0, . . . , yr
h(cid:48)(x0, . . . , xr
h(cid:48)(x0, . . . , xr
1)
−
1) = h(cid:48)(y0, . . . , yr
1) (cid:44) h(cid:48)(y0, . . . , yr
−
−
and zh(cid:48)(x0, . . . , xr
(cid:41)
1) or
−
1)
1/2w + 2/2w = 3/2w .
≤
1) div 2w = zh(cid:48)(y0, . . . , yr
−
−
−
1) div 2w
125
§5.3
Hash Tables
5.3.3 Hash Codes for Arrays and Strings
The method from the previous section works well for objects that have a
fixed, constant, number of components. However, it breaks down when
we want to use it with objects that have a variable number of components,
since it requires a random w-bit integer zi for each component. We could
use a pseudorandom sequence to generate as many zi’s as we need, but
then the zi’s are not mutually independent, and it becomes difficult to
prove that the pseudorandom numbers don’t interact badly with the hash
function we are using. In particular, the values of t and zi in the proof of
Theorem 5.3 are no longer independent.
A more rigorous approach is to base our hash codes on polynomials
over prime fields; these are just regular polynomials that are evaluated
modulo some prime number, p. This method is based on the following
theorem, which says that polynomials over prime fields behave pretty-
much like usual polynomials:
Theorem 5.4. Let p be a prime number, and let f (z) = x0z0 + x1z1 +
xr
the equation f (z) mod p = 0 has at most r
1 be a non-trivial polynomial with coefficients xi ∈ {
1 solutions for z
+
· · ·
1
. Then
−
}
1
0, . . . , p
1zr
−
0, . . . , p
−
.
−
To use Theorem 5.4, we hash a sequence of integers x0, . . . , xr
using a random integer z
0, . . . , p
0, . . . , p
1
2
∈ {
−
}
each xi ∈ {
mula
−
}
∈ {
}
−
1 with
−
via the for-
h(x0, . . . , xr
1) =
−
x0z0 +
+ xr
1zr
−
· · ·
1 + (p
−
−
1)zr
mod p .
(cid:16)
−
−
Note the extra (p
1)zr term at the end of the formula. It helps to think
1) as the last element, xr , in the sequence x0, . . . , xr . Note that this
of (p
element differs from every other element in the sequence (each of which
is in the set
1 as an end-of-sequence
marker.
). We can think of p
0, . . . , p
2
}
−
−
{
(cid:17)
The following theorem, which considers the case of two sequences of
the same length, shows that this hash function gives a good return for the
small amount of randomization needed to choose z:
Theorem 5.5. Let p > 2w + 1 be a prime, let x0, . . . , xr
be sequences of w-bit integers in
1 and y0, . . . , yr
−
, and assume xi
1 each
−
(cid:44) yi for at least
0, . . . , 2w
{
1
}
−
126
Hash Codes
§5.3
1
. Then
}
one index i
0, . . . , r
∈ {
−
h(x0, . . . , xr
Pr
{
Proof. The equation h(x0, . . . , xr
1) = h(y0, . . . , yr
−
1)
−
} ≤
(r
1)/p
}
−
.
1) = h(y0, . . . , yr
−
+ (xr
yr
−
1)zr
−
1
1) can be rewritten as
−
y0)z0 +
(x0 −
(cid:16)
· · ·
1 −
−
Since xi (cid:44) yi, this polynomial is non-trivial. Therefore, by Theorem 5.4,
1 solutions in z. The probability that we pick z to be one
it has at most r
1)/p.
of these solutions is therefore at most (r
−
(cid:17)
mod p = 0.
(5.6)
−
Note that this hash function also deals with the case in which two
sequences have different lengths, even when one of the sequences is a
prefix of the other. This is because this function effectively hashes the
infinite sequence
x0, . . . , xr
1, p
−
1, 0, 0, . . .
.
−
This guarantees that if we have two sequences of length r and r(cid:48) with
r > r(cid:48), then these two sequences differ at index i = r. In this case, (5.6)
becomes
1
i=r(cid:48)−
(cid:88)i=0
(xi −
yi)zi + (xr(cid:48) −
p + 1)zr(cid:48) +
i=r
1
−
(cid:88)i=r(cid:48)+1
xizi + (p
−
1)zr
mod p = 0 ,
which, by Theorem 5.4, has at most r solutions in z. This combined with
Theorem 5.5 suffice to prove the following more general theorem:
Theorem 5.6. Let p > 2w + 1 be a prime, let x0, . . . , xr
distinct sequences of w-bit integers in
0, . . . , 2w
1
{
−
. Then
}
−
1 and y0, . . . , yr(cid:48)−
1 be
h(x0, . . . , xr
Pr
{
1) = h(y0, . . . , yr
−
1)
−
} ≤
max
r, r(cid:48)
{
/p .
}
The following example code shows how this hash function is applied
to an object that contains an array, x, of values:
int hashCode() {
GeomVector
long p = (1L<<32)-5;
long z = 0x64b6055aL; // 32 bits from random.org
int z2 = 0x5067d19d;
// random odd 32 bit number
// prime: 2ˆ32 - 5
127
§5.4
Hash Tables
long s = 0;
long zi = 1;
for (int i = 0; i < x.length; i++) {
// reduce to 31 bits
long xi = (x[i].hashCode() * z2) >>> 1;
s = (s + zi * xi) % p;
zi = (zi * z) % p;
}
s = (s + zi * (p-1)) % p;
return (int)s;
}
The preceding code sacrifices some collision probability for imple-
mentation convenience. In particular, it applies the multiplicative hash
function from Section 5.1.1, with d = 31 to reduce x[i].hashCode() to a 31-
bit value. This is so that the additions and multiplications that are done
modulo the prime p = 232
5 can be carried out using unsigned 63-bit
arithmetic. Thus the probability of two different sequences, the longer of
which has length r, having the same hash code is at most
−
2/231 + r/(232
5)
−
rather than the r/(232
−
5) specified in Theorem 5.6.
5.4 Discussion and Exercises
Hash tables and hash codes represent an enormous and active field of re-
search that is just touched upon in this chapter. The online Bibliography
on Hashing [10] contains nearly 2000 entries.
A variety of different hash table implementations exist. The one de-
scribed in Section 5.1 is known as hashing with chaining (each array entry
contains a chain (List) of elements). Hashing with chaining dates back to
an internal IBM memorandum authored by H. P. Luhn and dated January
1953. This memorandum also seems to be one of the earliest references
to linked lists.
An alternative to hashing with chaining is that used by open address-
ing schemes, where all data is stored directly in an array. These schemes
128
Discussion and Exercises
§5.4
include the LinearHashTable structure of Section 5.2. This idea was also
proposed, independently, by a group at IBM in the 1950s. Open address-
ing schemes must deal with the problem of collision resolution: the case
where two values hash to the same array location. Different strategies
exist for collision resolution; these provide different performance guar-
antees and often require more sophisticated hash functions than the ones
described here.
Yet another category of hash table implementations are the so-called
perfect hashing methods. These are methods in which find(x) operations
take O(1) time in the worst-case. For static data sets, this can be accom-
plished by finding perfect hash functions for the data; these are functions
that map each piece of data to a unique array location. For data that
changes over time, perfect hashing methods include FKS two-level hash
tables [31, 24] and cuckoo hashing [57].
The hash functions presented in this chapter are probably among the
most practical methods currently known that can be proven to work well
for any set of data. Other provably good methods date back to the pio-
neering work of Carter and Wegman who introduced the notion of uni-
versal hashing and described several hash functions for different scenarios
[14]. Tabulation hashing, described in Section 5.2.3, is due to Carter and
Wegman [14], but its analysis, when applied to linear probing (and sev-
eral other hash table schemes) is due to Pˇatras¸cu and Thorup [60].
The idea of multiplicative hashing is very old and seems to be part of
the hashing folklore [48, Section 6.4]. However, the idea of choosing the
multiplier z to be a random odd number, and the analysis in Section 5.1.1
is due to Dietzfelbinger et al. [23]. This version of multiplicative hashing
is one of the simplest, but its collision probability of 2/2d is a factor of two
2d.
larger than what one could expect with a random function from 2w
The multiply-add hashing method uses the function
→
h(x) = ((zx + b) mod 22w) div 22w
d
−
where z and b are each randomly chosen from
. Multiply-add
}
hashing has a collision probability of only 1/2d [21], but requires 2w-bit
precision arithmetic.
0, . . . , 22w
{
−
1
There are a number of methods of obtaining hash codes from fixed-
length sequences of w-bit integers. One particularly fast method [11] is
129
§5.4
Hash Tables
the function
h(x0, . . . , xr
1)
−
r/2
1
i=0 ((x2i + a2i) mod 2w)((x2i+1 + a2i+1) mod 2w)
−
=
mod 22w
(cid:16)(cid:80)
1 are randomly chosen from
−
where r is even and a0, . . . , ar
. This
yields a 2w-bit hash code that has collision probability 1/2w. This can be
reduced to a w-bit hash code using multiplicative (or multiply-add) hash-
ing. This method is fast because it requires only r/2 2w-bit multiplications
whereas the method described in Section 5.3.2 requires r multiplications.
(The mod operations occur implicitly by using w and 2w-bit arithmetic
for the additions and multiplications, respectively.)
}
(cid:17)
0, . . . , 2w
{
The method from Section 5.3.3 of using polynomials over prime fields
to hash variable-length arrays and strings is due to Dietzfelbinger et al.
[22]. Due to its use of the mod operator which relies on a costly ma-
chine instruction, it is, unfortunately, not very fast. Some variants of this
method choose the prime p to be one of the form 2w
1, in which case
the mod operator can be replaced with addition (+) and bitwise-and (&)
operations [47, Section 3.6]. Another option is to apply one of the fast
methods for fixed-length strings to blocks of length c for some constant
c > 1 and then apply the prime field method to the resulting sequence of
r/c
(cid:100)
hash codes.
−
(cid:101)
Exercise 5.1. A certain university assigns each of its students student
numbers the first time they register for any course. These numbers are
sequential integers that started at 0 many years ago and are now in the
millions. Suppose we have a class of one hundred first year students and
we want to assign them hash codes based on their student numbers. Does
it make more sense to use the first two digits or the last two digits of their
student number? Justify your answer.
Exercise 5.2. Consider the hashing scheme in Section 5.1.1, and suppose
n = 2d and d
w/2.
≤
1. Show that, for any choice of the muliplier, z, there exists n values
that all have the same hash code. (Hint: This is easy, and doesn’t
require any number theory.)
130
Discussion and Exercises
§5.4
2. Given the multiplier, z, describe n values that all have the same
hash code. (Hint: This is harder, and requires some basic number
theory.)
Exercise 5.3. Prove that the bound 2/2d in Lemma 5.1 is the best possi-
ble bound by showing that, if x = 2w
2 and y = 3x, then Pr
hash(x) =
−
{
= 2/2d. (Hint look at the binary representations of zx and z3x
hash(y)
and use the fact that z3x = zx+2zx.)
−
}
d
Exercise 5.4. Reprove Lemma 5.4 using the full version of Stirling’s Ap-
proximation given in Section 1.3.2.
Exercise 5.5. Consider the following simplified version of the code for
adding an element x to a LinearHashTable, which simply stores x in the
first null array entry it finds. Explain why this could be very slow by
giving an example of a sequence of O(n) add(x), remove(x), and find(x)
operations that would take on the order of n2 time to execute.
boolean addSlow(T x) {
LinearHashTable
if (2*(q+1) > t.length) resize(); // max 50% occupancy
int i = hash(x);
while (t[i] != null) {
if (t[i] != del && x.equals(t[i])) return false;
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
t[i] = x;
n++; q++;
return true;
}
Exercise 5.6. Early versions of the Java hashCode() method for the String
class worked by not using all of the characters found in long strings. For
example, for a sixteen character string, the hash code was computed using
only the eight even-indexed characters. Explain why this was a very bad
idea by giving an example of large set of strings that all have the same
hash code.
Exercise 5.7. Suppose you have an object made up of two w-bit integers,
y does not make a good hash code for your object.
x and y. Show why x
Give an example of a large set of objects that would all have hash code 0.
⊕
131
§5.4
Hash Tables
Exercise 5.8. Suppose you have an object made up of two w-bit integers,
x and y. Show why x + y does not make a good hash code for your object.
Give an example of a large set of objects that would all have the same
hash code.
Exercise 5.9. Suppose you have an object made up of two w-bit integers,
x and y. Suppose that the hash code for your object is defined by some
deterministic function h(x, y) that produces a single w-bit integer. Prove
that there exists a large set of objects that have the same hash code.
Exercise 5.10. Let p = 2w
a positive integer x
−
1 for some positive integer w. Explain why, for
(x mod 2w) + (x div 2w)
x mod (2w
1) .
−
≡
(This gives an algorithm for computing x mod (2w
ting
−
1) by repeatedly set-
x = x&((1<<w)
1) + x>>>w
−
until x
2w
1.)
−
≤
Exercise 5.11. Find some commonly used hash table implementation
such as the (Java Collection Framework HashMap or the HashTable or
LinearHashTable implementations in this book, and design a program
that stores integers in this data structure so that there are integers, x,
such that find(x) takes linear time. That is, find a set of n integers for
which there are cn elements that hash to the same table location.
Depending on how good the implementation is, you may be able to
do this just by inspecting the code for the implementation, or you may
have to write some code that does trial insertions and searches, timing
how long it takes to add and find particular values. (This can be, and has
been, used to launch denial of service attacks on web servers [17].)
132
Chapter 6
Binary Trees
This chapter introduces one of the most fundamental structures in com-
puter science: binary trees. The use of the word tree here comes from
the fact that, when we draw them, the resultant drawing often resembles
the trees found in a forest. There are many ways of ways of defining bi-
nary trees. Mathematically, a binary tree is a connected, undirected, finite
graph with no cycles, and no vertex of degree greater than three.
For most computer science applications, binary trees are rooted: A
special node, r, of degree at most two is called the root of the tree. For
every node, u (cid:44) r, the second node on the path from u to r is called the
parent of u. Each of the other nodes adjacent to u is called a child of u.
Most of the binary trees we are interested in are ordered, so we distinguish
between the left child and right child of u.
In illustrations, binary trees are usually drawn from the root down-
ward, with the root at the top of the drawing and the left and right chil-
dren respectively given by left and right positions in the drawing (Fig-
ure 6.1). For example, Figure 6.2.a shows a binary tree with nine nodes.
Because binary trees are so important, a certain terminology has de-
veloped for them: The depth of a node, u, in a binary tree is the length of
the path from u to the root of the tree. If a node, w, is on the path from u
to r, then w is called an ancestor of u and u a descendant of w. The subtree
of a node, u, is the binary tree that is rooted at u and contains all of u’s
descendants. The height of a node, u, is the length of the longest path
from u to one of its descendants. The height of a tree is the height of its
root. A node, u, is a leaf if it has no children.
133
§6
Binary Trees
Figure 6.1: The parent, left child, and right child of the node u in a BinaryTree.
(a)
(b)
Figure 6.2: A binary tree with (a) nine real nodes and (b) ten external nodes.
134
uu.parentu.leftu.rightrrBinaryTree: A Basic Binary Tree
§6.1
We sometimes think of the tree as being augmented with external
nodes. Any node that does not have a left child has an external node as
its left child, and, correspondingly, any node that does not have a right
child has an external node as its right child (see Figure 6.2.b). It is easy
to verify, by induction, that a binary tree with n
1 real nodes has n + 1
external nodes.
≥
6.1 BinaryTree: A Basic Binary Tree
The simplest way to represent a node, u, in a binary tree is to explicitly
store the (at most three) neighbours of u:
class BTNode<Node extends BTNode<Node>> {
BinaryTree
Node left;
Node right;
Node parent;
}
When one of these three neighbours is not present, we set it to nil.
In this way, both external nodes of the tree and the parent of the root
correspond to the value nil.
The binary tree itself can then be represented by a reference to its root
node, r:
Node r;
BinaryTree
We can compute the depth of a node, u, in a binary tree by counting
the number of steps on the path from u to the root:
int depth(Node u) {
int d = 0;
while (u != r) {
u = u.parent;
d++;
BinaryTree
135
§6.1
Binary Trees
}
return d;
}
6.1.1 Recursive Algorithms
Using recursive algorithms makes it very easy to compute facts about bi-
nary trees. For example, to compute the size of (number of nodes in) a
binary tree rooted at node u, we recursively compute the sizes of the two
subtrees rooted at the children of u, sum up these sizes, and add one:
BinaryTree
int size(Node u) {
if (u == nil) return 0;
return 1 + size(u.left) + size(u.right);
}
To compute the height of a node u, we can compute the height of u’s
two subtrees, take the maximum, and add one:
int height(Node u) {
BinaryTree
if (u == nil) return -1;
return 1 + max(height(u.left), height(u.right));
}
6.1.2 Traversing Binary Trees
The two algorithms from the previous section both use recursion to visit
all the nodes in a binary tree. Each of them visits the nodes of the binary
tree in the same order as the following code:
BinaryTree
void traverse(Node u) {
if (u == nil) return;
traverse(u.left);
traverse(u.right);
}
136
BinaryTree: A Basic Binary Tree
§6.1
Using recursion this way produces very short, simple code, but it can
also be problematic. The maximum depth of the recursion is given by the
maximum depth of a node in the binary tree, i.e., the tree’s height. If the
height of the tree is very large, then this recursion could very well use
more stack space than is available, causing a crash.
To traverse a binary tree without recursion, you can use an algorithm
that relies on where it came from to determine where it will go next. See
Figure 6.3. If we arrive at a node u from u.parent, then the next thing to
do is to visit u.left. If we arrive at u from u.left, then the next thing to
do is to visit u.right. If we arrive at u from u.right, then we are done
visiting u’s subtree, and so we return to u.parent. The following code
implements this idea, with code included for handling the cases where
any of u.left, u.right, or u.parent is nil:
BinaryTree
void traverse2() {
Node u = r, prev = nil, next;
while (u != nil) {
if (prev == u.parent) {
if (u.left != nil) next = u.left;
else if (u.right != nil) next = u.right;
else next = u.parent;
} else if (prev == u.left) {
if (u.right != nil) next = u.right;
else next = u.parent;
} else {
next = u.parent;
}
prev = u;
u = next;
}
}
The same facts that can be computed with recursive algorithms can
also be computed in this way, without recursion. For example, to com-
pute the size of the tree we keep a counter, n, and increment n whenever
visiting a node for the first time:
137
§6.1
Binary Trees
Figure 6.3: The three cases that occur at node u when traversing a binary tree
non-recursively, and the resultant traversal of the tree.
BinaryTree
int size2() {
Node u = r, prev = nil, next;
int n = 0;
while (u != nil) {
if (prev == u.parent) {
n++;
if (u.left != nil) next = u.left;
else if (u.right != nil) next = u.right;
else next = u.parent;
} else if (prev == u.left) {
if (u.right != nil) next = u.right;
else next = u.parent;
} else {
next = u.parent;
}
prev = u;
u = next;
}
return n;
}
In some implementations of binary trees, the parent field is not used.
When this is the case, a non-recursive implementation is still possible,
but the implementation has to use a List (or Stack) to keep track of the
path from the current node to the root.
138
uu.parentu.leftu.rightrBinaryTree: A Basic Binary Tree
§6.1
Figure 6.4: During a breadth-first traversal, the nodes of a binary tree are visited
level-by-level, and left-to-right within each level.
A special kind of traversal that does not fit the pattern of the above
functions is the breadth-first traversal.
In a breadth-first traversal, the
nodes are visited level-by-level starting at the root and moving down,
visiting the nodes at each level from left to right (see Figure 6.4). This is
similar to the way that we would read a page of English text. Breadth-first
traversal is implemented using a queue, q, that initially contains only the
root, r. At each step, we extract the next node, u, from q, process u and
add u.left and u.right (if they are non-nil) to q:
void bfTraverse() {
BinaryTree
Queue<Node> q = new LinkedList<Node>();
if (r != nil) q.add(r);
while (!q.isEmpty()) {
Node u = q.remove();
if (u.left != nil) q.add(u.left);
if (u.right != nil) q.add(u.right);
}
}
139
r§6.2
Binary Trees
Figure 6.5: A binary search tree.
6.2 BinarySearchTree: An Unbalanced Binary Search
Tree
A BinarySearchTree is a special kind of binary tree in which each node,
u, also stores a data value, u.x, from some total order. The data values in a
binary search tree obey the binary search tree property: For a node, u, every
data value stored in the subtree rooted at u.left is less than u.x and every
data value stored in the subtree rooted at u.right is greater than u.x. An
example of a BinarySearchTree is shown in Figure 6.5.
6.2.1 Searching
The binary search tree property is extremely useful because it allows us
to quickly locate a value, x, in a binary search tree. To do this we start
searching for x at the root, r. When examining a node, u, there are three
cases:
1. If x < u.x, then the search proceeds to u.left;
2. If x > u.x, then the search proceeds to u.right;
3. If x = u.x, then we have found the node u containing x.
The search terminates when Case 3 occurs or when u = nil. In the former
case, we found x. In the latter case, we conclude that x is not in the binary
140
1461214133571198BinarySearchTree: An Unbalanced Binary Search Tree
§6.2
search tree.
BinarySearchTree
T findEQ(T x) {
Node u = r;
while (u != nil) {
int comp = compare(x, u.x);
if (comp < 0)
u = u.left;
else if (comp > 0)
u = u.right;
else
return u.x;
}
return null;
}
Two examples of searches in a binary search tree are shown in Fig-
ure 6.6. As the second example shows, even if we don’t find x in the tree,
we still gain some valuable information. If we look at the last node, u, at
which Case 1 occurred, we see that u.x is the smallest value in the tree that
is greater than x. Similarly, the last node at which Case 2 occurred con-
tains the largest value in the tree that is less than x. Therefore, by keeping
track of the last node, z, at which Case 1 occurs, a BinarySearchTree can
implement the find(x) operation that returns the smallest value stored in
the tree that is greater than or equal to x:
BinarySearchTree
T find(T x) {
Node w = r, z = nil;
while (w != nil) {
int comp = compare(x, w.x);
if (comp < 0) {
z = w;
w = w.left;
} else if (comp > 0) {
w = w.right;
} else {
return w.x;
}
141
§6.2
Binary Trees
(a)
(b)
Figure 6.6: An example of (a) a successful search (for 6) and (b) an unsuccessful
search (for 10) in a binary search tree.
}
return z == nil ? null : z.x;
}
6.2.2 Addition
To add a new value, x, to a BinarySearchTree, we first search for x. If we
find it, then there is no need to insert it. Otherwise, we store x at a leaf
child of the last node, p, encountered during the search for x. Whether the
new node is the left or right child of p depends on the result of comparing
x and p.x.
BinarySearchTree
boolean add(T x) {
Node p = findLast(x);
return addChild(p, newNode(x));
}
BinarySearchTree
Node findLast(T x) {
Node w = r, prev = nil;
while (w != nil) {
142
14612141335711981461214133571198BinarySearchTree: An Unbalanced Binary Search Tree
§6.2
prev = w;
int comp = compare(x, w.x);
if (comp < 0) {
w = w.left;
} else if (comp > 0) {
w = w.right;
} else {
return w;
}
}
return prev;
}
boolean addChild(Node p, Node u) {
BinarySearchTree
if (p == nil) {
r = u;
} else {
// inserting into empty tree
int comp = compare(u.x, p.x);
if (comp < 0) {
p.left = u;
} else if (comp > 0) {
p.right = u;
} else {
return false;
// u.x is already in the tree
}
u.parent = p;
}
n++;
return true;
}
An example is shown in Figure 6.7. The most time-consuming part
of this process is the initial search for x, which takes an amount of time
proportional to the height of the newly added node u. In the worst case,
this is equal to the height of the BinarySearchTree.
143
§6.2
Binary Trees
Figure 6.7: Inserting the value 8.5 into a binary search tree.
6.2.3 Removal
Deleting a value stored in a node, u, of a BinarySearchTree is a little
more difficult. If u is a leaf, then we can just detach u from its parent.
Even better: If u has only one child, then we can splice u from the tree by
having u.parent adopt u’s child (see Figure 6.8):
BinarySearchTree
void splice(Node u) {
Node s, p;
if (u.left != nil) {
s = u.left;
} else {
s = u.right;
}
if (u == r) {
r = s;
p = nil;
} else {
p = u.parent;
if (p.left == u) {
p.left = s;
} else {
p.right = s;
}
}
if (s != nil) {
144
14612141335711981461214138.53571198BinarySearchTree: An Unbalanced Binary Search Tree
§6.2
Figure 6.8: Removing a leaf (6) or a node with only one child (9) is easy.
s.parent = p;
}
n--;
}
Things get tricky, though, when u has two children. In this case, the
simplest thing to do is to find a node, w, that has less than two children
and such that w.x can replace u.x. To maintain the binary search tree
property, the value w.x should be close to the value of u.x. For example,
choosing w such that w.x is the smallest value greater than u.x will work.
Finding the node w is easy; it is the smallest value in the subtree rooted at
u.right. This node can be easily removed because it has no left child (see
Figure 6.9).
void remove(Node u) {
if (u.left == nil || u.right == nil) {
BinarySearchTree
splice(u);
} else {
Node w = u.right;
while (w.left != nil)
w = w.left;
u.x = w.x;
splice(w);
}
}
145
1345678121413911§6.2
Binary Trees
Figure 6.9: Deleting a value (11) from a node, u, with two children is done by
replacing u’s value with the smallest value in the right subtree of u.
6.2.4 Summary
The find(x), add(x), and remove(x) operations in a BinarySearchTree
each involve following a path from the root of the tree to some node in
the tree. Without knowing more about the shape of the tree it is difficult
to say much about the length of this path, except that it is less than n,
the number of nodes in the tree. The following (unimpressive) theorem
summarizes the performance of the BinarySearchTree data structure:
Theorem 6.1. BinarySearchTree implements the SSet interface and sup-
ports the operations add(x), remove(x), and find(x) in O(n) time per opera-
tion.
Theorem 6.1 compares poorly with Theorem 4.1, which shows that the
SkiplistSSet structure can implement the SSet interface with O(log n)
expected time per operation. The problem with the BinarySearchTree
structure is that it can become unbalanced. Instead of looking like the
tree in Figure 6.5 it can look like a long chain of n nodes, all but the last
having exactly one child.
There are a number of ways of avoiding unbalanced binary search
trees, all of which lead to data structures that have O(log n) time opera-
tions. In Chapter 7 we show how O(log n) expected time operations can
In Chapter 8 we show how O(log n)
be achieved with randomization.
amortized time operations can be achieved with partial rebuilding opera-
tions. In Chapter 9 we show how O(log n) worst-case time operations can
be achieved by simulating a tree that is not binary: one in which nodes
can have up to four children.
146
134567812141391113456781413912Discussion and Exercises
§6.3
6.3 Discussion and Exercises
Binary trees have been used to model relationships for thousands of years.
One reason for this is that binary trees naturally model (pedigree) family
trees. These are the family trees in which the root is a person, the left
and right children are the person’s parents, and so on, recursively. In
more recent centuries binary trees have also been used to model species
trees in biology, where the leaves of the tree represent extant species and
the internal nodes of the tree represent speciation events in which two
populations of a single species evolve into two separate species.
Binary search trees appear to have been discovered independently by
several groups in the 1950s [48, Section 6.2.2]. Further references to spe-
cific kinds of binary search trees are provided in subsequent chapters.
When implementing a binary tree from scratch, there are several de-
sign decisions to be made. One of these is the question of whether or
not each node stores a pointer to its parent. If most of the operations
simply follow a root-to-leaf path, then parent pointers are unnecessary,
waste space, and are a potential source of coding errors. On the other
hand, the lack of parent pointers means that tree traversals must be done
recursively or with the use of an explicit stack. Some other methods (like
inserting or deleting into some kinds of balanced binary search trees) are
also complicated by the lack of parent pointers.
Another design decision is concerned with how to store the parent,
left child, and right child pointers at a node. In the implementation given
here, these pointers are stored as separate variables. Another option is to
store them in an array, p, of length 3, so that u.p[0] is the left child of u,
u.p[1] is the right child of u, and u.p[2] is the parent of u. Using an array
this way means that some sequences of if statements can be simplified
into algebraic expressions.
An example of such a simplification occurs during tree traversal. If a
traversal arrives at a node u from u.p[i], then the next node in the traver-
sal is u.p[(i + 1) mod 3]. Similar examples occur when there is left-right
symmetry. For example, the sibling of u.p[i] is u.p[(i + 1) mod 2]. This
trick works whether u.p[i] is a left child (i = 0) or a right child (i = 1)
of u. In some cases this means that some complicated code that would
otherwise need to have both a left version and right version can be writ-
147
§6.3
Binary Trees
ten only once. See the methods rotateLeft(u) and rotateRight(u) on
page 163 for an example.
Exercise 6.1. Prove that a binary tree having n
1 nodes has n
1 edges.
−
≥
Exercise 6.2. Prove that a binary tree having n
external nodes.
≥
1 real nodes has n + 1
Exercise 6.3. Prove that, if a binary tree, T , has at least one leaf, then
either (a) T ’s root has at most one child or (b) T has more than one leaf.
Exercise 6.4. Implement a non-recursive method, size2(u), that com-
putes the size of the subtree rooted at node u.
Exercise 6.5. Write a non-recursive method, height2(u), that computes
the height of node u in a BinaryTree.
Exercise 6.6. A binary tree is size-balanced if, for every node u, the size
of the subtrees rooted at u.left and u.right differ by at most one. Write
a recursive method, isBalanced(), that tests if a binary tree is balanced.
Your method should run in O(n) time. (Be sure to test your code on some
large trees with different shapes; it is easy to write a method that takes
much longer than O(n) time.)
A pre-order traversal of a binary tree is a traversal that visits each node,
u, before any of its children. An in-order traversal visits u after visiting
all the nodes in u’s left subtree but before visiting any of the nodes in u’s
right subtree. A post-order traversal visits u only after visiting all other
nodes in u’s subtree. The pre/in/post-order numbering of a tree labels
the nodes of a tree with the integers 0, . . . , n
1 in the order that they
are encountered by a pre/in/post-order traversal. See Figure 6.10 for an
example.
−
Exercise 6.7. Create a subclass of BinaryTree whose nodes have fields
for storing pre-order, post-order, and in-order numbers. Write recursive
methods preOrderNumber(), inOrderNumber(), and postOrderNumbers()
that assign these numbers correctly. These methods should each run in
O(n) time.
148
Discussion and Exercises
§6.3
Figure 6.10: Pre-order, post-order, and in-order numberings of a binary tree.
149
214350810119760413211578961001234569111078§6.3
Binary Trees
Exercise 6.8. Implement the non-recursive functions nextPreOrder(u),
nextInOrder(u), and nextPostOrder(u) that return the node that follows
u in a pre-order, in-order, or post-order traversal, respectively. These
functions should take amortized constant time; if we start at any node
u and repeatedly call one of these functions and assign the return value
to u until u = null, then the cost of all these calls should be O(n).
Exercise 6.9. Suppose we are given a binary tree with pre-, post-, and
in-order numbers assigned to the nodes. Show how these numbers can be
used to answer each of the following questions in constant time:
1. Given a node u, determine the size of the subtree rooted at u.
2. Given a node u, determine the depth of u.
3. Given two nodes u and w, determine if u is an ancestor of w
Exercise 6.10. Suppose you are given a list of nodes with pre-order and
in-order numbers assigned. Prove that there is at most one possible tree
with this pre-order/in-order numbering and show how to construct it.
Exercise 6.11. Show that the shape of any binary tree on n nodes can
be represented using at most 2(n
1) bits. (Hint: think about recording
what happens during a traversal and then playing back that recording to
reconstruct the tree.)
−
Exercise 6.12. Illustrate what happens when we add the values 3.5 and
then 4.5 to the binary search tree in Figure 6.5.
Exercise 6.13. Illustrate what happens when we remove the values 3 and
then 5 from the binary search tree in Figure 6.5.
Exercise 6.14. Implement a BinarySearchTree method, getLE(x), that
returns a list of all items in the tree that are less than or equal to x. The
running time of your method should be O(n(cid:48) + h) where n(cid:48) is the number
of items less than or equal to x and h is the height of the tree.
Exercise 6.15. Describe how to add the elements
to an initially
empty BinarySearchTree in such a way that the resulting tree has height
n
1. How many ways are there to do this?
1, . . . , n
{
}
−
150
Discussion and Exercises
§6.3
Exercise 6.16. If we have some BinarySearchTree and perform the op-
erations add(x) followed by remove(x) (with the same value of x) do we
necessarily return to the original tree?
Exercise 6.17. Can a remove(x) operation increase the height of any node
in a BinarySearchTree? If so, by how much?
Exercise 6.18. Can an add(x) operation increase the height of any node
in a BinarySearchTree? Can it increase the height of the tree? If so, by
how much?
Exercise 6.19. Design and implement a version of BinarySearchTree
in which each node, u, maintains values u.size (the size of the subtree
rooted at u), u.depth (the depth of u), and u.height (the height of the
subtree rooted at u).
These values should be maintained, even during calls to the add(x)
and remove(x) operations, but this should not increase the cost of these
operations by more than a constant factor.
151
Chapter 7
Random Binary Search Trees
In this chapter, we present a binary search tree structure that uses ran-
domization to achieve O(log n) expected time for all operations.
7.1 Random Binary Search Trees
Consider the two binary search trees shown in Figure 7.1, each of which
has n = 15 nodes. The one on the left is a list and the other is a perfectly
balanced binary search tree. The one on the left has a height of n
1 = 14
and the one on the right has a height of three.
−
Imagine how these two trees could have been constructed. The one on
the left occurs if we start with an empty BinarySearchTree and add the
sequence
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
(cid:105)
(cid:104)
.
No other sequence of additions will create this tree (as you can prove by
induction on n). On the other hand, the tree on the right can be created
by the sequence
7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 8, 10, 12, 14
(cid:105)
(cid:104)
Other sequences work as well, including
and
7, 3, 1, 5, 0, 2, 4, 6, 11, 9, 13, 8, 10, 12, 14
(cid:105)
(cid:104)
7, 3, 1, 11, 5, 0, 2, 4, 6, 9, 13, 8, 10, 12, 14
(cid:105)
(cid:104)
.
,
.
153
§7.1
Random Binary Search Trees
Figure 7.1: Two binary search trees containing the integers 0, . . . , 14.
In fact, there are 21, 964, 800 addition sequences that generate the tree on
the right and only one that generates the tree on the left.
The above example gives some anecdotal evidence that, if we choose a
random permutation of 0, . . . , 14, and add it into a binary search tree, then
we are more likely to get a very balanced tree (the right side of Figure 7.1)
than we are to get a very unbalanced tree (the left side of Figure 7.1).
We can formalize this notion by studying random binary search trees.
A random binary search tree of size n is obtained in the following way: Take
a random permutation, x0, . . . , xn
1 and add its
elements, one by one, into a BinarySearchTree. By random permutation
we mean that each of the possible n! permutations (orderings) of 0, . . . , n
1
is equally likely, so that the probability of obtaining any particular per-
mutation is 1/n!.
1, of the integers 0, . . . , n
−
−
−
Note that the values 0, . . . , n
1 could be replaced by any ordered set of
n elements without changing any of the properties of the random binary
search tree. The element x
is simply standing in for the
element of rank x in an ordered set of size n.
0, . . . , n
∈ {
−
−
1
}
Before we can present our main result about random binary search
trees, we must take some time for a short digression to discuss a type of
number that comes up frequently when studying randomized structures.
For a non-negative integer, k, the k-th harmonic number, denoted Hk, is
154
0123...1401234567810121413911Random Binary Search Trees
§7.1
k
Figure 7.2: The kth harmonic number Hk =
i=1 1/i is upper- and lower-bounded
by two integrals. The value of these integrals is given by the area of the shaded
region, while the value of Hk is given by the area of the rectangles.
(cid:80)
defined as
Hk = 1 + 1/2 + 1/3 +
+ 1/k .
· · ·
The harmonic number Hk has no simple closed form, but it is very closely
related to the natural logarithm of k. In particular,
ln k < Hk ≤
ln k + 1 .
Readers who have studied calculus might notice that this is because the
k
integral
(1/x) dx = ln k. Keeping in mind that an integral can be in-
1
terpreted as the area between a curve and the x-axis, the value of Hk
k
(1/x) dx and upper-bounded by
can be lower-bounded by the integral
1
(cid:82)
1 +
k
(1/x) dx. (See Figure 7.2 for a graphical explanation.)
1
(cid:82)
(cid:82)
Lemma 7.1. In a random binary search tree of size n, the following statements
hold:
1. For any x
Hx+1 + Hn
0, . . . , n
−
O(1).1
∈ {
x
−
−
, the expected length of the search path for x is
1
}
0, . . . , n
1
}
−
, the expected length of the search path
1, n)
2. For any x
for x is H
(cid:100)
\ {
.
x
−(cid:100)
(cid:101)
1The expressions x+1 and n
(
−
+ Hn
∈
x
(cid:101)
x can be interpreted respectively as the number of elements
in the tree less than or equal to x and the number of elements in the tree greater than or
equal to x.
−
155
11/21/31/k...1230k...f(x)=1/x11/21/31/k...123k...§7.1
Random Binary Search Trees
We will prove Lemma 7.1 in the next section. For now, consider what
the two parts of Lemma 7.1 tell us. The first part tells us that if we search
for an element in a tree of size n, then the expected length of the search
path is at most 2 ln n+O(1). The second part tells us the same thing about
searching for a value not stored in the tree. When we compare the two
parts of the lemma, we see that it is only slightly faster to search for some-
thing that is in the tree compared to something that is not.
7.1.1 Proof of Lemma 7.1
The key observation needed to prove Lemma 7.1 is the following: The
search path for a value x in the open interval (
1, n) in a random binary
search tree, T , contains the node with key i < x if, and only if, in the
random permutation used to create T , i appears before any of
i + 1, i +
2, . . . ,
−
x
{
.
(cid:98)
(cid:99)}
−
x
(cid:98)
1,
(cid:99)}
x
(cid:99)
(cid:98)
To see this, refer to Figure 7.3 and notice that until some value in
is added, the search paths for each value in the open in-
i, i + 1, . . . ,
{
terval (i
+ 1) are identical. (Remember that for two values to have
different search paths, there must be some element in the tree that com-
pares differently with them.) Let j be the first element in
to
appear in the random permutation. Notice that j is now and will always
be on the search path for x. If j (cid:44) i then the node uj containing j is created
before the node ui that contains i. Later, when i is added, it will be added
to the subtree rooted at uj .left, since i < j. On the other hand, the search
path for x will never visit this subtree because it will proceed to uj .right
after visiting uj .
i, i +1, . . . ,
{
(cid:99)}
x
(cid:98)
Similarly, for i > x, i appears in the search path for x if and only if
in the random permutation
+ 1, . . . , i
i appears before any of
used to create T .
x
,
(cid:101)
x
(cid:100)
(cid:101)
{(cid:100)
1
}
−
Notice that, if we start with a random permutation of
x
,
(cid:101)
, then
the subsequences containing only
1
}
are also random permutations of their respective elements. Each element,
is equally likely
and
then, in the subsets
to appear before any other in its subset in the random permutation used
0, . . . , n
{
}
x
(cid:101)
(cid:100)
i, i + 1, . . . ,
i, i+1, . . . ,
+ 1, . . . , i
+1, . . . , i
x
,
(cid:101)
and
x
(cid:100)
x
(cid:98)
{(cid:100)
(cid:99)}
(cid:99)}
{(cid:100)
−
−
1
x
(cid:101)
(cid:98)
}
{
{
156
Random Binary Search Trees
§7.1
Figure 7.3: The value i < x is on the search path for x if and only if i is the first
added to the tree.
element among
x
i, i + 1, . . . ,
{
(cid:98)
(cid:99)}
to create T . So we have
Pr
i is on the search path for x
}
{
=
x
1/(
(cid:98)
1/(i
(cid:99) −
x
− (cid:100)
i + 1)
+ 1)
(cid:101)
(cid:40)
if i < x
if i > x
.
With this observation, the proof of Lemma 7.1 involves some simple
calculations with harmonic numbers:
Proof of Lemma 7.1. Let Ii be the indicator random variable that is equal
to one when i appears on the search path for x and zero otherwise. Then
the length of the search path is given by
Ii
0,...,n
1
(cid:88)i
−
∈{
x
}
}\{
so, if x
0, . . . , n
1
}
−
∈ {
, the expected length of the search path is given by
157
...,i,...,j−1j+1,...,bxc,...j§7.1
Random Binary Search Trees
(a)
(b)
Figure 7.4: The probabilities of an element being on the search path for x when
(a) x is an integer and (b) when x is not an integer.
(see Figure 7.4.a)
Ii +
n
1
−
(cid:88)i=x+1
x
1
−
(cid:88)i=0
E
Ii
=
=
=
=
x
1
−
(cid:88)i=0
x
1
−
(cid:88)i=0
x
1
−
E [Ii] +
n
1
−
(cid:88)i=x+1
E [Ii]
n
1
−
(cid:88)i=x+1
n
1
−
1/(
x
(cid:98)
(cid:99) −
i + 1) +
1/(i
x
(cid:101)
− (cid:100)
+ 1)
1/(x
i + 1) +
−
1/(i
x + 1)
−
(cid:88)i=x+1
(cid:88)i=0
1
+
2
1
3
+
+
· · ·
+
+
1
3
1
2
= Hx+1 + Hn
+
· · ·
x
−
−
1
x + 1
1
+
n
−
2 .
x
The corresponding calculations for a search value x
are almost identical (see Figure 7.4.b).
1, n)
(
−
∈
\ {
0, . . . , n
1
}
−
7.1.2 Summary
The following theorem summarizes the performance of a random binary
search tree:
158
01x−1xx+1n−1121213131x+11x1n−x············iPr{Ii=1}01bxcdxen−111212131bxc+11bxc1n−bxc············iPr{Ii=1}131Treap: A Randomized Binary Search Tree
§7.2
Theorem 7.1. A random binary search tree can be constructed in O(n log n)
time. In a random binary search tree, the find(x) operation takes O(log n)
expected time.
We should emphasize again that the expectation in Theorem 7.1 is
with respect to the random permutation used to create the random binary
search tree. In particular, it does not depend on a random choice of x; it
is true for every value of x.
7.2 Treap: A Randomized Binary Search Tree
The problem with random binary search trees is, of course, that they
are not dynamic. They don’t support the add(x) or remove(x) operations
needed to implement the SSet interface. In this section we describe a
data structure called a Treap that uses Lemma 7.1 to implement the SSet
interface.2
A node in a Treap is like a node in a BinarySearchTree in that it has
a data value, x, but it also contains a unique numerical priority, p, that is
assigned at random:
class Node<T> extends BSTNode<Node<T>,T> {
Treap
int p;
}
In addition to being a binary search tree, the nodes in a Treap also
obey the heap property:
• (Heap Property) At every node u, except the root, u.parent.p < u.p.
In other words, each node has a priority smaller than that of its two chil-
dren. An example is shown in Figure 7.5.
The heap and binary search tree conditions together ensure that, once
the key (x) and priority (p) for each node are defined, the shape of the
Treap is completely determined. The heap property tells us that the node
2The names Treap comes from the fact that this data structure is simultaneously a binary
search tree (Section 6.2) and a heap (Chapter 10).
159
§7.2
Random Binary Search Trees
Figure 7.5: An example of a Treap containing the integers 0, . . . , 9. Each node, u,
is illustrated as a box containing u.x, u.p.
with minimum priority has to be the root, r, of the Treap. The binary
search tree property tells us that all nodes with keys smaller than r.x are
stored in the subtree rooted at r.left and all nodes with keys larger than
r.x are stored in the subtree rooted at r.right.
The important point about the priority values in a Treap is that they
are unique and assigned at random. Because of this, there are two equiv-
alent ways we can think about a Treap. As defined above, a Treap obeys
the heap and binary search tree properties. Alternatively, we can think
of a Treap as a BinarySearchTree whose nodes were added in increasing
order of priority. For example, the Treap in Figure 7.5 can be obtained by
adding the sequence of (x, p) values
(3, 1), (1, 6), (0, 9), (5, 11), (4, 14), (9, 17), (7, 22), (6, 42), (8, 49), (2, 99)
(cid:105)
(cid:104)
into a BinarySearchTree.
Since the priorities are chosen randomly, this is equivalent to taking a
random permutation of the keys—in this case the permutation is
3, 1, 0, 5, 9, 4, 7, 6, 8, 2
(cid:104)
(cid:105)
—and adding these to a BinarySearchTree. But this means that the
shape of a treap is identical to that of a random binary search tree. In
160
6,420,91,62,993,15,114,147,229,178,49Treap: A Randomized Binary Search Tree
§7.2
particular, if we replace each key x by its rank,3 then Lemma 7.1 applies.
Restating Lemma 7.1 in terms of Treaps, we have:
Lemma 7.2. In a Treap that stores a set S of n keys, the following statements
hold:
1. For any x
S, the expected length of the search path for x is Hr(x)+1 +
∈
O(1).
Hn
−
r(x) −
2. For any x (cid:60) S, the expected length of the search path for x is Hr(x) +
r(x).
Hn
−
Here, r(x) denotes the rank of x in the set S
x
}
∪ {
.
Again, we emphasize that the expectation in Lemma 7.2 is taken over
the random choices of the priorities for each node. It does not require any
assumptions about the randomness in the keys.
Lemma 7.2 tells us that Treaps can implement the find(x) operation
efficiently. However, the real benefit of a Treap is that it can support the
add(x) and delete(x) operations. To do this, it needs to perform rotations
in order to maintain the heap property. Refer to Figure 7.6. A rotation
in a binary search tree is a local modification that takes a parent u of a
node w and makes w the parent of u, while preserving the binary search
tree property. Rotations come in two flavours: left or right depending on
whether w is a right or left child of u, respectively.
The code that implements this has to handle these two possibilities
and be careful of a boundary case (when u is the root), so the actual code
is a little longer than Figure 7.6 would lead a reader to believe:
BinarySearchTree
void rotateLeft(Node u) {
Node w = u.right;
w.parent = u.parent;
if (w.parent != nil) {
if (w.parent.left == u) {
w.parent.left = w;
} else {
3The rank of an element x in a set S of elements is the number of elements in S that are
less than x.
161
§7.2
Random Binary Search Trees
Figure 7.6: Left and right rotations in a binary search tree.
w.parent.right = w;
}
}
u.right = w.left;
if (u.right != nil) {
u.right.parent = u;
}
u.parent = w;
w.left = u;
if (u == r) { r = w; r.parent = nil; }
}
void rotateRight(Node u) {
Node w = u.left;
w.parent = u.parent;
if (w.parent != nil) {
if (w.parent.left == u) {
w.parent.left = w;
} else {
w.parent.right = w;
}
}
u.left = w.right;
if (u.left != nil) {
u.left.parent = u;
}
u.parent = w;
w.right = u;
162
rotateRight(u)⇒⇐rotateLeft(w)ABCwuABCuwTreap: A Randomized Binary Search Tree
§7.2
if (u == r) { r = w; r.parent = nil; }
}
In terms of the Treap data structure, the most important property of
a rotation is that the depth of w decreases by one while the depth of u
increases by one.
Using rotations, we can implement the add(x) operation as follows:
We create a new node, u, assign u.x = x, and pick a random value for u.p.
Next we add u using the usual add(x) algorithm for a BinarySearchTree,
so that u is now a leaf of the Treap. At this point, our Treap satisfies
the binary search tree property, but not necessarily the heap property. In
particular, it may be the case that u.parent.p > u.p. If this is the case, then
we perform a rotation at node w=u.parent so that u becomes the parent of
w. If u continues to violate the heap property, we will have to repeat this,
decreasing u’s depth by one every time, until u either becomes the root or
u.parent.p < u.p.
Treap
boolean add(T x) {
Node<T> u = newNode();
u.x = x;
u.p = rand.nextInt();
if (super.add(u)) {
bubbleUp(u);
return true;
}
return false;
}
void bubbleUp(Node<T> u) {
while (u.parent != nil && u.parent.p > u.p) {
if (u.parent.right == u) {
rotateLeft(u.parent);
} else {
rotateRight(u.parent);
}
}
if (u.parent == nil) {
r = u;
}
163
§7.2
}
Random Binary Search Trees
An example of an add(x) operation is shown in Figure 7.7.
The running time of the add(x) operation is given by the time it takes
to follow the search path for x plus the number of rotations performed
to move the newly-added node, u, up to its correct location in the Treap.
By Lemma 7.2, the expected length of the search path is at most 2 ln n +
O(1). Furthermore, each rotation decreases the depth of u. This stops if
u becomes the root, so the expected number of rotations cannot exceed
the expected length of the search path. Therefore, the expected running
time of the add(x) operation in a Treap is O(log n).
(Exercise 7.5 asks
you to show that the expected number of rotations performed during an
addition is actually only O(1).)
The remove(x) operation in a Treap is the opposite of the add(x) op-
eration. We search for the node, u, containing x, then perform rotations
to move u downwards until it becomes a leaf, and then we splice u from
the Treap. Notice that, to move u downwards, we can perform either a
left or right rotation at u, which will replace u with u.right or u.left,
respectively. The choice is made by the first of the following that apply:
1. If u.left and u.right are both null, then u is a leaf and no rotation
is performed.
2. If u.left (or u.right) is null, then perform a right (or left, respec-
tively) rotation at u.
3. If u.left.p < u.right.p (or u.left.p > u.right.p), then perform a
right rotation (or left rotation, respectively) at u.
These three rules ensure that the Treap doesn’t become disconnected and
that the heap property is restored once u is removed.
boolean remove(T x) {
Node<T> u = findLast(x);
if (u != nil && compare(u.x, x) == 0) {
Treap
trickleDown(u);
splice(u);
return true;
164
Treap: A Randomized Binary Search Tree
§7.2
Figure 7.7: Adding the value 1.5 into the Treap from Figure 7.5.
165
6,420,91,62,993,15,114,147,229,148,491.5,46,420,91,62,993,15,114,147,229,148,491.5,46,420,91,62,993,15,114,147,229,148,491.5,4§7.2
Random Binary Search Trees
}
return false;
}
void trickleDown(Node<T> u) {
while (u.left != nil || u.right != nil) {
if (u.left == nil) {
rotateLeft(u);
} else if (u.right == nil) {
rotateRight(u);
} else if (u.left.p < u.right.p) {
rotateRight(u);
} else {
rotateLeft(u);
}
if (r == u) {
r = u.parent;
}
}
}
An example of the remove(x) operation is shown in Figure 7.8.
The trick to analyze the running time of the remove(x) operation is to
notice that this operation reverses the add(x) operation. In particular, if
we were to reinsert x, using the same priority u.p, then the add(x) opera-
tion would do exactly the same number of rotations and would restore the
Treap to exactly the same state it was in before the remove(x) operation
took place. (Reading from bottom-to-top, Figure 7.8 illustrates the addi-
tion of the value 9 into a Treap.) This means that the expected running
time of the remove(x) on a Treap of size n is proportional to the expected
running time of the add(x) operation on a Treap of size n
1. We conclude
that the expected running time of remove(x) is O(log n).
−
7.2.1 Summary
The following theorem summarizes the performance of the Treap data
structure:
Theorem 7.2. A Treap implements the SSet interface. A Treap supports
the operations add(x), remove(x), and find(x) in O(log n) expected time per
166
Treap: A Randomized Binary Search Tree
§7.2
Figure 7.8: Removing the value 9 from the Treap in Figure 7.5.
167
6,420,91,62,993,15,114,147,229,178,496,420,91,62,993,15,114,147,229,178,496,420,91,62,993,15,114,147,229,178,496,420,91,62,993,15,114,147,228,49§7.3
Random Binary Search Trees
operation.
It is worth comparing the Treap data structure to the SkiplistSSet
data structure. Both implement the SSet operations in O(log n) expected
time per operation. In both data structures, add(x) and remove(x) involve
a search and then a constant number of pointer changes (see Exercise 7.5
below). Thus, for both these structures, the expected length of the search
path is the critical value in assessing their performance. In a SkiplistS-
Set, the expected length of a search path is
2 log n + O(1) ,
In a Treap, the expected length of a search path is
2 ln n + O(1)
≈
1.386 log n + O(1) .
Thus, the search paths in a Treap are considerably shorter and this trans-
lates into noticeably faster operations on Treaps than Skiplists. Exer-
cise 4.7 in Chapter 4 shows how the expected length of the search path in
a Skiplist can be reduced to
e ln n + O(1)
≈
1.884 log n + O(1)
by using biased coin tosses. Even with this optimization, the expected
length of search paths in a SkiplistSSet is noticeably longer than in a
Treap.
7.3 Discussion and Exercises
Random binary search trees have been studied extensively. Devroye [19]
gives a proof of Lemma 7.1 and related results. There are much stronger
results in the literature as well, the most impressive of which is due to
Reed [64], who shows that the expected height of a random binary search
tree is
α ln n
−
β ln ln n + O(1)
where α
equation α ln((2e/α)) = 1 and β =
the height is constant.
4.31107 is the unique solution on the interval [2,
3
) of the
2 ln(α/2) . Furthermore, the variance of
∞
≈
168
Discussion and Exercises
§7.3
The name Treap was coined by Seidel and Aragon [67] who discussed
Treaps and some of their variants. However, their basic structure was
studied much earlier by Vuillemin [76] who called them Cartesian trees.
One possible space-optimization of the Treap data structure is the
elimination of the explicit storage of the priority p in each node.
In-
stead, the priority of a node, u, is computed by hashing u’s address in
memory (in 32-bit Java, this is equivalent to hashing u.hashCode()). Al-
though a number of hash functions will probably work well for this in
practice, for the important parts of the proof of Lemma 7.1 to remain
valid, the hash function should be randomized and have the min-wise in-
dependent property: For any distinct values x1, . . . , xk, each of the hash val-
ues h(x1), . . . , h(xk) should be distinct with high probability and, for each
i
1, . . . , k
∈ {
,
}
h(xi) = min
Pr
{
h(x1), . . . , h(xk)
{
}} ≤
c/k
for some constant c. One such class of hash functions that is easy to im-
plement and fairly fast is tabulation hashing (Section 5.2.3).
Another Treap variant that doesn’t store priorities at each node is the
randomized binary search tree of Mart´ınez and Roura [51]. In this vari-
ant, every node, u, stores the size, u.size, of the subtree rooted at u. Both
the add(x) and remove(x) algorithms are randomized. The algorithm for
adding x to the subtree rooted at u does the following:
1. With probability 1/(size(u) + 1), the value x is added the usual way,
as a leaf, and rotations are then done to bring x up to the root of this
subtree.
2. Otherwise (with probability 1
1/(size(u) + 1)), the value x is re-
cursively added into one of the two subtrees rooted at u.left or
u.right, as appropriate.
−
The first case corresponds to an add(x) operation in a Treap where x’s
node receives a random priority that is smaller than any of the size(u)
priorities in u’s subtree, and this case occurs with exactly the same prob-
ability.
Removing a value x from a randomized binary search tree is similar to
the process of removing from a Treap. We find the node, u, that contains
x and then perform rotations that repeatedly increase the depth of u until
169
§7.3
Random Binary Search Trees
it becomes a leaf, at which point we can splice it from the tree. The choice
of whether to perform a left or right rotation at each step is randomized.
1. With probability u.left.size/(u.size
1), we perform a right rota-
tion at u, making u.left the root of the subtree that was formerly
rooted at u.
−
2. With probability u.right.size/(u.size
1), we perform a left rota-
tion at u, making u.right the root of the subtree that was formerly
rooted at u.
−
Again, we can easily verify that these are exactly the same probabilities
that the removal algorithm in a Treap will perform a left or right rotation
of u.
Randomized binary search trees have the disadvantage, compared to
treaps, that when adding and removing elements they make many ran-
dom choices, and they must maintain the sizes of subtrees. One advan-
tage of randomized binary search trees over treaps is that subtree sizes
can serve another useful purpose, namely to provide access by rank in
O(log n) expected time (see Exercise 7.10). In comparison, the random
priorities stored in treap nodes have no use other than keeping the treap
balanced.
Exercise 7.1. Illustrate the addition of 4.5 (with priority 7) and then 7.5
(with priority 20) on the Treap in Figure 7.5.
Exercise 7.2. Illustrate the removal of 5 and then 7 on the Treap in Fig-
ure 7.5.
Exercise 7.3. Prove the assertion that there are 21, 964, 800 sequences
that generate the tree on the right hand side of Figure 7.1. (Hint: Give a
recursive formula for the number of sequences that generate a complete
binary tree of height h and evaluate this formula for h = 3.)
Exercise 7.4. Design and implement the permute(a) method that takes as
input an array, a, that contains n distinct values and randomly permutes
a. The method should run in O(n) time and you should prove that each
of the n! possible permutations of a is equally probable.
170
Discussion and Exercises
§7.3
Exercise 7.5. Use both parts of Lemma 7.2 to prove that the expected
number of rotations performed by an add(x) operation (and hence also a
remove(x) operation) is O(1).
Exercise 7.6. Modify the Treap implementation given here so that it does
not explicitly store priorities. Instead, it should simulate them by hashing
the hashCode() of each node.
Exercise 7.7. Suppose that a binary search tree stores, at each node, u,
the height, u.height, of the subtree rooted at u, and the size, u.size of the
subtree rooted at u.
1. Show how, if we perform a left or right rotation at u, then these two
quantities can be updated, in constant time, for all nodes affected
by the rotation.
2. Explain why the same result is not possible if we try to also store
the depth, u.depth, of each node u.
Exercise 7.8. Design and implement an algorithm that constructs a Treap
from a sorted array, a, of n elements. This method should run in O(n)
worst-case time and should construct a Treap that is indistinguishable
from one in which the elements of a were added one at a time using the
add(x) method.
Exercise 7.9. This exercise works out the details of how one can effi-
ciently search a Treap given a pointer that is close to the node we are
searching for.
1. Design and implement a Treap implementation in which each node
keeps track of the minimum and maximum values in its subtree.
2. Using this extra information, add a fingerFind(x, u) method that
executes the find(x) operation with the help of a pointer to the node
u (which is hopefully not far from the node that contains x). This
operation should start at u and walk upwards until it reaches a node
w.max. From that point onwards, it should
w such that w.min
perform a standard search for x starting from w.
(One can show
that fingerFind(x, u) takes O(1 + log r) time, where r is the number
of elements in the treap whose value is between x and u.x.)
≤
≤
x
171
§7.3
Random Binary Search Trees
3. Extend your implementation into a version of a treap that starts
all its find(x) operations from the node most recently found by
find(x).
Exercise 7.10. Design and implement a version of a Treap that includes
a get(i) operation that returns the key with rank i in the Treap. (Hint:
Have each node, u, keep track of the size of the subtree rooted at u.)
Exercise 7.11. Implement a TreapList, an implementation of the List
interface as a treap. Each node in the treap should store a list item, and
an in-order traversal of the treap finds the items in the same order that
they occur in the list. All the List operations get(i), set(i, x), add(i, x)
and remove(i) should run in O(log n) expected time.
Exercise 7.12. Design and implement a version of a Treap that supports
the split(x) operation. This operation removes all values from the Treap
that are greater than x and returns a second Treap that contains all the
removed values.
Example: the code t2 = t.split(x) removes from t all values greater than
x and returns a new Treap t2 containing all these values. The split(x)
operation should run in O(log n) expected time.
Warning: For this modification to work properly and still allow the size()
method to run in constant time, it is necessary to implement the modifi-
cations in Exercise 7.10.
Exercise 7.13. Design and implement a version of a Treap that supports
the absorb(t2) operation, which can be thought of as the inverse of the
split(x) operation. This operation removes all values from the Treap
t2 and adds them to the receiver. This operation presupposes that the
smallest value in t2 is greater than the largest value in the receiver. The
absorb(t2) operation should run in O(log n) expected time.
Exercise 7.14. Implement Martinez’s randomized binary search trees, as
discussed in this section. Compare the performance of your implementa-
tion with that of the Treap implementation.
172
Chapter 8
Scapegoat Trees
In this chapter, we study a binary search tree data structure, the Scape-
goatTree. This structure is based on the common wisdom that, when
something goes wrong, the first thing people tend to do is find someone
to blame (the scapegoat). Once blame is firmly established, we can leave
the scapegoat to fix the problem.
A ScapegoatTree keeps itself balanced by partial rebuilding opera-
tions. During a partial rebuilding operation, an entire subtree is decon-
structed and rebuilt into a perfectly balanced subtree. There are many
ways of rebuilding a subtree rooted at node u into a perfectly balanced
tree. One of the simplest is to traverse u’s subtree, gathering all its nodes
into an array, a, and then to recursively build a balanced subtree using
a. If we let m = a.length/2, then the element a[m] becomes the root of the
new subtree, a[0], . . . , a[m
1] get stored recursively in the left subtree and
a[m + 1], . . . , a[a.length
−
1] get stored recursively in the right subtree.
−
void rebuild(Node<T> u) {
ScapegoatTree
int ns = size(u);
Node<T> p = u.parent;
Node<T>[] a = Array.newInstance(Node.class, ns);
packIntoArray(u, a, 0);
if (p == nil) {
r = buildBalanced(a, 0, ns);
r.parent = nil;
} else if (p.right == u) {
p.right = buildBalanced(a, 0, ns);
173
§8.1
Scapegoat Trees
p.right.parent = p;
} else {
p.left = buildBalanced(a, 0, ns);
p.left.parent = p;
}
}
int packIntoArray(Node<T> u, Node<T>[] a, int i) {
if (u == nil) {
return i;
}
i = packIntoArray(u.left, a, i);
a[i++] = u;
return packIntoArray(u.right, a, i);
}
Node<T> buildBalanced(Node<T>[] a, int i, int ns) {
if (ns == 0)
return nil;
int m = ns / 2;
a[i + m].left = buildBalanced(a, i, m);
if (a[i + m].left != nil)
a[i + m].left.parent = a[i + m];
a[i + m].right = buildBalanced(a, i + m + 1, ns - m - 1);
if (a[i + m].right != nil)
a[i + m].right.parent = a[i + m];
return a[i + m];
}
A call to rebuild(u) takes O(size(u)) time. The resulting subtree has
minimum height; there is no tree of smaller height that has size(u) nodes.
8.1 ScapegoatTree: A Binary Search Tree with Partial
Rebuilding
A ScapegoatTree is a BinarySearchTree that, in addition to keeping
track of the number, n, of nodes in the tree also keeps a counter, q, that
maintains an upper-bound on the number of nodes.
int q;
ScapegoatTree
174
ScapegoatTree: A Binary Search Tree with Partial Rebuilding
§8.1
Figure 8.1: A ScapegoatTree with 10 nodes and height 5.
At all times, n and q obey the following inequalities:
q/2
n
≤
≤
q .
In addition, a ScapegoatTree has logarithmic height; at all times, the
height of the scapegoat tree does not exceed
log3/2 q
≤
log3/2 2n < log3/2 n + 2 .
(8.1)
Even with this constraint, a ScapegoatTree can look surprisingly unbal-
anced. The tree in Figure 8.1 has q = n = 10 and height 5 < log3/2 10
5.679.
≈
Implementing the find(x) operation in a ScapegoatTree is done us-
ing the standard algorithm for searching in a BinarySearchTree (see Sec-
tion 6.2). This takes time proportional to the height of the tree which, by
(8.1) is O(log n).
To implement the add(x) operation, we first increment n and q and
then use the usual algorithm for adding x to a binary search tree; we
search for x and then add a new leaf u with u.x = x. At this point, we may
get lucky and the depth of u might not exceed log3/2 q. If so, then we leave
well enough alone and don’t do anything else.
Unfortunately, it will sometimes happen that depth(u) > log3/2 q. In
this case, we need to reduce the height. This isn’t a big job; there is only
175
0123456789§8.1
Scapegoat Trees
one node, namely u, whose depth exceeds log3/2 q. To fix u, we walk from
u back up to the root looking for a scapegoat, w. The scapegoat, w, is a very
unbalanced node. It has the property that
size(w.child)
size(w)
>
2
3
,
(8.2)
where w.child is the child of w on the path from the root to u. We’ll very
shortly prove that a scapegoat exists. For now, we can take it for granted.
Once we’ve found the scapegoat w, we completely destroy the subtree
rooted at w and rebuild it into a perfectly balanced binary search tree. We
know, from (8.2), that, even before the addition of u, w’s subtree was not a
complete binary tree. Therefore, when we rebuild w, the height decreases
by at least 1 so that the height of the ScapegoatTree is once again at most
log3/2 q.
boolean add(T x) {
ScapegoatTree
// first do basic insertion keeping track of depth
Node<T> u = newNode(x);
int d = addWithDepth(u);
if (d > log32(q)) {
// depth exceeded, find scapegoat
Node<T> w = u.parent;
while (3*size(w) <= 2*size(w.parent))
w = w.parent;
rebuild(w.parent);
}
return d >= 0;
}
If we ignore the cost of finding the scapegoat w and rebuilding the
subtree rooted at w, then the running time of add(x) is dominated by the
initial search, which takes O(log q) = O(log n) time. We will account for
the cost of finding the scapegoat and rebuilding using amortized analysis
in the next section.
The implementation of remove(x) in a ScapegoatTree is very simple.
We search for x and remove it using the usual algorithm for removing a
node from a BinarySearchTree. (Note that this can never increase the
176
ScapegoatTree: A Binary Search Tree with Partial Rebuilding
§8.1
Figure 8.2: Inserting 3.5 into a ScapegoatTree increases its height to 6, which vio-
lates (8.1) since 6 > log3/2 11
5.914. A scapegoat is found at the node containing
5.
≈
height of the tree.) Next, we decrement n, but leave q unchanged. Finally,
we check if q > 2n and, if so, then we rebuild the entire tree into a perfectly
balanced binary search tree and set q = n.
boolean remove(T x) {
if (super.remove(x)) {
ScapegoatTree
if (2*n < q) {
rebuild(r);
q = n;
}
return true;
}
return false;
}
Again, if we ignore the cost of rebuilding, the running time of the
remove(x) operation is proportional to the height of the tree, and is there-
fore O(log n).
177
01234567893.512233667>2367890123453.5§8.1
Scapegoat Trees
8.1.1 Analysis of Correctness and Running-Time
In this section, we analyze the correctness and amortized running time of
operations on a ScapegoatTree. We first prove the correctness by show-
ing that, when the add(x) operation results in a node that violates Condi-
tion (8.1), then we can always find a scapegoat:
Lemma 8.1. Let u be a node of depth h > log3/2 q in a ScapegoatTree. Then
there exists a node w on the path from u to the root such that
size(w)
size(parent(w))
> 2/3 .
Proof. Suppose, for the sake of contradiction, that this is not the case, and
size(w)
size(parent(w)) ≤
2/3 .
for all nodes w on the path from u to the root. Denote the path from the
2
root to u as r = u0, . . . , uh = u. Then, we have size(u0) = n, size(u1)
3 n,
size(u2)
4
9 n and, more generally,
≤
≤
size(ui)
≤
i
n .
2
3
(cid:18)
(cid:19)
But this gives a contradiction, since size(u)
1, hence
≥
size(u)
1
≤
2
3
≤
(cid:18)
h
n <
(cid:19)
2
3
(cid:18)
log3/2 q
(cid:19)
n
≤
2
3
(cid:18)
(cid:19)
log3/2 n
n =
n = 1 .
1
n
(cid:18)
(cid:19)
Next, we analyze the parts of the running time that are not yet ac-
counted for. There are two parts: The cost of calls to size(u) when search-
ing for scapegoat nodes, and the cost of calls to rebuild(w) when we find
a scapegoat w. The cost of calls to size(u) can be related to the cost of
calls to rebuild(w), as follows:
Lemma 8.2. During a call to add(x) in a ScapegoatTree, the cost of finding
the scapegoat w and rebuilding the subtree rooted at w is O(size(w)).
Proof. The cost of rebuilding the scapegoat node w, once we find it, is
O(size(w)). When searching for the scapegoat node, we call size(u) on a
178
ScapegoatTree: A Binary Search Tree with Partial Rebuilding
§8.1
sequence of nodes u0, . . . , uk until we find the scapegoat uk = w. However,
since uk is the first node in this sequence that is a scapegoat, we know that
size(ui) <
2
3
size(ui+1)
for all i
0, . . . , k
2
}
−
∈ {
. Therefore, the cost of all calls to size(u) is
k
O
(cid:88)i=0
size(uk
i)
−
= O
= O
1
k
−
(cid:88)i=0
1
k
−
size(uk) +
size(uk) +
size(uk
1)
−
i
−
size(uk)
i
(cid:19)
2
3
1
(cid:88)i=0 (cid:18)
k
−
= O
size(uk)
1 +
= O(size(uk)) = O(size(w)) ,
(cid:88)i=0 (cid:18)
(cid:19)
i
2
3
where the last line follows from the fact that the sum is a geometrically
decreasing series.
All that remains is to prove an upper-bound on the cost of all calls to
rebuild(u) during a sequence of m operations:
Lemma 8.3. Starting with an empty ScapegoatTree any sequence of m
add(x) and remove(x) operations causes at most O(m log m) time to be used
by rebuild(u) operations.
Proof. To prove this, we will use a credit scheme. We imagine that each
node stores a number of credits. Each credit can pay for some constant, c,
units of time spent rebuilding. The scheme gives out a total of O(m log m)
credits and every call to rebuild(u) is paid for with credits stored at u.
During an insertion or deletion, we give one credit to each node on
the path to the inserted node, or deleted node, u. In this way we hand
out at most log3/2 q
log3/2 m credits per operation. During a deletion we
also store an additional credit “on the side.” Thus, in total we give out at
most O(m log m) credits. All that remains is to show that these credits are
sufficient to pay for all calls to rebuild(u).
≤
179
§8.1
Scapegoat Trees
If we call rebuild(u) during an insertion, it is because u is a scapegoat.
Suppose, without loss of generality, that
size(u.left)
size(u)
>
2
3
.
Using the fact that
size(u) = 1 + size(u.left) + size(u.right)
we deduce that
and therefore
1
2
size(u.left) > size(u.right)
size(u.left)
−
size(u.right) >
1
2
size(u.left) >
1
3
size(u) .
Now, the last time a subtree containing u was rebuilt (or when u was
inserted, if a subtree containing u was never rebuilt), we had
size(u.left)
−
size(u.right)
1 .
≤
Therefore, the number of add(x) or remove(x) operations that have af-
fected u.left or u.right since then is at least
1
3
size(u)
1 .
−
and there are therefore at least this many credits stored at u that are avail-
able to pay for the O(size(u)) time it takes to call rebuild(u).
If we call rebuild(u) during a deletion, it is because q > 2n. In this
case, we have q
n > n credits stored “on the side,” and we use these
to pay for the O(n) time it takes to rebuild the root. This completes the
proof.
−
8.1.2 Summary
The following theorem summarizes the performance of the Scapegoat-
Tree data structure:
180
Discussion and Exercises
§8.2
Theorem 8.1. A ScapegoatTree implements the SSet interface. Ignoring
the cost of rebuild(u) operations, a ScapegoatTree supports the operations
add(x), remove(x), and find(x) in O(log n) time per operation.
Furthermore, beginning with an empty ScapegoatTree, any sequence of
m add(x) and remove(x) operations results in a total of O(m log m) time spent
during all calls to rebuild(u).
8.2 Discussion and Exercises
The term scapegoat tree is due to Galperin and Rivest [33], who define and
analyze these trees. However, the same structure was discovered earlier
by Andersson [5, 7], who called them general balanced trees since they can
have any shape as long as their height is small.
Experimenting with the ScapegoatTree implementation will reveal
that it is often considerably slower than the other SSet implementations
in this book. This may be somewhat surprising, since height bound of
log3/2 q
≈
1.709 log n + O(1)
is better than the expected length of a search path in a Skiplist and not
too far from that of a Treap. The implementation could be optimized by
storing the sizes of subtrees explicitly at each node or by reusing already
computed subtree sizes (Exercises 8.5 and 8.6). Even with these optimiza-
tions, there will always be sequences of add(x) and delete(x) operation
for which a ScapegoatTree takes longer than other SSet implementa-
tions.
This gap in performance is due to the fact that, unlike the other SSet
implementations discussed in this book, a ScapegoatTree can spend a lot
of time restructuring itself. Exercise 8.3 asks you to prove that there are
sequences of n operations in which a ScapegoatTree will spend on the or-
der of n log n time in calls to rebuild(u). This is in contrast to other SSet
implementations discussed in this book, which only make O(n) structural
changes during a sequence of n operations. This is, unfortunately, a nec-
essary consequence of the fact that a ScapegoatTree does all its restruc-
turing by calls to rebuild(u) [20].
Despite their lack of performance, there are applications in which a
181
§8.2
Scapegoat Trees
ScapegoatTree could be the right choice. This would occur any time
there is additional data associated with nodes that cannot be updated
in constant time when a rotation is performed, but that can be updated
during a rebuild(u) operation. In such cases, the ScapegoatTree and
related structures based on partial rebuilding may work. An example of
such an application is outlined in Exercise 8.11.
Exercise 8.1. Illustrate the addition of the values 1.5 and then 1.6 on the
ScapegoatTree in Figure 8.1.
Exercise 8.2. Illustrate what happens when the sequence 1, 5, 2, 4, 3 is
added to an empty ScapegoatTree, and show where the credits described
in the proof of Lemma 8.3 go, and how they are used during this sequence
of additions.
Exercise 8.3. Show that, if we start with an empty ScapegoatTree and
call add(x) for x = 1, 2, 3, . . . , n, then the total time spent during calls to
rebuild(u) is at least cn log n for some constant c > 0.
Exercise 8.4. The ScapegoatTree, as described in this chapter, guaran-
tees that the length of the search path does not exceed log3/2 q.
1. Design, analyze, and implement a modified version of Scapegoat-
Tree where the length of the search path does not exceed logb q,
where b is a parameter with 1 < b < 2.
2. What does your analysis and/or your experiments say about the
amortized cost of find(x), add(x) and remove(x) as a function of
n and b?
Exercise 8.5. Modify the add(x) method of the ScapegoatTree so that it
does not waste any time recomputing the sizes of subtrees that have al-
ready been computed. This is possible because, by the time the method
wants to compute size(w), it has already computed one of size(w.left)
or size(w.right). Compare the performance of your modified implemen-
tation with the implementation given here.
Exercise 8.6. Implement a second version of the ScapegoatTree data
structure that explicitly stores and maintains the sizes of the subtree
182
Discussion and Exercises
§8.2
rooted at each node. Compare the performance of the resulting imple-
mentation with that of the original ScapegoatTree implementation as
well as the implementation from Exercise 8.5.
Exercise 8.7. Reimplement the rebuild(u) method discussed at the be-
ginning of this chapter so that it does not require the use of an array to
store the nodes of the subtree being rebuilt. Instead, it should use re-
cursion to first connect the nodes into a linked list and then convert this
linked list into a perfectly balanced binary tree. (There are very elegant
recursive implementations of both steps.)
Exercise 8.8. Analyze and implement a WeightBalancedTree. This is a
tree in which each node u, except the root, maintains the balance invariant
(2/3)size(u.parent). The add(x) and remove(x) operations
that size(u)
are identical to the standard BinarySearchTree operations, except that
any time the balance invariant is violated at a node u, the subtree rooted
at u.parent is rebuilt. Your analysis should show that operations on a
WeightBalancedTree run in O(log n) amortized time.
≤
Exercise 8.9. Analyze and implement a CountdownTree. In a Countdown-
Tree each node u keeps a timer u.t. The add(x) and remove(x) opera-
tions are exactly the same as in a standard BinarySearchTree except that,
whenever one of these operations affects u’s subtree, u.t is decremented.
When u.t = 0 the entire subtree rooted at u is rebuilt into a perfectly
balanced binary search tree. When a node u is involved in a rebuilding
operation (either because u is rebuilt or one of u’s ancestors is rebuilt) u.t
is reset to size(u)/3.
Your analysis should show that operations on a CountdownTree run in
O(log n) amortized time. (Hint: First show that each node u satisfies some
version of a balance invariant.)
Exercise 8.10. Analyze and implement a DynamiteTree. In a Dynamite-
Tree each node u keeps tracks of the size of the subtree rooted at u in
a variable u.size. The add(x) and remove(x) operations are exactly the
same as in a standard BinarySearchTree except that, whenever one of
these operations affects a node u’s subtree, u explodes with probability
1/u.size. When u explodes, its entire subtree is rebuilt into a perfectly
balanced binary search tree.
183
§8.2
Scapegoat Trees
Your analysis should show that operations on a DynamiteTree run in
O(log n) expected time.
Exercise 8.11. Design and implement a Sequence data structure that
maintains a sequence (list) of elements. It supports these operations:
• addAfter(e): Add a new element after the element e in the se-
(If e is null, the new
quence. Return the newly added element.
element is added at the beginning of the sequence.)
• remove(e): Remove e from the sequence.
• testBefore(e1, e2): return true if and only if e1 comes before e2
in the sequence.
The first two operations should run in O(log n) amortized time. The third
operation should run in constant time.
The Sequence data structure can be implemented by storing the ele-
ments in something like a ScapegoatTree, in the same order that they oc-
cur in the sequence. To implement testBefore(e1, e2) in constant time,
each element e is labelled with an integer that encodes the path from the
root to e. In this way, testBefore(e1, e2) can be implemented by com-
paring the labels of e1 and e2.
184
Chapter 9
Red-Black Trees
In this chapter, we present red-black trees, a version of binary search trees
with logarithmic height. Red-black trees are one of the most widely used
data structures. They appear as the primary search structure in many
library implementations, including the Java Collections Framework and
several implementations of the C++ Standard Template Library. They are
also used within the Linux operating system kernel. There are several
reasons for the popularity of red-black trees:
1. A red-black tree storing n values has height at most 2 log n.
2. The add(x) and remove(x) operations on a red-black tree run in
O(log n) worst-case time.
3. The amortized number of rotations performed during an add(x) or
remove(x) operation is constant.
The first two of these properties already put red-black trees ahead of
skiplists, treaps, and scapegoat trees. Skiplists and treaps rely on ran-
domization and their O(log n) running times are only expected. Scapegoat
trees have a guaranteed bound on their height, but add(x) and remove(x)
only run in O(log n) amortized time. The third property is just icing on
the cake. It tells us that that the time needed to add or remove an element
x is dwarfed by the time it takes to find x.1
However, the nice properties of red-black trees come with a price: im-
plementation complexity. Maintaining a bound of 2 log n on the height
1Note that skiplists and treaps also have this property in the expected sense. See Exer-
cises 4.6 and 7.5.
185
§9.1
Red-Black Trees
Figure 9.1: A 2-4 tree of height 3.
is not easy. It requires a careful analysis of a number of cases. We must
ensure that the implementation does exactly the right thing in each case.
One misplaced rotation or change of colour produces a bug that can be
very difficult to understand and track down.
Rather than jumping directly into the implementation of red-black
trees, we will first provide some background on a related data structure:
2-4 trees. This will give some insight into how red-black trees were dis-
covered and why efficiently maintaining them is even possible.
9.1 2-4 Trees
A 2-4 tree is a rooted tree with the following properties:
Property 9.1 (height). All leaves have the same depth.
Property 9.2 (degree). Every internal node has 2, 3, or 4 children.
An example of a 2-4 tree is shown in Figure 9.1. The properties of 2-4
trees imply that their height is logarithmic in the number of leaves:
Lemma 9.1. A 2-4 tree with n leaves has height at most log n.
Proof. The lower-bound of 2 on the number of children of an internal
node implies that, if the height of a 2-4 tree is h, then it has at least 2h
leaves. In other words,
Taking logarithms on both sides of this inequality gives h
log n.
≤
2h .
n
≥
186
2-4 Trees
§9.1
9.1.1 Adding a Leaf
Adding a leaf to a 2-4 tree is easy (see Figure 9.2). If we want to add a
leaf u as the child of some node w on the second-last level, then we simply
make u a child of w. This certainly maintains the height property, but
could violate the degree property; if w had four children prior to adding
u, then w now has five children. In this case, we split w into two nodes,
w and w’, having two and three children, respectively. But now w’ has no
parent, so we recursively make w’ a child of w’s parent. Again, this may
cause w’s parent to have too many children in which case we split it. This
process goes on until we reach a node that has fewer than four children,
or until we split the root, r, into two nodes r and r(cid:48). In the latter case,
we make a new root that has r and r(cid:48) as children. This simultaneously
increases the depth of all leaves and so maintains the height property.
Since the height of the 2-4 tree is never more than log n, the process of
adding a leaf finishes after at most log n steps.
9.1.2 Removing a Leaf
Removing a leaf from a 2-4 tree is a little more tricky (see Figure 9.3). To
remove a leaf u from its parent w, we just remove it. If w had only two
children prior to the removal of u, then w is left with only one child and
violates the degree property.
To correct this, we look at w’s sibling, w(cid:48). The node w(cid:48) is sure to exist
since w’s parent had at least two children. If w(cid:48) has three or four children,
then we take one of these children from w(cid:48) and give it to w. Now w has two
children and w(cid:48) has two or three children and we are done.
On the other hand, if w(cid:48) has only two children, then we merge w and
w(cid:48) into a single node, w, that has three children. Next we recursively re-
move w(cid:48) from the parent of w(cid:48). This process ends when we reach a node,
u, where u or its sibling has more than two children, or when we reach
the root. In the latter case, if the root is left with only one child, then
we delete the root and make its child the new root. Again, this simul-
taneously decreases the height of every leaf and therefore maintains the
height property.
Again, since the height of the tree is never more than log n, the process
187
§9.1
Red-Black Trees
Figure 9.2: Adding a leaf to a 2-4 Tree. This process stops after one split because
w.parent has a degree of less than 4 before the addition.
188
wwuuww02-4 Trees
§9.1
Figure 9.3: Removing a leaf from a 2-4 Tree. This process goes all the way to the
root because each of u’s ancestors and their siblings have only two children.
189
u§9.2
Red-Black Trees
of removing a leaf finishes after at most log n steps.
9.2 RedBlackTree: A Simulated 2-4 Tree
A red-black tree is a binary search tree in which each node, u, has a colour
which is either red or black. Red is represented by the value 0 and black
by the value 1.
class Node<T> extends BSTNode<Node<T>,T> {
RedBlackTree
byte colour;
}
Before and after any operation on a red-black tree, the following two
properties are satisfied. Each property is defined both in terms of the
colours red and black, and in terms of the numeric values 0 and 1.
Property 9.3 (black-height). There are the same number of black nodes
on every root to leaf path. (The sum of the colours on any root to leaf path
is the same.)
Property 9.4 (no-red-edge). No two red nodes are adjacent. (For any node
u, except the root, u.colour + u.parent.colour
1.)
≥
Notice that we can always colour the root, r, of a red-black tree black
without violating either of these two properties, so we will assume that
the root is black, and the algorithms for updating a red-black tree will
maintain this. Another trick that simplifies red-black trees is to treat the
external nodes (represented by nil) as black nodes. This way, every real
node, u, of a red-black tree has exactly two children, each with a well-
defined colour. An example of a red-black tree is shown in Figure 9.4.
9.2.1 Red-Black Trees and 2-4 Trees
At first it might seem surprising that a red-black tree can be efficiently
updated to maintain the black-height and no-red-edge properties, and
it seems unusual to even consider these as useful properties. However,
190
RedBlackTree: A Simulated 2-4 Tree
§9.2
Figure 9.4: An example of a red-black tree with black-height 3. External (nil)
nodes are drawn as squares.
red-black trees were designed to be an efficient simulation of 2-4 trees as
binary trees.
Refer to Figure 9.5. Consider any red-black tree, T , having n nodes
and perform the following transformation: Remove each red node u and
connect u’s two children directly to the (black) parent of u. After this
transformation we are left with a tree T (cid:48) having only black nodes.
Every internal node in T (cid:48) has two, three, or four children: A black
node that started out with two black children will still have two black
children after this transformation. A black node that started out with
one red and one black child will have three children after this transfor-
mation. A black node that started out with two red children will have
four children after this transformation. Furthermore, the black-height
property now guarantees that every root-to-leaf path in T (cid:48) has the same
length. In other words, T (cid:48) is a 2-4 tree!
The 2-4 tree T (cid:48) has n + 1 leaves that correspond to the n + 1 external
nodes of the red-black tree. Therefore, this tree has height at most log(n +
1). Now, every root to leaf path in the 2-4 tree corresponds to a path
from the root of the red-black tree T to an external node. The first and
last node in this path are black and at most one out of every two internal
nodes is red, so this path has at most log(n + 1) black nodes and at most
log(n + 1)
1 red nodes. Therefore, the longest path from the root to any
internal node in T is at most
−
2 log(n + 1)
2
−
≤
2 log n ,
for any n
≥
1. This proves the most important property of red-black trees:
191
rednodeblacknode§9.2
Red-Black Trees
Figure 9.5: Every red-black tree has a corresponding 2-4 tree.
Lemma 9.2. The height of red-black tree with n nodes is at most 2 log n.
Now that we have seen the relationship between 2-4 trees and red-
black trees, it is not hard to believe that we can efficiently maintain a
red-black tree while adding and removing elements.
We have already seen that adding an element in a BinarySearchTree
can be done by adding a new leaf. Therefore, to implement add(x) in a
red-black tree we need a method of simulating splitting a node with five
children in a 2-4 tree. A 2-4 tree node with five children is represented
by a black node that has two red children, one of which also has a red
child. We can “split” this node by colouring it red and colouring its two
children black. An example of this is shown in Figure 9.6.
Similarly, implementing remove(x) requires a method of merging two
nodes and borrowing a child from a sibling. Merging two nodes is the in-
verse of a split (shown in Figure 9.6), and involves colouring two (black)
siblings red and colouring their (red) parent black. Borrowing from a sib-
ling is the most complicated of the procedures and involves both rotations
and recolouring nodes.
Of course, during all of this we must still maintain the no-red-edge
192
RedBlackTree: A Simulated 2-4 Tree
§9.2
Figure 9.6: Simulating a 2-4 tree split operation during an addition in a red-black
tree. (This simulates the 2-4 tree addition shown in Figure 9.2.)
193
wwuww0u§9.2
Red-Black Trees
property and the black-height property. While it is no longer surprising
that this can be done, there are a large number of cases that have to be
considered if we try to do a direct simulation of a 2-4 tree by a red-black
tree. At some point, it just becomes simpler to disregard the underlying
2-4 tree and work directly towards maintaining the properties of the red-
black tree.
9.2.2 Left-Leaning Red-Black Trees
No single definition of red-black trees exists. Rather, there is a family
of structures that manage to maintain the black-height and no-red-edge
properties during add(x) and remove(x) operations. Different structures
do this in different ways. Here, we implement a data structure that we
call a RedBlackTree. This structure implements a particular variant of
red-black trees that satisfies an additional property:
Property 9.5 (left-leaning). At any node u, if u.left is black, then u.right
is black.
Note that the red-black tree shown in Figure 9.4 does not satisfy the
left-leaning property; it is violated by the parent of the red node in the
rightmost path.
The reason for maintaining the left-leaning property is that it reduces
the number of cases encountered when updating the tree during add(x)
and remove(x) operations. In terms of 2-4 trees, it implies that every 2-4
tree has a unique representation: A node of degree two becomes a black
node with two black children. A node of degree three becomes a black
node whose left child is red and whose right child is black. A node of
degree four becomes a black node with two red children.
Before we describe the implementation of add(x) and remove(x) in de-
tail, we first present some simple subroutines used by these methods that
are illustrated in Figure 9.7. The first two subroutines are for manipulat-
ing colours while preserving the black-height property. The pushBlack(u)
method takes as input a black node u that has two red children and
colours u red and its two children black. The pullBlack(u) method re-
verses this operation:
194
RedBlackTree: A Simulated 2-4 Tree
§9.2
Figure 9.7: Flips, pulls and pushes
void pushBlack(Node<T> u) {
RedBlackTree
u.colour--;
u.left.colour++;
u.right.colour++;
}
void pullBlack(Node<T> u) {
u.colour++;
u.left.colour--;
u.right.colour--;
}
The flipLeft(u) method swaps the colours of u and u.right and then
performs a left rotation at u. This method reverses the colours of these
two nodes as well as their parent-child relationship:
RedBlackTree
void flipLeft(Node<T> u) {
swapColors(u, u.right);
rotateLeft(u);
}
The flipLeft(u) operation is especially useful in restoring the left-
leaning property at a node u that violates it (because u.left is black and
u.right is red). In this special case, we can be assured that this oper-
ation preserves both the black-height and no-red-edge properties. The
195
uupushBlack(u)⇓uupullBlack(u)⇓flipLeft(u)⇓uuflipRight(u)⇓uu§9.2
Red-Black Trees
flipRight(u) operation is symmetric with flipLeft(u), when the roles
of left and right are reversed.
RedBlackTree
void flipRight(Node<T> u) {
swapColors(u, u.left);
rotateRight(u);
}
9.2.3 Addition
To implement add(x) in a RedBlackTree, we perform a standard Binary-
SearchTree insertion to add a new leaf, u, with u.x = x and set u.colour =
red. Note that this does not change the black height of any node, so it
does not violate the black-height property. It may, however, violate the
left-leaning property (if u is the right child of its parent), and it may
violate the no-red-edge property (if u’s parent is red). To restore these
properties, we call the method addFixup(u).
RedBlackTree
boolean add(T x) {
Node<T> u = newNode(x);
u.colour = red;
boolean added = add(u);
if (added)
addFixup(u);
return added;
}
Illustrated in Figure 9.8, the addFixup(u) method takes as input a
node u whose colour is red and which may violate the no-red-edge prop-
erty and/or the left-leaning property. The following discussion is proba-
bly impossible to follow without referring to Figure 9.8 or recreating it on
a piece of paper. Indeed, the reader may wish to study this figure before
continuing.
If u is the root of the tree, then we can colour u black to restore both
properties. If u’s sibling is also red, then u’s parent must be black, so both
the left-leaning and no-red-edge properties already hold.
196
RedBlackTree: A Simulated 2-4 Tree
§9.2
Figure 9.8: A single round in the process of fixing Property 2 after an insertion.
197
uuuuwuwuwuwuwuwunewu=gflipLeft(w);u=wuw.colourflipRight(g)pushBlack(g)ggg.right.colouru.parent.left.colourreturnreturnreturnwwwwwuwunewu=gpushBlack(g)gg§9.2
Red-Black Trees
Otherwise, we first determine if u’s parent, w, violates the left-leaning
property and, if so, perform a flipLeft(w) operation and set u = w. This
leaves us in a well-defined state: u is the left child of its parent, w, so w
now satisfies the left-leaning property. All that remains is to ensure the
no-red-edge property at u. We only have to worry about the case in which
w is red, since otherwise u already satisfies the no-red-edge property.
Since we are not done yet, u is red and w is red. The no-red-edge prop-
erty (which is only violated by u and not by w) implies that u’s grand-
parent g exists and is black. If g’s right child is red, then the left-leaning
property ensures that both g’s children are red, and a call to pushBlack(g)
makes g red and w black. This restores the no-red-edge property at u, but
may cause it to be violated at g, so the whole process starts over with
u = g.
If g’s right child is black, then a call to flipRight(g) makes w the
(black) parent of g and gives w two red children, u and g. This ensures
that u satisfies the no-red-edge property and g satisfies the left-leaning
property. In this case we can stop.
RedBlackTree
void addFixup(Node<T> u) {
while (u.colour == red) {
if (u == r) { // u is the root - done
u.colour = black;
return;
}
Node<T> w = u.parent;
if (w.left.colour == black) { // ensure left-leaning
flipLeft(w);
u = w;
w = u.parent;
}
if (w.colour == black)
return; // no red-red edge = done
Node<T> g = w.parent; // grandparent of u
if (g.right.colour == black) {
flipRight(g);
return;
} else {
pushBlack(g);
198
RedBlackTree: A Simulated 2-4 Tree
§9.2
u = g;
}
}
}
The insertFixup(u) method takes constant time per iteration and
each iteration either finishes or moves u closer to the root. Therefore,
the insertFixup(u) method finishes after O(log n) iterations in O(log n)
time.
9.2.4 Removal
The remove(x) operation in a RedBlackTree is the most complicated to
Just
implement, and this is true of all known red-black tree variants.
like the remove(x) operation in a BinarySearchTree, this operation boils
down to finding a node w with only one child, u, and splicing w out of the
tree by having w.parent adopt u.
The problem with this is that, if w is black, then the black-height
property will now be violated at w.parent. We may avoid this prob-
lem, temporarily, by adding w.colour to u.colour. Of course, this in-
troduces two other problems: (1) if u and w both started out black, then
u.colour + w.colour = 2 (double black), which is an invalid colour. If
w was red, then it is replaced by a black node u, which may violate the
left-leaning property at u.parent. Both of these problems can be resolved
with a call to the removeFixup(u) method.
RedBlackTree
boolean remove(T x) {
Node<T> u = findLast(x);
if (u == nil || compare(u.x, x) != 0)
return false;
Node<T> w = u.right;
if (w == nil) {
w = u;
u = w.left;
} else {
while (w.left != nil)
w = w.left;
199
§9.2
Red-Black Trees
u.x = w.x;
u = w.right;
}
splice(w);
u.colour += w.colour;
u.parent = w.parent;
removeFixup(u);
return true;
}
The removeFixup(u) method takes as its input a node u whose colour
is black (1) or double-black (2). If u is double-black, then removeFixup(u)
performs a series of rotations and recolouring operations that move the
double-black node up the tree until it can be eliminated. During this
process, the node u changes until, at the end of this process, u refers to
the root of the subtree that has been changed. The root of this subtree
may have changed colour. In particular, it may have gone from red to
black, so the removeFixup(u) method finishes by checking if u’s parent
violates the left-leaning property and, if so, fixing it.
RedBlackTree
void removeFixup(Node<T> u) {
while (u.colour > black) {
if (u == r) {
u.colour = black;
} else if (u.parent.left.colour == red) {
u = removeFixupCase1(u);
} else if (u == u.parent.left) {
u = removeFixupCase2(u);
} else {
u = removeFixupCase3(u);
}
}
if (u != r) { // restore left-leaning property if needed
Node<T> w = u.parent;
if (w.right.colour == red && w.left.colour == black) {
flipLeft(w);
}
}
200
RedBlackTree: A Simulated 2-4 Tree
§9.2
}
The removeFixup(u) method is illustrated in Figure 9.9. Again, the
following text will be difficult, if not impossible, to follow without refer-
ring to Figure 9.9. Each iteration of the loop in removeFixup(u) processes
the double-black node u, based on one of four cases:
Case 0: u is the root. This is the easiest case to treat. We recolour u to be
black (this does not violate any of the red-black tree properties).
Case 1: u’s sibling, v, is red. In this case, u’s sibling is the left child of
its parent, w (by the left-leaning property). We perform a right-flip at w
and then proceed to the next iteration. Note that this action causes w’s
parent to violate the left-leaning property and the depth of u to increase.
However, it also implies that the next iteration will be in Case 3 with w
coloured red. When examining Case 3 below, we will see that the process
will stop during the next iteration.
Node<T> removeFixupCase1(Node<T> u) {
RedBlackTree
flipRight(u.parent);
return u;
}
Case 2: u’s sibling, v, is black, and u is the left child of its parent, w. In
this case, we call pullBlack(w), making u black, v red, and darkening the
colour of w to black or double-black. At this point, w does not satisfy the
left-leaning property, so we call flipLeft(w) to fix this.
At this point, w is red and v is the root of the subtree with which we
started. We need to check if w causes the no-red-edge property to be vi-
olated. We do this by inspecting w’s right child, q. If q is black, then w
satisfies the no-red-edge property and we can continue the next iteration
with u = v.
Otherwise (q is red), so both the no-red-edge property and the left-
leaning properties are violated at q and w, respectively. The left-leaning
property is restored with a call to rotateLeft(w), but the no-red-edge
property is still violated. At this point, q is the left child of v, w is the
left child of q, q and w are both red, and v is black or double-black. A
flipRight(v) makes q the parent of both v and w. Following this up by a
201
§9.2
Red-Black Trees
Figure 9.9: A single round in the process of eliminating a double-black node after
a removal.
202
newuuuwupullBlack(w)pullBlack(w)flipLeft(w)flipRight(w)uwuvvvvvvuuuuuwwwwwqqqqqq.colourrotateLeft(w)flipRight(v)pushBlack(q)vqvvvvvwwwwwwwqqqqq.colourrotateRight(w)flipLeft(v)pushBlack(q)vvvqqqwwwuuuuuuuuv.left.colourflipLeft(v)w(newu)qwupushBlack(v)v(newu)wwflipRight(w)v.right.colourvuwqvuwqvuwqflipLeft(v)vvremoveFixupCase1(u)removeFixupCase3(u)removeFixupCase2(u)RedBlackTree: A Simulated 2-4 Tree
§9.2
pushBlack(q) makes both v and w black and sets the colour of q back to
the original colour of w.
At this point, the double-black node is has been eliminated and the
no-red-edge and black-height properties are reestablished. Only one pos-
sible problem remains: the right child of v may be red, in which case the
left-leaning property would be violated. We check this and perform a
flipLeft(v) to correct it if necessary.
RedBlackTree
Node<T> removeFixupCase2(Node<T> u) {
Node<T> w = u.parent;
Node<T> v = w.right;
pullBlack(w); // w.left
flipLeft(w); // w is now red
Node<T> q = w.right;
if (q.colour == red) { // q-w is red-red
rotateLeft(w);
flipRight(v);
pushBlack(q);
if (v.right.colour == red)
flipLeft(v);
return q;
} else {
return v;
}
}
Case 3: u’s sibling is black and u is the right child of its parent, w. This
case is symmetric to Case 2 and is handled mostly the same way. The only
differences come from the fact that the left-leaning property is asymmet-
ric, so it requires different handling.
As before, we begin with a call to pullBlack(w), which makes v red
and u black. A call to flipRight(w) promotes v to the root of the subtree.
At this point w is red, and the code branches two ways depending on the
colour of w’s left child, q.
If q is red, then the code finishes up exactly the same way as Case 2
does, but is even simpler since there is no danger of v not satisfying the
left-leaning property.
203
§9.2
Red-Black Trees
The more complicated case occurs when q is black. In this case, we
examine the colour of v’s left child. If it is red, then v has two red children
and its extra black can be pushed down with a call to pushBlack(v). At
this point, v now has w’s original colour, and we are done.
If v’s left child is black, then v violates the left-leaning property, and
we restore this with a call to flipLeft(v). We then return the node v so
that the next iteration of removeFixup(u) then continues with u = v.
RedBlackTree
Node<T> removeFixupCase3(Node<T> u) {
Node<T> w = u.parent;
Node<T> v = w.left;
pullBlack(w);
flipRight(w); // w is now red
Node<T> q = w.left;
if (q.colour == red) { // q-w is red-red
rotateRight(w);
flipLeft(v);
pushBlack(q);
return q;
} else {
if (v.left.colour == red) {
pushBlack(v); // both v’s children are red
return v;
} else { // ensure left-leaning
flipLeft(v);
return w;
}
}
}
Each iteration of removeFixup(u) takes constant time. Cases 2 and 3
either finish or move u closer to the root of the tree. Case 0 (where u
is the root) always terminates and Case 1 leads immediately to Case 3,
which also terminates. Since the height of the tree is at most 2 log n, we
conclude that there are at most O(log n) iterations of removeFixup(u), so
removeFixup(u) runs in O(log n) time.
204
Summary
§9.3
9.3 Summary
The following theorem summarizes the performance of the RedBlack-
Tree data structure:
Theorem 9.1. A RedBlackTree implements the SSet interface and supports
the operations add(x), remove(x), and find(x) in O(log n) worst-case time per
operation.
Not included in the above theorem is the following extra bonus:
Theorem 9.2. Beginning with an empty RedBlackTree, any sequence of m
add(x) and remove(x) operations results in a total of O(m) time spent during
all calls addFixup(u) and removeFixup(u).
We only sketch a proof of Theorem 9.2. By comparing addFixup(u)
and removeFixup(u) with the algorithms for adding or removing a leaf
in a 2-4 tree, we can convince ourselves that this property is inherited
from a 2-4 tree. In particular, if we can show that the total time spent
splitting, merging, and borrowing in a 2-4 tree is O(m), then this implies
Theorem 9.2.
The proof of this theorem for 2-4 trees uses the potential method of
amortized analysis.2 Define the potential of an internal node u in a 2-4
tree as
Φ(u) =
1 if u has 2 children
0 if u has 3 children
3 if u has 4 children
and the potential of a 2-4 tree as the sum of the potentials of its nodes.
When a split occurs, it is because a node with four children becomes two
nodes, with two and three children. This means that the overall potential
drops by 3
0 = 2. When a merge occurs, two nodes that used to have
two children are replaced by one node with three children. The result is
0 = 2. Therefore, for every split or merge, the
a drop in potential of 2
potential decreases by two.
−
−
−
1
Next notice that, if we ignore splitting and merging of nodes, there are
only a constant number of nodes whose number of children is changed by
2See the proofs of Lemma 2.2 and Lemma 3.1 for other applications of the potential
method.
205
§9.4
Red-Black Trees
the addition or removal of a leaf. When adding a node, one node has its
number of children increase by one, increasing the potential by at most
three. During the removal of a leaf, one node has its number of children
decrease by one, increasing the potential by at most one, and two nodes
may be involved in a borrowing operation, increasing their total potential
by at most one.
To summarize, each merge and split causes the potential to drop by
at least two. Ignoring merging and splitting, each addition or removal
causes the potential to rise by at most three, and the potential is always
non-negative. Therefore, the number of splits and merges caused by m
additions or removals on an initially empty tree is at most 3m/2. Theo-
rem 9.2 is a consequence of this analysis and the correspondence between
2-4 trees and red-black trees.
9.4 Discussion and Exercises
Red-black trees were first introduced by Guibas and Sedgewick [38]. De-
spite their high implementation complexity they are found in some of
the most commonly used libraries and applications. Most algorithms and
data structures textbooks discuss some variant of red-black trees.
Andersson [6] describes a left-leaning version of balanced trees that is
similar to red-black trees but has the additional constraint that any node
has at most one red child. This implies that these trees simulate 2-3 trees
rather than 2-4 trees. They are significantly simpler, though, than the
RedBlackTree structure presented in this chapter.
Sedgewick [66] describes two versions of left-leaning red-black trees.
These use recursion along with a simulation of top-down splitting and
merging in 2-4 trees. The combination of these two techniques makes for
particularly short and elegant code.
A related, and older, data structure is the AVL tree [3]. AVL trees
are height-balanced: At each node u, the height of the subtree rooted at
u.left and the subtree rooted at u.right differ by at most one. It follows
immediately that, if F(h) is the minimum number of leaves in a tree of
206
Discussion and Exercises
§9.4
height h, then F(h) obeys the Fibonacci recurrence
F(h) = F(h
1) + F(h
2)
−
−
with base cases F(0) = 1 and F(1) = 1. This means F(h) is approximately
ϕh/√5, where ϕ = (1 + √5)/2
(More
1.61803399 is the golden ratio.
ϕh/√5
1/2.) Arguing as in the proof of Lemma 9.1,
precisely,
|
this implies
F(h)
| ≤
≈
−
h
≤
logϕ n
≈
1.440420088 log n ,
so AVL trees have smaller height than red-black trees. The height balanc-
ing can be maintained during add(x) and remove(x) operations by walk-
ing back up the path to the root and performing a rebalancing operation
at each node u where the height of u’s left and right subtrees differ by two.
See Figure 9.10.
Andersson’s variant of red-black trees, Sedgewick’s variant of red-
black trees, and AVL trees are all simpler to implement than the Red-
BlackTree structure defined here. Unfortunately, none of them can guar-
antee that the amortized time spent rebalancing is O(1) per update. In
particular, there is no analogue of Theorem 9.2 for those structures.
Exercise 9.1. Illustrate the 2-4 tree that corresponds to the RedBlackTree
in Figure 9.11.
Exercise 9.2. Illustrate the addition of 13, then 3.5, then 3.3 on the Red-
BlackTree in Figure 9.11.
Exercise 9.3. Illustrate the removal of 11, then 9, then 5 on the RedBlack-
Tree in Figure 9.11.
Exercise 9.4. Show that, for arbitrarily large values of n, there are red-
black trees with n nodes that have height 2 log n
O(1).
−
Exercise 9.5. Consider the operations pushBlack(u) and pullBlack(u).
What do these operations do to the underlying 2-4 tree that is being sim-
ulated by the red-black tree?
Exercise 9.6. Show that, for arbitrarily large values of n, there exist se-
quences of add(x) and remove(x) operations that lead to red-black trees
with n nodes that have height 2 log n
O(1).
−
207
§9.4
Red-Black Trees
Figure 9.10: Rebalancing in an AVL tree. At most two rotations are required to
convert a node whose subtrees have a height of h and h + 2 into a node whose
subtrees each have a height of at most h + 1.
Figure 9.11: A red-black tree on which to practice.
208
h+2hhh+2h+1543216119781210Discussion and Exercises
§9.4
Exercise 9.7. Why does the method remove(x) in the RedBlackTree im-
plementation perform the assignment u.parent = w.parent? Shouldn’t
this already be done by the call to splice(w)?
Exercise 9.8. Suppose a 2-4 tree, T , has n(cid:96) leaves and ni internal nodes.
1. What is the minimum value of ni, as a function of n(cid:96)?
2. What is the maximum value of ni, as a function of n(cid:96)?
3. If T (cid:48) is a red-black tree that represents T , then how many red nodes
does T (cid:48) have?
Exercise 9.9. Suppose you are given a binary search tree with n nodes
and a height of at most 2 log n
2. Is it always possible to colour the nodes
red and black so that the tree satisfies the black-height and no-red-edge
properties? If so, can it also be made to satisfy the left-leaning property?
−
Exercise 9.10. Suppose you have two red-black trees T1 and T2 that have
the same black height, h, and such that the largest key in T1 is smaller
than the smallest key in T2. Show how to merge T1 and T2 into a single
red-black tree in O(h) time.
Exercise 9.11. Extend your solution to Exercise 9.10 to the case where the
(cid:44) h2. The running-
two trees T1 and T2 have different black heights, h1
).
time should be O(max
h1, h2}
{
Exercise 9.12. Prove that, during an add(x) operation, an AVL tree must
perform at most one rebalancing operation (that involves at most two ro-
tations; see Figure 9.10). Give an example of an AVL tree and a remove(x)
operation on that tree that requires on the order of log n rebalancing op-
erations.
Exercise 9.13. Implement an AVLTree class that implements AVL trees as
described above. Compare its performance to that of the RedBlackTree
implementation. Which implementation has a faster find(x) operation?
Exercise 9.14. Design and implement a series of experiments that com-
pare the relative performance of find(x), add(x), and remove(x) for the
SSet implemeentations SkiplistSSet, ScapegoatTree, Treap, and Red-
BlackTree. Be sure to include multiple test scenarios, including cases
209
§9.4
Red-Black Trees
where the data is random, already sorted, is removed in random order, is
removed in sorted order, and so on.
210
Chapter 10
Heaps
In this chapter, we discuss two implementations of the extremely useful
priority Queue data structure. Both of these structures are a special kind
of binary tree called a heap, which means “a disorganized pile.” This
is in contrast to binary search trees that can be thought of as a highly
organized pile.
The first heap implementation uses an array to simulate a complete bi-
nary tree. This very fast implementation is the basis of one of the fastest
known sorting algorithms, namely heapsort (see Section 11.1.3). The sec-
ond implementation is based on more flexible binary trees. It supports a
meld(h) operation that allows the priority queue to absorb the elements
of a second priority queue h.
10.1 BinaryHeap: An Implicit Binary Tree
Our first implementation of a (priority) Queue is based on a technique that
is over four hundred years old. Eytzinger’s method allows us to represent
a complete binary tree as an array by laying out the nodes of the tree in
breadth-first order (see Section 6.1.2). In this way, the root is stored at
position 0, the root’s left child is stored at position 1, the root’s right child
at position 2, the left child of the left child of the root is stored at position
3, and so on. See Figure 10.1.
If we apply Eytzinger’s method to a sufficiently large tree, some pat-
terns emerge. The left child of the node at index i is at index left(i) =
211
§10.1
Heaps
Figure 10.1: Eytzinger’s method represents a complete binary tree as an array.
2i + 1 and the right child of the node at index i is at index right(i) =
2i + 2. The parent of the node at index i is at index parent(i) = (i
1)/2.
−
BinaryHeap
int left(int i) {
return 2*i + 1;
}
int right(int i) {
return 2*i + 2;
}
int parent(int i) {
return (i-1)/2;
}
A BinaryHeap uses this technique to implicitly represent a complete
binary tree in which the elements are heap-ordered: The value stored at
any index i is not smaller than the value stored at index parent(i), with
the exception of the root value, i = 0. It follows that the smallest value in
the priority Queue is therefore stored at position 0 (the root).
In a BinaryHeap, the n elements are stored in an array a:
T[] a;
int n;
BinaryHeap
212
7381941001112131465212453678910111213140BinaryHeap: An Implicit Binary Tree
§10.1
Implementing the add(x) operation is fairly straightforward. As with
all array-based structures, we first check to see if a is full (by checking if
a.length = n) and, if so, we grow a. Next, we place x at location a[n] and
increment n. At this point, all that remains is to ensure that we maintain
the heap property. We do this by repeatedly swapping x with its parent
until x is no longer smaller than its parent. See Figure 10.2.
BinaryHeap
boolean add(T x) {
if (n + 1 > a.length) resize();
a[n++] = x;
bubbleUp(n-1);
return true;
}
void bubbleUp(int i) {
int p = parent(i);
while (i > 0 && compare(a[i], a[p]) < 0) {
swap(i,p);
i = p;
p = parent(i);
}
}
Implementing the remove() operation, which removes the smallest
value from the heap, is a little trickier. We know where the smallest value
is (at the root), but we need to replace it after we remove it and ensure
that we maintain the heap property.
The easiest way to do this is to replace the root with the value a[n
1],
delete that value, and decrement n. Unfortunately, the new root element
is now probably not the smallest element, so it needs to be moved down-
wards. We do this by repeatedly comparing this element to its two chil-
dren. If it is the smallest of the three then we are done. Otherwise, we
swap this element with the smallest of its two children and continue.
−
T remove() {
T x = a[0];
a[0] = a[--n];
trickleDown(0);
BinaryHeap
213
§10.1
Heaps
Figure 10.2: Adding the value 6 to a BinaryHeap.
214
19176993226934551650898265017161969329355419176993226934556165086982650171619693293554191769932269345516898261716196932935546505061917699322693455161245367891011121314092617161969329355450865068BinaryHeap: An Implicit Binary Tree
§10.1
if (3*n < a.length) resize();
return x;
}
void trickleDown(int i) {
do {
int j = -1;
int r = right(i);
if (r < n && compare(a[r], a[i]) < 0) {
int l = left(i);
if (compare(a[l], a[r]) < 0) {
j = l;
} else {
j = r;
}
} else {
int l = left(i);
if (l < n && compare(a[l], a[i]) < 0) {
j = l;
}
}
if (j >= 0) swap(i, j);
i = j;
} while (i >= 0);
}
As with other array-based structures, we will ignore the time spent
in calls to resize(), since these can be accounted for using the amortiza-
tion argument from Lemma 2.1. The running times of both add(x) and
remove() then depend on the height of the (implicit) binary tree. Luckily,
this is a complete binary tree; every level except the last has the maximum
possible number of nodes. Therefore, if the height of this tree is h, then it
has at least 2h nodes. Stated another way
Taking logarithms on both sides of this equation gives
2h .
n
≥
log n .
h
≤
Therefore, both the add(x) and remove() operation run in O(log n) time.
215
§10.1
Heaps
Figure 10.3: Removing the minimum value, 4, from a BinaryHeap.
216
1917699322693455169261716196932935545086506819176993226935516926171619693293558668505019176993226935516926171619693293558865065019176993226935516124536789101112131409261716196932935565086850MeldableHeap: A Randomized Meldable Heap
§10.2
10.1.1 Summary
The following theorem summarizes the performance of a BinaryHeap:
Theorem 10.1. A BinaryHeap implements the (priority) Queue interface.
Ignoring the cost of calls to resize(), a BinaryHeap supports the operations
add(x) and remove() in O(log n) time per operation.
Furthermore, beginning with an empty BinaryHeap, any sequence of m
add(x) and remove() operations results in a total of O(m) time spent during
all calls to resize().
10.2 MeldableHeap: A Randomized Meldable Heap
In this section, we describe the MeldableHeap, a priority Queue imple-
mentation in which the underlying structure is also a heap-ordered bi-
nary tree. However, unlike a BinaryHeap in which the underlying binary
tree is completely defined by the number of elements, there are no re-
strictions on the shape of the binary tree that underlies a MeldableHeap;
anything goes.
The add(x) and remove() operations in a MeldableHeap are imple-
mented in terms of the merge(h1, h2) operation. This operation takes two
heap nodes h1 and h2 and merges them, returning a heap node that is the
root of a heap that contains all elements in the subtree rooted at h1 and
all elements in the subtree rooted at h2.
The nice thing about a merge(h1, h2) operation is that it can be defined
recursively. See Figure 10.4. If either h1 or h2 is nil, then we are merging
with an empty set, so we return h2 or h1, respectively. Otherwise, assume
h1.x
h2.x since, if h1.x > h2.x, then we can reverse the roles of h1 and
h2. Then we know that the root of the merged heap will contain h1.x, and
we can recursively merge h2 with h1.left or h1.right, as we wish. This
is where randomization comes in, and we toss a coin to decide whether to
merge h2 with h1.left or h1.right:
≤
Node<T> merge(Node<T> h1, Node<T> h2) {
if (h1 == nil) return h2;
MeldableHeap
217
§10.2
Heaps
Figure 10.4: Merging h1 and h2 is done by merging h2 with one of h1.left or
h1.right.
if (h2 == nil) return h1;
if (compare(h2.x, h1.x) < 0) return merge(h2, h1);
// now we know h1.x <= h2.x
if (rand.nextBoolean()) {
h1.left = merge(h1.left, h2);
h1.left.parent = h1;
} else {
h1.right = merge(h1.right, h2);
h1.right.parent = h1;
}
return h1;
}
In the next section, we show that merge(h1, h2) runs in O(log n) ex-
218
19179264551625322893199989201917926455165082532289319998920merge(h1.right,h2)merge(h1,h2)⇓850h1h2MeldableHeap: A Randomized Meldable Heap
§10.2
pected time, where n is the total number of elements in h1 and h2.
With access to a merge(h1, h2) operation, the add(x) operation is easy.
We create a new node u containing x and then merge u with the root of
our heap:
MeldableHeap
boolean add(T x) {
Node<T> u = newNode();
u.x = x;
r = merge(u, r);
r.parent = nil;
n++;
return true;
}
This takes O(log(n + 1)) = O(log n) expected time.
The remove() operation is similarly easy. The node we want to remove
is the root, so we just merge its two children and make the result the root:
MeldableHeap
T remove() {
T x = r.x;
r = merge(r.left, r.right);
if (r != nil) r.parent = nil;
n--;
return x;
}
Again, this takes O(log n) expected time.
Additionally, a MeldableHeap can implement many other operations
in O(log n) expected time, including:
• remove(u): remove the node u (and its key u.x) from the heap.
• absorb(h): add all the elements of the MeldableHeap h to this heap,
emptying h in the process.
Each of these operations can be implemented using a constant number of
merge(h1, h2) operations that each take O(log n) expected time.
219
§10.2
Heaps
10.2.1 Analysis of merge(h1, h2)
The analysis of merge(h1, h2) is based on the analysis of a random walk in
a binary tree. A random walk in a binary tree starts at the root of the tree.
At each step in the random walk, a coin is tossed and, depending on the
result of this coin toss, the walk proceeds to the left or to the right child
of the current node. The walk ends when it falls off the tree (the current
node becomes nil).
The following lemma is somewhat remarkable because it does not de-
pend at all on the shape of the binary tree:
Lemma 10.1. The expected length of a random walk in a binary tree with n
nodes is at most log(n + 1).
Proof. The proof is by induction on n. In the base case, n = 0 and the
walk has length 0 = log(n + 1). Suppose now that the result is true for all
non-negative integers n(cid:48) < n.
Let n1 denote the size of the root’s left subtree, so that n2 = n
n1 −
1
is the size of the root’s right subtree. Starting at the root, the walk takes
one step and then continues in a subtree of size n1 or n2. By our inductive
hypothesis, the expected length of the walk is then
−
E[W ] = 1 +
1
2
log(n1 + 1) +
1
2
log(n2 + 1) ,
since each of n1 and n2 are less than n. Since log is a concave function,
E[W ] is maximized when n1 = n2 = (n
1)/2. Therefore, the expected
number of steps taken by the random walk is
−
E[W ] = 1 +
log(n1 + 1) +
log(n2 + 1)
1
2
1 + log((n
1
2
1)/2 + 1)
≤
= 1 + log((n + 1)/2)
−
= log(n + 1) .
We make a quick digression to note that, for readers who know a little
about information theory, the proof of Lemma 10.1 can be stated in terms
of entropy.
220
MeldableHeap: A Randomized Meldable Heap
§10.2
Information Theoretic Proof of Lemma 10.1. Let di denote the depth of the
ith external node and recall that a binary tree with n nodes has n + 1 exter-
nal nodes. The probability of the random walk reaching the ith external
node is exactly pi = 1/2di , so the expected length of the random walk is
given by
n
n
n
H =
pidi =
pi log
2di
=
pi log(1/pi)
(cid:88)i=0
(cid:88)i=0
(cid:16)
(cid:17)
(cid:88)i=0
The right hand side of this equation is easily recognizable as the entropy
of a probability distribution over n + 1 elements. A basic fact about the
entropy of a distribution over n + 1 elements is that it does not exceed
log(n + 1), which proves the lemma.
With this result on random walks, we can now easily prove that the
running time of the merge(h1, h2) operation is O(log n).
Lemma 10.2. If h1 and h2 are the roots of two heaps containing n1 and n2
nodes, respectively, then the expected running time of merge(h1, h2) is at most
O(log n), where n = n1 + n2.
Proof. Each step of the merge algorithm takes one step of a random walk,
either in the heap rooted at h1 or the heap rooted at h2. The algorithm
terminates when either of these two random walks fall out of its corre-
sponding tree (when h1 = null or h2 = null). Therefore, the expected
number of steps performed by the merge algorithm is at most
log(n1 + 1) + log(n2 + 1)
2 log n .
≤
10.2.2 Summary
The following theorem summarizes the performance of a MeldableHeap:
Theorem 10.2. A MeldableHeap implements the (priority) Queue interface.
A MeldableHeap supports the operations add(x) and remove() in O(log n)
expected time per operation.
221
§10.3
Heaps
10.3 Discussion and Exercises
The implicit representation of a complete binary tree as an array, or list,
seems to have been first proposed by Eytzinger [27]. He used this rep-
resentation in books containing pedigree family trees of noble families.
The BinaryHeap data structure described here was first introduced by
Williams [78].
The randomized MeldableHeap data structure described here appears
to have first been proposed by Gambin and Malinowski [34]. Other meld-
able heap implementations exist, including leftist heaps [16, 48, Sec-
tion 5.3.2], binomial heaps [75], Fibonacci heaps [30], pairing heaps [29],
and skew heaps [72], although none of these are as simple as the Meld-
ableHeap structure.
≤
Some of the above structures also support a decreaseKey(u, y) oper-
ation in which the value stored at node u is decreased to y. (It is a pre-
condition that y
u.x.) In most of the preceding structures, this opera-
tion can be supported in O(log n) time by removing node u and adding
y. However, some of these structures can implement decreaseKey(u, y)
more efficiently. In particular, decreaseKey(u, y) takes O(1) amortized
time in Fibonacci heaps and O(log log n) amortized time in a special ver-
sion of pairing heaps [25]. This more efficient decreaseKey(u, y) opera-
tion has applications in speeding up several graph algorithms, including
Dijkstra’s shortest path algorithm [30].
Exercise 10.1. Illustrate the addition of the values 7 and then 3 to the
BinaryHeap shown at the end of Figure 10.2.
Exercise 10.2. Illustrate the removal of the next two values (6 and 8) on
the BinaryHeap shown at the end of Figure 10.3.
Exercise 10.3. Implement the remove(i) method, that removes the value
stored in a[i] in a BinaryHeap. This method should run in O(log n) time.
Next, explain why this method is not likely to be useful.
Exercise 10.4. A d-ary tree is a generalization of a binary tree in which
each internal node has d children. Using Eytzinger’s method it is also
possible to represent complete d-ary trees using arrays. Work out the
222
Discussion and Exercises
§10.3
equations that, given an index i, determine the index of i’s parent and
each of i’s d children in this representation.
Exercise 10.5. Using what you learned in Exercise 10.4, design and im-
plement a DaryHeap, the d-ary generalization of a BinaryHeap. Analyze
the running times of operations on a DaryHeap and test the performance
of your DaryHeap implementation against that of the BinaryHeap imple-
mentation given here.
Exercise 10.6. Illustrate the addition of the values 17 and then 82 in the
MeldableHeap h1 shown in Figure 10.4. Use a coin to simulate a random
bit when needed.
Exercise 10.7. Illustrate the removal of the next two values (4 and 8)
in the MeldableHeap h1 shown in Figure 10.4. Use a coin to simulate a
random bit when needed.
Exercise 10.8. Implement the remove(u) method, that removes the node
u from a MeldableHeap. This method should run in O(log n) expected
time.
Exercise 10.9. Show how to find the second smallest value in a Binary-
Heap or MeldableHeap in constant time.
Exercise 10.10. Show how to find the kth smallest value in a BinaryHeap
or MeldableHeap in O(k log k) time.
(Hint: Using another heap might
help.)
Exercise 10.11. Suppose you are given k sorted lists, of total length n. Us-
ing a heap, show how to merge these into a single sorted list in O(n log k)
time. (Hint: Starting with the case k = 2 can be instructive.)
223
Chapter 11
Sorting Algorithms
This chapter discusses algorithms for sorting a set of n items. This might
seem like a strange topic for a book on data structures, but there are sev-
eral good reasons for including it here. The most obvious reason is that
two of these sorting algorithms (quicksort and heap-sort) are intimately
related to two of the data structures we have already studied (random
binary search trees and heaps, respectively).
The first part of this chapter discusses algorithms that sort using only
comparisons and presents three algorithms that run in O(n log n) time. As
it turns out, all three algorithms are asymptotically optimal; no algorithm
that uses only comparisons can avoid doing roughly n log n comparisons
in the worst case and even the average case.
Before continuing, we should note that any of the SSet or priority
Queue implementations presented in previous chapters can also be used
to obtain an O(n log n) time sorting algorithm. For example, we can sort
n items by performing n add(x) operations followed by n remove() op-
erations on a BinaryHeap or MeldableHeap. Alternatively, we can use n
add(x) operations on any of the binary search tree data structures and
then perform an in-order traversal (Exercise 6.8) to extract the elements
in sorted order. However, in both cases we go through a lot of overhead
to build a structure that is never fully used. Sorting is such an important
problem that it is worthwhile developing direct methods that are as fast,
simple, and space-efficient as possible.
The second part of this chapter shows that, if we allow other opera-
tions besides comparisons, then all bets are off. Indeed, by using array-
225
§11.1
Sorting Algorithms
indexing, it is possible to sort a set of n integers in the range
in O(cn) time.
{
0, . . . , nc
1
}
−
11.1 Comparison-Based Sorting
In this section, we present three sorting algorithms: merge-sort, quick-
sort, and heap-sort. Each of these algorithms takes an input array a and
sorts the elements of a into non-decreasing order in O(n log n) (expected)
time. These algorithms are all comparison-based. Their second argument,
c, is a Comparator that implements the compare(a, b) method. These al-
gorithms don’t care what type of data is being sorted; the only opera-
tion they do on the data is comparisons using the compare(a, b) method.
Recall, from Section 1.2.4, that compare(a, b) returns a negative value if
a < b, a positive value if a > b, and zero if a = b.
11.1.1 Merge-Sort
The merge-sort algorithm is a classic example of recursive divide and con-
quer: If the length of a is at most 1, then a is already sorted, so we do
1]
nothing. Otherwise, we split a into two halves, a0 = a[0], . . . , a[n/2
and a1 = a[n/2], . . . , a[n
1]. We recursively sort a0 and a1, and then we
merge (the now sorted) a0 and a1 to get our fully sorted array a:
−
−
Algorithms
<T> void mergeSort(T[] a, Comparator<T> c) {
if (a.length <= 1) return;
T[] a0 = Arrays.copyOfRange(a, 0, a.length/2);
T[] a1 = Arrays.copyOfRange(a, a.length/2, a.length);
mergeSort(a0, c);
mergeSort(a1, c);
merge(a0, a1, a, c);
}
An example is shown in Figure 11.1.
Compared to sorting, merging the two sorted arrays a0 and a1 is fairly
easy. We add elements to a one at a time. If a0 or a1 is empty, then we
add the next elements from the other (non-empty) array. Otherwise, we
226
Comparison-Based Sorting
§11.1
Figure 11.1: The execution of mergeSort(a, c)
take the minimum of the next element in a0 and the next element in a1
and add it to a:
<T> void merge(T[] a0, T[] a1, T[] a, Comparator<T> c) {
Algorithms
int i0 = 0, i1 = 0;
for (int i = 0; i < a.length; i++) {
if (i0 == a0.length)
a[i] = a1[i1++];
else if (i1 == a1.length)
a[i] = a0[i0++];
else if (compare(a0[i0], a1[i1]) < 0)
a[i] = a0[i0++];
else
a[i] = a1[i1++];
}
}
Notice that the merge(a0, a1, a, c) algorithm performs at most n
comparisons before running out of elements in one of a0 or a1.
1
−
To understand the running-time of merge-sort, it is easiest to think
of it in terms of its recursion tree. Suppose for now that n is a power of
227
246851301379101112124536789101112130854026973121101113mergeSort(a0,c)mergeSort(a1,c)9731211011a0a185402613aaa0a1merge(a0,a1,a)§11.1
Sorting Algorithms
Figure 11.2: The merge-sort recursion tree.
two, so that n = 2log n, and log n is an integer. Refer to Figure 11.2. Merge-
sort turns the problem of sorting n elements into two problems, each of
sorting n/2 elements. These two subproblem are then turned into two
problems each, for a total of four subproblems, each of size n/4. These
four subproblems become eight subproblems, each of size n/8, and so
on. At the bottom of this process, n/2 subproblems, each of size two, are
converted into n problems, each of size one. For each subproblem of size
n/2i, the time spent merging and copying data is O(n/2i). Since there are
2i subproblems of size n/2i, the total time spent working on problems of
size 2i, not counting recursive calls, is
×
Therefore, the total amount of time taken by merge-sort is
2i
O(n/2i) = O(n) .
log n
O(n) = O(n log n) .
(cid:88)i=0
The proof of the following theorem is based on preceding analysis,
but has to be a little more careful to deal with the cases where n is not a
power of 2.
Theorem 11.1. The mergeSort(a, c) algorithm runs in O(n log n) time and
performs at most n log n comparisons.
228
2112112112112112n2n2nn4n4n4n4n8n8n8n8n8n8n8n8........................++++++++++++++++···++++++11=n=n=n=n=n=n++++++···Comparison-Based Sorting
§11.1
Proof. The proof is by induction on n. The base case, in which n = 1,
is trivial; when presented with an array of length 0 or 1 the algorithm
simply returns without performing any comparisons.
Merging two sorted lists of total length n requires at most n
1 compar-
isons. Let C(n) denote the maximum number of comparisons performed
by mergeSort(a, c) on an array a of length n. If n is even, then we apply
the inductive hypothesis to the two subproblems and obtain
−
1 + 2C(n/2)
1 + 2((n/2) log(n/2))
1 + n log(n/2)
C(n)
≤
n
n
≤
= n
= n
−
−
−
1 + n log n
n
−
−
< n log n .
The case where n is odd is slightly more complicated. For this case, we
use two inequalities that are easy to verify:
log(x + 1)
≤
log(x) + 1 ,
for all x
1 and
≥
log(x + 1/2) + log(x
1/2)
−
≤
2 log(x) ,
(11.1)
(11.2)
for all x
1/2. Inequality (11.1) comes from the fact that log(x) + 1 =
log(2x) while (11.2) follows from the fact that log is a concave function.
With these tools in hand we have, for odd n,
≥
1/2) log(n/2
1/2)
−
−
log(n/2
−
−
1/2))
1 + C(
(cid:100)
n/2
(cid:100)
1 +
n/2
n/2
) + C(
(cid:98)
(cid:101)
n/2
log
(cid:100)
)
(cid:99)
n/2
(cid:99)
(cid:98)
+
(cid:99)
(cid:101)
1 + (n/2 + 1/2) log(n/2 + 1/2) + (n/2
(cid:101)
n/2
log
(cid:98)
1 + n log(n/2) + (1/2)(log(n/2 + 1/2)
C(n)
≤
n
n
n
n
−
−
−
−
≤
= n
≤
1 + n log(n/2) + 1/2
−
≤
< n + n log(n/2)
= n + n(log n
= n log n .
1)
−
229
§11.1
Sorting Algorithms
11.1.2 Quicksort
The quicksort algorithm is another classic divide and conquer algorithm.
Unlike merge-sort, which does merging after solving the two subprob-
lems, quicksort does all of its work upfront.
Quicksort is simple to describe: Pick a random pivot element, x, from
a; partition a into the set of elements less than x, the set of elements
equal to x, and the set of elements greater than x; and, finally, recursively
sort the first and third sets in this partition. An example is shown in
Figure 11.3.
<T> void quickSort(T[] a, Comparator<T> c) {
quickSort(a, 0, a.length, c);
Algorithms
}
<T> void quickSort(T[] a, int i, int n, Comparator<T> c) {
if (n <= 1) return;
T x = a[i + rand.nextInt(n)];
int p = i-1, j = i, q = i+n;
// a[i..p]<x, a[p+1..q-1]??x, a[q..i+n-1]>x
while (j < q) {
int comp = compare(a[j], x);
if (comp < 0) {
// move to beginning of array
swap(a, j++, ++p);
} else if (comp > 0) {
swap(a, j, --q); // move to end of array
} else {
j++;
}
// keep in the middle
}
// a[i..p]<x, a[p+1..q-1]=x, a[q..i+n-1]>x
quickSort(a, i, p-i+1, c);
quickSort(a, q, n-(q-i), c);
}
All of this is done in place, so that instead of making copies of subar-
rays being sorted, the quickSort(a, i, n, c) method only sorts the subarray
1]. Initially, this method is invoked with the arguments
a[i], . . . , a[i + n
quickSort(a, 0, a.length, c).
−
230
Comparison-Based Sorting
§11.1
Figure 11.3: An example execution of quickSort(a, 0, 14, c)
At the heart of the quicksort algorithm is the in-place partitioning al-
gorithm. This algorithm, without using any extra space, swaps elements
in a and computes indices p and q so that
i
≤
p
< x if 0
= x if p < i < q
n
> x if q
≤
i
≤
≤
1
−
a[i]
This partitioning, which is done by the while loop in the code, works by
iteratively increasing p and decreasing q while maintaining the first and
last of these conditions. At each step, the element at position j is either
moved to the front, left where it is, or moved to the back. In the first two
cases, j is incremented, while in the last case, j is not incremented since
the new element at position j has not yet been processed.
Quicksort is very closely related to the random binary search trees
In fact, if the input to quicksort consists of n
studied in Section 7.1.
distinct elements, then the quicksort recursion tree is a random binary
search tree. To see this, recall that when constructing a random binary
search tree the first thing we do is pick a random element x and make it
the root of the tree. After this, every element will eventually be compared
to x, with smaller elements going into the left subtree and larger elements
into the right.
In quicksort, we select a random element x and immediately compare
everything to x, putting the smaller elements at the beginning of the array
and larger elements at the end of the array. Quicksort then recursively
231
854026713854026731211011139124536891011121307124536708101112139121011139xquickSort(a,10,4)quickSort(a,0,9)§11.1
Sorting Algorithms
sorts the beginning of the array and the end of the array, while the random
binary search tree recursively inserts smaller elements in the left subtree
of the root and larger elements in the right subtree of the root.
The above correspondence between random binary search trees and
quicksort means that we can translate Lemma 7.1 to a statement about
quicksort:
Lemma 11.1. When quicksort is called to sort an array containing the inte-
gers 0, . . . , n
1, the expected number of times element i is compared to a pivot
element is at most Hi+1 + Hn
−
i.
−
A little summing up of harmonic numbers gives us the following the-
orem about the running time of quicksort:
Theorem 11.2. When quicksort is called to sort an array containing n distinct
elements, the expected number of comparisons performed is at most 2n ln n +
O(n).
Proof. Let T be the number of comparisons performed by quicksort when
sorting n distinct elements. Using Lemma 11.1 and linearity of expecta-
tion, we have:
n
1
−
E[T ] =
(Hi+1 + Hn
(cid:88)i=0
n
= 2
Hi
i)
−
(cid:88)i=1
n
2
Hn
(cid:88)i=1
2n ln n + 2n = 2n ln n + O(n)
≤
≤
Theorem 11.3 describes the case where the elements being sorted are
all distinct. When the input array, a, contains duplicate elements, the
expected running time of quicksort is no worse, and can be even better;
any time a duplicate element x is chosen as a pivot, all occurrences of x get
grouped together and do not take part in either of the two subproblems.
Theorem 11.3. The quickSort(a, c) method runs in O(n log n) expected
time and the expected number of comparisons it performs is at most 2n ln n +
O(n).
232
Comparison-Based Sorting
§11.1
Figure 11.4: A snapshot of the execution of heapSort(a, c). The shaded part of
the array is already sorted. The unshaded part is a BinaryHeap. During the next
iteration, element 5 will be placed into array location 8.
11.1.3 Heap-sort
The heap-sort algorithm is another in-place sorting algorithm. Heap-sort
uses the binary heaps discussed in Section 10.1. Recall that the Binary-
Heap data structure represents a heap using a single array. The heap-sort
algorithm converts the input array a into a heap and then repeatedly ex-
tracts the minimum value.
−
More specifically, a heap stores n elements in an array, a, at array lo-
cations a[0], . . . , a[n
1] with the smallest value stored at the root, a[0].
After transforming a into a BinaryHeap, the heap-sort algorithm repeat-
edly swaps a[0] and a[n
1], decrements n, and calls trickleDown(0) so
that a[0], . . . , a[n
2] once again are a valid heap representation. When
this process ends (because n = 0) the elements of a are stored in decreas-
ing order, so a is reversed to obtain the final sorted order.1 Figure 11.4
shows an example of the execution of heapSort(a, c).
−
−
BinaryHeap
<T> void sort(T[] a, Comparator<T> c) {
BinaryHeap<T> h = new BinaryHeap<T>(a, c);
while (h.n > 1) {
1The algorithm could alternatively redefine the compare(x, y) function so that the heap
sort algorithm stores the elements directly in ascending order.
233
96138107111251110129135786421031245368111213141507§11.1
Sorting Algorithms
h.swap(--h.n, 0);
h.trickleDown(0);
}
Collections.reverse(Arrays.asList(a));
}
A key subroutine in heap sort is the constructor for turning an un-
sorted array a into a heap. It would be easy to do this in O(n log n) time by
repeatedly calling the BinaryHeap add(x) method, but we can do better by
using a bottom-up algorithm. Recall that, in a binary heap, the children
of a[i] are stored at positions a[2i + 1] and a[2i + 2]. This implies that
the elements a[
1] have no children. In other words, each
], . . . , a[n
(cid:99)
1] is a sub-heap of size 1. Now, working backwards,
of a[
n/2
−
(cid:99)
(cid:98)
we can call trickleDown(i) for each i
. This works, be-
cause by the time we call trickleDown(i), each of the two children of a[i]
are the root of a sub-heap, so calling trickleDown(i) makes a[i] into the
root of its own subheap.
1, . . . , 0
}
], . . . , a[n
n/2
n/2
∈ {(cid:98)
(cid:99) −
−
(cid:98)
BinaryHeap
BinaryHeap(T[] a, Comparator<T> c) {
this.c = c;
this.a = a;
n = a.length;
for (int i = n/2-1; i >= 0; i--) {
trickleDown(i);
}
}
The interesting thing about this bottom-up strategy is that it is more
efficient than calling add(x) n times. To see this, notice that, for n/2 el-
ements, we do no work at all, for n/4 elements, we call trickleDown(i)
on a subheap rooted at a[i] and whose height is one, for n/8 elements,
we call trickleDown(i) on a subheap whose height is two, and so on.
Since the work done by trickleDown(i) is proportional to the height of
the sub-heap rooted at a[i], this means that the total work done is at most
log n
(cid:88)i=1
O((i
−
1)n/2i)
∞
≤
(cid:88)i=1
O(in/2i) = O(n)
∞
(cid:88)i=1
i/2i = O(2n) = O(n) .
234
Comparison-Based Sorting
§11.1
∞i=1 i/2i is
The second-last equality follows by recognizing that the sum
equal, by definition of expected value, to the expected number of times
we toss a coin up to and including the first time the coin comes up as
heads and applying Lemma 4.2.
(cid:80)
The following theorem describes the performance of heapSort(a, c).
Theorem 11.4. The heapSort(a, c) method runs in O(n log n) time and per-
forms at most 2n log n + O(n) comparisons.
Proof. The algorithm runs in three steps: (1) transforming a into a heap,
(2) repeatedly extracting the minimum element from a, and (3) revers-
ing the elements in a. We have just argued that step 1 takes O(n) time
and performs O(n) comparisons. Step 3 takes O(n) time and performs no
comparisons. Step 2 performs n calls to trickleDown(0). The ith such
call operates on a heap of size n
i) com-
parisons. Summing this over i gives
i and performs at most 2 log(n
−
−
n
i
−
2 log(n
n
i
−
2 log n = 2n log n
i)
−
≤
(cid:88)i=0
(cid:88)i=0
Adding the number of comparisons performed in each of the three steps
completes the proof.
11.1.4 A Lower-Bound for Comparison-Based Sorting
We have now seen three comparison-based sorting algorithms that each
run in O(n log n) time. By now, we should be wondering if faster algo-
rithms exist. The short answer to this question is no. If the only oper-
ations allowed on the elements of a are comparisons, then no algorithm
can avoid doing roughly n log n comparisons. This is not difficult to prove,
but requires a little imagination. Ultimately, it follows from the fact that
log(n!) = log n + log(n
1) +
−
· · ·
+ log(1) = n log n
O(n) .
−
(Proving this fact is left as Exercise 11.11.)
We will start by focusing our attention on deterministic algorithms
like merge-sort and heap-sort and on a particular fixed value of n. Imag-
ine such an algorithm is being used to sort n distinct elements. The key
235
§11.1
Sorting Algorithms
Figure 11.5: A comparison tree for sorting an array a[0], a[1], a[2] of length n = 3.
−
−
−
1] and a[n
to proving the lower-bound is to observe that, for a deterministic algo-
rithm with a fixed value of n, the first pair of elements that are compared
is always the same. For example, in heapSort(a, c), when n is even, the
1 and the first comparison is
first call to trickleDown(i) is with i = n/2
between elements a[n/2
1].
Since all input elements are distinct, this first comparison has only
two possible outcomes. The second comparison done by the algorithm
may depend on the outcome of the first comparison. The third compar-
ison may depend on the results of the first two, and so on. In this way,
any deterministic comparison-based sorting algorithm can be viewed as
a rooted binary comparison tree. Each internal node, u, of this tree is la-
belled with a pair of indices u.i and u.j. If a[u.i] < a[u.j] the algorithm
proceeds to the left subtree, otherwise it proceeds to the right subtree.
1]
Each leaf w of this tree is labelled with a permutation w.p[0], . . . , w.p[n
of 0, . . . , n
1. This permutation represents the one that is required to sort
a if the comparison tree reaches this leaf. That is,
−
−
a[w.p[0]] < a[w.p[1]] <
< a[w.p[n
1]] .
−
· · ·
An example of a comparison tree for an array of size n = 3 is shown in
Figure 11.5.
The comparison tree for a sorting algorithm tells us everything about
the algorithm. It tells us exactly the sequence of comparisons that will be
performed for any input array, a, having n distinct elements and it tells
us how the algorithm will reorder a in order to sort it. Consequently, the
comparison tree must have at least n! leaves; if not, then there are two
distinct permutations that lead to the same leaf; therefore, the algorithm
236
a[1]≶a[2]a[0]≶a[2]a[1]<a[0]<a[2]a[0]≶a[1]a[1]≶a[2]a[0]≶a[2]a[0]<a[1]<a[2]a[0]<a[2]<a[1]a[2]<a[0]<a[1]a[1]<a[2]<a[0]a[2]<a[1]<a[0]<><><><><>Comparison-Based Sorting
§11.1
Figure 11.6: A comparison tree that does not correctly sort every input permuta-
tion.
does not correctly sort at least one of these permutations.
For example, the comparison tree in Figure 11.6 has only 4 < 3! = 6
leaves. Inspecting this tree, we see that the two input arrays 3, 1, 2 and
3, 2, 1 both lead to the rightmost leaf. On the input 3, 1, 2 this leaf correctly
outputs a[1] = 1, a[2] = 2, a[0] = 3. However, on the input 3, 2, 1, this node
incorrectly outputs a[1] = 2, a[2] = 1, a[0] = 3. This discussion leads to the
primary lower-bound for comparison-based algorithms.
Theorem 11.5. For any deterministic comparison-based sorting algorithm
and any integer n
1, there exists an input array a of length n such that
performs at least log(n!) = n log n
O(n) comparisons when sorting a.
≥
−
A
A
Proof. By the preceding discussion, the comparison tree defined by
A
must have at least n! leaves. An easy inductive proof shows that any
binary tree with k leaves has a height of at least log k. Therefore, the
has a leaf, w, with a depth of at least log(n!) and
comparison tree for
there is an input array a that leads to this leaf. The input array a is an
input for which
does at least log(n!) comparisons.
A
A
Theorem 11.5 deals with deterministic algorithms like merge-sort and
heap-sort, but doesn’t tell us anything about randomized algorithms like
quicksort. Could a randomized algorithm beat the log(n!) lower bound
on the number of comparisons? The answer, again, is no. Again, the way
to prove it is to think differently about what a randomized algorithm is.
In the following discussion, we will assume that our decision trees
have been “cleaned up” in the following way: Any node that can not be
reached by some input array a is removed. This cleaning up implies that
the tree has exactly n! leaves. It has at least n! leaves because, otherwise, it
237
a[0]≶a[2]a[1]<a[0]<a[2]a[0]≶a[1]a[1]≶a[2]a[0]<a[1]<a[2]a[0]<a[2]<a[1]a[1]<a[2]<a[0]<><><>§11.2
Sorting Algorithms
could not sort correctly. It has at most n! leaves since each of the possible
n! permutation of n distinct elements follows exactly one root to leaf path
in the decision tree.
R
We can think of a randomized sorting algorithm,
, as a determin-
istic algorithm that takes two inputs: The input array a that should be
sorted and a long sequence b = b1, b2, b3, . . . , bm of random real numbers
in the range [0, 1]. The random numbers provide the randomization for
the algorithm. When the algorithm wants to toss a coin or make a ran-
dom choice, it does so by using some element from b. For example, to
compute the index of the first pivot in quicksort, the algorithm could use
the formula
Now, notice that if we fix b to some particular sequence ˆb then
becomes a deterministic sorting algorithm,
comparison tree,
T
1, . . . , n
mutation of
}
from the n! leaves of
R
(ˆb), that has an associated
R
(ˆb). Next, notice that if we select a to be a random per-
, then this is equivalent to selecting a random leaf, w,
(ˆb).
{
.
nb1(cid:99)
(cid:98)
Exercise 11.13 asks you to prove that, if we select a random leaf from
any binary tree with k leaves, then the expected depth of that leaf is at
least log k. Therefore, the expected number of comparisons performed by
(ˆb) when given an input array containing a
the (deterministic) algorithm
R
is at least log(n!). Finally, notice that this
random permutation of
1, . . . , n
}
is true for every choice of ˆb, therefore it holds even for
. This completes
R
the proof of the lower-bound for randomized algorithms.
{
T
Theorem 11.6. For any integer n
≥
comparison-based sorting algorithm
done by
n log n
A
O(n).
−
when sorting a random permutation of
1 and any (deterministic or randomized)
, the expected number of comparisons
is at least log(n!) =
A
1, . . . , n
}
{
11.2 Counting Sort and Radix Sort
In this section we study two sorting algorithms that are not comparison-
based. Specialized for sorting small integers, these algorithms elude the
lower-bounds of Theorem 11.5 by using (parts of) the elements in a as
238
Counting Sort and Radix Sort
§11.2
indices into an array. Consider a statement of the form
c[a[i]] = 1 .
This statement executes in constant time, but has c.length possible dif-
ferent outcomes, depending on the value of a[i]. This means that the
execution of an algorithm that makes such a statement cannot be mod-
elled as a binary tree. Ultimately, this is the reason that the algorithms in
this section are able to sort faster than comparison-based algorithms.
11.2.1 Counting Sort
Suppose we have an input array a consisting of n integers, each in the
1. The counting-sort algorithm sorts a using an auxiliary
range 0, . . . , k
array c of counters. It outputs a sorted version of a as an auxiliary array
b.
−
The idea behind counting-sort is simple: For each i
,
1
}
count the number of occurrences of i in a and store this in c[i]. Now,
after sorting, the output will look like c[0] occurrences of 0, followed by
c[1] occurrences of 1, followed by c[2] occurrences of 2,. . . , followed by
1. The code that does this is very slick, and its
c[k
execution is illustrated in Figure 11.7:
1] occurrences of k
0, . . . , k
∈ {
−
−
−
Algorithms
int[] countingSort(int[] a, int k) {
int c[] = new int[k];
for (int i = 0; i < a.length; i++)
c[a[i]]++;
for (int i = 1; i < k; i++)
c[i] += c[i-1];
int b[] = new int[a.length];
for (int i = a.length-1; i >= 0; i--)
b[--c[a[i]]] = a[i];
return b;
}
The first for loop in this code sets each counter c[i] so that it counts
the number of occurrences of i in a. By using the values of a as indices,
239
§11.2
Sorting Algorithms
Figure 11.7: The operation of counting sort on an array of length n = 20 that stores
integers 0, . . . , k
1 = 9.
−
240
7290120974469109325932312112050123456789ca3589111213151520c001234567891011121314151617181900011222344567799999b72901209744691093259a35891112131520c00123456789Counting Sort and Radix Sort
§11.2
these counters can all be computed in O(n) time with a single for loop. At
this point, we could use c to fill in the output array b directly. However,
this would not work if the elements of a have associated data. Therefore
we spend a little extra effort to copy the elements of a into b.
The next for loop, which takes O(k) time, computes a running-sum
of the counters so that c[i] becomes the number of elements in a that are
less than or equal to i. In particular, for every i
, the output
array, b, will have
0, . . . , k
1
}
∈ {
−
b[c[i
−
1]] = b[c[i
1] + 1] =
= b[c[i]
1] = i .
−
· · ·
−
Finally, the algorithm scans a backwards to place its elements, in order,
into an output array b. When scanning, the element a[i] = j is placed at
location b[c[j]
1] and the value c[j] is decremented.
−
Theorem 11.7. The countingSort(a, k) method can sort an array a contain-
ing n integers in the set
in O(n + k) time.
1
0, . . . , k
{
−
}
The counting-sort algorithm has the nice property of being stable; it
preserves the relative order of equal elements. If two elements a[i] and
a[j] have the same value, and i < j then a[i] will appear before a[j] in b.
This will be useful in the next section.
11.2.2 Radix-Sort
Counting-sort is very efficient for sorting an array of integers when the
length, n, of the array is not much smaller than the maximum value, k
1,
that appears in the array. The radix-sort algorithm, which we now de-
scribe, uses several passes of counting-sort to allow for a much greater
range of maximum values.
−
Radix-sort sorts w-bit integers by using w/d passes of counting-sort to
sort these integers d bits at a time.2 More precisely, radix sort first sorts
the integers by their least significant d bits, then their next significant d
bits, and so on until, in the last pass, the integers are sorted by their most
significant d bits.
2We assume that d divides w, otherwise we can always increase w to d
w/d
.
(cid:101)
(cid:100)
241
§11.2
Sorting Algorithms
Figure 11.8: Using radixsort to sort w = 8-bit integers by using 4 passes of count-
ing sort on d = 2-bit integers.
Algorithms
int[] radixSort(int[] a) {
int[] b = null;
for (int p = 0; p < w/d; p++) {
int c[] = new int[1<<d];
// the next three for loops implement counting-sort
b = new int[a.length];
for (int i = 0; i < a.length; i++)
c[(a[i] >> d*p)&((1<<d)-1)]++;
for (int i = 1; i < 1<<d; i++)
c[i] += c[i-1];
for (int i = a.length-1; i >= 0; i--)
b[--c[(a[i] >> d*p)&((1<<d)-1)]] = a[i];
a = b;
}
return b;
}
∗
−
p)&((1<<d)
(In this code, the expression (a[i]>>d
1) extracts the in-
teger whose binary representation is given by bits (p + 1)d
1, . . . , pd of
a[i].) An example of the steps of this algorithm is shown in Figure 11.8.
This remarkable algorithm sorts correctly because counting-sort is a
stable sorting algorithm. If x < y are two elements of a, and the most
significant bit at which x differs from y has index r, then x will be placed
r/d
before y during pass
and subsequent passes will not change the rel-
(cid:98)
ative order of x and y.
−
(cid:99)
242
01010001000000011100100000101000000011111111000010101010010101011100100000101000111100000101000100000001010101011010101000001111000000011111000001010101110010000010100010101010010100010000111100000001110010000000111101010001010101010010100010101010111100000000000100001111001010000101000101010101101010101100100011110000Discussion and Exercises
§11.3
Radix-sort performs w/d passes of counting-sort. Each pass requires
O(n + 2d) time. Therefore, the performance of radix-sort is given by the
following theorem.
Theorem 11.8. For any integer d > 0, the radixSort(a, k) method can sort
an array a containing n w-bit integers in O((w/d)(n + 2d)) time.
If we think, instead, of the elements of the array being in the range
we obtain the following version of The-
, and take d =
log n
(cid:101)
(cid:100)
0, . . . , nc
1
{
}
−
orem 11.8.
Corollary 11.1. The radixSort(a, k) method can sort an array a containing
n integer values in the range
in O(cn) time.
0, . . . , nc
{
1
}
−
11.3 Discussion and Exercises
Sorting is the fundamental algorithmic problem in computer science, and
it has a long history. Knuth [48] attributes the merge-sort algorithm to
von Neumann (1945). Quicksort is due to Hoare [39]. The original heap-
sort algorithm is due to Williams [78], but the version presented here (in
which the heap is constructed bottom-up in O(n) time) is due to Floyd
[28]. Lower-bounds for comparison-based sorting appear to be folklore.
The following table summarizes the performance of these comparison-
based algorithms:
comparisons
in-place
Merge-sort
Quicksort
Heap-sort
n log n
1.38n log n + O(n) expected
worst-case No
Yes
2n log n + O(n) worst-case Yes
Each of these comparison-based algorithms has its advantages and
disadvantages. Merge-sort does the fewest comparisons and does not rely
on randomization. Unfortunately, it uses an auxilliary array during its
merge phase. Allocating this array can be expensive and is a potential
point of failure if memory is limited. Quicksort is an in-place algorithm
and is a close second in terms of the number of comparisons, but is ran-
domized, so this running time is not always guaranteed. Heap-sort does
the most comparisons, but it is in-place and deterministic.
243
§11.3
Sorting Algorithms
There is one setting in which merge-sort is a clear-winner; this occurs
when sorting a linked-list. In this case, the auxiliary array is not needed;
two sorted linked lists are very easily merged into a single sorted linked-
list by pointer manipulations (see Exercise 11.2).
The counting-sort and radix-sort algorithms described here are due
to Seward [68, Section 2.4.6]. However, variants of radix-sort have been
used since the 1920s to sort punch cards using punched card sorting ma-
chines. These machines can sort a stack of cards into two piles based on
the existence (or not) of a hole in a specific location on the card. Repeat-
ing this process for different hole locations gives an implementation of
radix-sort.
Finally, we note that counting sort and radix-sort can be used to sort
other types of numbers besides non-negative integers. Straightforward
modifications of counting sort can sort integers, in any interval
,
}
in O(n + b
a) time. Similarly, radix sort can sort integers in the same
−
interval in O(n(logn(b
a)) time. Finally, both of these algorithms can also
be used to sort floating point numbers in the IEEE 754 floating point for-
mat. This is because the IEEE format is designed to allow the comparison
of two floating point numbers by comparing their values as if they were
integers in a signed-magnitude binary representation [2].
a, . . . , b
{
−
Exercise 11.1. Illustrate the execution of merge-sort and heap-sort on an
input array containing 1, 7, 4, 6, 2, 8, 3, 5. Give a sample illustration of one
possible execution of quicksort on the same array.
Exercise 11.2. Implement a version of the merge-sort algorithm that sorts
a DLList without using an auxiliary array. (See Exercise 3.13.)
Exercise 11.3. Some implementations of quickSort(a, i, n, c) always use
a[i] as a pivot. Give an example of an input array of length n in which
such an implementation would perform
comparisons.
n
2
Exercise 11.4. Some implementations of quickSort(a, i, n, c) always use
a[i + n/2] as a pivot. Given an example of an input array of length n in
which such an implementation would perform
comparisons.
(cid:0)
(cid:1)
n
2
Exercise 11.5. Show that, for any implementation of quickSort(a, i, n, c)
that chooses a pivot deterministically, without first looking at any values
(cid:0)
(cid:1)
244
Discussion and Exercises
§11.3
in a[i], . . . , a[i + n
this implementation to perform
−
n
2
comparisons.
1], there exists an input array of length n that causes
(cid:1)
(cid:0)
Exercise 11.6. Design a Comparator, c, that you could pass as an argu-
ment to quickSort(a, i, n, c) and that would cause quicksort to perform
n
comparisons. (Hint: Your comparator does not actually need to look
2
at the values being compared.)
(cid:0)
Exercise 11.7. Analyze the expected number of comparisons done by
Quicksort a little more carefully than the proof of Theorem 11.3. In par-
n + Hn.
ticular, show that the expected number of comparisons is 2nHn
(cid:1)
−
Exercise 11.8. Describe an input array that causes heap sort to perform
O(n) comparisons. Justify your answer.
at least 2n log n
−
Exercise 11.9. The heap sort implementation described here sorts the
elements into reverse sorted order and then reverses the array. This last
step could be avoided by defining a new Comparator that negates the
results of the input Comparator, c. Explain why this would not be a good
optimization.
(Hint: Consider how many negations would need to be
done in relation to how long it takes to reverse the array.)
Exercise 11.10. Find another pair of permutations of 1, 2, 3 that are not
correctly sorted by the comparison tree in Figure 11.6.
Exercise 11.11. Prove that log n! = n log n
O(n).
−
Exercise 11.12. Prove that a binary tree with k leaves has height at least
log k.
Exercise 11.13. Prove that, if we pick a random leaf from a binary tree
with k leaves, then the expected height of this leaf is at least log k.
Exercise 11.14. The implementation of radixSort(a, k) given here works
when the input array, a contains only non-negative integers. Extend this
implementation so that it also works correctly when a contains both neg-
ative and non-negative integers.
245
Chapter 12
Graphs
In this chapter, we study two representations of graphs and basic algo-
rithms that use these representations.
Mathematically, a (directed) graph is a pair G = (V , E) where V is a set
of vertices and E is a set of ordered pairs of vertices called edges. An edge
(i, j) is directed from i to j; i is called the source of the edge and j is
called the target. A path in G is a sequence of vertices v0, . . . , vk such that,
for every i
1, vi) is in E. A path v0, . . . , vk is a cycle
−
if, additionally, the edge (vk, v0) is in E. A path (or cycle) is simple if all
of its vertices are unique. If there is a path from some vertex vi to some
vertex vj then we say that vj is reachable from vi. An example of a graph
is shown in Figure 12.1.
, the edge (vi
1, . . . , k
∈ {
}
Due to their ability to model so many phenomena, graphs have an
enormous number of applications. There are many obvious examples.
Computer networks can be modelled as graphs, with vertices correspond-
ing to computers and edges corresponding to (directed) communication
links between those computers. City streets can be modelled as graphs,
with vertices representing intersections and edges representing streets
joining consecutive intersections.
Less obvious examples occur as soon as we realize that graphs can
model any pairwise relationships within a set. For example, in a uni-
versity setting we might have a timetable conflict graph whose vertices
represent courses offered in the university and in which the edge (i, j) is
present if and only if there is at least one student that is taking both class
i and class j. Thus, an edge indicates that the exam for class i should not
247
§12
Graphs
Figure 12.1: A graph with twelve vertices. Vertices are drawn as numbered circles
and edges are drawn as pointed curves pointing from source to target.
be scheduled at the same time as the exam for class j.
Throughout this section, we will use n to denote the number of ver-
tices of G and m to denote the number of edges of G. That is, n =
and
V
|
m =
. Any other data
}
that we would like to associate with the elements of V can be stored in an
array of length n.
. Furthermore, we will assume that V =
|
0, . . . , n
E
|
−
1
{
|
Some typical operations performed on graphs are:
• addEdge(i, j): Add the edge (i, j) to E.
• removeEdge(i, j): Remove the edge (i, j) from E.
• hasEdge(i, j): Check if the edge (i, j)
E
∈
• outEdges(i): Return a List of all integers j such that (i, j)
E
∈
• inEdges(i): Return a List of all integers j such that (j, i)
E
∈
Note that these operations are not terribly difficult to implement ef-
ficiently. For example, the first three operations can be implemented di-
rectly by using a USet, so they can be implemented in constant expected
time using the hash tables discussed in Chapter 5. The last two opera-
tions can be implemented in constant time by storing, for each vertex, a
list of its adjacent vertices.
248
01237654891011AdjacencyMatrix: Representing a Graph by a Matrix
§12.1
However, different applications of graphs have different performance
requirements for these operations and, ideally, we can use the simplest
implementation that satisfies all the application’s requirements. For this
reason, we discuss two broad categories of graph representations.
12.1 AdjacencyMatrix: Representing a Graph by a Matrix
An adjacency matrix is a way of representing an n vertex graph G = (V , E)
by an n
n matrix, a, whose entries are boolean values.
×
AdjacencyMatrix
int n;
boolean[][] a;
AdjacencyMatrix(int n0) {
n = n0;
a = new boolean[n][n];
}
The matrix entry a[i][j] is defined as
a[i][j] =
∈
if (i, j)
true
E
false otherwise
The adjacency matrix for the graph in Figure 12.1 is shown in Figure 12.2.
In this representation, the operations addEdge(i, j), removeEdge(i, j),
and hasEdge(i, j) just involve setting or reading the matrix entry a[i][j]:
AdjacencyMatrix
void addEdge(int i, int j) {
a[i][j] = true;
}
void removeEdge(int i, int j) {
a[i][j] = false;
}
boolean hasEdge(int i, int j) {
return a[i][j];
}
249
§12.1
Graphs
0
0
1
1
0
1
0
0
0
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
0
2
0
1
0
1
0
1
1
0
0
0
0
0
3
0
0
1
0
0
0
0
1
0
0
0
0
4
1
0
0
0
0
1
0
0
1
0
0
0
5
0
1
0
0
1
0
1
0
0
1
0
0
6
0
1
1
0
0
1
0
1
0
0
1
0
7
0
0
0
1
0
0
1
0
0
0
0
1
8
0
0
0
0
1
0
0
0
0
1
0
0
9
0
0
0
0
0
1
0
0
1
0
1
0
10
0
0
0
0
0
0
1
0
0
1
0
1
11
0
0
0
0
0
0
0
1
0
0
1
0
0
1
2
3
4
5
6
7
8
9
10
11
Figure 12.2: A graph and its adjacency matrix.
250
01237654891011AdjacencyMatrix: Representing a Graph by a Matrix
§12.1
These operations clearly take constant time per operation.
Where the adjacency matrix performs poorly is with the outEdges(i)
and inEdges(i) operations. To implement these, we must scan all n en-
tries in the corresponding row or column of a and gather up all the in-
dices, j, where a[i][j], respectively a[j][i], is true.
List<Integer> outEdges(int i) {
AdjacencyMatrix
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < n; j++)
if (a[i][j]) edges.add(j);
return edges;
}
List<Integer> inEdges(int i) {
List<Integer> edges = new ArrayList<Integer>();
for (int j = 0; j < n; j++)
if (a[j][i]) edges.add(j);
return edges;
}
These operations clearly take O(n) time per operation.
Another drawback of the adjacency matrix representation is that it
n boolean matrix, so it requires at least n2 bits
is large. It stores an n
of memory. The implementation here uses a matrix of boolean values
so it actually uses on the order of n2 bytes of memory. A more careful
implementation, which packs w boolean values into each word of memory,
could reduce this space usage to O(n2/w) words of memory.
×
Theorem 12.1. The AdjacencyMatrix data structure implements the Graph
interface. An AdjacencyMatrix supports the operations
• addEdge(i, j), removeEdge(i, j), and hasEdge(i, j) in constant time
per operation; and
• inEdges(i), and outEdges(i) in O(n) time per operation.
The space used by an AdjacencyMatrix is O(n2).
Despite its high memory requirements and poor performance of the
inEdges(i) and outEdges(i) operations, an AdjacencyMatrix can still be
251
§12.2
Graphs
useful for some applications. In particular, when the graph G is dense, i.e.,
it has close to n2 edges, then a memory usage of n2 may be acceptable.
The AdjacencyMatrix data structure is also commonly used because
algebraic operations on the matrix a can be used to efficiently compute
properties of the graph G. This is a topic for a course on algorithms,
but we point out one such property here: If we treat the entries of a as
integers (1 for true and 0 for false) and multiply a by itself using matrix
multiplication then we get the matrix a2. Recall, from the definition of
matrix multiplication, that
a2[i][j] =
n
1
−
(cid:88)k=0
a[i][k]
·
a[k][j] .
Interpreting this sum in terms of the graph G, this formula counts the
number of vertices, k, such that G contains both edges (i, k) and (k, j).
That is, it counts the number of paths from i to j (through intermediate
vertices, k) whose length is exactly two. This observation is the founda-
tion of an algorithm that computes the shortest paths between all pairs of
vertices in G using only O(log n) matrix multiplications.
12.2 AdjacencyLists: A Graph as a Collection of Lists
Adjacency list representations of graphs take a more vertex-centric ap-
proach. There are many possible implementations of adjacency lists. In
this section, we present a simple one. At the end of the section, we dis-
cuss different possibilities. In an adjacency list representation, the graph
G = (V , E) is represented as an array, adj, of lists. The list adj[i] contains
a list of all the vertices adjacent to vertex i. That is, it contains every
index j such that (i, j)
E.
∈
AdjacencyLists
int n;
List<Integer>[] adj;
AdjacencyLists(int n0) {
n = n0;
adj = (List<Integer>[])new List[n];
for (int i = 0; i < n; i++)
252
AdjacencyLists: A Graph as a Collection of Lists
§12.2
3
2
7
2
1
3
6
4
0
5
8
0
1
4
1
0
2
6
5
6
5
2
7
10
5
1
2
6
9
4
8
4
9
7
6
3
11
9
8
5
10
10
9
6
11
11
10
7
Figure 12.3: A graph and its adjacency lists
adj[i] = new ArrayStack<Integer>();
}
(An example is shown in Figure 12.3.) In this particular implementa-
tion, we represent each list in adj as an ArrayStack, because we would
like constant time access by position. Other options are also possible.
Specifically, we could have implemented adj as a DLList.
The addEdge(i, j) operation just appends the value j to the list adj[i]:
AdjacencyLists
void addEdge(int i, int j) {
adj[i].add(j);
}
This takes constant time.
The removeEdge(i, j) operation searches through the list adj[i] until
it finds j and then removes it:
253
01237654891011§12.2
Graphs
AdjacencyLists
void removeEdge(int i, int j) {
Iterator<Integer> it = adj[i].iterator();
while (it.hasNext()) {
if (it.next() == j) {
it.remove();
return;
}
}
}
This takes O(deg(i)) time, where deg(i) (the degree of i) counts the
number of edges in E that have i as their source.
The hasEdge(i, j) operation is similar; it searches through the list
adj[i] until it finds j (and returns true), or reaches the end of the list
(and returns false):
AdjacencyLists
boolean hasEdge(int i, int j) {
return adj[i].contains(j);
}
This also takes O(deg(i)) time.
The outEdges(i) operation is very simple; it returns the list adj[i] :
List<Integer> outEdges(int i) {
AdjacencyLists
return adj[i];
}
This clearly takes constant time.
The inEdges(i) operation is much more work.
It scans over every
vertex j checking if the edge (i, j) exists and, if so, adding j to the output
list:
List<Integer> inEdges(int i) {
List<Integer> edges = new ArrayStack<Integer>();
AdjacencyLists
254
AdjacencyLists: A Graph as a Collection of Lists
§12.2
for (int j = 0; j < n; j++)
if (adj[j].contains(i))
edges.add(j);
return edges;
}
This operation is very slow. It scans the adjacency list of every vertex,
so it takes O(n + m) time.
The following theorem summarizes the performance of the above data
structure:
Theorem 12.2. The AdjacencyLists data structure implements the Graph
interface. An AdjacencyLists supports the operations
• addEdge(i, j) in constant time per operation;
• removeEdge(i, j) and hasEdge(i, j) in O(deg(i)) time per operation;
• outEdges(i) in constant time per operation; and
• inEdges(i) in O(n + m) time per operation.
The space used by a AdjacencyLists is O(n + m).
As alluded to earlier, there are many different choices to be made
when implementing a graph as an adjacency list. Some questions that
come up include:
• What type of collection should be used to store each element of adj?
One could use an array-based list, a linked-list, or even a hashtable.
• Should there be a second adjacency list, inadj, that stores, for each
i, the list of vertices, j, such that (j, i)
E? This can greatly reduce
the running-time of the inEdges(i) operation, but requires slightly
more work when adding or removing edges.
∈
• Should the entry for the edge (i, j) in adj[i] be linked by a reference
to the corresponding entry in inadj[j]?
• Should edges be first-class objects with their own associated data?
In this way, adj would contain lists of edges rather than lists of
vertices (integers).
255
§12.3
Graphs
Most of these questions come down to a tradeoff between complexity (and
space) of implementation and performance features of the implementa-
tion.
12.3 Graph Traversal
In this section we present two algorithms for exploring a graph, starting
at one of its vertices, i, and finding all vertices that are reachable from
i. Both of these algorithms are best suited to graphs represented using
an adjacency list representation. Therefore, when analyzing these algo-
rithms we will assume that the underlying representation is an Adjacen-
cyLists.
12.3.1 Breadth-First Search
The bread-first-search algorithm starts at a vertex i and visits, first the
neighbours of i, then the neighbours of the neighbours of i, then the
neighbours of the neighbours of the neighbours of i, and so on.
This algorithm is a generalization of the breadth-first traversal algo-
rithm for binary trees (Section 6.1.2), and is very similar; it uses a queue,
q, that initially contains only i. It then repeatedly extracts an element
from q and adds its neighbours to q, provided that these neighbours have
never been in q before. The only major difference between the breadth-
first-search algorithm for graphs and the one for trees is that the algo-
rithm for graphs has to ensure that it does not add the same vertex to q
more than once. It does this by using an auxiliary boolean array, seen,
that tracks which vertices have already been discovered.
void bfs(Graph g, int r) {
Algorithms
boolean[] seen = new boolean[g.nVertices()];
Queue<Integer> q = new SLList<Integer>();
q.add(r);
seen[r] = true;
while (!q.isEmpty()) {
int i = q.remove();
for (Integer j : g.outEdges(i)) {
256
Graph Traversal
§12.3
Figure 12.4: An example of bread-first-search starting at node 0. Nodes are la-
belled with the order in which they are added to q. Edges that result in nodes
being added to q are drawn in black, other edges are drawn in grey.
if (!seen[j]) {
q.add(j);
seen[j] = true;
}
}
}
}
An example of running bfs(g, 0) on the graph from Figure 12.1 is
shown in Figure 12.4. Different executions are possible, depending on
the ordering of the adjacency lists; Figure 12.4 uses the adjacency lists in
Figure 12.3.
Analyzing the running-time of the bfs(g, i) routine is fairly straight-
forward. The use of the seen array ensures that no vertex is added to q
more than once. Adding (and later removing) each vertex from q takes
constant time per vertex for a total of O(n) time. Since each vertex is pro-
cessed by the inner loop at most once, each adjacency list is processed at
most once, so each edge of G is processed at most once. This processing,
which is done in the inner loop takes constant time per iteration, for a
total of O(m) time. Therefore, the entire algorithm runs in O(n + m) time.
The following theorem summarizes the performance of the bfs(g, r)
algorithm.
257
01378452610911§12.3
Graphs
Theorem 12.3. When given as input a Graph, g, that is implemented using
the AdjacencyLists data structure, the bfs(g, r) algorithm runs in O(n + m)
time.
A breadth-first traversal has some very special properties. Calling
bfs(g, r) will eventually enqueue (and eventually dequeue) every vertex
j such that there is a directed path from r to j. Moreover, the vertices at
distance 0 from r (r itself) will enter q before the vertices at distance 1,
which will enter q before the vertices at distance 2, and so on. Thus, the
bfs(g, r) method visits vertices in increasing order of distance from r and
vertices that cannot be reached from r are never visited at all.
A particularly useful application of the breadth-first-search algorithm
is, therefore, in computing shortest paths. To compute the shortest path
from r to every other vertex, we use a variant of bfs(g, r) that uses an
auxilliary array, p, of length n. When a new vertex j is added to q, we set
p[j] = i. In this way, p[j] becomes the second last node on a shortest path
from r to j. Repeating this, by taking p[p[j], p[p[p[j]]], and so on we can
reconstruct the (reversal of) a shortest path from r to j.
12.3.2 Depth-First Search
The depth-first-search algorithm is similar to the standard algorithm for
traversing binary trees; it first fully explores one subtree before returning
to the current node and then exploring the other subtree. Another way to
think of depth-first-search is by saying that it is similar to breadth-first
search except that it uses a stack instead of a queue.
During the execution of the depth-first-search algorithm, each vertex,
i, is assigned a colour, c[i]: white if we have never seen the vertex before,
grey if we are currently visiting that vertex, and black if we are done
visiting that vertex. The easiest way to think of depth-first-search is as a
recursive algorithm. It starts by visiting r. When visiting a vertex i, we
first mark i as grey. Next, we scan i’s adjacency list and recursively visit
any white vertex we find in this list. Finally, we are done processing i, so
we colour i black and return.
void dfs(Graph g, int r) {
Algorithms
258
Graph Traversal
§12.3
Figure 12.5: An example of depth-first-search starting at node 0. Nodes are la-
belled with the order in which they are processed. Edges that result in a recursive
call are drawn in black, other edges are drawn in grey.
byte[] c = new byte[g.nVertices()];
dfs(g, r, c);
}
void dfs(Graph g, int i, byte[] c) {
c[i] = grey; // currently visiting i
for (Integer j : g.outEdges(i)) {
if (c[j] == white) {
c[j] = grey;
dfs(g, j, c);
}
}
c[i] = black; // done visiting i
}
An example of the execution of this algorithm is shown in Figure 12.5.
Although depth-first-search may best be thought of as a recursive al-
gorithm, recursion is not the best way to implement it. Indeed, the code
given above will fail for many large graphs by causing a stack overflow.
An alternative implementation is to replace the recursion stack with an
explicit stack, s. The following implementation does just that:
void dfs2(Graph g, int r) {
Algorithms
259
01234111098765§12.3
Graphs
byte[] c = new byte[g.nVertices()];
Stack<Integer> s = new Stack<Integer>();
s.push(r);
while (!s.isEmpty()) {
int i = s.pop();
if (c[i] == white) {
c[i] = grey;
for (int j : g.outEdges(i))
s.push(j);
}
}
}
In the preceding code, when the next vertex, i, is processed, i is
coloured grey and then replaced, on the stack, with its adjacent vertices.
During the next iteration, one of these vertices will be visited.
Not surprisingly, the running times of dfs(g, r) and dfs2(g, r) are the
same as that of bfs(g, r):
Theorem 12.4. When given as input a Graph, g, that is implemented using
the AdjacencyLists data structure, the dfs(g, r) and dfs2(g, r) algorithms
each run in O(n + m) time.
As with the breadth-first-search algorithm, there is an underlying tree
associated with each execution of depth-first-search. When a node i (cid:44) r
goes from white to grey, this is because dfs(g, i, c) was called recursively
while processing some node i(cid:48). (In the case of dfs2(g, r) algorithm, i is
one of the nodes that replaced i(cid:48) on the stack.) If we think of i(cid:48) as the
parent of i, then we obtain a tree rooted at r. In Figure 12.5, this tree is a
path from vertex 0 to vertex 11.
An important property of the depth-first-search algorithm is the fol-
lowing: Suppose that when node i is coloured grey, there exists a path
from i to some other node j that uses only white vertices. Then j will be
coloured first grey then black before i is coloured black. (This can be
proven by contradiction, by considering any path P from i to j.)
One application of this property is the detection of cycles. Refer to
Figure 12.6. Consider some cycle, C, that can be reached from r. Let
i be the first node of C that is coloured grey, and let j be the node that
260
Discussion and Exercises
§12.4
Figure 12.6: The depth-first-search algorithm can be used to detect cycles in G.
The node j is coloured grey while i is still grey. This implies that there is a path,
P , from i to j in the depth-first-search tree, and the edge (j, i) implies that P is
also a cycle.
precedes i on the cycle C. Then, by the above property, j will be coloured
grey and the edge (j, i) will be considered by the algorithm while i is still
grey. Thus, the algorithm can conclude that there is a path, P , from i to
j in the depth-first-search tree and the edge (j, i) exists. Therefore, P is
also a cycle.
12.4 Discussion and Exercises
The running times of the depth-first-search and breadth-first-search al-
gorithms are somewhat overstated by the Theorems 12.3 and 12.4. De-
fine nr as the number of vertices, i, of G, for which there exists a path
from r to i. Define mr as the number of edges that have these vertices
as their sources. Then the following theorem is a more precise statement
of the running times of the breadth-first-search and depth-first-search al-
gorithms. (This more refined statement of the running time is useful in
some of the applications of these algorithms outlined in the exercises.)
Theorem 12.5. When given as input a Graph, g, that is implemented using
the AdjacencyLists data structure, the bfs(g, r), dfs(g, r) and dfs2(g, r)
algorithms each run in O(nr + mr) time.
Breadth-first search seems to have been discovered independently by
Moore [52] and Lee [49] in the contexts of maze exploration and circuit
routing, respectively.
Adjacency-list representations of graphs were presented by Hopcroft
and Tarjan [40] as an alternative to the (then more common) adjacency-
261
ijCP§12.4
Graphs
Figure 12.7: An example graph.
matrix representation. This representation, as well as depth-first-search,
played a major part in the celebrated Hopcroft-Tarjan planarity testing
algorithm that can determine, in O(n) time, if a graph can be drawn, in
the plane, and in such a way that no pair of edges cross each other [41].
In the following exercises, an undirected graph is one in which, for
every i and j, the edge (i, j) is present if and only if the edge (j, i) is
present.
Exercise 12.1. Draw an adjacency list representation and an adjacency
matrix representation of the graph in Figure 12.7.
Exercise 12.2. The incidence matrix representation of a graph, G, is an
n
m matrix, A, where
×
Ai,j =
1 if vertex i the source of edge j
−
+1 if vertex i the target of edge j
0
otherwise.
1. Draw the incident matrix representation of the graph in Figure 12.7.
2. Design, analyze and implement an incidence matrix representation
of a graph. Be sure to analyze the space, the cost of addEdge(i, j),
removeEdge(i, j), hasEdge(i, j), inEdges(i), and outEdges(i).
Exercise 12.3. Illustrate an execution of the bfs(G, 0) and dfs(G, 0) on the
graph, G, in Figure 12.7.
262
6150947283Discussion and Exercises
§12.4
Exercise 12.4. Let G be an undirected graph. We say G is connected if, for
every pair of vertices i and j in G, there is a path from i to j (since G
is undirected, there is also a path from j to i). Show how to test if G is
connected in O(n + m) time.
Exercise 12.5. Let G be an undirected graph. A connected-component la-
belling of G partitions the vertices of G into maximal sets, each of which
forms a connected subgraph. Show how to compute a connected compo-
nent labelling of G in O(n + m) time.
Exercise 12.6. Let G be an undirected graph. A spanning forest of G is a
collection of trees, one per component, whose edges are edges of G and
whose vertices contain all vertices of G. Show how to compute a spanning
forest of of G in O(n + m) time.
Exercise 12.7. We say that a graph G is strongly-connected if, for every
pair of vertices i and j in G, there is a path from i to j. Show how to test
if G is strongly-connected in O(n + m) time.
Exercise 12.8. Given a graph G = (V , E) and some special vertex r
V ,
show how to compute the length of the shortest path from r to i for every
vertex i
∈
V .
∈
Exercise 12.9. Give a (simple) example where the dfs(g, r) code visits the
nodes of a graph in an order that is different from that of the dfs2(g, r)
code. Write a version of dfs2(g, r) that always visits nodes in exactly the
same order as dfs(g, r). (Hint: Just start tracing the execution of each
algorithm on some graph where r is the source of more than 1 edge.)
Exercise 12.10. A universal sink in a graph G is a vertex that is the target
1 edges and the source of no edges.1 Design and implement an
of n
algorithm that tests if a graph G, represented as an AdjacencyMatrix,
has a universal sink. Your algorithm should run in O(n) time.
−
1A universal sink, v, is also sometimes called a celebrity: Everyone in the room recognizes
v, but v doesn’t recognize anyone else in the room.
263
Chapter 13
Data Structures for Integers
In this chapter, we return to the problem of implementing an SSet. The
difference now is that we assume the elements stored in the SSet are w-bit
integers. That is, we want to implement add(x), remove(x), and find(x)
where x
. It is not too hard to think of plenty of applications
}
where the data—or at least the key that we use for sorting the data—is an
integer.
0, . . . , 2w
∈ {
−
1
We will discuss three data structures, each building on the ideas of
the previous. The first structure, the BinaryTrie performs all three SSet
operations in O(w) time. This is not very impressive, since any subset of
0, . . . , 2w
w. All the other SSet imple-
{
mentations discussed in this book perform all operations in O(log n) time
so they are all at least as fast as a BinaryTrie.
2w, so that log n
has size n
≤
≤
−
1
}
The second structure, the XFastTrie, speeds up the search in a Bina-
ryTrie by using hashing. With this speedup, the find(x) operation runs
in O(log w) time. However, add(x) and remove(x) operations in an XFast-
w).
Trie still take O(w) time and the space used by an XFastTrie is O(n
The third data structure, the YFastTrie, uses an XFastTrie to store
only a sample of roughly one out of every w elements and stores the re-
maining elements in a standard SSet structure. This trick reduces the
running time of add(x) and remove(x) to O(log w) and decreases the space
to O(n).
·
The implementations used as examples in this chapter can store any
type of data, as long as an integer can be associated with it. In the code
samples, the variable ix is always the integer value associated with x, and
265
§13.1
Data Structures for Integers
Figure 13.1: The integers stored in a binary trie are encoded as root-to-leaf paths.
the method in.intValue(x) converts x to its associated integer. In the text,
however, we will simply treat x as if it is an integer.
13.1 BinaryTrie: A digital search tree
A BinaryTrie encodes a set of w bit integers in a binary tree. All leaves in
the tree have depth w and each integer is encoded as a root-to-leaf path.
The path for the integer x turns left at level i if the ith most significant
bit of x is a 0 and turns right if it is a 1. Figure 13.1 shows an example
for the case w = 4, in which the trie stores the integers 3(0011), 9(1001),
12(1100), and 13(1101).
Because the search path for a value x depends on the bits of x, it
will be helpful to name the children of a node, u, u.child[0] (left) and
u.child[1] (right). These child pointers will actually serve double-duty.
Since the leaves in a binary trie have no children, the pointers are used to
string the leaves together into a doubly-linked list. For a leaf in the bi-
nary trie u.child[0] (prev) is the node that comes before u in the list and
u.child[1] (next) is the node that follows u in the list. A special node,
dummy, is used both before the first node and after the last node in the list
(see Section 3.2).
Each node, u, also contains an additional pointer u.jump. If u’s left
266
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?BinaryTrie: A digital search tree
§13.1
Figure 13.2: A BinaryTrie with jump pointers shown as curved dashed edges.
child is missing, then u.jump points to the smallest leaf in u’s subtree.
If u’s right child is missing, then u.jump points to the largest leaf in u’s
subtree. An example of a BinaryTrie, showing jump pointers and the
doubly-linked list at the leaves, is shown in Figure 13.2.
The find(x) operation in a BinaryTrie is fairly straightforward. We
try to follow the search path for x in the trie. If we reach a leaf, then we
have found x. If we reach a node u where we cannot proceed (because
u is missing a child), then we follow u.jump, which takes us either to the
smallest leaf larger than x or the largest leaf smaller than x. Which of
these two cases occurs depends on whether u is missing its left or right
child, respectively. In the former case (u is missing its left child), we have
found the node we want. In the latter case (u is missing its right child),
we can use the linked list to reach the node we want. Each of these cases
is illustrated in Figure 13.3.
BinaryTrie
T find(T x) {
int i, c = 0, ix = it.intValue(x);
Node u = r;
for (i = 0; i < w; i++) {
c = (ix >>> w-i-1) & 1;
if (u.child[c] == null) break;
u = u.child[c];
}
267
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?§13.1
Data Structures for Integers
Figure 13.3: The paths followed by find(5) and find(8).
if (i == w) return u.x;
u = (c == 0) ? u.jump : u.jump.child[next];
return u == dummy ? null : u.x;
// found it
}
The running-time of the find(x) method is dominated by the time it
takes to follow a root-to-leaf path, so it runs in O(w) time.
The add(x) operation in a BinaryTrie is also fairly straightforward,
but has a lot of work to do:
1. It follows the search path for x until reaching a node u where it can
no longer proceed.
2. It creates the remainder of the search path from u to a leaf that
contains x.
3. It adds the node, u(cid:48), containing x to the linked list of leaves (it has
access to the predecessor, pred, of u(cid:48) in the linked list from the jump
pointer of the last node, u, encountered during step 1.)
4. It walks back up the search path for x adjusting jump pointers at the
nodes whose jump pointer should now point to x.
An addition is illustrated in Figure 13.4.
268
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?find(5)find(8)BinaryTrie: A digital search tree
§13.1
Figure 13.4: Adding the values 2 and 15 to the BinaryTrie in Figure 13.2.
boolean add(T x) {
BinaryTrie
int i, c = 0, ix = it.intValue(x);
Node u = r;
// 1 - search for ix until falling out of the trie
for (i = 0; i < w; i++) {
c = (ix >>> w-i-1) & 1;
if (u.child[c] == null) break;
u = u.child[c];
}
if (i == w) return false; // already contains x - abort
Node pred = (c == right) ? u.jump : u.jump.child[0];
u.jump = null;
// 2 - add path to ix
for (; i < w; i++) {
// u will have two children shortly
c = (ix >>> w-i-1) & 1;
u.child[c] = newNode();
u.child[c].parent = u;
u = u.child[c];
}
u.x = x;
// 3 - add u to linked list
u.child[prev] = pred;
u.child[next] = pred.child[next];
269
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?§13.1
Data Structures for Integers
u.child[prev].child[next] = u;
u.child[next].child[prev] = u;
// 4 - walk back up, updating jump pointers
Node v = u.parent;
while (v != null) {
if ((v.child[left] == null
&& (v.jump == null || it.intValue(v.jump.x) > ix))
|| (v.child[right] == null
&& (v.jump == null || it.intValue(v.jump.x) < ix)))
v.jump = u;
v = v.parent;
}
n++;
return true;
}
This method performs one walk down the search path for x and one
walk back up. Each step of these walks takes constant time, so the add(x)
method runs in O(w) time.
The remove(x) operation undoes the work of add(x). Like add(x), it
has a lot of work to do:
1. It follows the search path for x until reaching the leaf, u, containing
x.
2. It removes u from the doubly-linked list.
3. It deletes u and then walks back up the search path for x deleting
nodes until reaching a node v that has a child that is not on the
search path for x.
4. It walks upwards from v to the root updating any jump pointers that
point to u.
A removal is illustrated in Figure 13.5.
BinaryTrie
boolean remove(T x) {
// 1 - find leaf, u, containing x
int i, c, ix = it.intValue(x);
Node u = r;
270
BinaryTrie: A digital search tree
§13.1
Figure 13.5: Removing the value 9 from the BinaryTrie in Figure 13.2.
for (i = 0; i < w; i++) {
c = (ix >>> w-i-1) & 1;
if (u.child[c] == null) return false;
u = u.child[c];
}
// 2 - remove u from linked list
u.child[prev].child[next] = u.child[next];
u.child[next].child[prev] = u.child[prev];
Node v = u;
// 3 - delete nodes on path to u
for (i = w-1; i >= 0; i--) {
c = (ix >>> w-i-1) & 1;
v = v.parent;
v.child[c] = null;
if (v.child[1-c] != null) break;
}
// 4 - update jump pointers
c = (ix >>> w-i-1) & 1;
v.jump = u.child[1-c];
v = v.parent;
i--;
for (; i >= 0; i--) {
c = (ix >>> w-i-1) & 1;
if (v.jump == u)
v.jump = u.child[1-c];
271
1001234567811121314150???1???????00??01??11??000?001?010?011?101?110?111?10??9100?§13.2
Data Structures for Integers
v = v.parent;
}
n--;
return true;
}
Theorem 13.1. A BinaryTrie implements the SSet interface for w-bit inte-
gers. A BinaryTrie supports the operations add(x), remove(x), and find(x)
in O(w) time per operation. The space used by a BinaryTrie that stores n
values is O(n
w).
·
13.2 XFastTrie: Searching in Doubly-Logarithmic Time
The performance of the BinaryTrie structure is not very impressive. The
number of elements, n, stored in the structure is at most 2w, so log n
w.
In other words, any of the comparison-based SSet structures described
in other parts of this book are at least as efficient as a BinaryTrie, and
are not restricted to only storing integers.
≤
Next we describe the XFastTrie, which is just a BinaryTrie with w + 1
hash tables—one for each level of the trie. These hash tables are used to
speed up the find(x) operation to O(log w) time. Recall that the find(x)
operation in a BinaryTrie is almost complete once we reach a node, u,
where the search path for x would like to proceed to u.right (or u.left)
but u has no right (respectively, left) child. At this point, the search uses
u.jump to jump to a leaf, v, of the BinaryTrie and either return v or its
successor in the linked list of leaves. An XFastTrie speeds up the search
process by using binary search on the levels of the trie to locate the node
u.
To use binary search, we need a way to determine if the node u we
are looking for is above a particular level, i, of if u is at or below level
i. This information is given by the highest-order i bits in the binary
representation of x; these bits determine the search path that x takes from
the root to level i. For an example, refer to Figure 13.6; in this figure the
last node, u, on search path for 14 (whose binary representation is 1110)
is the node labelled 11(cid:63)(cid:63) at level 2 because there is no node labelled 111(cid:63)
at level 3. Thus, we can label each node at level i with an i-bit integer.
272
XFastTrie: Searching in Doubly-Logarithmic Time
§13.2
Figure 13.6: Since there is no node labelled 111(cid:63), the search path for 14 (1110)
ends at the node labelled 11(cid:63)(cid:63) .
Then, the node u we are searching for would be at or below level i if and
only if there is a node at level i whose label matches the highest-order i
bits of x.
In an XFastTrie, we store, for each i
, all the nodes at level
i in a USet, t[i], that is implemented as a hash table (Chapter 5). Using
this USet allows us to check in constant expected time if there is a node
at level i whose label matches the highest-order i bits of x. In fact, we
can even find this node using t[i].find(x>>>(w
0, . . . , w
}
i))
∈ {
The hash tables t[0], . . . , t[w] allow us to use binary search to find u.
Initially, we know that u is at some level i with 0
i < w + 1. We therefore
initialize l = 0 and h = w + 1 and repeatedly look at the hash table t[i],
where i =
If t[i] contains a node whose label matches x’s
highest-order i bits then we set l = i (u is at or below level i); otherwise
we set h = i (u is above level i). This process terminates when h
1,
in which case we determine that u is at level l. We then complete the
find(x) operation using u.jump and the doubly-linked list of leaves.
(l + h)/2
(cid:98)
.
(cid:99)
≤
≤
−
l
−
XFastTrie
T find(T x) {
int l = 0, h = w+1, ix = it.intValue(x);
Node v, u = r, q = newNode();
while (h-l > 1) {
273
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?01234111§13.2
Data Structures for Integers
int i = (l+h)/2;
q.prefix = ix >>> w-i;
if ((v = t[i].find(q)) == null) {
h = i;
} else {
u = v;
l = i;
}
}
if (l == w) return u.x;
Node pred = (((ix >>> w-l-1) & 1) == 1)
? u.jump : u.jump.child[0];
return (pred.child[next] == dummy)
? null : pred.child[next].x;
}
Each iteration of the while loop in the above method decreases h
l
by roughly a factor of two, so this loop finds u after O(log w) iterations.
Each iteration performs a constant amount of work and one find(x) op-
eration in a USet, which takes a constant expected amount of time. The
remaining work takes only constant time, so the find(x) method in an
XFastTrie takes only O(log w) expected time.
−
The add(x) and remove(x) methods for an XFastTrie are almost iden-
tical to the same methods in a BinaryTrie. The only modifications are
for managing the hash tables t[0],. . . ,t[w]. During the add(x) operation,
when a new node is created at level i, this node is added to t[i]. During
a remove(x) operation, when a node is removed form level i, this node
is removed from t[i]. Since adding and removing from a hash table take
constant expected time, this does not increase the running times of add(x)
and remove(x) by more than a constant factor. We omit a code listing for
add(x) and remove(x) since the code is almost identical to the (long) code
listing already provided for the same methods in a BinaryTrie.
The following theorem summarizes the performance of an XFastTrie:
Theorem 13.2. An XFastTrie implements the SSet interface for w-bit inte-
gers. An XFastTrie supports the operations
• add(x) and remove(x) in O(w) expected time per operation and
• find(x) in O(log w) expected time per operation.
274
YFastTrie: A Doubly-Logarithmic Time SSet
§13.3
The space used by an XFastTrie that stores n values is O(n
w).
·
13.3 YFastTrie: A Doubly-Logarithmic Time SSet
The XFastTrie is a vast—even exponential—improvement over the Bi-
naryTrie in terms of query time, but the add(x) and remove(x) operations
are still not terribly fast. Furthermore, the space usage, O(n
w), is higher
than the other SSet implementations described in this book, which all
use O(n) space. These two problems are related; if n add(x) operations
build a structure of size n
w, then the add(x) operation requires at least
on the order of w time (and space) per operation.
·
·
The YFastTrie, discussed next, simultaneously improves the space
and speed of XFastTries. A YFastTrie uses an XFastTrie, xft, but only
stores O(n/w) values in xft. In this way, the total space used by xft is only
O(n). Furthermore, only one out of every w add(x) or remove(x) operations
in the YFastTrie results in an add(x) or remove(x) operation in xft. By
doing this, the average cost incurred by calls to xft’s add(x) and remove(x)
operations is only constant.
−
The obvious question becomes: If xft only stores n/w elements, where
1/w) elements go? These elements move into sec-
do the remaining n(1
ondary structures, in this case an extended version of treaps (Section 7.2).
There are roughly n/w of these secondary structures so, on average, each
of them stores O(w) items. Treaps support logarithmic time SSet opera-
tions, so the operations on these treaps will run in O(log w) time, as re-
quired.
More concretely, a YFastTrie contains an XFastTrie, xft, that con-
tains a random sample of the data, where each element appears in the
sample independently with probability 1/w. For convenience, the value
2w
1 denote the
−
elements stored in xft. Associated with each element, xi, is a treap, ti,
that stores all values in the range xi
1 + 1, . . . , xi. This is illustrated in
−
Figure 13.7.
1, is always contained in xft. Let x0 < x1 <
< xk
· · ·
−
The find(x) operation in a YFastTrie is fairly easy. We search for x in
xft and find some value xi associated with the treap ti. We then use the
treap find(x) method on ti to answer the query. The entire method is a
275
§13.3
Data Structures for Integers
Figure 13.7: A YFastTrie containing the values 0, 1, 3, 4, 6, 8, 9, 10, 11, and 13.
one-liner:
T find(T x) {
YFastTrie
return xft.find(new Pair<T>(it.intValue(x))).t.find(x);
}
The first find(x) operation (on xft) takes O(log w) time. The second
find(x) operation (on a treap) takes O(log r) time, where r is the size of
the treap. Later in this section, we will show that the expected size of the
treap is O(w) so that this operation takes O(log w) time.1
Adding an element to a YFastTrie is also fairly simple—most of the
time. The add(x) method calls xft.find(x) to locate the treap, t, into
which x should be inserted. It then calls t.add(x) to add x to t. At this
point, it tosses a biased coin that comes up as heads with probability 1/w
1/w. If this coin comes up heads, then x
and as tails with probability 1
will be added to xft.
−
1This is an application of Jensen’s Inequality: If E[r] = w, then E[log r]
log w.
≤
276
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?0,1,34,5,8,910,11,13YFastTrie: A Doubly-Logarithmic Time SSet
§13.3
This is where things get a little more complicated. When x is added
to xft, the treap t needs to be split into two treaps, t1 and t(cid:48). The treap
t1 contains all the values less than or equal to x; t(cid:48) is the original treap,
t, with the elements of t1 removed. Once this is done, we add the pair
(x, t1) to xft. Figure 13.8 shows an example.
YFastTrie
boolean add(T x) {
int ix = it.intValue(x);
STreap<T> t = xft.find(new Pair<T>(ix)).t;
if (t.add(x)) {
n++;
if (rand.nextInt(w) == 0) {
STreap<T> t1 = t.split(x);
xft.add(new Pair<T>(ix, t1));
}
return true;
}
return false;
}
Adding x to t takes O(log w) time. Exercise 7.12 shows that splitting
t into t1 and t(cid:48) can also be done in O(log w) expected time. Adding the
pair (x,t1) to xft takes O(w) time, but only happens with probability 1/w.
Therefore, the expected running time of the add(x) operation is
O(log w) +
1
w
O(w) = O(log w) .
The remove(x) method undoes the work performed by add(x). We use
xft to find the leaf, u, in xft that contains the answer to xft.find(x).
From u, we get the treap, t, containing x and remove x from t. If x was
also stored in xft (and x is not equal to 2w
1) then we remove x from xft
and add the elements from x’s treap to the treap, t2, that is stored by u’s
successor in the linked list. This is illustrated in Figure 13.9.
−
YFastTrie
boolean remove(T x) {
int ix = it.intValue(x);
Node<T> u = xft.findNode(ix);
277
§13.3
Data Structures for Integers
Figure 13.8: Adding the values 2 and 6 to a YFastTrie. The coin toss for 6 came
up heads, so 6 was added to xft and the treap containing 4, 5, 6, 8, 9 was split.
boolean ret = u.x.t.remove(x);
if (ret) n--;
if (u.x.x == ix && ix != 0xffffffff) {
STreap<T> t2 = u.child[1].x.t;
t2.absorb(u.x.t);
xft.remove(u.x);
}
return ret;
}
Finding the node u in xft takes O(log w) expected time. Removing
x from t takes O(log w) expected time. Again, Exercise 7.12 shows that
merging all the elements of t into t2 can be done in O(log w) time. If
necessary, removing x from xft takes O(w) time, but x is only contained
in xft with probability 1/w. Therefore, the expected time to remove an
element from a YFastTrie is O(log w).
Earlier in the discussion, we delayed arguing about the sizes of treaps
in this structure until later. Before finishing this chapter, we prove the
result we need.
278
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?0,1,2,34,5,8,910,11,134,5,68,9YFastTrie: A Doubly-Logarithmic Time SSet
§13.3
Figure 13.9: Removing the values 1 and 9 from a YFastTrie in Figure 13.8.
Lemma 13.1. Let x be an integer stored in a YFastTrie and let nx denote the
number of elements in the treap, t, that contains x. Then E[nx]
2w
1.
≤
−
· · ·
· · ·
< xi = x < xi+1 <
Proof. Refer to Figure 13.10. Let x1 < x2 <
< xn
denote the elements stored in the YFastTrie. The treap t contains some
elements greater than or equal to x. These are xi, xi+1, . . . , xi+j
1, where
−
xi+j
1 is the only one of these elements in which the biased coin toss per-
−
formed in the add(x) method turned up as heads. In other words, E[j] is
equal to the expected number of biased coin tosses required to obtain the
first heads.2 Each coin toss is independent and turns up as heads with
probability 1/w, so E[j]
w. (See Lemma 4.2 for an analysis of this for the
case w = 2.)
≤
Similarly, the elements of t smaller than x are xi
k where all
−
these k coin tosses turn up as tails and the coin toss for xi
1 turns up as
−
heads. Therefore, E[k]
1, since this is the same coin tossing exper-
iment considered in the preceding paragraph, but one in which the last
1, . . . , xi
k
−
≤
−
w
−
2This analysis ignores the fact that j never exceeds n
E[j], so the upper bound still holds.
i + 1. However, this only decreases
−
279
01234567891011121314150???1???????00??01??10??11??000?001?010?011?100?101?110?111?0,1,2,38,10,11,134,5,68,9§13.4
Data Structures for Integers
Figure 13.10: The number of elements in the treap t containing x is determined
by two coin tossing experiments.
toss is not counted. In summary, nx = j + k, so
E[nx] = E[j + k] = E[j] + E[k]
2w
−
≤
1 .
Lemma 13.1 was the last piece in the proof of the following theorem,
which summarizes the performance of the YFastTrie:
Theorem 13.3. A YFastTrie implements the SSet interface for w-bit inte-
gers. A YFastTrie supports the operations add(x), remove(x), and find(x)
in O(log w) expected time per operation. The space used by a YFastTrie that
stores n values is O(n + w).
The w term in the space requirement comes from the fact that xft al-
ways stores the value 2w
1. The implementation could be modified (at
the expense of adding some extra cases to the code) so that it is unneces-
sary to store this value. In this case, the space requirement in the theorem
becomes O(n).
−
13.4 Discussion and Exercises
The first data structure to provide O(log w) time add(x), remove(x), and
find(x) operations was proposed by van Emde Boas and has since be-
come known as the van Emde Boas (or stratified) tree [74]. The original
van Emde Boas structure had size 2w, making it impractical for large in-
tegers.
The XFastTrie and YFastTrie data structures were discovered by
Willard [77]. The XFastTrie structure is closely related to van Emde Boas
trees; for instance, the hash tables in an XFastTrie replace arrays in a
280
xi=xxi+1xi+2xi+j−2xi+j−1...TTTTHxi−1Txi−2Txi−k+1Txi−kTxi−k−1H.........z }| {elementsintreap,t,containingx| {z }| {z }kjDiscussion and Exercises
§13.4
van Emde Boas tree. That is, instead of storing the hash table t[i], a
van Emde Boas tree stores an array of length 2i.
Another structure for storing integers is Fredman and Willard’s fusion
trees [32]. This structure can store n w-bit integers in O(n) space so that
the find(x) operation runs in O((log n)/(log w)) time. By using a fusion tree
when log w >
log n, one obtains an
O(n) space data structure that can implement the find(x) operation in
log n) time. Recent lower-bound results of Pˇatras¸cu and Thorup [59]
O(
show that these results are more or less optimal, at least for structures
that use only O(n) space.
log n and a YFastTrie when log w
(cid:112)
(cid:112)
(cid:112)
≤
Exercise 13.1. Design and implement a simplified version of a Binary-
Trie that does not have a linked list or jump pointers, but for which
find(x)
still runs in O(w) time.
Exercise 13.2. Design and implement a simplified implementation of an
XFastTrie that doesn’t use a binary trie at all. Instead, your implementa-
tion should store everything in a doubly-linked list and w + 1 hash tables.
Exercise 13.3. We can think of a BinaryTrie as a structure that stores
bit strings of length w in such a way that each bitstring is represented as a
root to leaf path. Extend this idea into an SSet implementation that stores
variable-length strings and implements add(s), remove(s), and find(s) in
time proporitional to the length of s.
Hint: Each node in your data structure should store a hash table that is
indexed by character values.
0, . . . 2w
Exercise 13.4. For an integer x
, let d(x) denote the difference
between x and the value returned by find(x) [if find(x) returns null,
then define d(x) as 2w]. For example, if find(23) returns 43, then d(23) =
20.
1
}
∈ {
−
1. Design and implement a modified version of the find(x) operation
in an XFastTrie that runs in O(1 + log d(x)) expected time. Hint:
The hash table t[w] contains all the values, x, such that d(x) = 0, so
that would be a good place to start.
2. Design and implement a modified version of the find(x) operation
in an XFastTrie that runs in O(1 + log log d(x)) expected time.
281
Chapter 14
External Memory Searching
Throughout this book, we have been using the w-bit word-RAM model of
computation defined in Section 1.4. An implicit assumption of this model
is that our computer has a large enough random access memory to store
all of the data in the data structure. In some situations, this assumption
is not valid. There exist collections of data so large that no computer has
enough memory to store them. In such cases, the application must resort
to storing the data on some external storage medium such as a hard disk,
a solid state disk, or even a network file server (which has its own external
storage).
Accessing an item from external storage is extremely slow. The hard
disk attached to the computer on which this book was written has an aver-
age access time of 19ms and the solid state drive attached to the computer
has an average access time of 0.3ms. In contrast, the random access mem-
ory in the computer has an average access time of less than 0.000113ms.
Accessing RAM is more than 2 500 times faster than accessing the solid
state drive and more than 160 000 times faster than accessing the hard
drive.
These speeds are fairly typical; accessing a random byte from RAM is
thousands of times faster than accessing a random byte from a hard disk
or solid-state drive. Access time, however, does not tell the whole story.
When we access a byte from a hard disk or solid state disk, an entire block
of the disk is read. Each of the drives attached to the computer has a
block size of 4 096; each time we read one byte, the drive gives us a block
containing 4 096 bytes. If we organize our data structure carefully, this
283
§14
External Memory Searching
Figure 14.1: In the external memory model, accessing an individual item, x, in
the external memory requires reading the entire block containing x into RAM.
means that each disk access could yield 4 096 bytes that are helpful in
completing whatever operation we are doing.
This is the idea behind the external memory model of computation, il-
lustrated schematically in Figure 14.1. In this model, the computer has
access to a large external memory in which all of the data resides. This
memory is divided into memory blocks each containing B words. The
computer also has limited internal memory on which it can perform com-
putations. Transferring a block between internal memory and external
memory takes constant time. Computations performed within the in-
ternal memory are free; they take no time at all. The fact that internal
memory computations are free may seem a bit strange, but it simply em-
phasizes the fact that external memory is so much slower than RAM.
In the full-blown external memory model, the size of the internal
memory is also a parameter. However, for the data structures described
in this chapter, it is sufficient to have an internal memory of size O(B +
logB n). That is, the memory needs to be capable of storing a constant
number of blocks and a recursion stack of height O(logB n). In most cases,
the O(B) term dominates the memory requirement. For example, even
2160. In deci-
with the relatively small value B = 32, B
logB n for all n
≥
≤
284
xdiskRAMExternalMemoryxxCPUThe Block Store
§14.1
mal, B
≥
logB n for any
1 461 501 637 330 902 918 203 684 832 716 283 019 655 932 542 976 .
n
≤
14.1 The Block Store
The notion of external memory includes a large number of possible differ-
ent devices, each of which has its own block size and is accessed with its
own collection of system calls. To simplify the exposition of this chapter
so that we can focus on the common ideas, we encapsulate external mem-
ory devices with an object called a BlockStore. A BlockStore stores a
collection of memory blocks, each of size B. Each block is uniquely iden-
tified by its integer index. A BlockStore supports these operations:
1. readBlock(i): Return the contents of the block whose index is i.
2. writeBlock(i, b): Write contents of b to the block whose index is i.
3. placeBlock(b): Return a new index and store the contents of b at
this index.
4. freeBlock(i): Free the block whose index is i. This indicates that
the contents of this block are no longer used so the external memory
allocated by this block may be reused.
The easiest way to imagine a BlockStore is to imagine it as storing
a file on disk that is partitioned into blocks, each containing B bytes. In
this way, readBlock(i) and writeBlock(i, b) simply read and write bytes
iB, . . . , (i + 1)B
1 of this file. In addition, a simple BlockStore could
keep a free list of blocks that are available for use. Blocks freed with
freeBlock(i) are added to the free list. In this way, placeBlock(b) can
use a block from the free list or, if none is available, append a new block
to the end of the file.
−
14.2 B-Trees
In this section, we discuss a generalization of binary trees, called B-trees,
which is efficient in the external memory model. Alternatively, B-trees
285
§14.2
External Memory Searching
can be viewed as the natural generalization of 2-4 trees described in Sec-
tion 9.1. (A 2-4 tree is a special case of a B-tree that we get by setting
B = 2.)
≥
For any integer B
2, a B-tree is a tree in which all of the leaves have
the same depth and every non-root internal node, u, has at least B chil-
dren and at most 2B children. The children of u are stored in an array,
u.children. The required number of children is relaxed at the root, which
can have anywhere between 2 and 2B children.
If the height of a B-tree is h, then it follows that the number, (cid:96), of
leaves in the B-tree satisfies
2Bh
1
−
(cid:96)
≤
≤
2(2B)h
1 .
−
Taking the logarithm of the first inequality and rearranging terms yields:
1
log (cid:96)
−
log B
h
≤
+ 1
log (cid:96)
log B
+ 1
≤
= logB (cid:96) + 1 .
That is, the height of a B-tree is proportional to the base-B logarithm of
the number of leaves.
1 and these are stored in u.keys[0], . . . , u.keys[k
Each node, u, in B-tree stores an array of keys u.keys[0], . . . , u.keys[2B
−
1]. If u is an internal node with k children, then the number of keys stored
at u is exactly k
2]. The
−
k + 1 array entries in u.keys are set to null. If u is a non-
remaining 2B
root leaf node, then u contains between B
1 keys. The keys in
a B-tree respect an order similar to the keys in a binary search tree. For
any node, u, that stores k
1 and 2B
1 keys,
−
−
−
−
−
u.keys[0] < u.keys[1] <
< u.keys[k
2] .
−
· · ·
If u is an internal node, then for every i
, u.keys[i] is larger
than every key stored in the subtree rooted at u.children[i] but smaller
than every key stored in the subtree rooted at u.children[i + 1]. Infor-
mally,
0, . . . , k
∈ {
−
2
}
u.children[i]
u.keys[i]
≺
≺
u.children[i + 1] .
286
B-Trees
§14.2
Figure 14.2: A B-tree with B = 2.
An example of a B-tree with B = 2 is shown in Figure 14.2.
Note that the data stored in a B-tree node has size O(B). Therefore, in
an external memory setting, the value of B in a B-tree is chosen so that
a node fits into a single external memory block. In this way, the time
it takes to perform a B-tree operation in the external memory model is
proportional to the number of nodes that are accessed (read or written)
by the operation.
For example, if the keys are 4 byte integers and the node indices are
also 4 bytes, then setting B = 256 means that each node stores
(4 + 4)
2B = 8
×
×
512 = 4096
bytes of data. This would be a perfect value of B for the hard disk or
solid state drive discussed in the introduction to this chaper, which have
a block size of 4096 bytes.
The BTree class, which implements a B-tree, stores a BlockStore, bs,
that stores BTree nodes as well as the index, ri, of the root node. As
usual, an integer, n, is used to keep track of the number of items in the
data structure:
int n;
BlockStore<Node> bs;
int ri;
BTree
287
01245789111213151618192022233614172110§14.2
External Memory Searching
Figure 14.3: A successful search (for the value 4) and an unsuccessful search (for
the value 16.5) in a B-tree. Shaded nodes show where the value of z is updated
during the searches.
14.2.1 Searching
The implementation of the find(x) operation, which is illustrated in Fig-
ure 14.3, generalizes the find(x) operation in a binary search tree. The
search for x starts at the root and uses the keys stored at a node, u, to
determine in which of u’s children the search should continue.
More specifically, at a node u, the search checks if x is stored in u.keys.
If so, x has been found and the search is complete. Otherwise, the search
finds the smallest integer, i, such that u.keys[i] > x and continues the
If no key in u.keys is
search in the subtree rooted at u.children[i].
greater than x, then the search continues in u’s rightmost child. Just like
binary search trees, the algorithm keeps track of the most recently seen
key, z, that is larger than x. In case x is not found, z is returned as the
smallest value that is greater or equal to x.
BTree
T find(T x) {
T z = null;
int ui = ri;
while (ui >= 0) {
Node u = bs.readBlock(ui);
int i = findIt(u.keys, x);
if (i < 0) return u.keys[-(i+1)]; // found it
if (u.keys[i] != null)
z = u.keys[i];
ui = u.children[i];
}
288
0124578911121315161819202223361417211016.5B-Trees
§14.2
Figure 14.4: The execution of findIt(a, 27).
return z;
}
Central to the find(x) method is the findIt(a, x) method that searches
in a null-padded sorted array, a, for the value x. This method, illustrated
in Figure 14.4, works for any array, a, where a[0], . . . , a[k
1] is a sequence
1] are all set to null. If x
of keys in sorted order and a[k], . . . , a[a.length
is in the array at position i, then findIt(a, x) returns
1. Otherwise,
it returns the smallest index, i, such that a[i] > x or a[i] = null.
−
−
−
−
i
int findIt(T[] a, T x) {
int lo = 0, hi = a.length;
while (hi != lo) {
BTree
int m = (hi+lo)/2;
int cmp = a[m] == null ? -1 : compare(x, a[m]);
if (cmp < 0)
hi = m;
// look in first half
else if (cmp > 0)
lo = m+1;
// look in second half
else
return -m-1; // found it
}
return lo;
}
The findIt(a, x) method uses a binary search that halves the search
space at each step, so it runs in O(log(a.length)) time. In our setting,
a.length = 2B, so findIt(a, x) runs in O(log B) time.
289
10415283941051461672283194510–11–12–13–14–1527a§14.2
External Memory Searching
We can analyze the running time of a B-tree find(x) operation both
in the usual word-RAM model (where every instruction counts) and in
the external memory model (where we only count the number of nodes
accessed). Since each leaf in a B-tree stores at least one key and the height
of a B-Tree with (cid:96) leaves is O(logB (cid:96)), the height of a B-tree that stores
n keys is O(logB n). Therefore, in the external memory model, the time
taken by the find(x) operation is O(logB n). To determine the running
time in the word-RAM model, we have to account for the cost of calling
findIt(a, x) for each node we access, so the running time of find(x) in
the word-RAM model is
O(logB n)
×
O(log B) = O(log n) .
14.2.2 Addition
One important difference between B-trees and the BinarySearchTree
data structure from Section 6.2 is that the nodes of a B-tree do not store
pointers to their parents. The reason for this will be explained shortly.
The lack of parent pointers means that the add(x) and remove(x) opera-
tions on B-trees are most easily implemented using recursion.
Like all balanced search trees, some form of rebalancing is required
during an add(x) operation. In a B-tree, this is done by splitting nodes. Re-
fer to Figure 14.5 for what follows. Although splitting takes place across
two levels of recursion, it is best understood as an operation that takes a
node u containing 2B keys and having 2B + 1 children. It creates a new
node, w, that adopts u.children[B], . . . , u.children[2B]. The new node w
1]. At this point, u
also takes u’s B largest keys, u.keys[B], . . . , u.keys[2B
has B children and B keys. The extra key, u.keys[B
1], is passed up to
the parent of u, which also adopts w.
−
−
Notice that the splitting operation modifies three nodes: u, u’s parent,
and the new node, w. This is why it is important that the nodes of a B-
tree do not maintain parent pointers. If they did, then the B + 1 children
adopted by w would all need to have their parent pointers modified. This
would increase the number of external memory accesses from 3 to B + 4
and would make B-trees much less efficient for large values of B.
The add(x) method in a B-tree is illustrated in Figure 14.6. At a high
290
B-Trees
§14.2
u.split()
⇓
Figure 14.5: Splitting the node u in a B-tree (B = 3). Notice that the key
u.keys[2] = m passes from u to its parent.
291
hjmoqsbdfuACEVGIKNPRTu¢¢¢hjmoqsbdfmACEVGIKNPRTuuw¢§14.2
External Memory Searching
⇓
⇓
Figure 14.6: The add(x) operation in a BTree. Adding the value 21 results in two
nodes being split.
level, this method finds a leaf, u, at which to add the value x.
If this
causes u to become overfull (because it already contained B
1 keys), then
u is split. If this causes u’s parent to become overfull, then u’s parent is
also split, which may cause u’s grandparent to become overfull, and so
on. This process continues, moving up the tree one level at a time until
reaching a node that is not overfull or until the root is split. In the former
case, the process stops. In the latter case, a new root is created whose two
children become the nodes obtained when the original root was split.
−
The executive summary of the add(x) method is that it walks from the
root to a leaf searching for x, adds x to this leaf, and then walks back up
to the root, splitting any overfull nodes it encounters along the way. With
this high level view in mind, we can now delve into the details of how
this method can be implemented recursively.
292
0124578911121315161819202324361417221021012457891112131516182023243614172210211919012457891112131516182023243614172210211917B-Trees
§14.2
The real work of add(x) is done by the addRecursive(x, ui) method,
which adds the value x to the subtree whose root, u, has the identifier ui.
If u is a leaf, then x is simply inserted into u.keys. Otherwise, x is added
recursively into the appropriate child, u(cid:48), of u. The result of this recursive
call is normally null but may also be a reference to a newly-created node,
w, that was created because u(cid:48) was split. In this case, u adopts w and takes
its first key, completing the splitting operation on u(cid:48).
After the value x has been added (either to u or to a descendant of
u), the addRecursive(x, ui) method checks to see if u is storing too many
1) keys. If so, then u needs to be split with a call to the
(more than 2B
u.split() method. The result of calling u.split() is a new node that is
used as the return value for addRecursive(x, ui).
−
BTree
Node addRecursive(T x, int ui) {
Node u = bs.readBlock(ui);
int i = findIt(u.keys, x);
if (i < 0) throw new DuplicateValueException();
if (u.children[i] < 0) { // leaf node, just add it
u.add(x, -1);
bs.writeBlock(u.id, u);
} else {
Node w = addRecursive(x, u.children[i]);
if (w != null) { // child was split, w is new child
x = w.remove(0);
bs.writeBlock(w.id, w);
u.add(x, w.id);
bs.writeBlock(u.id, u);
}
}
return u.isFull() ? u.split() : null;
}
The addRecursive(x, ui) method is a helper for the add(x) method,
which calls addRecursive(x, ri) to insert x into the root of the B-tree. If
addRecursive(x, ri) causes the root to split, then a new root is created
that takes as its children both the old root and the new node created by
the splitting of the old root.
293
§14.2
External Memory Searching
boolean add(T x) {
Node w;
try {
BTree
w = addRecursive(x, ri);
} catch (DuplicateValueException e) {
return false;
}
if (w != null) {
// root was split, make new root
Node newroot = new Node();
x = w.remove(0);
bs.writeBlock(w.id, w);
newroot.children[0] = ri;
newroot.keys[0] = x;
newroot.children[1] = w.id;
ri = newroot.id;
bs.writeBlock(ri, newroot);
}
n++;
return true;
}
The add(x) method and its helper, addRecursive(x, ui), can be ana-
lyzed in two phases:
Downward phase: During the downward phase of the recursion, before
x has been added, they access a sequence of BTree nodes and call
findIt(a, x) on each node. As with the find(x) method, this takes
O(logB n) time in the external memory model and O(log n) time in
the word-RAM model.
Upward phase: During the upward phase of the recursion, after x has
been added, these methods perform a sequence of at most O(logB n)
splits. Each split involves only three nodes, so this phase takes
O(logB n) time in the external memory model. However, each split
involves moving B keys and children from one node to another, so
in the word-RAM model, this takes O(B log n) time.
Recall that the value of B can be quite large, much larger than even
log n. Therefore, in the word-RAM model, adding a value to a B-tree can
294
B-Trees
§14.2
be much slower than adding into a balanced binary search tree. Later,
in Section 14.2.4, we will show that the situation is not quite so bad; the
amortized number of split operations done during an add(x) operation
is constant. This shows that the (amortized) running time of the add(x)
operation in the word-RAM model is O(B + log n).
14.2.3 Removal
The remove(x) operation in a BTree is, again, most easily implemented as
a recursive method. Although the recursive implementation of remove(x)
spreads the complexity across several methods, the overall process, which
is illustrated in Figure 14.7, is fairly straightforward. By shuffling keys
around, removal is reduced to the problem of removing a value, x(cid:48), from
some leaf, u. Removing x(cid:48) may leave u with less than B
1 keys; this
situation is called an underflow.
−
When an underflow occurs, u either borrows keys from, or is merged
with, one of its siblings. If u is merged with a sibling, then u’s parent will
now have one less child and one less key, which can cause u’s parent to
underflow; this is again corrected by borrowing or merging, but merging
may cause u’s grandparent to underflow. This process works its way back
up to the root until there is no more underflow or until the root has its
last two children merged into a single child. When the latter case occurs,
the root is removed and its lone child becomes the new root.
Next we delve into the details of how each of these steps is imple-
mented. The first job of the remove(x) method is to find the element x
that should be removed. If x is found in a leaf, then x is removed from
this leaf. Otherwise, if x is found at u.keys[i] for some internal node, u,
then the algorithm removes the smallest value, x(cid:48), in the subtree rooted at
u.children[i + 1]. The value x(cid:48) is the smallest value stored in the BTree
that is greater than x. The value of x(cid:48) is then used to replace x in u.keys[i].
This process is illustrated in Figure 14.8.
The removeRecursive(x, ui) method is a recursive implementation of
the preceding algorithm:
boolean removeRecursive(T x, int ui) {
BTree
295
§14.2
External Memory Searching
⇓
merge(v, w)
⇓
shiftLR(w, v)
⇓
Figure 14.7: Removing the value 4 from a B-tree results in one merge and one
borrowing operation.
296
14111213151618192022233141721101411121315161819202223314172110vw111121315161819202223314172110wv111213151618192022231721141013B-Trees
§14.2
⇓
Figure 14.8: The remove(x) operation in a BTree. To remove the value x = 10 we
replace it with the the value x(cid:48) = 11 and remove 11 from the leaf that contains it.
// didn’t find it
if (ui < 0) return false;
Node u = bs.readBlock(ui);
int i = findIt(u.keys, x);
if (i < 0) { // found it
i = -(i+1);
if (u.isLeaf()) {
u.remove(i);
} else {
u.keys[i] = removeSmallest(u.children[i+1]);
checkUnderflow(u, i+1);
}
return true;
} else if (removeRecursive(x, u.children[i])) {
checkUnderflow(u, i);
return true;
}
return false;
}
T removeSmallest(int ui) {
Node u = bs.readBlock(ui);
if (u.isLeaf())
297
01245789111213151618192022233614172110012457891213151618192022233614172111§14.2
External Memory Searching
return u.remove(0);
T y = removeSmallest(u.children[0]);
checkUnderflow(u, 0);
return y;
}
−
Note that, after recursively removing the value x from the ith child
of u, removeRecursive(x, ui) needs to ensure that this child still has at
least B
1 keys. In the preceding code, this is done using a method called
checkUnderflow(x, i), which checks for and corrects an underflow in the
ith child of u. Let w be the ith child of u. If w has only B
2 keys, then this
needs to be fixed. The fix requires using a sibling of w. This can be either
child i + 1 of u or child i
1 of u,
which is the sibling, v, of w directly to its left. The only time this doesn’t
work is when i = 0, in which case we use the sibling directly to w’s right.
1 of u. We will usually use child i
−
−
−
void checkUnderflow(Node u, int i) {
if (u.children[i] < 0) return;
if (i == 0)
BTree
checkUnderflowZero(u, i); // use u’s right sibling
else
checkUnderflowNonZero(u,i);
}
In the following, we focus on the case when i (cid:44) 0 so that any under-
flow at the ith child of u will be corrected with the help of the (i
1)st
child of u. The case i = 0 is similar and the details can be found in the
accompanying source code.
−
To fix an underflow at node w, we need to find more keys (and possibly
also children), for w. There are two ways to do this:
Borrowing: If w has a sibling, v, with more than B
1 keys, then w can
borrow some keys (and possibly also children) from v. More specif-
ically, if v stores size(v) keys, then between them, v and w have a
total of
−
2 + size(w)
B
−
2B
2
−
≥
298
B-Trees
§14.2
shiftRL(v, w)
⇓
Figure 14.9: If v has more than B
−
1 keys, then w can borrow keys from v.
keys. We can therefore shift keys from v to w so that each of v and w
has at least B
1 keys. This process is illustrated in Figure 14.9.
−
Merging: If v has only B
−
1 keys, we must do something more drastic,
since v cannot afford to give any keys to w. Therefore, we merge v
and w as shown in Figure 14.10. The merge operation is the opposite
3
of the split operation. It takes two nodes that contain a total of 2B
keys and merges them into a single node that contains 2B
2 keys.
(The additional key comes from the fact that, when we merge v and
w, their common parent, u, now has one less child and therefore
needs to give up one of its keys.)
−
−
void checkUnderflowNonZero(Node u, int i) {
Node w = bs.readBlock(u.children[i]);
// w is child of u
BTree
299
hmqbdfoACETGIKNPRsvwuj¢vwhoqbdfmACETGIKNPRsju¢¢§14.2
External Memory Searching
merge(v, w)
⇓
Figure 14.10: Merging two siblings v and w in a B-tree (B = 3).
300
hobdfmACERGIKNPqvwju¢¢hjmobdfqACERGIKNPuB-Trees
§14.2
if (w.size() < B-1) { // underflow at w
Node v = bs.readBlock(u.children[i-1]); // v left of w
if (v.size() > B) {
// w can borrow from v
shiftLR(u, i-1, v, w);
} else { // v will absorb w
merge(u, i-1, v, w);
}
}
}
void checkUnderflowZero(Node u, int i) {
Node w = bs.readBlock(u.children[i]); // w is child of u
if (w.size() < B-1) { // underflow at w
Node v = bs.readBlock(u.children[i+1]); // v right of w
if (v.size() > B) { // w can borrow from v
shiftRL(u, i, v, w);
} else { // w will absorb w
merge(u, i, w, v);
u.children[i] = w.id;
}
}
}
To summarize, the remove(x) method in a B-tree follows a root to leaf
path, removes a key x(cid:48) from a leaf, u, and then performs zero or more
merge operations involving u and its ancestors, and performs at most one
borrowing operation. Since each merge and borrow operation involves
modifying only three nodes, and only O(logB n) of these operations occur,
the entire process takes O(logB n) time in the external memory model.
Again, however, each merge and borrow operation takes O(B) time in
the word-RAM model, so (for now) the most we can say about the run-
ning time required by remove(x) in the word-RAM model is that it is
O(B logB n).
14.2.4 Amortized Analysis of B-Trees
Thus far, we have shown that
1. In the external memory model, the running time of find(x), add(x),
and remove(x) in a B-tree is O(logB n).
301
§14.2
External Memory Searching
2. In the word-RAM model, the running time of find(x) is O(log n)
and the running time of add(x) and remove(x) is O(B log n).
The following lemma shows that, so far, we have overestimated the
number of merge and split operations performed by B-trees.
Lemma 14.1. Starting with an empty B-tree and performing any sequence
of m add(x) and remove(x) operations results in at most 3m/2 splits, merges,
and borrows being performed.
Proof. The proof of this has already been sketched in Section 9.3 for the
special case in which B = 2. The lemma can be proven using a credit
scheme, in which
1. each split, merge, or borrow operation is paid for with two credits,
i.e., a credit is removed each time one of these operations occurs;
and
2. at most three credits are created during any add(x) or remove(x)
operation.
Since at most 3m credits are ever created and each split, merge, and bor-
row is paid for with with two credits, it follows that at most 3m/2 splits,
merges, and borrows are performed. These credits are illustrated using
the
symbol in Figures 14.5, 14.9, and 14.10.
To keep track of these credits the proof maintains the following credit
¢
1 keys stores one credit and any
invariant: Any non-root node with B
1 keys stores three credits. A node that stores at least B
node with 2B
keys and most 2B
2 keys need not store any credits. What remains is to
show that we can maintain the credit invariant and satisfy properties 1
and 2, above, during each add(x) and remove(x) operation.
−
−
−
Adding: The add(x) method does not perform any merges or borrows, so
we need only consider split operations that occur as a result of calls to
add(x).
Each split operation occurs because a key is added to a node, u, that
1 keys. When this happens, u is split into two nodes,
1 and B keys, respectively. Prior to this operation, u
1 keys, and hence three credits. Two of these credits can
already contains 2B
u(cid:48) and u(cid:48)(cid:48) having B
was storing 2B
−
−
−
302
B-Trees
§14.2
be used to pay for the split and the other credit can be given to u(cid:48) (which
has B
1 keys) to maintain the credit invariant. Therefore, we can pay for
the split and maintain the credit invariant during any split.
−
The only other modification to nodes that occur during an add(x) op-
eration happens after all splits, if any, are complete. This modification
involves adding a new key to some node u(cid:48). If, prior to this, u(cid:48) had 2B
2
children, then it now has 2B
1 children and must therefore receive three
credits. These are the only credits given out by the add(x) method.
−
−
Removing: During a call to remove(x), zero or more merges occur and
are possibly followed by a single borrow. Each merge occurs because
two nodes, v and w, each of which had exactly B
1 keys prior to call-
ing remove(x) were merged into a single node with exactly 2B
2 keys.
Each such merge therefore frees up two credits that can be used to pay
for the merge.
−
−
After any merges are performed, at most one borrow operation occurs,
after which no further merges or borrows occur. This borrow operation
only occurs if we remove a key from a leaf, v, that has B
1 keys. The
node v therefore has one credit, and this credit goes towards the cost of
the borrow. This single credit is not enough to pay for the borrow, so we
create one credit to complete the payment.
−
At this point, we have created one credit and we still need to show that
the credit invariant can be maintained. In the worst case, v’s sibling, w,
has exactly B keys before the borrow so that, afterwards, both v and w have
1 keys. This means that v and w each should be storing a credit when
B
the operation is complete. Therefore, in this case, we create an additional
two credits to give to v and w. Since a borrow happens at most once during
a remove(x) operation, this means that we create at most three credits, as
required.
−
If the remove(x) operation does not include a borrow operation, this
is because it finishes by removing a key from some node that, prior to the
operation, had B or more keys. In the worst case, this node had exactly B
keys, so that it now has B
1 keys and must be given one credit, which we
create.
−
In either case—whether the removal finishes with a borrow operation
or not—at most three credits need to be created during a call to remove(x)
303
§14.3
External Memory Searching
to maintain the credit invariant and pay for all borrows and merges that
occur. This completes the proof of the lemma.
The purpose of Lemma 14.1 is to show that, in the word-RAM model
the cost of splits, merges and joins during a sequence of m add(x) and
remove(x) operations is only O(Bm). That is, the amortized cost per op-
eration is only O(B), so the amortized cost of add(x) and remove(x) in the
word-RAM model is O(B + log n). This is summarized by the following
pair of theorems:
Theorem 14.1 (External Memory B-Trees). A BTree implements the SSet
In the external memory model, a BTree supports the operations
interface.
add(x), remove(x), and find(x) in O(logB n) time per operation.
Theorem 14.2 (Word-RAM B-Trees). A BTree implements the SSet inter-
face. In the word-RAM model, and ignoring the cost of splits, merges, and
borrows, a BTree supports the operations add(x), remove(x), and find(x)
in O(log n) time per operation. Furthermore, beginning with an empty BTree,
any sequence of m add(x) and remove(x) operations results in a total of O(Bm)
time spent performing splits, merges, and borrows.
14.3 Discussion and Exercises
The external memory model of computation was introduced by Aggarwal
and Vitter [4]. It is sometimes also called the I/O model or the disk access
model.
B-Trees are to external memory searching what binary search trees
are to internal memory searching. B-trees were introduced by Bayer and
McCreight [9] in 1970 and, less than ten years later, the title of Comer’s
ACM Computing Surveys article referred to them as ubiquitous [15].
Like binary search trees, there are many variants of B-Trees, including
B+-trees, B∗-trees, and counted B-trees. B-trees are indeed ubiquitous and
are the primary data structure in many file systems, including Apple’s
HFS+, Microsoft’s NTFS, and Linux’s Ext4; every major database system;
and key-value stores used in cloud computing. Graefe’s recent survey
[36] provides a 200+ page overview of the many modern applications,
variants, and optimizations of B-trees.
304
Discussion and Exercises
§14.3
B-trees implement the SSet interface. If only the USet interface is
needed, then external memory hashing could be used as an alternative
to B-trees. External memory hashing schemes do exist; see, for example,
Jensen and Pagh [43]. These schemes implement the USet operations in
O(1) expected time in the external memory model. However, for a vari-
ety of reasons, many applications still use B-trees even though they only
require USet operations.
One reason B-trees are such a popular choice is that they often per-
form better than their O(logB n) running time bounds suggest. The rea-
son for this is that, in external memory settings, the value of B is typically
quite large—in the hundreds or even thousands. This means that 99% or
even 99.9% of the data in a B-tree is stored in the leaves. In a database
system with a large memory, it may be possible to cache all the internal
nodes of a B-tree in RAM, since they only represent 1% or 0.1% of the
total data set. When this happens, this means that a search in a B-tree
involves a very fast search in RAM, through the internal nodes, followed
by a single external memory access to retrieve a leaf.
Exercise 14.1. Show what happens when the keys 1.5 and then 7.5 are
added to the B-tree in Figure 14.2.
Exercise 14.2. Show what happens when the keys 3 and then 4 are re-
moved from the B-tree in Figure 14.2.
Exercise 14.3. What is the maximum number of internal nodes in a B-
tree that stores n keys (as a function of n and B)?
Exercise 14.4. The introduction to this chapter claims that B-trees only
need an internal memory of size O(B + logB n). However, the implemen-
tation given here actually requires more memory.
1. Show that the implementation of the add(x) and remove(x) meth-
ods given in this chapter use an internal memory proportional to
B logB n.
2. Describe how these methods could be modified in order to reduce
their memory consumption to O(B + logB n).
Exercise 14.5. Draw the credits used in the proof of Lemma 14.1 on the
trees in Figures 14.6 and 14.7. Verify that (with three additional credits)
305
§14.3
External Memory Searching
it is possible to pay for the splits, merges, and borrows and maintain the
credit invariant.
Exercise 14.6. Design a modified version of a B-tree in which nodes can
have anywhere from B up to 3B children (and hence B
1
keys). Show that this new version of B-trees performs only O(m/B) splits,
merges, and borrows during a sequence of m operations. (Hint: For this
to work, you will have to be more agressive with merging, sometimes
merging two nodes before it is strictly necessary.)
1 up to 3B
−
−
Exercise 14.7. In this exercise, you will design a modified method of
splitting and merging in B-trees that asymptotically reduces the num-
ber of splits, borrows and merges by considering up to three nodes at a
time.
1. Let u be an overfull node and let v be a sibling immediately to the
right of u. There are two ways to fix the overflow at u:
(a) u can give some of its keys to v; or
(b) u can be split and the keys of u and v can be evenly distributed
among u, v, and the newly created node, w.
Show that this can always be done in such a way that, after the oper-
ation, each of the (at most 3) affected nodes has at least B + αB keys
αB keys, for some constant α > 0.
and at most 2B
−
2. Let u be an underfull node and let v and w be siblings of u There are
two ways to fix the underflow at u:
(a) keys can be redistributed among u, v, and w; or
(b) u, v, and w can be merged into two nodes and the keys of u, v,
and w can be redistributed amongst these nodes.
Show that this can always be done in such a way that, after the oper-
ation, each of the (at most 3) affected nodes has at least B + αB keys
αB keys, for some constant α > 0.
and at most 2B
−
3. Show that, with these modifications, the number of merges, bor-
rows, and splits that occur during m operations is O(m/B).
306
Discussion and Exercises
§14.3
Figure 14.11: A B+-tree is a B-tree on top of a doubly-linked list of blocks.
Exercise 14.8. A B+-tree, illustrated in Figure 14.11 stores every key in a
leaf and keeps its leaves stored as a doubly-linked list. As usual, each leaf
stores between B
1 keys. Above this list is a standard B-tree
that stores the largest value from each leaf but the last.
1 and 2B
−
−
1. Describe fast implementations of add(x), remove(x), and find(x) in
a B+-tree.
2. Explain how to efficiently implement the findRange(x, y) method,
that reports all values greater than x and less than or equal to y, in
a B+-tree.
3. Implement a class, BPlusTree, that implements find(x), add(x),
remove(x), and findRange(x, y).
4. B+-trees duplicate some of the keys because they are stored both in
the B-tree and in the list. Explain why this duplication does not add
up to much for large values of B.
307
01234567891011121314141617241012147B-treeBibliography
[1] Free eBooks by Project Gutenberg. URL: http://www.gutenberg.
org/ [cited 2011-10-12].
[2] IEEE Standard for Floating-Point Arithmetic. Technical report, Mi-
croprocessor Standards Committee of the IEEE Computer Society,
3 Park Avenue, New York, NY 10016-5997, USA, August 2008.
doi:10.1109/IEEESTD.2008.4610935.
[3] G. Adelson-Velskii and E. Landis. An algorithm for the organization
of information. Soviet Mathematics Doklady, 3(1259-1262):4, 1962.
[4] A. Aggarwal and J. S. Vitter. The input/output complexity of sort-
ing and related problems. Communications of the ACM, 31(9):1116–
1127, 1988.
[5] A. Andersson. Improving partial rebuilding by using simple balance
criteria. In F. K. H. A. Dehne, J.-R. Sack, and N. Santoro, editors, Al-
gorithms and Data Structures, Workshop WADS ’89, Ottawa, Canada,
August 17–19, 1989, Proceedings, volume 382 of Lecture Notes in
Computer Science, pages 393–402. Springer, 1989.
[6] A. Andersson. Balanced search trees made simple. In F. K. H. A.
Dehne, J.-R. Sack, N. Santoro, and S. Whitesides, editors, Algorithms
and Data Structures, Third Workshop, WADS ’93, Montr´eal, Canada,
August 11–13, 1993, Proceedings, volume 709 of Lecture Notes in
Computer Science, pages 60–71. Springer, 1993.
[7] A. Andersson. General balanced trees.
Journal of Algorithms,
30(1):1–18, 1999.
309
Bibliography
[8] A. Bagchi, A. L. Buchsbaum, and M. T. Goodrich. Biased skip lists. In
P. Bose and P. Morin, editors, Algorithms and Computation, 13th Inter-
national Symposium, ISAAC 2002 Vancouver, BC, Canada, November
21–23, 2002, Proceedings, volume 2518 of Lecture Notes in Computer
Science, pages 1–13. Springer, 2002.
[9] R. Bayer and E. M. McCreight. Organization and maintenance of
large ordered indexes. In SIGFIDET Workshop, pages 107–141. ACM,
1970.
[10] Bibliography on hashing. URL: http://liinwww.ira.uka.de/
bibliography/Theory/hash.html [cited 2011-07-20].
[11] J. Black, S. Halevi, H. Krawczyk, T. Krovetz, and P. Rogaway. UMAC:
Fast and secure message authentication.
In M. J. Wiener, editor,
Advances in Cryptology - CRYPTO ’99, 19th Annual International
Cryptology Conference, Santa Barbara, California, USA, August 15–19,
1999, Proceedings, volume 1666 of Lecture Notes in Computer Science,
pages 79–79. Springer, 1999.
[12] P. Bose, K. Dou¨ıeb, and S. Langerman. Dynamic optimality for skip
lists and b-trees. In S.-H. Teng, editor, Proceedings of the Nineteenth
Annual ACM-SIAM Symposium on Discrete Algorithms, SODA 2008,
San Francisco, California, USA, January 20–22, 2008, pages 1106–
1114. SIAM, 2008.
[13] A. Brodnik, S. Carlsson, E. D. Demaine,
I. Munro, and
R. Sedgewick. Resizable arrays in optimal time and space. In Dehne
et al. [18], pages 37–48.
J.
[14] J. Carter and M. Wegman. Universal classes of hash functions. Jour-
nal of computer and system sciences, 18(2):143–154, 1979.
[15] D. Comer.
The ubiquitous B-tree.
ACM Computing Surveys,
11(2):121–137, 1979.
[16] C. Crane. Linear lists and priority queues as balanced binary trees.
Technical Report STAN-CS-72-259, Computer Science Department,
Stanford University, 1972.
310
Bibliography
[17] S. Crosby and D. Wallach. Denial of service via algorithmic complex-
ity attacks. In Proceedings of the 12th USENIX Security Symposium,
pages 29–44, 2003.
[18] F. K. H. A. Dehne, A. Gupta, J.-R. Sack, and R. Tamassia, editors.
Algorithms and Data Structures, 6th International Workshop, WADS
’99, Vancouver, British Columbia, Canada, August 11–14, 1999, Pro-
ceedings, volume 1663 of Lecture Notes in Computer Science. Springer,
1999.
[19] L. Devroye. Applications of the theory of records in the study of
random trees. Acta Informatica, 26(1):123–130, 1988.
[20] P. Dietz and J. Zhang. Lower bounds for monotonic list labeling.
In J. R. Gilbert and R. G. Karlsson, editors, SWAT 90, 2nd Scandi-
navian Workshop on Algorithm Theory, Bergen, Norway, July 11–14,
1990, Proceedings, volume 447 of Lecture Notes in Computer Science,
pages 173–180. Springer, 1990.
[21] M. Dietzfelbinger. Universal hashing and k-wise independent ran-
dom variables via integer arithmetic without primes. In C. Puech
and R. Reischuk, editors, STACS 96, 13th Annual Symposium on The-
oretical Aspects of Computer Science, Grenoble, France, February 22–24,
1996, Proceedings, volume 1046 of Lecture Notes in Computer Science,
pages 567–580. Springer, 1996.
[22] M. Dietzfelbinger, J. Gil, Y. Matias, and N. Pippenger. Polynomial
hash functions are reliable. In W. Kuich, editor, Automata, Languages
and Programming, 19th International Colloquium, ICALP92, Vienna,
Austria, July 13–17, 1992, Proceedings, volume 623 of Lecture Notes
in Computer Science, pages 235–246. Springer, 1992.
[23] M. Dietzfelbinger, T. Hagerup, J. Katajainen, and M. Penttonen. A
reliable randomized algorithm for the closest-pair problem. Journal
of Algorithms, 25(1):19–51, 1997.
[24] M. Dietzfelbinger, A. R. Karlin, K. Mehlhorn, F. M. auf der Heide,
H. Rohnert, and R. E. Tarjan. Dynamic perfect hashing: Upper and
lower bounds. SIAM J. Comput., 23(4):738–761, 1994.
311
Bibliography
[25] A. Elmasry. Pairing heaps with O(log log n) decrease cost. In Pro-
ceedings of the twentieth Annual ACM-SIAM Symposium on Discrete
Algorithms, pages 471–476. Society for Industrial and Applied Math-
ematics, 2009.
[26] F. Ergun, S. C. Sahinalp, J. Sharp, and R. Sinha. Biased dictionaries
with fast insert/deletes. In Proceedings of the thirty-third annual ACM
symposium on Theory of computing, pages 483–491, New York, NY,
USA, 2001. ACM.
[27] M. Eytzinger. Thesaurus principum hac aetate in Europa viventium
(Cologne). 1590. In commentaries, ‘Eytzinger’ may appear in variant
forms, including: Aitsingeri, Aitsingero, Aitsingerum, Eyzingern.
[28] R. W. Floyd. Algorithm 245: Treesort 3. Communications of the ACM,
7(12):701, 1964.
[29] M. Fredman, R. Sedgewick, D. Sleator, and R. Tarjan. The pairing
heap: A new form of self-adjusting heap. Algorithmica, 1(1):111–
129, 1986.
[30] M. Fredman and R. Tarjan.
improved network optimization algorithms.
34(3):596–615, 1987.
Fibonacci heaps and their uses in
Journal of the ACM,
[31] M. L. Fredman, J. Koml ´os, and E. Szemer´edi. Storing a sparse table
with 0 (1) worst case access time. Journal of the ACM, 31(3):538–544,
1984.
[32] M. L. Fredman and D. E. Willard. Surpassing the information theo-
retic bound with fusion trees. Journal of computer and system sciences,
47(3):424–436, 1993.
[33] I. Galperin and R. Rivest. Scapegoat trees.
In Proceedings of the
fourth annual ACM-SIAM Symposium on Discrete algorithms, pages
165–174. Society for Industrial and Applied Mathematics, 1993.
[34] A. Gambin and A. Malinowski. Randomized meldable priority
queues. In SOFSEM98: Theory and Practice of Informatics, pages 344–
349. Springer, 1998.
312
Bibliography
[35] M. T. Goodrich and J. G. Kloss. Tiered vectors: Efficient dynamic
arrays for rank-based sequences. In Dehne et al. [18], pages 205–
216.
[36] G. Graefe. Modern b-tree techniques. Foundations and Trends in
Databases, 3(4):203–402, 2010.
[37] R. L. Graham, D. E. Knuth, and O. Patashnik. Concrete Mathematics.
Addison-Wesley, 2nd edition, 1994.
[38] L. Guibas and R. Sedgewick. A dichromatic framework for balanced
trees. In 19th Annual Symposium on Foundations of Computer Science,
Ann Arbor, Michigan, 16–18 October 1978, Proceedings, pages 8–21.
IEEE Computer Society, 1978.
[39] C. A. R. Hoare. Algorithm 64: Quicksort. Communications of the
ACM, 4(7):321, 1961.
[40] J. E. Hopcroft and R. E. Tarjan. Algorithm 447: Efficient algorithms
for graph manipulation. Communications of the ACM, 16(6):372–378,
1973.
[41] J. E. Hopcroft and R. E. Tarjan. Efficient planarity testing. Journal of
the ACM, 21(4):549–568, 1974.
[42] HP-UX process management white paper, version 1.3, 1997. URL:
http://h21007.www2.hp.com/portal/download/files/prot/
files/STK/pdfs/proc_mgt.pdf [cited 2011-07-20].
[43] M. S. Jensen and R. Pagh. Optimality in external memory hashing.
Algorithmica, 52(3):403–411, 2008.
[44] P. Kirschenhofer, C. Martinez, and H. Prodinger. Analysis of an op-
timized search algorithm for skip lists. Theoretical Computer Science,
144:199–220, 1995.
[45] P. Kirschenhofer and H. Prodinger. The path length of random skip
lists. Acta Informatica, 31:775–792, 1994.
[46] D. Knuth. Fundamental Algorithms, volume 1 of The Art of Computer
Programming. Addison-Wesley, third edition, 1997.
313
Bibliography
[47] D. Knuth. Seminumerical Algorithms, volume 2 of The Art of Com-
puter Programming. Addison-Wesley, third edition, 1997.
[48] D. Knuth. Sorting and Searching, volume 3 of The Art of Computer
Programming. Addison-Wesley, second edition, 1997.
[49] C. Y. Lee. An algorithm for path connection and its applications.
IRE Transaction on Electronic Computers, EC-10(3):346–365, 1961.
[50] E. Lehman, F. T. Leighton, and A. R. Meyer. Mathematics for Com-
puter Science. 2011. URL: http://courses.csail.mit.edu/6.042/
spring12/mcs.pdf [cited 2012-09-06].
[51] C. Mart´ınez and S. Roura. Randomized binary search trees. Journal
of the ACM, 45(2):288–323, 1998.
[52] E. F. Moore. The shortest path through a maze. In Proceedings of the
International Symposium on the Theory of Switching, pages 285–292,
1959.
[53] J. I. Munro, T. Papadakis, and R. Sedgewick. Deterministic skip lists.
In Proceedings of the third annual ACM-SIAM symposium on Discrete
algorithms (SODA’92), pages 367–375, Philadelphia, PA, USA, 1992.
Society for Industrial and Applied Mathematics.
[54] Oracle. The Collections Framework. URL: http://download.oracle.
com/javase/1.5.0/docs/guide/collections/ [cited 2011-07-19].
[55] Oracle.
Java Platform Standard Ed. 6. URL: http://download.
oracle.com/javase/6/docs/api/ [cited 2011-07-19].
[56] Oracle. The Java Tutorials. URL: http://download.oracle.com/
javase/tutorial/ [cited 2011-07-19].
[57] R. Pagh and F. Rodler. Cuckoo hashing.
Journal of Algorithms,
51(2):122–144, 2004.
[58] T. Papadakis, J. I. Munro, and P. V. Poblete. Average search and
update costs in skip lists. BIT, 32:316–332, 1992.
314
Bibliography
[59] M. Pˇatras¸cu and M. Thorup. Randomization does not help search-
ing predecessors. In N. Bansal, K. Pruhs, and C. Stein, editors, Pro-
ceedings of the Eighteenth Annual ACM-SIAM Symposium on Discrete
Algorithms, SODA 2007, New Orleans, Louisiana, USA, January 7–9,
2007, pages 555–564. SIAM, 2007.
[60] M. Pˇatras¸cu and M. Thorup. The power of simple tabulation hash-
ing. Journal of the ACM, 59(3):14, 2012.
[61] W. Pugh. A skip list cookbook. Technical report, Institute for Ad-
vanced Computer Studies, Department of Computer Science, Uni-
versity of Maryland, College Park, 1989. URL: ftp://ftp.cs.umd.
edu/pub/skipLists/cookbook.pdf [cited 2011-07-20].
[62] W. Pugh. Skip lists: A probabilistic alternative to balanced trees.
Communications of the ACM, 33(6):668–676, 1990.
[63] Redis. URL: http://redis.io/ [cited 2011-07-20].
[64] B. Reed. The height of a random binary search tree. Journal of the
ACM, 50(3):306–332, 2003.
[65] S. M. Ross. Probability Models for Computer Science. Academic Press,
Inc., Orlando, FL, USA, 2001.
[66] R. Sedgewick. Left-leaning red-black trees, September 2008. URL:
http://www.cs.princeton.edu/˜rs/talks/LLRB/LLRB.pdf [cited
2011-07-21].
[67] R. Seidel and C. Aragon. Randomized search trees. Algorithmica,
16(4):464–497, 1996.
[68] H. H. Seward. Information sorting in the application of electronic
digital computers to business operations. Master’s thesis, Mas-
sachusetts Institute of Technology, Digital Computer Laboratory,
1954.
[69] Z. Shao, J. H. Reppy, and A. W. Appel. Unrolling lists. In Proceed-
ings of the 1994 ACM conference LISP and Functional Programming
(LFP’94), pages 185–195, New York, 1994. ACM.
315
Bibliography
[70] P. Sinha. A memory-efficient doubly linked list. Linux Journal, 129,
2005. URL: http://www.linuxjournal.com/article/6828 [cited
2013-06-05].
[71] SkipDB.
URL: http://dekorte.com/projects/opensource/
SkipDB/ [cited 2011-07-20].
[72] D. Sleator and R. Tarjan. Self-adjusting binary trees. In Proceedings
of the 15th Annual ACM Symposium on Theory of Computing, 25–27
April, 1983, Boston, Massachusetts, USA, pages 235–245. ACM, ACM,
1983.
[73] S. P. Thompson. Calculus Made Easy. MacMillan, Toronto, 1914.
Project Gutenberg EBook 33283. URL: http://www.gutenberg.
org/ebooks/33283 [cited 2012-06-14].
[74] P. van Emde Boas. Preserving order in a forest in less than logarith-
mic time and linear space. Inf. Process. Lett., 6(3):80–82, 1977.
[75] J. Vuillemin. A data structure for manipulating priority queues.
Communications of the ACM, 21(4):309–315, 1978.
[76] J. Vuillemin. A unifying look at data structures. Communications of
the ACM, 23(4):229–239, 1980.
[77] D. E. Willard. Log-logarithmic worst-case range queries are possible
in space Θ(N ). Inf. Process. Lett., 17(2):81–84, 1983.
[78] J. Williams. Algorithm 232: Heapsort. Communications of the ACM,
7(6):347–348, 1964.
316
Index
9-1-1, 2
abstract data type, see interface
adjacency list, 252
adjacency matrix, 249
algorithmic complexity attack, 132
amortized cost, 21
amortized running time, 20
ancestor, 133
array
circular, 38
ArrayDeque, 40
ArrayQueue, 36
arrays, 29
ArrayStack, 30
asymptotic notation, 12
AVL tree, 206
B∗-tree, 304
B+-tree, 304
B-tree, 286
backing array, 29
Bag, 28
BDeque, 71
Bibliography on Hashing, 128
big-Oh notation, 12
binary heap, 211
binary logarithm, 10
binary search, 272, 289
binary search tree, 140
height balanced, 206
partial rebuilding, 173
random, 154
randomized, 169
red-black, 185
size-balanced, 148
versus skiplist, 105
binary search tree property, 140
binary tree, 133
complete, 215
heap-ordered, 212
search, 140
binary-tree traversal, 136
BinaryHeap, 211
BinarySearchTree, 140
BinaryTree, 135
BinaryTrie, 266
binomial coefficients, 12
binomial heap, 222
black node, 190
black-height property, 190
block, 283, 284
block store, 285
BlockStore, 285
borrow, 298
bounded deque, 71
317
BPlusTree, 307
breadth-first traversal, 139
breadth-first-search, 256
celebrity, see universal sink
ChainedHashTable, 107
chaining, 107
child, 133
left, 133
right, 133
circular array, 38
coin toss, 17, 98
collision resolution, 129
colour, 190
Comparator, 226
compare(a, b), 226
compare(x, y), 9
comparison tree, 236
comparison-based sorting, 226
complete binary tree, 215
complexity
space, 20
time, 20
conflict graph, 247
connected components, 263
connected graph, 263
contact list, 1
conted B-tree, 304
correctness, 20
CountdownTree, 183
counting-sort, 239
credit invariant, 302
credit scheme, 179, 302
CubishArrayStack, 61
cuckoo hashing, 129
cycle, 247
Index
cycle detection, 260
DaryHeap, 223
decreaseKey(u, y), 222
degree, 254
dependencies, 22
depth, 133
depth-first-search, 258
deque, 6
bounded, 71
descendant, 133
dictionary, 8
directed edge, 247
directed graph, 247
disk access model, 304
divide-and-conquer, 226
DLList, 67
doubly-linked list, 67
DualArrayDeque, 43
dummy node, 67
Dyck word, 28
DynamiteTree, 183
e (Euler’s constant), 10
edge, 247
emergency services, 2
Euler’s constant, 10
expected cost, 21
expected running time, 17, 20
expected value, 17
exponential, 10
Ext4, 304
external memory, 283
external memory hashing, 305
external memory model, 284
external storage, 283
318
Index
Eytzinger’s method, 211
hashing
factorial, 11
family tree, 147
FastArrayStack, 35
Fibonacci heap, 222
FIFO queue, 5
file system, 1
finger, 103, 171
finger search
in a skiplist, 103
in a treap, 171
fusion tree, 281
general balanced tree, 181
git, xii
Google, 3
graph, 247
connected, 263
strongly-connected, 263
undirected, 262
Hk (harmonic number), 154
hard disk, 283
harmonic number, 154
hash code, 107, 122
for arrays, 125
for compound objects, 123
for primitive data, 123
for strings, 125
hash function
perfect, 129
hash table, 107
cuckoo, 129
two-level, 129
hash value, 107
hash(x), 107
multiplicative, 110, 129
multiply-add, 129
tabulation, 169
universal, 129
hashing with chaining, 107, 128
heap, 211
binary, 211
binomial, 222
Fibonacci, 222
leftist, 222
pairing, 222
skew, 222
heap order, 212
heap property, 159
heap-ordered binary tree, 212
heap-sort, 233
height
in a tree, 133
of a skiplist, 87
of a tree, 133
height-balanced, 206
HFS+, 304
I/O model, 304
in-order number, 148
in-order traversal, 148
in-place algorithm, 243
incidence matrix, 262
indicator random variable, 17
interface, 4
Java Collections Framework, 26
Java Runtime Environment, 60
leaf, 133
left child, 133
319
Index
left rotation, 161
left-leaning property, 194
left-leaning red-black tree, 194
leftist heap, 222
LIFO queue, see also stack, 5
linear probing, 114
LinearHashTable, 114
linearity of expectation, 17
linked list, 63
doubly-, 67
singly-, 63
space-efficient, 71
unrolled, see also SEList
List, 6
logarithm, 10
binary, 10
natural, 10
lower-bound, 235
map, 8
matched string, 28
MeldableHeap, 217
memcpy(d, s, n), 36
memory manager, 60
merge, 187, 299
merge-sort, 84, 226
min-wise independence, 169
MinDeque, 85
MinQueue, 85
MinStack, 85
modular arithmetic, 37
multiplicative hashing, 110, 129
multiply-add hashing, 129
no-red-edge property, 190
NTFS, 304
number
in-order, 148
post-order, 148
pre-order, 148
O notation, 12
open addressing, 114, 128
Open Source, xi
ordered tree, 133
pair, 8
pairing heap, 222
palindrome, 83
parent, 133
partial rebuilding, 173
path, 247
pedigree family tree, 147, 222
perfect hash function, 129
perfect hashing, 129
permutation, 11
random, 154
pivot element, 230
planarity testing, 262
post-order number, 148
post-order traversal, 148
potential, 48
potential method, 48, 80, 205
pre-order number, 148
pre-order traversal, 148
prime field, 126
priority queue, see also heap, 5
probability, 15
n, 22
natural logarithm, 10
queue
FIFO, 5
320
Index
LIFO, 5
priority, 5
quicksort, 230
radix-sort, 241
RAM, 18
random binary search tree, 154
random permutation, 154
randomization, 15
randomized algorithm, 15
randomized binary search tree, 169
randomized data structure, 15
RandomQueue, 60
reachable vertex, 247
recursive algorithm, 136
red node, 190
red-black tree, 185, 194
RedBlackTree, 194
remix, xi
right child, 133
right rotation, 161
rooted tree, 133
RootishArrayStack, 49
rotation, 161
run, 118
running time, 20
amortized, 20
expected, 17, 20
worst-case, 20
scapegoat, 173
ScapegoatTree, 174
search path
in a BinaryTrie, 266
in a binary search tree, 140
in a skiplist, 88
secondary structure, 275
SEList, 71
sentinel node, 88
Sequence, 184
share, xi
simple path/cycle, 247
singly-linked list, 63
size-balanced, 148
skew heap, 222
skiplist, 87
versus binary search tree, 105
SkiplistList, 93
SkiplistSSet, 90
SLList, 63
social network, 1
solid-state drive, 283
sorting algorithm
comparison-based, 226
sorting lower-bound, 235
source, 247
space complexity, 20
spanning forest, 263
speciation event, 147
species tree, 147
split, 187, 290
square roots, 56
SSet, 9
stable sorting algorithm, 241
stack, 5
std :: copy(a0, a1, b), 36
Stirling’s Approximation, 11
stratified tree, 280
string
matched, 28
strongly-connected graph, 263
successor search, 9
321
Index
System.arraycopy(s, i, d, j, n), 36
word-RAM, 18
worst-case running time, 20
XFastTrie, 272
XOR-list, 82
YFastTrie, 275
tabulation hashing, 121, 169
target, 247
tiered-vector, 59
time complexity, 20
traversal
breadth-first, 139
in-order, 148
of a binary tree, 136
post-order, 148
pre-order, 148
Treap, 159
TreapList, 172
tree, 133
d-ary, 222
binary, 133
ordered, 133
rooted, 133
tree traversal, 136
Treque, 60
two-level hash table, 129
underflow, 295
undirected graph, 262
universal hashing, 129
universal sink, 263
unrolled linked list, see also SEList
USet, 8
van Emde Boas tree, 280
vertex, 247
wasted space, 54
web search, 1
WeightBalancedTree, 183
word, 19
322
|