This file is indexed.

/usr/include/liborigin/tree.hh is in liborigin-dev 20080225-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
/* 

   $Id: tree.hh,v 1.147 2007/10/19 11:24:24 peekas Exp $

   STL-like templated tree class.
   Copyright (C) 2001-2006  Kasper Peeters <kasper.peeters@aei.mpg.de>.

*/

/** \mainpage tree.hh
    \author   Kasper Peeters
    \version  2.4
    \date     18-Oct-2007
    \see      http://www.aei.mpg.de/~peekas/tree/
    \see      http://www.aei.mpg.de/~peekas/tree/ChangeLog

   The tree.hh library for C++ provides an STL-like container class
   for n-ary trees, templated over the data stored at the
   nodes. Various types of iterators are provided (post-order,
   pre-order, and others). Where possible the access methods are
   compatible with the STL or alternative algorithms are
   available. 
*/


/*
   The tree.hh code is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 or 3.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/** \todo 
   - New-style move members are not completely finished yet.
   - It would be good to have an iterator which can iterate over all
     nodes below a given node.
   - Fixed depth iterators do not iterate over the entire range if there
     are 'holes' in the tree.
   - If a range uses const iter_base& as end iterator, things will
     inevitably go wrong, because upcast from iter_base to a non-sibling_iter
     is incorrect. This upcast should be removed (and then all illegal uses
     as previously in 'equal' will be flagged by the compiler). This requires
     new copy constructors though.
   - There's a bug in replace(sibling_iterator, ...) when the ranges
     sit next to each other. Turned up in append_child(iter,iter)
     but has been avoided now.
   - "std::operator<" does not work correctly on our iterators, and for some
     reason a globally defined template operator< did not get picked up. 
     Using a comparison class now, but this should be investigated.
*/

#ifndef tree_hh_
#define tree_hh_

#include <cassert>
#include <memory>
#include <stdexcept>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <cstddef>

// HP-style construct/destroy have gone from the standard,
// so here is a copy.

namespace kp {

template <class T1, class T2>
void constructor(T1* p, T2& val) 
   {
   new ((void *) p) T1(val);
   }

template <class T1>
void constructor(T1* p) 
   {
   new ((void *) p) T1;
   }

template <class T1>
void destructor(T1* p)
   {
   p->~T1();
   }

};

/// A node in the tree, combining links to other nodes as well as the actual data.
template<class T>
class tree_node_ { // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
   public:
      tree_node_<T> *parent;
      tree_node_<T> *first_child, *last_child;
      tree_node_<T> *prev_sibling, *next_sibling;
      T data;
}; // __attribute__((packed));

template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
class tree {
   protected:
      typedef tree_node_<T> tree_node;
   public:
      /// Value of the data stored at a node.
      typedef T value_type;

      class iterator_base;
      class pre_order_iterator;
      class post_order_iterator;
      class sibling_iterator;
      class leaf_iterator;

      tree();
      tree(const T&);
      tree(const iterator_base&);
      tree(const tree<T, tree_node_allocator>&);
      ~tree();
      void operator=(const tree<T, tree_node_allocator>&);

      /// Base class for iterators, only pointers stored, no traversal logic.
#ifdef __SGI_STL_PORT
      class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
#else
      class iterator_base {
#endif
         public:
            typedef T                               value_type;
            typedef T*                              pointer;
            typedef T&                              reference;
            typedef size_t                          size_type;
            typedef ptrdiff_t                       difference_type;
            typedef std::bidirectional_iterator_tag iterator_category;

            iterator_base();
            iterator_base(tree_node *);

            T&             operator*() const;
            T*             operator->() const;

            /// When called, the next increment/decrement skips children of this node.
            void         skip_children();
            /// Number of children of the node pointed to by the iterator.
            unsigned int number_of_children() const;

            sibling_iterator begin() const;
            sibling_iterator end() const;

            tree_node *node;
         protected:
            bool skip_current_children_;
      };

      /// Depth-first iterator, first accessing the node, then its children.
      class pre_order_iterator : public iterator_base { 
         public:
            pre_order_iterator();
            pre_order_iterator(tree_node *);
            pre_order_iterator(const iterator_base&);
            pre_order_iterator(const sibling_iterator&);

            bool    operator==(const pre_order_iterator&) const;
            bool    operator!=(const pre_order_iterator&) const;
            pre_order_iterator&  operator++();
            pre_order_iterator&  operator--();
            pre_order_iterator   operator++(int);
            pre_order_iterator   operator--(int);
            pre_order_iterator&  operator+=(unsigned int);
            pre_order_iterator&  operator-=(unsigned int);
      };

      /// Depth-first iterator, first accessing the children, then the node itself.
      class post_order_iterator : public iterator_base {
         public:
            post_order_iterator();
            post_order_iterator(tree_node *);
            post_order_iterator(const iterator_base&);
            post_order_iterator(const sibling_iterator&);

            bool    operator==(const post_order_iterator&) const;
            bool    operator!=(const post_order_iterator&) const;
            post_order_iterator&  operator++();
            post_order_iterator&  operator--();
            post_order_iterator   operator++(int);
            post_order_iterator   operator--(int);
            post_order_iterator&  operator+=(unsigned int);
            post_order_iterator&  operator-=(unsigned int);

            /// Set iterator to the first child as deep as possible down the tree.
            void descend_all();
      };

      /// Breadth-first iterator, using a queue
      class breadth_first_queued_iterator : public iterator_base {
         public:
            breadth_first_queued_iterator();
            breadth_first_queued_iterator(tree_node *);
            breadth_first_queued_iterator(const iterator_base&);

            bool    operator==(const breadth_first_queued_iterator&) const;
            bool    operator!=(const breadth_first_queued_iterator&) const;
            breadth_first_queued_iterator&  operator++();
            breadth_first_queued_iterator   operator++(int);
            breadth_first_queued_iterator&  operator+=(unsigned int);

         private:
            std::queue<tree_node *> traversal_queue;
      };

      /// The default iterator types throughout the tree class.
      typedef pre_order_iterator            iterator;
      typedef breadth_first_queued_iterator breadth_first_iterator;

      /// Iterator which traverses only the nodes at a given depth from the root.
      class fixed_depth_iterator : public iterator_base {
         public:
            fixed_depth_iterator();
            fixed_depth_iterator(tree_node *);
            fixed_depth_iterator(const iterator_base&);
            fixed_depth_iterator(const sibling_iterator&);
            fixed_depth_iterator(const fixed_depth_iterator&);

            bool    operator==(const fixed_depth_iterator&) const;
            bool    operator!=(const fixed_depth_iterator&) const;
            fixed_depth_iterator&  operator++();
            fixed_depth_iterator&  operator--();
            fixed_depth_iterator   operator++(int);
            fixed_depth_iterator   operator--(int);
            fixed_depth_iterator&  operator+=(unsigned int);
            fixed_depth_iterator&  operator-=(unsigned int);

            tree_node *first_parent_;
         private:
            void set_first_parent_();
            void find_leftmost_parent_();
      };

      /// Iterator which traverses only the nodes which are siblings of each other.
      class sibling_iterator : public iterator_base {
         public:
            sibling_iterator();
            sibling_iterator(tree_node *);
            sibling_iterator(const sibling_iterator&);
            sibling_iterator(const iterator_base&);

            bool    operator==(const sibling_iterator&) const;
            bool    operator!=(const sibling_iterator&) const;
            sibling_iterator&  operator++();
            sibling_iterator&  operator--();
            sibling_iterator   operator++(int);
            sibling_iterator   operator--(int);
            sibling_iterator&  operator+=(unsigned int);
            sibling_iterator&  operator-=(unsigned int);

