So I have a program that reads data/text files, with 1 number on each line and makes a binary search tree from it. When i insert a node, there should be two data variables updated, searchcost and key.. Key being the actual number of the element, and the searchcost is 1+the depth of the node. For data files that have numbers up to a certain amount insert works. But if it has numbers above say..3047 then the insert function gives a stack overflow error. Here is the insert method. Debugging says its coming from

t = new BinaryNode(x,count);

but I have no clue why.

BinaryNode *BinarySearchTree::insert(int x,
BinaryNode *t) 
{
static long count;
count++;



if (t == NULL){ t = new BinaryNode(x,count);
count=0;
}
else if (x < t->key){
	t->left = insert(x, t->left);

}
else if (x > t->key){
	
t->right = insert(x, t->right);

}
else
throw DuplicateItem();
return t;
}

>>static long count;
>>count++;

Your program is incrementing an uninitialized integer and then you use that integer later on in the function. initialize it to 0 static long count = 0;

Edited 6 Years Ago by Ancient Dragon: n/a

>>static long count;
>>count++;

Your program is incrementing an uninitialized integer and then you use that integer later on in the function. initialize it to 0 static long count = 0;

I think it sets it to 0 by default but just for conventions sake I set it to 0. However i still get the stack overflow error. The funny thing is that when I enter in a list with 4095 numbers in random order, it does not give me a stack overflow...however when i enter in a list with 4095 numbers in increasing order (linear tree) it does....I'm pretty confused =/

Post entire code so that we can compile and test it.

Heh...its pretty long... the file 12l is the text document with 4095 numbers in increasing order. Just enter in "12l.txt" when the prompt comes up.

Edited 6 Years Ago by karolik: n/a

Attachments
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
1
#include "BinaryNode.h"

long BinaryNode::size(BinaryNode *t) { //recursive
if (t == NULL)
return 0;
else
return 1 + size(t->left) + size(t->right);
}
int BinaryNode::height(BinaryNode *t) { //recursive
if (t == NULL)
return -1;
else {
int hlf = height(t->left), hrt = height(t->right);
return (hlf > hrt) ? 1+hlf : 1+hrt; // 1+max(hlf,hrt)
}
}
BinaryNode *BinaryNode::copy( ) { // recursive
BinaryNode *root = new BinaryNode(key);
if (left != NULL)
root->left = left->copy();
if (right != NULL)
root->right = right->copy();
return root;
}
#include <cstddef> // to define NULL
#include "RuntimeException.h"
class BinaryNode {
private:
friend class BinaryTree;
friend class BinarySearchTree;

int key;
BinaryNode *left, *right;
long searchcost;
public:
BinaryNode(int el = 0, long el2 =0, BinaryNode *lt = NULL,
BinaryNode *rt = NULL) : //constructor
key(el), searchcost(el2), left(lt), right(rt) { }
// functions
BinaryNode *getLeft() { return left; }
BinaryNode *getRight() { return right; }
int getElem() { return key; }
long size(BinaryNode *t);
int height(BinaryNode *t);
BinaryNode *copy();
};
#include "BinarySearchTree.h"
#include <iostream>
#include "RuntimeException.h"
using namespace std;

BinaryNode *BinarySearchTree::findMin(BinaryNode *t) throw(EmptyTree)

{
if (t == NULL) throw EmptyTree();
while (t->left != NULL) t = t->left;
return t;
}


BinaryNode *BinarySearchTree::find(int x, BinaryNode *t) throw(ItemNotFound )

{
while (t != NULL)
if (x < t->key) t = t->left;
else if (x > t->key) t = t->right;
else return t; // found
throw ItemNotFound();
}
BinaryNode *BinarySearchTree::insert(int x,
BinaryNode *t) 
{
static long count=0;
count++;



if (t == NULL){ t = new BinaryNode(x,count);
count=0;
}
else if (x < t->key){
	t->left = insert(x, t->left);

}
else if (x > t->key){
	
t->right = insert(x, t->right);

}
else
throw DuplicateItem();
return t;
}
BinaryNode *BinarySearchTree::removeMin(BinaryNode *t) throw(ItemNotFound)
 // private function
{
if (t == NULL) throw ItemNotFound();
if (t->left != NULL)
t->left = removeMin(t->left);
else {
BinaryNode *node = t;
t = t->right;
traverse(t);
delete node;
}
return t;
}
void BinarySearchTree::traverse(BinaryNode *t) throw(ItemNotFound)
{

	if(t!=NULL){
		t->searchcost--;
		traverse(t->left);
		traverse(t->right);

	}
}

