/[hs]/btree/main.cpp
ViewVC logotype

Contents of /btree/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations) (download)
Fri May 2 22:34:16 2003 UTC (16 years, 3 months ago) by jeremyd
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +4 -2 lines
updated for vs.net 2003

1 // Jeremy Drake
2 // Period 2
3 // Binary tree
4 #include <fstream>
5 #include <iostream>
6 #include "fileopen.h"
7 #include "btree.h"
8
9 using namespace std;
10
11 int main()
12 {
13 int choice;
14 ifstream fin;
15 int n;
16 char c;
17 btre t;
18
19 while(1)
20 {
21 cout << "1) Read a file from disk, build the binary tree" << endl;
22 cout << "2) Print the tree" << endl;
23 cout << "3) Search the tree" << endl;
24 cout << "4) Delete from the tree" << endl;
25 cout << "5) Count the nodes in the tree" << endl;
26 cout << "6) Count the leaves in the tree" << endl;
27 cout << "7) Sum of values in the tree" << endl;
28 cout << "8) Mirror the tree" << endl;
29 cout << "9) Height of tree" << endl;
30 cout << "10) Width of tree" << endl;
31 cout << "0) Quit" << endl;
32
33 cin>>choice;
34
35 switch(choice)
36 {
37 case 1:
38
39 OpenFileForReading(fin);
40
41 while(fin >> c)
42 t.insert(c);
43
44 break;
45 case 2:
46 cout << endl << "1) In order" << endl;
47 cout << "2) Pre-order" << endl;
48 cout << "3) Post-order" << endl;
49 cout << "4) In order leaves" << endl;
50 cin>>choice;
51 switch(choice)
52 {
53 case 1:
54 t.inorder(NULL, &btre::display);
55 cout << endl;
56 break;
57 case 2:
58 t.preorder(NULL, &btre::display);
59 cout << endl;
60 break;
61 case 3:
62 t.preorder(NULL, &btre::display);
63 cout << endl;
64 break;
65 case 4:
66 t.inorderLeaves(NULL, &btre::display);
67 cout << endl;
68 break;
69
70 default:
71 cout << "boo" << endl;
72 }
73 break;
74 case 3:
75 cout << "Enter what to search for: ";
76 cin >> n;
77 if(t.search(n))
78 cout << "Found it!" << endl;
79 else
80 cout << "Not found!" << endl;
81 break;
82 case 4:
83 cout << "Enter what to delete: ";
84 cin >> n;
85 if(t.del(n))
86 cout << "Deleted" << endl;
87 else
88 cout << "Not deleted -- Not Found" << endl;
89 break;
90
91 case 5:
92 n=0;
93 t.inorder(NULL, &btre::count, (void *)&n);
94 cout << endl << "# of nodes = " << n << endl;
95 break;
96 case 6:
97 n=0;
98 t.inorderLeaves(NULL, &btre::count, (void *)&n);
99 cout << endl << "# of leaves = " << n << endl;
100 break;
101 case 7:
102 n=0;
103 t.inorder(NULL, &btre::sum, (void *)&n);
104 cout << endl << "sum = " << n << endl;
105 break;
106 case 8:
107 t.postorder(NULL, &btre::mirror, NULL);
108 break;
109 case 9:
110 cout << t.height() << endl;
111 break;
112 case 10:
113 cout << t.width() << endl;
114 break;
115 case 0:
116 return 0;
117
118 default:
119 cout << "something else" << endl;
120 }
121 }
122 return 0;
123 }
124

cvs@jdrake.com
ViewVC Help
Powered by ViewVC 1.1.13