            tree_node *range_first() const;
            tree_node *range_last() const;
            tree_node *parent_;
         private:
            void set_parent_();
      };

      /// Iterator which traverses only the leaves.
      class leaf_iterator : public iterator_base {
         public:
            leaf_iterator();
            leaf_iterator(tree_node *);
            leaf_iterator(const sibling_iterator&);
            leaf_iterator(const iterator_base&);

            bool    operator==(const leaf_iterator&) const;
            bool    operator!=(const leaf_iterator&) const;
            leaf_iterator&  operator++();
            leaf_iterator&  operator--();
            leaf_iterator   operator++(int);
            leaf_iterator   operator--(int);
            leaf_iterator&  operator+=(unsigned int);
            leaf_iterator&  operator-=(unsigned int);
      };

      /// Return iterator to the beginning of the tree.
      inline pre_order_iterator   begin() const;
      /// Return iterator to the end of the tree.
      inline pre_order_iterator   end() const;
      /// Return post-order iterator to the beginning of the tree.
      post_order_iterator  begin_post() const;
      /// Return post-order iterator to the end of the tree.
      post_order_iterator  end_post() const;
      /// Return fixed-depth iterator to the first node at a given depth from the given iterator.
      fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const;
      /// Return fixed-depth iterator to end of the nodes at given depth from the given iterator.
      fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const;
      /// Return breadth-first iterator to the first node at a given depth.
      breadth_first_queued_iterator begin_breadth_first() const;
      /// Return breadth-first iterator to end of the nodes at given depth.
      breadth_first_queued_iterator end_breadth_first() const;
      /// Return sibling iterator to the first child of given node.
      sibling_iterator     begin(const iterator_base&) const;
      /// Return sibling iterator to the end of the children of a given node.
      sibling_iterator     end(const iterator_base&) const;
      /// Return leaf iterator to the first leaf of the tree.
      leaf_iterator   begin_leaf() const;
      /// Return leaf iterator to the last leaf of the tree.
      leaf_iterator   end_leaf() const;

      /// Return iterator to the parent of a node.
      template<typename iter> static iter parent(iter);
      /// Return iterator to the previous sibling of a node.
      template<typename iter> iter previous_sibling(iter) const;
      /// Return iterator to the next sibling of a node.
      template<typename iter> iter next_sibling(iter) const;
      /// Return iterator to the next node at a given depth.
      template<typename iter> iter next_at_same_depth(iter) const;

      /// Erase all nodes of the tree.
      void     clear();
      /// Erase element at position pointed to by iterator, return incremented iterator.
      template<typename iter> iter erase(iter);
      /// Erase all children of the node pointed to by iterator.
      void     erase_children(const iterator_base&);

      /// Insert empty node as last/first child of node pointed to by position.
      template<typename iter> iter append_child(iter position); 
      template<typename iter> iter prepend_child(iter position); 
      /// Insert node as last/first child of node pointed to by position.
      template<typename iter> iter append_child(iter position, const T& x);
      template<typename iter> iter prepend_child(iter position, const T& x);
      /// Append the node (plus its children) at other_position as last/first child of position.
      template<typename iter> iter append_child(iter position, iter other_position);
      template<typename iter> iter prepend_child(iter position, iter other_position);
      /// Append the nodes in the from-to range (plus their children) as last/first children of position.
      template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to);
      template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to);

      /// Short-hand to insert topmost node in otherwise empty tree.
      pre_order_iterator set_head(const T& x);
      /// Insert node as previous sibling of node pointed to by position.
      template<typename iter> iter insert(iter position, const T& x);
      /// Specialisation of previous member.
      sibling_iterator insert(sibling_iterator position, const T& x);
      /// Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position.
      template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree);
      /// Insert node as next sibling of node pointed to by position.
      template<typename iter> iter insert_after(iter position, const T& x);
      /// Insert node (with children) pointed to by subtree as next sibling of node pointed to by position.
      template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree);

      /// Replace node at 'position' with other node (keeping same children); 'position' becomes invalid.
      template<typename iter> iter replace(iter position, const T& x);
      /// Replace node at 'position' with subtree starting at 'from' (do not erase subtree at 'from'); see above.
      template<typename iter> iter replace(iter position, const iterator_base& from);
      /// Replace string of siblings (plus their children) with copy of a new string (with children); see above
      sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end, 
                               sibling_iterator new_begin,  sibling_iterator new_end); 

      /// Move all children of node at 'position' to be siblings, returns position.
      template<typename iter> iter flatten(iter position);
      /// Move nodes in range to be children of 'position'.
      template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end);
      /// Move all child nodes of 'from' to be children of 'position'.
      template<typename iter> iter reparent(iter position, iter from);

      /// Replace node with a new node, making the old node a child of the new node.
      template<typename iter> iter wrap(iter position, const T& x);

      /// Move 'source' node (plus its children) to become the next sibling of 'target'.
      template<typename iter> iter move_after(iter target, iter source);
      /// Move 'source' node (plus its children) to become the previous sibling of 'target'.
      template<typename iter> iter move_before(iter target, iter source);
      sibling_iterator move_before(sibling_iterator target, sibling_iterator source);
      /// Move 'source' node (plus its children) to become the node at 'target' (erasing the node at 'target').
      template<typename iter> iter move_ontop(iter target, iter source);

      /// Merge with other tree, creating new branches and leaves only if they are not already present.
      void     merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator, 
                     bool duplicate_leaves=false);
      /// Sort (std::sort only moves values of nodes, this one moves children as well).
      void     sort(sibling_iterator from, sibling_iterator to, bool deep=false);
      template<class StrictWeakOrdering>
      void     sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false);
      /// Compare two ranges of nodes (compares nodes as well as tree structure).
      template<typename iter>
      bool     equal(const iter& one, const iter& two, const iter& three) const;
      template<typename iter, class BinaryPredicate>
      bool     equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const;
      template<typename iter>
      bool     equal_subtree(const iter& one, const iter& two) const;
      template<typename iter, class BinaryPredicate>
      bool     equal_subtree(const iter& one, const iter& two, BinaryPredicate) const;
      /// Extract a new tree formed by the range of siblings plus all their children.
      tree     subtree(sibling_iterator from, sibling_iterator to) const;
      void     subtree(tree&, sibling_iterator from, sibling_iterator to) const;
      /// Exchange the node (plus subtree) with its sibling node (do nothing if no sibling present).
      void     swap(sibling_iterator it);
      /// Exchange two nodes (plus subtrees)
      void     swap(iterator, iterator);
      
      /// Count the total number of nodes.
      int      size() const;
      /// Count the total number of nodes below the indicated node (plus one).
      int      size(const iterator_base&) const;
      /// Check if tree is empty.
      bool     empty() const;
      /// Compute the depth to the root.
      int      depth(const iterator_base&) const;
      /// Determine the maximal depth of the tree.
      int      max_depth() const;
      /// Determine the maximal depth of the tree below a given one.
      int      max_depth(const iterator_base&) const;
      /// Count the number of children of node at position.
      static unsigned int number_of_children(const iterator_base&);
      /// Count the number of 'next' siblings of node at iterator.
      unsigned int number_of_siblings(const iterator_base&) const;
      /// Determine whether node at position is in the subtrees with root in the range.
      bool     is_in_subtree(const iterator_base& position, const iterator_base& begin, 
                             const iterator_base& end) const;
      /// Determine whether the iterator is an 'end' iterator and thus not actually pointing to a node.
      bool     is_valid(const iterator_base&) const;

      /// Determine the index of a node in the range of siblings to which it belongs.
      unsigned int index(sibling_iterator it) const;
      /// Inverse of 'index': return the n-th child of the node at position.
      sibling_iterator  child(const iterator_base& position, unsigned int) const;
      
      /// Comparator class for iterators (compares pointer values; why doesn't this work automatically?)
      class iterator_base_less {
         public:
            bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
                            const typename tree<T, tree_node_allocator>::iterator_base& two) const
               {
               return one.node < two.node;
               }
      };
      tree_node *head, *feet;    // head/feet are always dummy; if an iterator points to them it is invalid
   private:
      tree_node_allocator alloc_;
      void head_initialise_();
      void copy_(const tree<T, tree_node_allocator>& other);

      /// Comparator class for two nodes of a tree (used for sorting and searching).
      template<class StrictWeakOrdering>
      class compare_nodes {
         public:
            compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
            
            bool operator()(const tree_node *a, const tree_node *b) 
               {
               static StrictWeakOrdering comp;
               return comp(a->data, b->data);
               }
         private:
            StrictWeakOrdering comp_;
      };
};