BinaryNode *BinarySearchTree::remove(int x, BinaryNode *t) throw(ItemNotFound)
 // private function
{ if (t == NULL) throw ItemNotFound();
if (x< t->key)
t->left = remove(x, t->left);
else if (x > t->key)
t->right = remove(x, t->right);
else if (t->left != NULL && t->right != NULL) {
// item x is found; t has two children
t->key = findMin(t->right)->key;
t->right = removeMin(t->right);
} else { //t has only one child

BinaryNode *node = t;
t= (t->left != NULL) ? t->left : t->right;

traverse(t);

delete node;
}
return t;
}
void BinarySearchTree::display(BinaryNode *t) const
{
	if(t)
	{
	display(t->left);
	cout<<t->key<<"["<<t->searchcost<<"] ";
	display(t->right);


	}


}
long BinarySearchTree::sum(BinaryNode *t) const
{
static long count=0;
long count2;
if(t)
	{
	sum(t->left);
	sum(t->right);
	count=count+(t->searchcost);
	if((t->searchcost)==1){
		count2=count;
		count=0;
		return count2;
	}
	}



}
#include "BinaryTree.h"

class BinarySearchTree { // uses the class BinaryNode
private: BinaryNode *root;
// some private functions will be here
public: // constructor
class EmptyTree : public RuntimeException {
	public:
		EmptyTree() : RuntimeException("tree is empty") {}
	};
class ItemNotFound : public RuntimeException {
	public:
		ItemNotFound() : RuntimeException("item not found") {}
	};
class DuplicateItem : public RuntimeException {
	public:
		DuplicateItem() : RuntimeException("duplicate item") {}
	};
BinarySearchTree( ) { root = NULL; }
~BinarySearchTree( ) { BinaryTree::deleteTree(root); root = NULL; }
long size() {return (root==NULL) ? 0 : root->size(root);}
int height() {return (root==NULL) ? 0 : root->height(root);}
BinaryNode* findMin(BinaryNode *t) throw(EmptyTree);
BinaryNode* removeMin(BinaryNode *t) throw(ItemNotFound);
BinaryNode *find(int x,BinaryNode *t) throw(ItemNotFound );
BinaryNode *insert(int x,BinaryNode *t) throw(DuplicateItem);
BinaryNode* remove(int x,BinaryNode *t) throw(ItemNotFound); 
void insert(int x) throw(DuplicateItem)
{ root = insert(x, root); }
void remove(int x) throw(ItemNotFound)
{ root = remove(x, root); }
void traverse(BinaryNode *t) throw(ItemNotFound);
BinaryNode *find(int x) throw(ItemNotFound)
{ return find(x, root); }
bool isEmpty( ){ return root == NULL; }
void display() const
{display(root);}
void display(BinaryNode *t) const;
long sum(BinaryNode *t) const;
long sum() const
{return sum(root);}
};
// merge t1, t2 and root (with rootItem)
#include "BinaryTree.h"
#include <iostream>
using namespace std;



// delete a tree rooted at "root"
void BinaryTree::deleteTree(BinaryNode *root)
{ // postorder traversal
if (root == NULL) return; // nothing to delete
if (root->left != NULL)
deleteTree(root->left);
if (root->right != NULL)
deleteTree(root->right);
delete root;
}
#include "BinaryNode.h"
class BinaryTree {
private:
BinaryNode *root;
friend class BinarySearchTree;
static void deleteTree(BinaryNode *root);
public: // class BinaryTree (cont)
BinaryTree( ) { root = NULL; }
BinaryTree(int el) { root = new BinaryNode(el); }
~BinaryTree() { deleteTree(root); root = NULL; }
// functions
BinaryNode *getRoot( ) { return root; }
bool isEmpty( ) { return root == NULL; }
int size( ) { return (root == NULL) ? 0 :
root->size(root); }
int height( ) { return (root == NULL) ? 0 :
root->height(root); }
void copy(BinaryTree& rhs) {
if (this != &rhs) {
deleteTree(root); // make tree empty
if (rhs.root != NULL) root = rhs.root->copy(); }
}







};
#include "Parser.h"
#include <iostream>
#include "Evaluator.h"
#include <string>
#include <fstream>
#include "BinarySearchTree.h"

using namespace std;
int main() {
	

	
	long sum=0;
	const char *x;
	string filename;
	while(cin>>filename){
	x=filename.c_str();
	ifstream infile;
	infile.open (x, ifstream::in);
	long num;
	string line;
	const char *p;
	BinarySearchTree tree;
	 if (!infile)
     {
          cerr<<"Unable to open file"<<endl;
		           
	 }
	if (infile.is_open()){
		while (getline(infile,line)){
			p=line.c_str();
			num=atol(p);
			cout<<num<<" ";
			tree.insert(num);
			
			
		}
	}
	cout<<endl;	
	tree.display();
	
	
	sum=tree.sum();

	
	cout<<"Total number of nodes: "<<tree.size()<<endl;
	double avg;
	avg=sum/tree.size();
	cout<<"Average Search Cost: "<<avg<<endl;
	}
	return 0;
}

nvm

Oh here are the 4095 numbers in random order...As you can see it works with that.

Attachments
913
2908
509
1002
1698
3624
3393
345
3937
186
221
3040
3741
862
2367
765
2687
2906
3392
1127
2526
1328
3664
1780
2914
2114
944
2415
3129
1081
930
3189
3558
1847
979
429
356
2830
1045
653
4034
3429
824
338
823
962
3997
3302
199
3833
2828
2791
1815
433
3708
3901
2715
2659
1631
505
3648
3696
3963
372
2057
3352
3380
4071
2557
2325
4078
3321
3388
2460
1384
3667
3556
2205
3845
2787
2909
2826
2876
1370
1200
4058
1036
1104
1834
1342
1892
2400
1432
77
2265
3546
2006
1823
1468
3193
3566
1950
629
3075
2053
963
2135
40
3463
31
2116
4054
4041
3593
28
4035
717
3415
93
3006
3405
2736
270
3303
1465
50
2653
701
1109
739
1989
3316
2036
693
1119
2861
3097
2705
1981
2201
416
1020
92
3329
2271
326
8
2898
158
2063
245
1512
1484
3571
1251
3941
19
1881
2567
593
2335
3960
2227
734
2755
3354
3827
3200
3686
1223
1951
1886
1790
367
2226
1315
1585
2208
1857
3719
1156
1100
3754
1571
628
2762
3981
3155
1141
206
2285
216
1157
265
564
1932
4016
3394
3437
635
3192
2004
4043
541
1319
3282
2252
2586
408
557
79
2899
2276
3820
3013
99
3699
4059
3000
2174
1616
2696
3110
4018
1726
2467
569
822
185
3494
1944
3811
1906
3142
3239
1024
3450
3622
2153
456
3616
1080
1065
121
3586
2378
1243
756
978
334
2059
3202
3341
188
1198
2892
2614
3588
1623
2808
1799
1097
806
732
2749
3416
1533
2487
310
2106
23
4006
3731
3052
236
2522
1545
1612
80
1674
3045
1193
1398
3162
3780
2930
2300
991
1199
666
1012
276
3180
1123
859
2316
3037
1938
3114
3908
1331
1879
3505
2108
2647
209
518
227
2042
2022
935
3585
48
2452
1844
2564
2488
1386
2341
1786
2070
325
3640
2778
478
1349
210
1107
702
3718
3363
3853
2811
1110
3898
3691
877
2277
2740
1481
1301
499
648
74
1197
2942
692
1904
2662
2128
1764
1431
484
866
1068
1044
1340
292
86
735
376
1230
2815
70
2139
482
39
1203
3803
3291
2468
3423
1668
1248
258
2223
3250
3568
1244
1641
982
2790
683
2127
3231
3788
3605
3930
2269
1518
3488
4084
2171
1788
3210
3209
3844
3197
1476
725
2281
641
4044
949
1876
652
173
3218
2001
2095
1561
1358
1588
3029
1802
3767
848
81
2162
905
1620
1675
1785
689
1771
805
1191
4007
531
103
729
3564
3800
2515
3550
163
2710
1806
3600
2638
2666
1371
3611
2385
793
1103
1143
3760
3476
1293
3591
1111
1299
1693
2440
3188
3112
2963
727
543
2051
1779
2132
2610
3001
1454
1714
1731
2975
2993
3290
3629
3797
3828
1791
2559
3859
1574
196
3095
1438
1896
883
3829
501
2677
1979
3770
2631
1078
3917
2426
642
2912
2263
2628
794
2745
2445
243
1863
1814
1816
2756
1817
252
118
3794
1697
1519
230
3824
3025
615
826
2577
2954
3211
3875
3053
437
3259
3982
1532
1987
1873
442
3685
599
551
2461
277
3274
3016
3237
96
1239
3384
2309
1690
1954
4023
1959
115
2175
2374
3940
3846
2763
1382
894
542
2547
3061
3641
604
2511
1611
1206
2750
1706
3208
3735
1207
1493
3276
617
1284
3261
1541
34
3284
2199
2447
25
1256
2788
865
2840
643
3487
837
4029
2027
2405
2773
1794
2578
1075
3956
1313
1330
94
3538
349
1647
992
4009
1411
3882
2703
533
29
1030
2554
1543
507
2303
348
3373
933
984
616
2741
1985
3632
2965
767
1421
237
2024
698
336
1086
358
3048
990
993
1173
2853
1241
2009
2350
2060
1034
3426
3655
4091
1467
2473
3289
3804
457
420
1622
1375
1564
1827
2229
2606
1407
746
2494
1898
2475
2676
3743
553
3474
4068
1492
1810
1713
2255
3238
399
1163
1592
1914
2838
3555
3672
1462
516
1715
2054
3410
1346
2283
1660
228
1217
3522
892
2784
3413
203
2234
2584
1744
3172
3589
3775
1357
1567
45
2905
3867
3983
3544
2910
106
1436
907
712
1470
3213
3991
1775
1351
2029
3897
1765
1688
1719
3054
2007
4088
3612
3660
764
849
3530
1196
2834
1506
303
2825
4036
3825
1608
2995
1265
814
279
3272
2123
1071
925
2133
3170
2394
1007
834
2157
2877
3949
4014
3574
2040
246
250
2272
194
2951
3738
514
148
4002
1910
1186
2000
4067
3993
3924
269
3807
1474
314
1395
1098
3857
700
162
224
3003
3674
3205
1645
3420
1084
1170
1915
1125
2777
2712
2270
1326
123
1803
2354
2872
1238
3020
441
1061
1885
3880
2794
197
1664
625
1542
2324
3869
2176
1218
1503
455
2509
733
3962
2121
3367
2184
3042
474
2752
434
1035
2499
2779
2500
337
1686
328
3400
1039
2159
2328
3745
52
1037
2141
3868
1732
1322
3181
3823
3078
3454
1183
3852
453
3247
3812
748
1942
1279
2047
773
1350
2590
3717
1341
1746
2807
2366
2887
1652
1901
1412
2663
2714
931
1763
3734
2311
3601
1128
1968
2237
2398
3771
2194
1905
3724
1166
1354
3712
136
4074
3774
3948
3919
3621
232
851
4083
139
396
114
3668
3438
2934
2347
2661
3570
2753
352
1392
3596
3063
2195
566
3369
3713
1424
804
3445
3953
3365
740
1426
2516
1573
1287
2730
1142
1753
309
2527
2685
2953
2327
2479
298
3176
463
1182
3761
1294
69
2083
3264
3666
1566
1997
3383
596
3597
3467
3964
966
3482
1479
1928
280
3481
890
2211
863
2082
3267
2122
468
3883
3501
2323
2204
1515
290
1466
54
1108
3064
2604
1805
3926
2935
1909
3998
2596
969
3606
2649
3281
3510
1998
3094
3813
1627
1308
3891
138
677
1691
10
3320
4040
2203
3702
3818
771
540
3954
357
3301
2512
3098
1276
425
3733
4079
985
1060
3843
332
1589
3216
2062
3160
3143
784
2929
3580
1842
324
1736
3989
3984
263
3035
3444
443
18
3618
1538
2163
521
481
1864
1773
3776
3008
1073
451
1159
1449
1396
2365
459
1410
1485
423
4027
1378
1095
2532
3293
1502
2810
1511
559
1560
2536
3360
2711
56
3543
2239
2969
3431
2137
2185
2228
1807
3377
1832
1841
3698
268
3736
301
3575
1232
4073
3594
3424
3091
1936
2944
3801
3171
3654
1049
1789
294
830
546
1499
2196
2273
1268
432
737
2849
2945
936
1523
1883
2181
3138
1681
3905
2187
3432
3679
4028
3581
2358
668
2519
3751
595
2670
1116
2931
2412
766
82
815
567
387
1309
1237
1076
1619
3961
893
506
1601
1427
3273
997
4048
3167
488
3390
1254
3521
2947
3058
3747
2403
2381
647
21
3936
340
688
586
1227
1228
3526
3119
2839
2142
1820
3215
1014
3326
1487
3514
2429
1050
1475
302
912
1583
3603
3385
3946
2858
3573
3927
3479
1941
3442
3235
1054
2034
405
1680
3174
315
1604
656
3701
1856
2817
3535
1961
3164
143
3175
1894
1853
911
873
3608
207
3092
2961
3988
1286
3809
3387
755
3028
2101
3088
697
3307
44
61
4053
2820
2989
1665
1855
494
1528
2903
1913
636
808
829
4069
3011
287
2732
973
2384
422
2140
3493
1599
2505
2420
2882
3168
1136
137
3395
3199
49
1787
2695
1266
2925
4031
889
1525
3854
3887
1530
3769
3787
444
2812
244
2018
2087
1778
3974
3322
1632
2372
1552
762
2768
744
2076
1091
3721
1304
3051
1085
1451
4055
2388
2109
502
613
1303
3746
2845
2213
777
2514
663
1130
2537
2605
1162
3714
1835
1325
1333
4089
792
2254
1761
1088
1768
1408
2534
3243
3464
4008
2701
3560
3715
2681
1610
1634
201
3324
293
2551
950
2020
3345
2182
1594
1463
308
3815
667
366
3268
264
3144
3245
741
1139
32
2657
3826
3446
847
3263
1320
3312
1273
855
146
703
248
3478
3406
3925
3626
486
1993
2576
2313
2392
1935
2743
2061
1258
3525
1556
3039
151
1537
2332
2984
1296
3107
267
2260
2709
2236
3337
2688
2371
4030
853
3397
2588
2301
3370
3587
2530
1966
1819
164
2333
1347
2671
220
3295
1051
903
603
1048
2462
3928
466
3335
2979
3889
1960
2134
939
3955
7
1005
3234
2131
2926
1699
532
1356
2250
3673
2241
3814
1526
3019
4004
175
1678
211
1324
58
3240
1348
1752
1593
3357
3462
810
2186
1923
1249
2795
3938
3999
100
2485
2442
788
2972
3553
2816
1490
583
708
409
343
1129
1240
2541
2572
3090
2846
4047
1747
3233
585
2193
2446
3531
3496
3050
846
3141
1743
800
1801
461
2902
3404
787
1181
1770
2297
360
1670
2444
1360
1274
2682
1190
1416
1939
769
2145
1818
781
510
2238
37
631
3739
994
1748
3294
3041
2433
255
2151
817
2334
1684
1428
346
554
3634
3391
1514
363
2894
3132
1535
1029
3503
3319
1548
2262
17
887
3841
1261
440
1500
3561
2678
2071
1215
2875
1661
1374
1696
513
3099
2988
3658
295
3362
528
464
3545
904
1740
2355
843
3076
831
3201
1399
116
1118
3542
1345
3922
2618
3089
3206
2524
2136
3684
3792
614
154
938
517
2099
3473
4070
1312
2150
3856
2742
2802
835
3361
1339
3528
715
2386
11
1443
1154
3972
341
187
915
1201
1550
3681
1126
2383
3047
2338
368
3007
3219
561
916
479
1846
1269
2033
3642
1365
2915
2497
4061
3072
3515
946
1013
3592
860
2482
2562
3106
238
2019
1565
791
3615
1618
3744
3083
1767
1667
811
745
2352
601
3678
2158
856
3100
1072
3665
2179
3349
419
552
3154
390
412
2259
627
72
3816
1861
2990
751
3786
1234
3351
1229
3286
1259
128
1869
749
2597
3966
1912
1756
2391
1164
3540
2393
401
545
3781
26
2973
2744
2401
954
1417
3773
3669
608
165
3347
2668
1363
4038
2716
288
620
602
2292
389
112
51
3551
4063
38
2251
2048
2438
972
36
251
1727
15
2593
1520
3292
2377
2924
2104
3070
353
2881
3044
675
4052
1891
3729
3034
2231
76
2245
2889
354
1650
2077
2351
127
2535
2
2437
1455
549
485
2198
181
2529
3066
2308
3014
1373
1153
1625
3027
3547
2119
286
3309
857
87
46
381
3886
465
3004
4017
910
577
2672
3169
723
2339
1843
1041
219
2550
3692
2747
2950
3399
3755
2561
1769
3026
2478
1336
3885
1052
2940
192
2466
579
403
113
46

You need to also post RuntimeException.h and its corresponding *.cpp file if there is one.

Edited 6 Years Ago by Ancient Dragon: n/a

Whoops, must've missed it.

Attachments
#ifndef RUNTIMEEXCEPTION_H_
#define RUNTIMEEXCEPTION_H_

#include <string>

class RuntimeException {// generic run-time exception
 private:
   std::string errorMsg;
 public:
   RuntimeException(const std::string& err) { errorMsg = err; }
   std::string getMessage() const { return errorMsg; }
};

inline std::ostream& operator<<(std::ostream& out, const RuntimeException& e)
{
   out << e.getMessage();
   return out;
}

#endif

using vc++ 2008 express I get a lot of these warnings
warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

The Parser.h you include in main.cpp -- it that one of your header files? There is also one in the Windows SDK, which is the one that vc++ 2008 express is picking up.

using vc++ 2008 express I get a lot of these warnings
warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

The Parser.h you include in main.cpp -- it that one of your header files? There is also one in the Windows SDK, which is the one that vc++ 2008 express is picking up.

Parser.h is from another project so I don't even need that for my header. Actually i don't need "Evaluator.h" either...both are from another project. About the warnings...I just ignore them since the program works for the most part..

Edited 6 Years Ago by karolik: n/a

One of the problems is that when the current value (509) is less than the value at the root (913) there is no provision to insert the new value at the root of the list. Instead, it inserts it at t->left, but it should be added at t. Learning to format your program better to make it more readable might help you understand what is going on

BinaryNode *BinarySearchTree::insert(int x,
BinaryNode *t) 
{
    static long count=0;
    count++;
    if (t == NULL)
    { 
        t = new BinaryNode(x,count);
        count=0;
    }
// t->key == 913 and x = 509
    else if (x < t->key)
    {
      // this is wrong when x < t->key
	    t->left = insert(x, t->left);

    }
    else if (x > t->key)
    {
	
        t->right = insert(x, t->right);

    }
    else
        throw DuplicateItem();
    return t;
}

One of the problems is that when the current value (509) is less than the value at the root (913) there is no provision to insert the new value at the root of the list. Instead, it inserts it at t->left, but it should be added at t. Learning to format your program better to make it more readable might help you understand what is going on

BinaryNode *BinarySearchTree::insert(int x,
BinaryNode *t) 
{
    static long count=0;
    count++;
    if (t == NULL)
    { 
        t = new BinaryNode(x,count);
        count=0;
    }
// t->key == 913 and x = 509
    else if (x < t->key)
    {
      // this is wrong when x < t->key
	    t->left = insert(x, t->left);

    }
    else if (x > t->key)
    {
	
        t->right = insert(x, t->right);

    }
    else
        throw DuplicateItem();
    return t;
}

Its supposed to insert in the order that the list is given in...So 503 would be to the left of 913. Its not making a linear list with the random list. It works with the random list... However, its screwing up on the other list, 12l.txt...the one that starts with 1 and goes to 4095 in that respective order.

Edited 6 Years Ago by karolik: n/a

Ok, sorry I misunderstood. I won't be able to look at it again for the next 36 hours (sleep + work + honey-do-list).

Thats cool, I understand..Unfortunately for me I need to get this done by Sunday and this is the only me stopping me from finishing it..programming wise. Out of 36 data test files, 12l is the only one that doesn't work =/.

I think i figured out the problem. Its making a 4095 number linear tree, with 4095 recursive calls and thus overflowing the stack....Now how to fix that is the next problem.

Edited 6 Years Ago by karolik: n/a

This question has already been answered. Start a new discussion instead.