//template <class T, class tree_node_allocator>
//class iterator_base_less {
// public:
//    bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
//                  const typename tree<T, tree_node_allocator>::iterator_base& two) const
//       {
//       txtout << "operatorclass<" << one.node < two.node << std::endl;
//       return one.node < two.node;
//       }
//};

// template <class T, class tree_node_allocator>
// bool operator<(const typename tree<T, tree_node_allocator>::iterator& one,
//                const typename tree<T, tree_node_allocator>::iterator& two)
//    {
//    txtout << "operator< " << one.node < two.node << std::endl;
//    if(one.node < two.node) return true;
//    return false;
//    }
// 
// template <class T, class tree_node_allocator>
// bool operator==(const typename tree<T, tree_node_allocator>::iterator& one,
//                const typename tree<T, tree_node_allocator>::iterator& two)
//    {
//    txtout << "operator== " << one.node == two.node << std::endl;
//    if(one.node == two.node) return true;
//    return false;
//    }
// 
// template <class T, class tree_node_allocator>
// bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one,
//                const typename tree<T, tree_node_allocator>::iterator_base& two)
//    {
//    txtout << "operator> " << one.node < two.node << std::endl;
//    if(one.node > two.node) return true;
//    return false;
//    }



// Tree

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree() 
   {
   head_initialise_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const T& x) 
   {
   head_initialise_();
   set_head(x);
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const iterator_base& other)
   {
   head_initialise_();
   set_head((*other));
   replace(begin(), other);
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::~tree()
   {
   clear();
   alloc_.deallocate(head,1);
   alloc_.deallocate(feet,1);
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::head_initialise_() 
   { 
   head = alloc_.allocate(1,0); // MSVC does not have default second argument 
   feet = alloc_.allocate(1,0);

   head->parent=0;
   head->first_child=0;
   head->last_child=0;
   head->prev_sibling=0; //head;
   head->next_sibling=feet; //head;

   feet->parent=0;
   feet->first_child=0;
   feet->last_child=0;
   feet->prev_sibling=head;
   feet->next_sibling=0;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other)
   {
   copy_(other);
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other)
   {
   head_initialise_();
   copy_(other);
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other) 
   {
   clear();
   pre_order_iterator it=other.begin(), to=begin();
   while(it!=other.end()) {
      to=insert(to, (*it));
      it.skip_children();
      ++it;
      }
   to=begin();
   it=other.begin();
   while(it!=other.end()) {
      to=replace(to, it);
      to.skip_children();
      it.skip_children();
      ++to;
      ++it;
      }
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::clear()
   {
   if(head)
      while(head->next_sibling!=feet)
         erase(pre_order_iterator(head->next_sibling));
   }

template<class T, class tree_node_allocator> 
void tree<T, tree_node_allocator>::erase_children(const iterator_base& it)
   {
// std::cout << "erase_children " << it.node << std::endl;
   if(it.node==0) return;

   tree_node *cur=it.node->first_child;
   tree_node *prev=0;

   while(cur!=0) {
      prev=cur;
      cur=cur->next_sibling;
      erase_children(pre_order_iterator(prev));
      kp::destructor(&prev->data);
      alloc_.deallocate(prev,1);
      }
   it.node->first_child=0;
   it.node->last_child=0;
// std::cout << "exit" << std::endl;
   }

template<class T, class tree_node_allocator> 
template<class iter>
iter tree<T, tree_node_allocator>::erase(iter it)
   {
   tree_node *cur=it.node;
   assert(cur!=head);
   iter ret=it;
   ret.skip_children();
   ++ret;
   erase_children(it);
   if(cur->prev_sibling==0) {
      cur->parent->first_child=cur->next_sibling;
      }
   else {
      cur->prev_sibling->next_sibling=cur->next_sibling;
      }
   if(cur->next_sibling==0) {
      cur->parent->last_child=cur->prev_sibling;
      }
   else {
      cur->next_sibling->prev_sibling=cur->prev_sibling;
      }

   kp::destructor(&cur->data);
   alloc_.deallocate(cur,1);
   return ret;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const
   {
   return pre_order_iterator(head->next_sibling);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const
   {
   return pre_order_iterator(feet);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const
   {
   return breadth_first_queued_iterator(head->next_sibling);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const
   {
   return breadth_first_queued_iterator();
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const
   {
   tree_node *tmp=head->next_sibling;
   if(tmp!=feet) {
      while(tmp->first_child)
         tmp=tmp->first_child;
      }
   return post_order_iterator(tmp);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const
   {
   return post_order_iterator(feet);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const
   {
   tree_node *tmp=pos.node;
   unsigned int curdepth=0;
   while(curdepth<dp) { // go down one level
      while(tmp->first_child==0) {
         if(tmp->next_sibling==0) {
            // try to walk up and then right again
            do {
               tmp=tmp->parent;
               if(tmp==0) 
                  throw std::range_error("tree: begin_fixed out of range");
               --curdepth;
               } while(tmp->next_sibling==0);
            }
         tmp=tmp->next_sibling;
         }
      tmp=tmp->first_child;
      ++curdepth;
      }
   return tmp;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const
   {
   assert(1==0); // FIXME: not correct yet: use is_valid() as a temporary workaround 
   tree_node *tmp=pos.node;
   unsigned int curdepth=1;
   while(curdepth<dp) { // go down one level
      while(tmp->first_child==0) {
         tmp=tmp->next_sibling;
         if(tmp==0)
            throw std::range_error("tree: end_fixed out of range");
         }
      tmp=tmp->first_child;
      ++curdepth;
      }
   return tmp;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const
   {
   assert(pos.node!=0);
   if(pos.node->first_child==0) {
      return end(pos);
      }
   return pos.node->first_child;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const
   {
   sibling_iterator ret(0);
   ret.parent_=pos.node;
   return ret;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const
   {
   tree_node *tmp=head->next_sibling;
   if(tmp!=feet) {
      while(tmp->first_child)
         tmp=tmp->first_child;
      }
   return leaf_iterator(tmp);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const
   {
   return leaf_iterator(feet);
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::parent(iter position) 
   {
   assert(position.node!=0);
   return iter(position.node->parent);
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::previous_sibling(iter position) const
   {
   assert(position.node!=0);
   iter ret(position);
   ret.node=position.node->prev_sibling;
   return ret;
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::next_sibling(iter position) const
   {
   assert(position.node!=0);
   iter ret(position);
   ret.node=position.node->next_sibling;
   return ret;
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const
   {
   assert(position.node!=0);
   iter ret(position);

   if(position.node->next_sibling) {
      ret.node=position.node->next_sibling;
      }
   else { 
      int relative_depth=0;
      upper:
      do {
         ret.node=ret.node->parent;
         if(ret.node==0) return ret;
         --relative_depth;
         } while(ret.node->next_sibling==0);
      lower:
      ret.node=ret.node->next_sibling;
      while(ret.node->first_child==0) {
         if(ret.node->next_sibling==0)
            goto upper;
         ret.node=ret.node->next_sibling;
         if(ret.node==0) return ret;
         }
      while(relative_depth<0 && ret.node->first_child!=0) {
         ret.node=ret.node->first_child;
         ++relative_depth;
         }
      if(relative_depth<0) {
         if(ret.node->next_sibling==0) goto upper;
         else                          goto lower;
         }
      }
   return ret;
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::append_child(iter position)
   {
   assert(position.node!=head);
   assert(position.node);

   tree_node *tmp=alloc_.allocate(1,0);
   kp::constructor(&tmp->data);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node;
   if(position.node->last_child!=0) {
      position.node->last_child->next_sibling=tmp;
      }
   else {
      position.node->first_child=tmp;
      }
   tmp->prev_sibling=position.node->last_child;
   position.node->last_child=tmp;
   tmp->next_sibling=0;
   return tmp;
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position)
   {
   assert(position.node!=head);
   assert(position.node);

   tree_node *tmp=alloc_.allocate(1,0);
   kp::constructor(&tmp->data);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node;
   if(position.node->first_child!=0) {
      position.node->first_child->prev_sibling=tmp;
      }
   else {
      position.node->last_child=tmp;
      }
   tmp->next_sibling=position.node->first_child;
   position.node->prev_child=tmp;
   tmp->prev_sibling=0;
   return tmp;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_child(iter position, const T& x)
   {
   // If your program fails here you probably used 'append_child' to add the top
   // node to an empty tree. From version 1.45 the top element should be added
   // using 'insert'. See the documentation for further information, and sorry about
   // the API change.
   assert(position.node!=head);
   assert(position.node);

   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, x);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node;
   if(position.node->last_child!=0) {
      position.node->last_child->next_sibling=tmp;
      }
   else {
      position.node->first_child=tmp;
      }
   tmp->prev_sibling=position.node->last_child;
   position.node->last_child=tmp;
   tmp->next_sibling=0;
   return tmp;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x)
   {
   assert(position.node!=head);
   assert(position.node);

   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, x);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node;
   if(position.node->first_child!=0) {
      position.node->first_child->prev_sibling=tmp;
      }
   else {
      position.node->last_child=tmp;
      }
   tmp->next_sibling=position.node->first_child;
   position.node->first_child=tmp;
   tmp->prev_sibling=0;
   return tmp;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_child(iter position, iter other)
   {
   assert(position.node!=head);
   assert(position.node);

   sibling_iterator aargh=append_child(position, value_type());
   return replace(aargh, other);
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other)
   {
   assert(position.node!=head);
   assert(position.node);

   sibling_iterator aargh=prepend_child(position, value_type());
   return replace(aargh, other);
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to)
   {
   assert(position.node!=head);
   assert(position.node);

   iter ret=from;

   while(from!=to) {
      insert_subtree(position.end(), from);
      ++from;
      }
   return ret;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to)
   {
   assert(position.node!=head);
   assert(position.node);

   iter ret=from;

   while(from!=to) {
      insert_subtree(position.begin(), from);
      ++from;
      }
   return ret;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x)
   {
   assert(head->next_sibling==feet);
   return insert(iterator(feet), x);
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::insert(iter position, const T& x)
   {
   if(position.node==0) {
      position.node=feet; // Backward compatibility: when calling insert on a null node,
                          // insert before the feet.
      }
   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, x);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node->parent;
   tmp->next_sibling=position.node;
   tmp->prev_sibling=position.node->prev_sibling;
   position.node->prev_sibling=tmp;

   if(tmp->prev_sibling==0) {
      if(tmp->parent) // when inserting nodes at the head, there is no parent
         tmp->parent->first_child=tmp;
      }
   else
      tmp->prev_sibling->next_sibling=tmp;
   return tmp;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, const T& x)
   {
   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, x);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->next_sibling=position.node;
   if(position.node==0) { // iterator points to end of a subtree
      tmp->parent=position.parent_;
      tmp->prev_sibling=position.range_last();
      tmp->parent->last_child=tmp;
      }
   else {
      tmp->parent=position.node->parent;
      tmp->prev_sibling=position.node->prev_sibling;
      position.node->prev_sibling=tmp;
      }

   if(tmp->prev_sibling==0) {
      if(tmp->parent) // when inserting nodes at the head, there is no parent
         tmp->parent->first_child=tmp;
      }
   else
      tmp->prev_sibling->next_sibling=tmp;
   return tmp;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x)
   {
   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, x);
   tmp->first_child=0;
   tmp->last_child=0;

   tmp->parent=position.node->parent;
   tmp->prev_sibling=position.node;
   tmp->next_sibling=position.node->next_sibling;
   position.node->next_sibling=tmp;

   if(tmp->next_sibling==0) {
      if(tmp->parent) // when inserting nodes at the head, there is no parent
         tmp->parent->last_child=tmp;
      }
   else {
      tmp->next_sibling->prev_sibling=tmp;
      }
   return tmp;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::insert_subtree(iter position, const iterator_base& subtree)
   {
   // insert dummy
   iter it=insert(position, value_type());
   // replace dummy with subtree
   return replace(it, subtree);
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::insert_subtree_after(iter position, const iterator_base& subtree)
   {
   // insert dummy
   iter it=insert_after(position, value_type());
   // replace dummy with subtree
   return replace(it, subtree);
   }

// template <class T, class tree_node_allocator>
// template <class iter>
// iter tree<T, tree_node_allocator>::insert_subtree(sibling_iterator position, iter subtree)
//    {
//    // insert dummy
//    iter it(insert(position, value_type()));
//    // replace dummy with subtree
//    return replace(it, subtree);
//    }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::replace(iter position, const T& x)
   {
   kp::destructor(&position.node->data);
   kp::constructor(&position.node->data, x);
   return position;
   }

template <class T, class tree_node_allocator>
template <class iter>
iter tree<T, tree_node_allocator>::replace(iter position, const iterator_base& from)
   {
   assert(position.node!=head);
   tree_node *current_from=from.node;
   tree_node *start_from=from.node;
   tree_node *current_to  =position.node;

   // replace the node at position with head of the replacement tree at from
// std::cout << "warning!" << position.node << std::endl;
   erase_children(position);  
// std::cout << "no warning!" << std::endl;
   tree_node* tmp = alloc_.allocate(1,0);
   kp::constructor(&tmp->data, (*from));
   tmp->first_child=0;
   tmp->last_child=0;
   if(current_to->prev_sibling==0) {
      if(current_to->parent!=0)
         current_to->parent->first_child=tmp;
      }
   else {
      current_to->prev_sibling->next_sibling=tmp;
      }
   tmp->prev_sibling=current_to->prev_sibling;
   if(current_to->next_sibling==0) {
      if(current_to->parent!=0)
         current_to->parent->last_child=tmp;
      }
   else {
      current_to->next_sibling->prev_sibling=tmp;
      }
   tmp->next_sibling=current_to->next_sibling;
   tmp->parent=current_to->parent;
   kp::destructor(&current_to->data);
   alloc_.deallocate(current_to,1);
   current_to=tmp;
   
   // only at this stage can we fix 'last'
   tree_node *last=from.node->next_sibling;

   pre_order_iterator toit=tmp;
   // copy all children
   do {
      assert(current_from!=0);
      if(current_from->first_child != 0) {
         current_from=current_from->first_child;
         toit=append_child(toit, current_from->data);
         }
      else {
         while(current_from->next_sibling==0 && current_from!=start_from) {
            current_from=current_from->parent;
            toit=parent(toit);
            assert(current_from!=0);
            }
         current_from=current_from->next_sibling;
         if(current_from!=last) {
            toit=append_child(parent(toit), current_from->data);
            }
         }
      } while(current_from!=last);

   return current_to;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::replace(
   sibling_iterator orig_begin, 
   sibling_iterator orig_end, 
   sibling_iterator new_begin, 
   sibling_iterator new_end)
   {
   tree_node *orig_first=orig_begin.node;
   tree_node *new_first=new_begin.node;
   tree_node *orig_last=orig_first;
   while((++orig_begin)!=orig_end)
      orig_last=orig_last->next_sibling;
   tree_node *new_last=new_first;
   while((++new_begin)!=new_end)
      new_last=new_last->next_sibling;

   // insert all siblings in new_first..new_last before orig_first
   bool first=true;
   pre_order_iterator ret;
   while(1==1) {
      pre_order_iterator tt=insert_subtree(pre_order_iterator(orig_first), pre_order_iterator(new_first));
      if(first) {
         ret=tt;
         first=false;
         }
      if(new_first==new_last)
         break;
      new_first=new_first->next_sibling;
      }

   // erase old range of siblings
   bool last=false;
   tree_node *next=orig_first;
   while(1==1) {
      if(next==orig_last) 
         last=true;
      next=next->next_sibling;
      erase((pre_order_iterator)orig_first);
      if(last) 
         break;
      orig_first=next;
      }
   return ret;
   }

template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::flatten(iter position)
   {
   if(position.node->first_child==0)
      return position;

   tree_node *tmp=position.node->first_child;
   while(tmp) {
      tmp->parent=position.node->parent;
      tmp=tmp->next_sibling;
      } 
   if(position.node->next_sibling) {
      position.node->last_child->next_sibling=position.node->next_sibling;
      position.node->next_sibling->prev_sibling=position.node->last_child;
      }
   else {
      position.node->parent->last_child=position.node->last_child;
      }
   position.node->next_sibling=position.node->first_child;
   position.node->next_sibling->prev_sibling=position.node;
   position.node->first_child=0;
   position.node->last_child=0;

   return position;
   }


template <class T, class tree_node_allocator>
template <typename iter>
iter tree<T, tree_node_allocator>::reparent(iter position, sibling_iterator begin, sibling_iterator end)
   {
   tree_node *first=begin.node;
   tree_node *last=first;

   assert(first!=position.node);
   
   if(begin==end) return begin;
   // determine last node
   while((++begin)!=end) {
      last=last->next_sibling;
      }
   // move subtree
   if(first->prev_sibling==0) {
      first->parent->first_child=last->next_sibling;
      }
   else {
      first->prev_sibling->next_sibling=last->next_sibling;
      }
   if(last->next_sibling==0) {
      last->parent->last_child=first->prev_sibling;
      }
   else {
      last->next_sibling->prev_sibling=first->prev_sibling;
      }
   if(position.node->first_child==0) {
      position.node->first_child=first;
      position.node->last_child=last;
      first->prev_sibling=0;
      }
   else {
      position.node->last_child->next_sibling=first;
      first->prev_sibling=position.node->last_child;
      position.node->last_child=last;
      }
   last->next_sibling=0;

   tree_node *pos=first;
   while(1==1) {
      pos->parent=position.node;
      if(pos==last) break;
      pos=pos->next_sibling;
      }

   return first;
   }

template <class T, class tree_node_allocator>
template <typename iter> iter tree<T, tree_node_allocator>::reparent(iter position, iter from)
   {
   if(from.node->first_child==0) return position;
   return reparent(position, from.node->first_child, end(from));
   }

template <class T, class tree_node_allocator>
template <typename iter> iter tree<T, tree_node_allocator>::wrap(iter position, const T& x)
   {
   assert(position.node!=0);
   sibling_iterator fr=position, to=position;
   ++to;
   iter ret = insert(position, x);
   reparent(ret, fr, to);
   return ret;
   }

template <class T, class tree_node_allocator>
template <typename iter> iter tree<T, tree_node_allocator>::move_after(iter target, iter source)
   {
   tree_node *dst=target.node;
   tree_node *src=source.node;
   assert(dst);
   assert(src);

   if(dst==src) return source;
   if(dst->next_sibling)
      if(dst->next_sibling==src) // already in the right spot
         return source;

   // take src out of the tree
   if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
   else                     src->parent->first_child=src->next_sibling;
   if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
   else                     src->parent->last_child=src->prev_sibling;

   // connect it to the new point
   if(dst->next_sibling!=0) dst->next_sibling->prev_sibling=src;
   else                     dst->parent->last_child=src;
   src->next_sibling=dst->next_sibling;
   dst->next_sibling=src;
   src->prev_sibling=dst;
   src->parent=dst->parent;
   return src;
   }

template <class T, class tree_node_allocator>
template <typename iter> iter tree<T, tree_node_allocator>::move_before(iter target, iter source)
   {
   tree_node *dst=target.node;
   tree_node *src=source.node;
   assert(dst);
   assert(src);

   if(dst==src) return source;
   if(dst->prev_sibling)
      if(dst->prev_sibling==src) // already in the right spot
         return source;

   // take src out of the tree
   if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
   else                     src->parent->first_child=src->next_sibling;
   if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
   else                     src->parent->last_child=src->prev_sibling;

   // connect it to the new point
   if(dst->prev_sibling!=0) dst->prev_sibling->next_sibling=src;
   else                     dst->parent->first_child=src;
   src->prev_sibling=dst->prev_sibling;
   dst->prev_sibling=src;
   src->next_sibling=dst;
   src->parent=dst->parent;
   return src;
   }

// specialisation for sibling_iterators
template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::move_before(sibling_iterator target, 
                                                                                         sibling_iterator source)
   {
   tree_node *dst=target.node;
   tree_node *src=source.node;
   tree_node *dst_prev_sibling;
   if(dst==0) { // must then be an end iterator
      dst_prev_sibling=target.parent_->last_child;
      assert(dst_prev_sibling);
      }
   else dst_prev_sibling=dst->prev_sibling;
   assert(src);

   if(dst==src) return source;
   if(dst_prev_sibling)
      if(dst_prev_sibling==src) // already in the right spot
         return source;

   // take src out of the tree
   if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
   else                     src->parent->first_child=src->next_sibling;
   if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
   else                     src->parent->last_child=src->prev_sibling;

   // connect it to the new point
   if(dst_prev_sibling!=0) dst_prev_sibling->next_sibling=src;
   else                    target.parent_->first_child=src;
   src->prev_sibling=dst_prev_sibling;
   if(dst) {
      dst->prev_sibling=src;
      src->parent=dst->parent;
      }
   src->next_sibling=dst;
   return src;
   }

template <class T, class tree_node_allocator>
template <typename iter> iter tree<T, tree_node_allocator>::move_ontop(iter target, iter source)
   {
   tree_node *dst=target.node;
   tree_node *src=source.node;
   assert(dst);
   assert(src);

   if(dst==src) return source;

   // remember connection points
   tree_node *b_prev_sibling=dst->prev_sibling;
   tree_node *b_next_sibling=dst->next_sibling;
   tree_node *b_parent=dst->parent;

   // remove target
   erase(target);

   // take src out of the tree
   if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
   else                     src->parent->first_child=src->next_sibling;
   if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
   else                     src->parent->last_child=src->prev_sibling;

   // connect it to the new point
   if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src;
   else                  b_parent->first_child=src;
   if(b_next_sibling!=0) b_next_sibling->prev_sibling=src;
   else                  b_parent->last_child=src;
   src->prev_sibling=b_prev_sibling;
   src->next_sibling=b_next_sibling;
   src->parent=b_parent;
   return src;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::merge(sibling_iterator to1,   sibling_iterator to2,
                                          sibling_iterator from1, sibling_iterator from2,
                                          bool duplicate_leaves)
   {
   sibling_iterator fnd;
   while(from1!=from2) {
      if((fnd=std::find(to1, to2, (*from1))) != to2) { // element found
         if(from1.begin()==from1.end()) { // full depth reached
            if(duplicate_leaves)
               append_child(parent(to1), (*from1));
            }
         else { // descend further
            merge(fnd.begin(), fnd.end(), from1.begin(), from1.end(), duplicate_leaves);
            }
         }
      else { // element missing
         insert_subtree(to2, from1);
         }
      ++from1;
      }
   }


template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, bool deep)
   {
   std::less<T> comp;
   sort(from, to, comp, deep);
   }

template <class T, class tree_node_allocator>
template <class StrictWeakOrdering>
void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, 
                                        StrictWeakOrdering comp, bool deep)
   {
   if(from==to) return;
   // make list of sorted nodes
   // CHECK: if multiset stores equivalent nodes in the order in which they
   // are inserted, then this routine should be called 'stable_sort'.
   std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> > nodes(comp);
   sibling_iterator it=from, it2=to;
   while(it != to) {
      nodes.insert(it.node);
      ++it;
      }
   // reassemble
   --it2;

   // prev and next are the nodes before and after the sorted range
   tree_node *prev=from.node->prev_sibling;
   tree_node *next=it2.node->next_sibling;
   typename std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> >::iterator nit=nodes.begin(), eit=nodes.end();
   if(prev==0) {
      if((*nit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
         (*nit)->parent->first_child=(*nit);
      }
   else prev->next_sibling=(*nit);

   --eit;
   while(nit!=eit) {
      (*nit)->prev_sibling=prev;
      if(prev)
         prev->next_sibling=(*nit);
      prev=(*nit);
      ++nit;
      }
   // prev now points to the last-but-one node in the sorted range
   if(prev)
      prev->next_sibling=(*eit);

   // eit points to the last node in the sorted range.
   (*eit)->next_sibling=next;
   (*eit)->prev_sibling=prev; // missed in the loop above
   if(next==0) {
      if((*eit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
         (*eit)->parent->last_child=(*eit);
      }
   else next->prev_sibling=(*eit);

   if(deep) {  // sort the children of each node too
      sibling_iterator bcs(*nodes.begin());
      sibling_iterator ecs(*eit);
      ++ecs;
      while(bcs!=ecs) {
         sort(begin(bcs), end(bcs), comp, deep);
         ++bcs;
         }
      }
   }

template <class T, class tree_node_allocator>
template <typename iter>
bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_) const
   {
   std::equal_to<T> comp;
   return equal(one_, two, three_, comp);
   }

template <class T, class tree_node_allocator>
template <typename iter>
bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_) const
   {
   std::equal_to<T> comp;
   return equal_subtree(one_, two_, comp);
   }

template <class T, class tree_node_allocator>
template <typename iter, class BinaryPredicate>
bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun) const
   {
   pre_order_iterator one(one_), three(three_);

// if(one==two && is_valid(three) && three.number_of_children()!=0)
//    return false;
   while(one!=two && is_valid(three)) {
      if(!fun(*one,*three))
         return false;
      if(one.number_of_children()!=three.number_of_children()) 
         return false;
      ++one;
      ++three;
      }
   return true;
   }

template <class T, class tree_node_allocator>
template <typename iter, class BinaryPredicate>
bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_, BinaryPredicate fun) const
   {
   pre_order_iterator one(one_), two(two_);

   if(!fun(*one,*two)) return false;
   if(number_of_children(one)!=number_of_children(two)) return false;
   return equal(begin(one),end(one),begin(two),fun);
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator> tree<T, tree_node_allocator>::subtree(sibling_iterator from, sibling_iterator to) const
   {
   tree tmp;
   tmp.set_head(value_type());
   tmp.replace(tmp.begin(), tmp.end(), from, to);
   return tmp;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::subtree(tree& tmp, sibling_iterator from, sibling_iterator to) const
   {
   tmp.set_head(value_type());
   tmp.replace(tmp.begin(), tmp.end(), from, to);
   }

template <class T, class tree_node_allocator>
int tree<T, tree_node_allocator>::size() const
   {
   int i=0;
   pre_order_iterator it=begin(), eit=end();
   while(it!=eit) {
      ++i;
      ++it;
      }
   return i;
   }

template <class T, class tree_node_allocator>
int tree<T, tree_node_allocator>::size(const iterator_base& top) const
   {
   int i=0;
   pre_order_iterator it=top, eit=top;
   eit.skip_children();
   ++eit;
   while(it!=eit) {
      ++i;
      ++it;
      }
   return i;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::empty() const
   {
   pre_order_iterator it=begin(), eit=end();
   return (it==eit);
   }

template <class T, class tree_node_allocator>
int tree<T, tree_node_allocator>::depth(const iterator_base& it) const
   {
   tree_node* pos=it.node;
   assert(pos!=0);
   int ret=0;
   while(pos->parent!=0) {
      pos=pos->parent;
      ++ret;
      }
   return ret;
   }

template <class T, class tree_node_allocator>
int tree<T, tree_node_allocator>::max_depth() const
   {
   return max_depth(begin());
   }


template <class T, class tree_node_allocator>
int tree<T, tree_node_allocator>::max_depth(const iterator_base& pos) const
   {
   tree_node *tmp=pos.node;
   int curdepth=0, maxdepth=0;
   while(true) { // try to walk the bottom of the tree
      while(tmp->first_child==0) {
         if(tmp==pos.node) return maxdepth;
         if(tmp->next_sibling==0) {
            // try to walk up and then right again
            do {
               tmp=tmp->parent;
               if(tmp==0) return maxdepth;
               --curdepth;
               } while(tmp->next_sibling==0);
            }
         if(tmp==pos.node) return maxdepth;
         tmp=tmp->next_sibling;
         }
      tmp=tmp->first_child;
      ++curdepth;
      maxdepth=std::max(curdepth, maxdepth);
      } 
   }

template <class T, class tree_node_allocator>
unsigned int tree<T, tree_node_allocator>::number_of_children(const iterator_base& it) 
   {
   tree_node *pos=it.node->first_child;
   if(pos==0) return 0;
   
   unsigned int ret=1;
//   while(pos!=it.node->last_child) {
//      ++ret;
//      pos=pos->next_sibling;
//      }
   while((pos=pos->next_sibling))
      ++ret;
   return ret;
   }

template <class T, class tree_node_allocator>
unsigned int tree<T, tree_node_allocator>::number_of_siblings(const iterator_base& it) const
   {
   tree_node *pos=it.node;
   unsigned int ret=0;
   // count forward
   while(pos->next_sibling && 
         pos->next_sibling!=head &&
         pos->next_sibling!=feet) {
      ++ret;
      pos=pos->next_sibling;
      }
   // count backward
   pos=it.node;
   while(pos->prev_sibling && 
         pos->prev_sibling!=head &&
         pos->prev_sibling!=feet) {
      ++ret;
      pos=pos->prev_sibling;
      }
   
   return ret;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::swap(sibling_iterator it)
   {
   tree_node *nxt=it.node->next_sibling;
   if(nxt) {
      if(it.node->prev_sibling)
         it.node->prev_sibling->next_sibling=nxt;
      else
         it.node->parent->first_child=nxt;
      nxt->prev_sibling=it.node->prev_sibling;
      tree_node *nxtnxt=nxt->next_sibling;
      if(nxtnxt)
         nxtnxt->prev_sibling=it.node;
      else
         it.node->parent->last_child=it.node;
      nxt->next_sibling=it.node;
      it.node->prev_sibling=nxt;
      it.node->next_sibling=nxtnxt;
      }
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::swap(iterator one, iterator two)
   {
   // if one and two are adjacent siblings, use the sibling swap
   if(one.node->next_sibling==two.node) swap(one);
   else if(two.node->next_sibling==one.node) swap(two);
   else {
      tree_node *nxt1=one.node->next_sibling;
      tree_node *nxt2=two.node->next_sibling;
      tree_node *pre1=one.node->prev_sibling;
      tree_node *pre2=two.node->prev_sibling;
      tree_node *par1=one.node->parent;
      tree_node *par2=two.node->parent;

      // reconnect
      one.node->parent=par2;
      one.node->next_sibling=nxt2;
      if(nxt2) nxt2->prev_sibling=one.node;
      else     par2->last_child=one.node;
      one.node->prev_sibling=pre2;
      if(pre2) pre2->next_sibling=one.node;
      else     par2->first_child=one.node;    

      two.node->parent=par1;
      two.node->next_sibling=nxt1;
      if(nxt1) nxt1->prev_sibling=two.node;
      else     par1->last_child=two.node;
      two.node->prev_sibling=pre1;
      if(pre1) pre1->next_sibling=two.node;
      else     par1->first_child=two.node;
      }
   }

// template <class BinaryPredicate>
// tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::find_subtree(
//    sibling_iterator subfrom, sibling_iterator subto, iterator from, iterator to, 
//    BinaryPredicate fun) const
//    {
//    assert(1==0); // this routine is not finished yet.
//    while(from!=to) {
//       if(fun(*subfrom, *from)) {
//          
//          }
//       }
//    return to;
//    }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::is_in_subtree(const iterator_base& it, const iterator_base& begin, 
                                                 const iterator_base& end) const
   {
   // FIXME: this should be optimised.
   pre_order_iterator tmp=begin;
   while(tmp!=end) {
      if(tmp==it) return true;
      ++tmp;
      }
   return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::is_valid(const iterator_base& it) const
   {
   if(it.node==0 || it.node==feet || it.node==head) return false;
   else return true;
   }

template <class T, class tree_node_allocator>
unsigned int tree<T, tree_node_allocator>::index(sibling_iterator it) const
   {
   unsigned int ind=0;
   if(it.node->parent==0) {
      while(it.node->prev_sibling!=head) {
         it.node=it.node->prev_sibling;
         ++ind;
         }
      }
   else {
      while(it.node->prev_sibling!=0) {
         it.node=it.node->prev_sibling;
         ++ind;
         }
      }
   return ind;
   }


template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::child(const iterator_base& it, unsigned int num) const
   {
   tree_node *tmp=it.node->first_child;
   while(num--) {
      assert(tmp!=0);
      tmp=tmp->next_sibling;
      }
   return tmp;
   }




// Iterator base

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::iterator_base::iterator_base()
   : node(0), skip_current_children_(false)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::iterator_base::iterator_base(tree_node *tn)
   : node(tn), skip_current_children_(false)
   {
   }

template <class T, class tree_node_allocator>
T& tree<T, tree_node_allocator>::iterator_base::operator*() const
   {
   return node->data;
   }

template <class T, class tree_node_allocator>
T* tree<T, tree_node_allocator>::iterator_base::operator->() const
   {
   return &(node->data);
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::post_order_iterator::operator!=(const post_order_iterator& other) const
   {
   if(other.node!=this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::post_order_iterator::operator==(const post_order_iterator& other) const
   {
   if(other.node==this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=(const pre_order_iterator& other) const
   {
   if(other.node!=this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::pre_order_iterator::operator==(const pre_order_iterator& other) const
   {
   if(other.node==this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::sibling_iterator::operator!=(const sibling_iterator& other) const
   {
   if(other.node!=this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::sibling_iterator::operator==(const sibling_iterator& other) const
   {
   if(other.node==this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::leaf_iterator::operator!=(const leaf_iterator& other) const
   {
   if(other.node!=this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::leaf_iterator::operator==(const leaf_iterator& other) const
   {
   if(other.node==this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::begin() const
   {
   if(node->first_child==0) 
      return end();

   sibling_iterator ret(node->first_child);
   ret.parent_=this->node;
   return ret;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::end() const
   {
   sibling_iterator ret(0);
   ret.parent_=node;
   return ret;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::iterator_base::skip_children()
   {
   skip_current_children_=true;
   }

template <class T, class tree_node_allocator>
unsigned int tree<T, tree_node_allocator>::iterator_base::number_of_children() const
   {
   tree_node *pos=node->first_child;
   if(pos==0) return 0;
   
   unsigned int ret=1;
   while(pos!=node->last_child) {
      ++ret;
      pos=pos->next_sibling;
      }
   return ret;
   }



// Pre-order iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator() 
   : iterator_base(0)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const iterator_base &other)
   : iterator_base(other.node)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const sibling_iterator& other)
   : iterator_base(other.node)
   {
   if(this->node==0) {
      if(other.range_last()!=0)
         this->node=other.range_last();
      else 
         this->node=other.parent_;
      this->skip_children();
      ++(*this);
      }
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++()
   {
   assert(this->node!=0);
   if(!this->skip_current_children_ && this->node->first_child != 0) {
      this->node=this->node->first_child;
      }
   else {
      this->skip_current_children_=false;
      while(this->node->next_sibling==0) {
         this->node=this->node->parent;
         if(this->node==0)
            return *this;
         }
      this->node=this->node->next_sibling;
      }
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--()
   {
   assert(this->node!=0);
   if(this->node->prev_sibling) {
      this->node=this->node->prev_sibling;
      while(this->node->last_child)
         this->node=this->node->last_child;
      }
   else {
      this->node=this->node->parent;
      if(this->node==0)
         return *this;
      }
   return *this;
}

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++(int n)
   {
   pre_order_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--(int n)
{
  pre_order_iterator copy = *this;
  --(*this);
  return copy;
}

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=(unsigned int num)
   {
   while(num>0) {
      --(*this);
      --num;
      }
   return (*this);
   }



// Post-order iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator() 
   : iterator_base(0)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other)
   : iterator_base(other.node)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other)
   : iterator_base(other.node)
   {
   if(this->node==0) {
      if(other.range_last()!=0)
         this->node=other.range_last();
      else 
         this->node=other.parent_;
      this->skip_children();
      ++(*this);
      }
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++()
   {
   assert(this->node!=0);
   if(this->node->next_sibling==0) {
      this->node=this->node->parent;
      this->skip_current_children_=false;
      }
   else {
      this->node=this->node->next_sibling;
      if(this->skip_current_children_) {
         this->skip_current_children_=false;
         }
      else {
         while(this->node->first_child)
            this->node=this->node->first_child;
         }
      }
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--()
   {
   assert(this->node!=0);
   if(this->skip_current_children_ || this->node->last_child==0) {
      this->skip_current_children_=false;
      while(this->node->prev_sibling==0)
         this->node=this->node->parent;
      this->node=this->node->prev_sibling;
      }
   else {
      this->node=this->node->last_child;
      }
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int)
   {
   post_order_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int)
   {
   post_order_iterator copy = *this;
   --(*this);
   return copy;
   }


template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num)
   {
   while(num>0) {
      --(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::post_order_iterator::descend_all()
   {
   assert(this->node!=0);
   while(this->node->first_child)
      this->node=this->node->first_child;
   }


// Breadth-first iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator()
   : iterator_base()
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   traversal_queue.push(tn);
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(const iterator_base& other)
   : iterator_base(other.node)
   {
   traversal_queue.push(other.node);
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=(const breadth_first_queued_iterator& other) const
   {
   if(other.node!=this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==(const breadth_first_queued_iterator& other) const
   {
   if(other.node==this->node) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++()
   {
   assert(this->node!=0);

   // Add child nodes and pop current node
   sibling_iterator sib=this->begin();
   while(sib!=this->end()) {
      traversal_queue.push(sib.node);
      ++sib;
      }
   traversal_queue.pop();
   if(traversal_queue.size()>0)
      this->node=traversal_queue.front();
   else 
      this->node=0;
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++(int n)
   {
   breadth_first_queued_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --num;
      }
   return (*this);
   }



// Fixed depth iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator()
   : iterator_base()
   {
   set_first_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   set_first_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other)
   : iterator_base(other.node)
   {
   set_first_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other)
   : iterator_base(other.node), first_parent_(other.parent_)
   {
   find_leftmost_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other)
   : iterator_base(other.node), first_parent_(other.first_parent_)
   {
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==(const fixed_depth_iterator& other) const
   {
   if(other.node==this->node && other.first_parent_==first_parent_) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=(const fixed_depth_iterator& other) const
   {
   if(other.node!=this->node || other.first_parent_!=first_parent_) return true;
   else return false;
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::fixed_depth_iterator::set_first_parent_()
   {
   return; // FIXME: we do not use first_parent_ yet, and it actually needs some serious reworking if
           // it is ever to work at the 'head' level.
   first_parent_=0;
   if(this->node==0) return;
   if(this->node->parent!=0)
      first_parent_=this->node->parent;
   if(first_parent_)
      find_leftmost_parent_();
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::fixed_depth_iterator::find_leftmost_parent_()
   {
   return; // FIXME: see 'set_first_parent()'
   tree_node *tmppar=first_parent_;
   while(tmppar->prev_sibling) {
      tmppar=tmppar->prev_sibling;
      if(tmppar->first_child)
         first_parent_=tmppar;
      }
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++()
   {
   assert(this->node!=0);

   if(this->node->next_sibling) {
      this->node=this->node->next_sibling;
      }
   else { 
      int relative_depth=0;
      upper:
      do {
         this->node=this->node->parent;
         if(this->node==0) return *this;
         --relative_depth;
         } while(this->node->next_sibling==0);
      lower:
      this->node=this->node->next_sibling;
      while(this->node->first_child==0) {
         if(this->node->next_sibling==0)
            goto upper;
         this->node=this->node->next_sibling;
         if(this->node==0) return *this;
         }
      while(relative_depth<0 && this->node->first_child!=0) {
         this->node=this->node->first_child;
         ++relative_depth;
         }
      if(relative_depth<0) {
         if(this->node->next_sibling==0) goto upper;
         else                          goto lower;
         }
      }
   return *this;

// if(this->node->next_sibling!=0) {
//    this->node=this->node->next_sibling;
//    assert(this->node!=0);
//    if(this->node->parent==0 && this->node->next_sibling==0) // feet element
//       this->node=0;
//    }
// else {
//    tree_node *par=this->node->parent;
//    do {
//       par=par->next_sibling;
//       if(par==0) { // FIXME: need to keep track of this!
//          this->node=0;
//          return *this;
//          }
//       } while(par->first_child==0);
//    this->node=par->first_child;
//    }
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--()
   {
   assert(this->node!=0);
   if(this->node->prev_sibling!=0) {
      this->node=this->node->prev_sibling;
      assert(this->node!=0);
      if(this->node->parent==0 && this->node->prev_sibling==0) // head element
         this->node=0;
      }
   else {
      tree_node *par=this->node->parent;
      do {
         par=par->prev_sibling;
         if(par==0) { // FIXME: need to keep track of this!
            this->node=0;
            return *this;
            }
         } while(par->last_child==0);
      this->node=par->last_child;
      }
   return *this;
}

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int)
   {
   fixed_depth_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int)
{
  fixed_depth_iterator copy = *this;
  --(*this);
  return copy;
}

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num)
   {
   while(num>0) {
      --(*this);
      --(num);
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --(num);
      }
   return *this;
   }

// FIXME: add the other members of fixed_depth_iterator.


// Sibling iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator() 
   : iterator_base()
   {
   set_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   set_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other)
   : iterator_base(other.node)
   {
   set_parent_();
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator& other)
   : iterator_base(other), parent_(other.parent_)
   {
   }

template <class T, class tree_node_allocator>
void tree<T, tree_node_allocator>::sibling_iterator::set_parent_()
   {
   parent_=0;
   if(this->node==0) return;
   if(this->node->parent!=0)
      parent_=this->node->parent;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++()
   {
   if(this->node)
      this->node=this->node->next_sibling;
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--()
   {
   if(this->node) this->node=this->node->prev_sibling;
   else {
      assert(parent_);
      this->node=parent_->last_child;
      }
   return *this;
}

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int)
   {
   sibling_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int)
   {
   sibling_iterator copy = *this;
   --(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num)
   {
   while(num>0) {
      --(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const
   {
   tree_node *tmp=parent_->first_child;
   return tmp;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const
   {
   return parent_->last_child;
   }

// Leaf iterator

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator() 
   : iterator_base(0)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(tree_node *tn)
   : iterator_base(tn)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const iterator_base &other)
   : iterator_base(other.node)
   {
   }

template <class T, class tree_node_allocator>
tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const sibling_iterator& other)
   : iterator_base(other.node)
   {
   if(this->node==0) {
      if(other.range_last()!=0)
         this->node=other.range_last();
      else 
         this->node=other.parent_;
      ++(*this);
      }
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++()
   {
   assert(this->node!=0);
   while(this->node->next_sibling==0) {
      if (this->node->parent==0) return *this;
      this->node=this->node->parent;
      }
   this->node=this->node->next_sibling;
   while(this->node->first_child)
      this->node=this->node->first_child;
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--()
   {
   assert(this->node!=0);
   while (this->node->prev_sibling==0) {
      if (this->node->parent==0) return *this;
      this->node=this->node->parent;
      }
   this->node=this->node->prev_sibling;
   while(this->node->last_child)
      this->node=this->node->last_child;
   return *this;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++(int)
   {
   leaf_iterator copy = *this;
   ++(*this);
   return copy;
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--(int)
   {
   leaf_iterator copy = *this;
   --(*this);
   return copy;
   }


template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=(unsigned int num)
   {
   while(num>0) {
      ++(*this);
      --num;
      }
   return (*this);
   }

template <class T, class tree_node_allocator>
typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=(unsigned int num)
   {
   while(num>0) {
      --(*this);
      --num;
      }
   return (*this);
   }

#endif

// Local variables:
// default-tab-width: 3
// End: