└── AVLTree
├── AVLTree
├── AVLTree.vcxproj.filters
├── set.h
├── AVLTree.vcxproj
├── avl.h
├── driver.cpp
└── bst.h
└── AVLTree.sln
/AVLTree/AVLTree/AVLTree.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Header Files
20 |
21 |
22 | Header Files
23 |
24 |
25 | Header Files
26 |
27 |
28 |
29 |
30 | Source Files
31 |
32 |
33 |
--------------------------------------------------------------------------------
/AVLTree/AVLTree.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2010
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AVLTree", "AVLTree\AVLTree.vcxproj", "{2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Debug|x64.ActiveCfg = Debug|x64
17 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Debug|x64.Build.0 = Debug|x64
18 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Debug|x86.ActiveCfg = Debug|Win32
19 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Debug|x86.Build.0 = Debug|Win32
20 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Release|x64.ActiveCfg = Release|x64
21 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Release|x64.Build.0 = Release|x64
22 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Release|x86.ActiveCfg = Release|Win32
23 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {222AE0CC-2CF2-400E-B47C-E784B1463D82}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/AVLTree/AVLTree/set.h:
--------------------------------------------------------------------------------
1 | #include "avl.h"
2 | using namespace std;
3 | namespace PB_BST
4 | {
5 | template
6 | class set : public avl
7 | {
8 | public:
9 | using avl::avl;
10 | bool insert(T d) { return avl::insert(d, this->root); }
11 | void print(ostream& out)const { this->print(this->root, out); }
12 | void print(node* cur, ostream& out) const;
13 | void printXlevel(ostream& out) const { this->printXlevel(this->root, out); }
14 | void printXlevel(node* cur, ostream& out) const;
15 | bool remove(T noderemove);
16 | bool isMember(T searchNode);
17 | set Union(set rhs);
18 | set intersection(set rhs);
19 |
20 | ~set() { this->delTree(); }
21 | protected:
22 | node* &getroot() { return this->root; }
23 | };
24 |
25 |
26 | template
27 | void set::print(node* cur, ostream& out) const
28 | {
29 | bst::print(cur, out);
30 | }
31 |
32 | template
33 | void set::printXlevel(node* cur, ostream& out) const
34 | {
35 | bst::printXlevel(cur, out);
36 | }
37 |
38 | template
39 | bool set::remove(T noderemove)
40 | {
41 | try {
42 | this->popFirstOf(noderemove);
43 | }
44 | catch (invalid_argument) {
45 | return false;
46 | }
47 | return true;
48 | }
49 |
50 | template
51 | bool set::isMember(T searchNode)
52 | {
53 | node* marker = nullptr;
54 | bst::findFirstOf(searchNode, getroot(), marker);
55 | return marker != nullptr;
56 | }
57 |
58 |
59 | template
60 | set set::Union(set rhs)
61 | {
62 | set newTree = *this;
63 | newTree.addTree(rhs.getroot());
64 | return newTree;
65 | }
66 |
67 | template
68 | set set::intersection(set rhs)
69 | {
70 | set newTree;
71 | set tmp = rhs;
72 | while (!tmp.isempty())
73 | {
74 | T tmpValue = bst::popNode(tmp.getroot());
75 | if (isMember(tmpValue))
76 | newTree.insert(tmpValue);
77 | }
78 | return newTree;
79 | }
80 | }
--------------------------------------------------------------------------------
/AVLTree/AVLTree/AVLTree.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 15.0
23 | {2D43A6F7-9267-4E2B-AD2E-FB4466B95FEC}
24 | AVLTree
25 | 10.0.16299.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v141
32 | MultiByte
33 |
34 |
35 | Application
36 | false
37 | v141
38 | true
39 | MultiByte
40 |
41 |
42 | Application
43 | true
44 | v141
45 | MultiByte
46 |
47 |
48 | Application
49 | false
50 | v141
51 | true
52 | MultiByte
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | Disabled
77 | true
78 | true
79 |
80 |
81 |
82 |
83 | Level3
84 | Disabled
85 | true
86 | true
87 |
88 |
89 |
90 |
91 | Level3
92 | MaxSpeed
93 | true
94 | true
95 | true
96 | true
97 |
98 |
99 | true
100 | true
101 |
102 |
103 |
104 |
105 | Level3
106 | MaxSpeed
107 | true
108 | true
109 | true
110 | true
111 |
112 |
113 | true
114 | true
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
--------------------------------------------------------------------------------
/AVLTree/AVLTree/avl.h:
--------------------------------------------------------------------------------
1 | #ifndef AVL_H
2 | #define AVL_H
3 | //--------------------------------------------------------------------
4 | // An AVL tree class
5 | // Compiled under MS Visual C++.Net 2005
6 | // by Paul Bladek
7 | // May, 2008
8 | //--------------------------------------------------------------------
9 |
10 | #include "bst.h"
11 | using namespace std;
12 |
13 |
14 | namespace PB_BST
15 | {
16 | //--------------------------------------------------------------------
17 | // AVL Tree
18 | // Class: avl: public bst
19 | // REQUIRES Type T be able convert from int & have < & == defined
20 | //
21 | // File: avl.h
22 | // Title: avl template Class -- inherits from bst
23 | // Description: This file contains the class definition for avl
24 | //
25 | // Programmer: Paul Bladek
26 | //
27 | // Date: May 2008
28 | // Version: 1.0
29 | //
30 | // Environment: Hardware: IBM Pentium
31 | // Software: Windows XP or .Net Framework for execution;
32 | //
33 | // Compiles under Microsoft Visual C++.Net 2005
34 | //
35 | // class avl:
36 | //
37 | //
38 | // Methods:
39 | //
40 | // inline:
41 | // avl() -- default constructor
42 | // avl(const avl& t) -- copy constructor (deep copy)
43 | // avl& operator+=(const T d) -- adds an element to the tree
44 | // avl operator+(const T d) -- adds an element to the tree
45 | // void insert(T d) -- adds an element to the tree
46 | // ~avl() -- destructor
47 | //
48 | // non-inline:
49 | // avl& operator=(const avl& t) -- deep copy
50 | // avl& operator+=(const avl& t) -- adds an element to the tree
51 | // void insert(T d, node* &cur) -- adds an element to the tree
52 | // T popnode(node* &cur) -- returns element in node deleted
53 | // T poplow(node* &cur) -- returns element in leftmost node (deleted)
54 | // T popfirst(const T& d, node* np)
55 | // -- returns element in first node matching d (deleted)
56 | // protected:
57 | // node* rotateRight(node *nodeN) -- balances tree
58 | // node* rotateLeft(node *nodeN) -- balances tree
59 | // node* rotateRightLeft(node *nodeN) -- balances tree
60 | // node* rotateLeftRight(node *nodeN) -- balances tree
61 | // node* rebalance(node *&nodeN) -- balances tree
62 | // int getHeightDifference(const node *const nodeN)const
63 | // -- returns the height difference between the left & right subtrees
64 | //
65 | //
66 | // History Log:
67 | // May 20, 2008, PB completed version 1.0
68 | //-------------------------------------------------------------------
69 | template
70 | class avl: public bst
71 | {
72 | public:
73 | avl() : bst(){}
74 | avl(const avl& t) : bst(t){}
75 | avl& operator=(const avl& t);
76 | avl& operator+=(const avl& t);
77 | avl& operator+=(const T d){insert(d); return *this;}
78 | avl operator+(const T d)
79 | {avl temp = *this; temp.insert(d); return temp;}
80 | bool insert(T d) { return insert(d, bst::root);}
81 | bool insert(T d, node* &cur);
82 | T popnode(node* &cur);
83 | T poplow(node* &cur);
84 | T popfirst(const T& d, node* np);
85 | ~avl() { bst::delTree();}
86 | protected:
87 | node* rotateRight(node *nodeN);
88 | node* rotateLeft(node *nodeN);
89 | node* rotateRightLeft(node *nodeN);
90 | node* rotateLeftRight(node *nodeN);
91 | node* rebalance(node *&nodeN);
92 | int getHeightDifference(const node *const nodeN)const;
93 | };
94 |
95 | //////////////////////////////////////////////////////////
96 | // Define function - inline function
97 | //////////////////////////////////////////////////////////
98 | template
99 | inline avl& avl::operator=(const avl& t)
100 | {
101 | if (this != &t)
102 | {
103 | // Check current tree is empty or not. if not empty, clear tree
104 | if (!this->isempty())
105 | this->delTree(bst::root);
106 | // Check the source tree empty or not
107 | if (!t.isempty())
108 | {
109 | bst::root = new node(*(t.root));
110 | }
111 | }
112 | return *this;
113 | }
114 |
115 |
116 | template
117 | inline avl& avl::operator+=(const avl& t)
118 | {
119 | avl tmp = t;
120 | while (!tmp.isempty())
121 | {
122 | T tmpValue = bst::popNode(tmp.getroot());
123 | insert(tmpValue, this->root);
124 | }
125 | tmp.delTree();
126 | return *this;
127 | }
128 |
129 | template
130 | inline bool avl::insert(T d, node* &cur)
131 | {
132 | if (cur != nullptr)
133 | {
134 | if (d < cur->value())
135 | {
136 | insert(d, cur->left);
137 | rebalance(cur);
138 | }
139 | else if (d > cur->value())
140 | {
141 | insert(d, cur->right);
142 | rebalance(cur);
143 | }
144 | else
145 | {
146 | // Node was existed. Return false
147 | return false;
148 | }
149 | if (bst::root != nullptr)
150 | {
151 | bst::root->setHeight();
152 | }
153 | }
154 | else
155 | {
156 | cur = new node(d);
157 | if (this->isempty())
158 | {
159 | bst::root = cur;
160 | }
161 | }
162 | return true;
163 | }
164 |
165 | template
166 | T avl::popnode(node* &cur)
167 | {
168 | T conents = bst::popNode(cur);
169 | rebalance(cur);
170 | return conents;
171 | }
172 |
173 | template
174 | T avl::poplow(node* &cur)
175 | {
176 | T conents = bst::popLow(cur);
177 | rebalance(cur);
178 | return conents;
179 | }
180 |
181 | template
182 | T avl::popfirst(const T& d, node* np)
183 | {
184 | node* parent = *bst::parentptr;
185 | T contents = bst::popFirstOf(d, np);
186 | rebalance(np);
187 | if (!contents)
188 | {
189 | return 0;
190 | }
191 | return contents;
192 | }
193 |
194 | template
195 | node* avl::rotateRight(node *nodeN)
196 | {
197 | node *tmp;
198 | tmp = nodeN->left;
199 | nodeN->left = tmp->right;
200 | tmp->right = nodeN;
201 | nodeN->setHeight();
202 | tmp->setHeight();
203 | return tmp;
204 | }
205 |
206 | template
207 | node* avl::rotateLeft(node *nodeN)
208 | {
209 | node *tmp;
210 | tmp = nodeN->right;
211 | nodeN->right = tmp->left;
212 | tmp->left = nodeN;
213 | nodeN->setHeight();
214 | tmp->setHeight();
215 | return tmp;
216 | }
217 |
218 | template
219 | node* avl::rotateRightLeft(node *nodeN)
220 | {
221 | node *tmp;
222 | tmp = nodeN->right;
223 | nodeN->right = rotateRight(tmp);
224 | return rotateLeft(nodeN);
225 | }
226 |
227 | template
228 | node* avl::rotateLeftRight(node *nodeN)
229 | {
230 | node *tmp;
231 | tmp = nodeN->left;
232 | nodeN->left = rotateLeft(tmp);
233 | return rotateRight(nodeN);
234 | }
235 |
236 | template
237 | node* avl::rebalance(node *&nodeN)
238 | {
239 | int balance = getHeightDifference(nodeN);
240 | if (nodeN == NULL)
241 | return nodeN;
242 | if (balance > 1)
243 | {
244 | if (getHeightDifference(nodeN->left) > 0)
245 | {
246 | nodeN = rotateRight(nodeN);
247 | }
248 | else
249 | {
250 | nodeN = rotateLeftRight(nodeN);
251 | }
252 | }
253 | else if (balance < -1)
254 | {
255 | if (getHeightDifference(nodeN->right) < 0)
256 | {
257 | nodeN = rotateLeft(nodeN);
258 | }
259 | else
260 | {
261 | nodeN = rotateRightLeft(nodeN);
262 | }
263 | }
264 | return nodeN;
265 | }
266 |
267 | template
268 | int avl::getHeightDifference(const node *const nodeN)const
269 | {
270 | int leftHeight = 0;
271 | int rightHeight = 0;
272 | if (nodeN == nullptr)
273 | return 0;
274 | if (nodeN->left != nullptr)
275 | leftHeight = (nodeN->left)->getHeight();
276 |
277 | if (nodeN->right != nullptr)
278 | rightHeight = (nodeN->right)->getHeight();
279 |
280 | return leftHeight - rightHeight;
281 | }
282 | }
283 | #endif
284 |
--------------------------------------------------------------------------------
/AVLTree/AVLTree/driver.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "set.h"
4 | using namespace std;
5 |
6 | //--------------------------------------------------------------------
7 | // Test Driver for bst and node classes
8 | // Compiled under MS Visual C++.Net 20013, Windows 7
9 | // by Paul Bladek
10 | // December, 2000
11 | // revised April, 2005
12 | // revised June, 2015
13 | //--------------------------------------------------------------------
14 | int main(void)
15 | {
16 | using PB_BST::bst;
17 | using PB_BST::node;
18 | using PB_BST::avl;
19 | using PB_BST::set;
20 | try
21 | {
22 | //avl tree;
23 | //node* marker = nullptr;
24 | //char c;
25 | //cout << "enter some characters, followed by .\n" ;
26 | //do
27 | //{
28 | // cin.get(c);
29 | // if(c == '\n')
30 | // break;
31 | // tree.insert(c);
32 | //}
33 | //while(true);
34 | ////
35 | //tree.printXlevel(cout);
36 | //tree.popFirstOf('D', tree.getroot());
37 |
38 | //tree.printXlevel(cout);
39 | ////
40 | //avl tree2 = tree; // test overloaded =
41 | //tree2 += tree; // test overloaded +
42 | //cout << "tree 1:" << endl;
43 | //cout << tree << " : " << tree.getNumberOfNodes()
44 | // << "nodes" << endl; // test overloaded << and print()
45 | //tree.printXlevel(cout);
46 | //cout << endl << "tree 2:" << tree2
47 | // << tree.getNumberOfNodes() << "nodes" << endl;
48 | //tree2.printXlevel(cout);
49 | //cout << endl << endl << "Enter character to match: ";
50 | //cin >> c;
51 | //tree.findFirstOf(c, tree.getroot(), marker);
52 |
53 | //// set marker to first match of c
54 | //if(marker != nullptr)
55 | // cout << marker->value();
56 | //else
57 | // cout << "not found";
58 | //cout << "\n Enter character to delete: ";
59 | //cin >> c;
60 | //cin.ignore(FILENAME_MAX, '\n');
61 | //c = tree.popFirstOf(c);
62 | //
63 | //// test popFirstOf(), popnode(), poplow()
64 | //cout << "<" << c << "> " << tree << endl;
65 | //tree.delTree(); // delete first tree
66 | //tree2 += 'Z'; // add in another element , test +=
67 | //tree2 = tree2 + 'Y'; // test +
68 | //cout << tree2 << endl;
69 | //cout << "tree 1:" << endl;
70 | //cout << tree << " : " << tree.getNumberOfNodes()
71 | // << "nodes" << endl; // test overloaded << and print()
72 | //tree.printXlevel(cout);
73 | //cout << endl << "tree 2:" << tree2
74 | // << tree.getNumberOfNodes() << "nodes" << endl;
75 | //tree2.printXlevel(cout);
76 | //cout << endl;
77 | //tree.delTree(); // delete second tree
78 |
79 | avl tree1;
80 | node* marker1 = nullptr;
81 | char c;
82 | cout << "****************** TEST AVL TREE *****************" << endl;
83 | cout << "=====Test INSERT function =======" << endl;
84 | cout << "Insert the first Tree:" << endl;
85 | cout << "enter some characters, followed by .\n" ;
86 | do
87 | {
88 | cin.get(c);
89 | //cout << c << endl;
90 | if(c == '\n')
91 | break;
92 | tree1.insert(c);
93 | }
94 | while(true);
95 | cout << "First tree after insert all node: " << endl;
96 | tree1.printXlevel(cout);
97 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
98 | cout << endl << "=====Test Operator + ======" << endl;
99 | avl tree2;
100 | node* marker2 = nullptr;
101 | cout << endl << "**Add more node: " << endl;
102 | cout << "Enter value of new node: ";
103 | cin.get(c);
104 | cin.ignore(256, '\n');
105 | tree1 += c;
106 | cout << "New tree: " << endl;
107 |
108 | tree1.printXlevel(cout);
109 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
110 |
111 | cout << endl << "**Insert the second Tree:" << endl;
112 | cout << "enter some characters, followed by .\n";
113 | do
114 | {
115 | cin.get(c);
116 | //cout << c << endl;
117 | if (c == '\n')
118 | break;
119 | tree2.insert(c);
120 | } while (true);
121 | cout << "Second tree after insert all node: " << endl;
122 | tree2.printXlevel(cout);
123 | cout << tree2 << " : " << tree2.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
124 |
125 | cout << endl << "AVL tree Total: FirstTree += SecondTree" << endl;
126 | tree1 += tree2;
127 | tree1.printXlevel(cout);
128 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
129 |
130 | cout << endl << "===== Test popnode =====" << endl;
131 | cout << "**Pop root node." << endl;
132 | node* tmpNode = tree1.getroot();
133 | tree1.popnode(tmpNode);
134 | cout << "New tree after pop root node: " << endl;
135 | tree1.printXlevel(cout);
136 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
137 |
138 | cout << endl << "**Pop low node." << endl;
139 | tmpNode = tree1.getroot();
140 | tree1.poplow(tmpNode);
141 | cout << "New tree after pop low node: " << endl;
142 | tree1.printXlevel(cout);
143 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
144 |
145 | cout << endl << "**Pop popfirst node." << endl;
146 | tmpNode = tree1.getroot();
147 | cout << "Enter the node want to pop from tree: ";
148 | cin.get(c);
149 | cin.ignore(256, '\n');
150 | tree1.popfirst(c, tmpNode);
151 | cout << "New tree after pop node: " << c << endl;
152 | tree1.printXlevel(cout);
153 | cout << tree1 << " : " << tree1.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
154 |
155 | cout << "******************END TEST AVL TREE *****************" << endl << endl;
156 |
157 | cout << "****************** TEST SET TREE *****************" << endl;
158 | set testSet = set();
159 | cout << "enter some characters, followed by .\n";
160 | do
161 | {
162 | cin.get(c);
163 | //cout << c << endl;
164 | if (c == '\n')
165 | break;
166 | testSet.insert(c);
167 | } while (true);
168 | cout << "The set tree" << c << endl;
169 | testSet.printXlevel(cout);
170 | cout << testSet << " : " << testSet.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
171 |
172 | cout << endl << "**Test isMember function" << endl;
173 | cout << "Enter the character want to check exist in set: ";
174 | cin.get(c);
175 | cin.ignore(256, '\n');
176 | if (testSet.isMember(c) == true)
177 | {
178 | cout << "Character " << c << " is the member of set" << endl;
179 | }
180 | else
181 | {
182 | cout << "Character " << c << " is NOT member of set" << endl;
183 | }
184 |
185 | cout << endl << "**Test Union function" << endl;
186 | cout << "Union selft: " << endl;
187 | set newSet = testSet.Union(testSet);
188 |
189 | cout << "The new set: " << c << endl;
190 | newSet.printXlevel(cout);
191 | cout << newSet << " : " << newSet.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
192 |
193 | set newSet2 = set();
194 | cout << endl << "Enter new set. \nenter some characters, followed by .\n";
195 | do
196 | {
197 | cin.get(c);
198 | //cout << c << endl;
199 | if (c == '\n')
200 | break;
201 | newSet2.insert(c);
202 | } while (true);
203 |
204 | cout << "The new set input: " << c << endl;
205 | newSet2.printXlevel(cout);
206 | cout << newSet2 << " : " << newSet2.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
207 |
208 | set newSet3 = testSet.Union(newSet2);
209 | cout << "The union new set: " << c << endl;
210 | newSet3.printXlevel(cout);
211 | cout << newSet3 << " : " << newSet3.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
212 |
213 | cout << endl << "** Test intersection function" << endl;
214 | set newSet4 = testSet.intersection(newSet2);
215 | cout << "The intersection new set: " << c << endl;
216 | newSet4.printXlevel(cout);
217 | cout << newSet4 << " : " << newSet4.getNumberOfNodes() << " nodes" << endl; // test overloaded << and print()
218 | cout << "****************** END TEST SET TREE *****************" << endl;
219 | }
220 | catch(invalid_argument e)
221 | {
222 | cout << "Exception: " << e.what() << endl;
223 | cout << "press \"Enter\" to continue" << endl;
224 | cin.get(); // keep window open
225 | return EXIT_FAILURE;
226 | }
227 | catch(bad_alloc e)
228 | {
229 | cout << "Exception: " << e.what() << endl;
230 | cout << "press \"Enter\" to continue" << endl;
231 | cin.get(); // keep window open
232 | return EXIT_FAILURE;
233 | }
234 | catch(exception e)
235 | {
236 | cout << "Exception: " << e.what() << endl;
237 | cout << "press \"Enter\" to continue" << endl;
238 | cin.get(); // keep window open
239 | return EXIT_FAILURE;
240 | }
241 | cout << "press \"Enter\" to continue" << endl;
242 | cin.get(); // keep window open
243 | return EXIT_SUCCESS;
244 | }
245 |
246 |
--------------------------------------------------------------------------------
/AVLTree/AVLTree/bst.h:
--------------------------------------------------------------------------------
1 |
2 | #ifndef BST_H
3 | #define BST_H
4 | //--------------------------------------------------------------------
5 | // A rudimentary Binary Search Tree program
6 | // Compiled under MS Visual C++.Net 2005, 2010, 2013
7 | // allows duplicates
8 | // by Paul Bladek
9 | // December, 2000
10 | // revised April, 2005
11 | // revised May, 2008
12 | // revised May, 2012
13 | // revised June, 2013
14 | //--------------------------------------------------------------------
15 |
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 |
24 | using namespace std;
25 |
26 | namespace PB_BST
27 | {
28 | // CLASS DEFINITIONS
29 | template
30 | //--------------------------------------------------------------------
31 | // BST NODE
32 | // REQUIRES Type T be able to convert from int & have < & == defined
33 | //--------------------------------------------------------------------
34 | class node
35 | {
36 | public:
37 | node(T d = 1) : m_data(d), m_height(1), left(nullptr),
38 | right(nullptr){}
39 | node(const node& n); // Copy Constructor
40 | node& operator=(const node& n);
41 | T value() const {return m_data;} // Accessor
42 | operator T() const {return m_data;} // cast to data type
43 | void setdata(T d) {m_data = d;}
44 | int getHeight() const {return m_height;}
45 | int setHeight();
46 | private:
47 | T m_data;
48 | int m_height;
49 | public: // to freely use these
50 | node* left;
51 | node* right;
52 | };
53 |
54 | //--------------------------------------------------------------------
55 | // COPY CONSTRUCTOR
56 | // throws bad_alloc
57 | //--------------------------------------------------------------------
58 | template
59 | node::node(const node& n)
60 | : m_data(n.m_data), m_height(n.getHeight()), left(nullptr),
61 | right(nullptr)
62 | {
63 | if(n.left != nullptr)
64 | left = new node(*(n.left));
65 | if(n.right != nullptr)
66 | right = new node(*(n.right));
67 | }
68 |
69 | //--------------------------------------------------------------------
70 | // = operator
71 | // throws bad_alloc
72 | //--------------------------------------------------------------------
73 | template
74 | node& node::operator=(const node& n) // overloaded =
75 | {
76 | if(this != &n)
77 | {
78 | m_data = n.m_data;
79 | m_height = n.getHeight();
80 | if(n.left != nullptr)
81 | left = new node(*(n.left));
82 | else
83 | left = nullptr;
84 | if(n.right != nullptr)
85 | right = new node(*(n.right));
86 | else
87 | right = nullptr;
88 | }
89 | return *this;
90 | }
91 |
92 |
93 | //--------------------------------------------------------------------
94 | // recursively sets the Height of the node
95 | //--------------------------------------------------------------------
96 | template
97 | int node::setHeight()
98 | {
99 | int lHeight = 0;
100 | int rHeight = 0;
101 | m_height = 1;
102 |
103 | if(left != nullptr)
104 | lHeight = left->setHeight();
105 | if(right != nullptr)
106 | rHeight = right->setHeight();
107 | return (m_height += (lHeight > rHeight) ? lHeight : rHeight);
108 | }
109 |
110 | //--------------------------------------------------------------------
111 | // Binary Search Tree -- Basic Implementation
112 | //--------------------------------------------------------------------
113 | template
114 | class bst
115 | {
116 | public:
117 | bst() : root(nullptr), parentptr(&root) {}
118 | bst(const bst& t) : root(nullptr), parentptr(&root)
119 | {if(t.root != nullptr) root = new node(*(t.root));}
120 |
121 | node* &getroot() {return root;}
122 |
123 | bool isempty() const {return (root == nullptr);}
124 |
125 | bst& operator=(const bst& t);
126 | bst& operator+=(const bst& t);
127 | bst& operator+=(const T d){insert(d, root); return *this;}
128 | bst operator+(const T d) {bst temp = *this;
129 | temp.insert(d, temp.root); return temp;}
130 | void findFirstOf(const T& d, node* &np, node* &match);
131 | virtual bool insert(T d);
132 | void delTree() {delTree(root);}
133 | void print(ostream& out)const {print(root, out);}
134 | void print(node* cur, ostream& out) const;
135 | void printXlevel(ostream& out) const {printXlevel(root, out);}
136 | void printXlevel(node* cur, ostream& out) const;
137 | //void print(node* cur, ostream& out, int level2print) const;
138 | T popNode(node* &cur);
139 | T popLow(node* &cur);
140 | T popHigh(node* &cur);
141 | T popFirstOf(const T& d) {return popFirstOf(d, root);}
142 | T popFirstOf(const T& d, node*& np);
143 | int getHeight() const
144 | {if(isempty()) return 0; return root->getHeight();}
145 | void setHeight() {if(root != nullptr) root->setHeight();}
146 |
147 | void setLevel(node* cur, vector& levelVector,
148 | int level2print, int position = 0) const;
149 | int getNumberOfNodes() const {return getNumberOfNodes(root);}
150 | int getNumberOfNodes(node* np) const;
151 | ~bst(){delTree(root);}
152 | protected:
153 | virtual bool insert(T d, node* &cur);
154 | node* root; // root of this tree
155 | node** parentptr; // holding pointer needed by some functions
156 |
157 | virtual void addTree(const node* np); // used by +
158 | protected:
159 | void delTree(node* &cur);
160 | };
161 |
162 | //--------------------------------------------------------------------
163 | // overloaded =
164 | // throws bad_alloc
165 | //--------------------------------------------------------------------
166 | template
167 | bst& bst::operator=(const bst& t)
168 | {
169 | if(this != &t)
170 | {
171 | if(!isempty())
172 | delTree(root);
173 | if(!t.isempty())
174 | {
175 | root = new node(*(t.root));
176 | }
177 | }
178 | return *this;
179 | }
180 |
181 | //--------------------------------------------------------------------
182 | // overloaded +=
183 | // throws bad_alloc
184 | //--------------------------------------------------------------------
185 | template
186 | bst& bst::operator+=(const bst& t)
187 | {
188 | addTree(t.root);
189 | return *this;
190 | }
191 |
192 | //--------------------------------------------------------------------
193 | // recursively adds in the contents of a second tree
194 | //--------------------------------------------------------------------
195 | template
196 | void bst::addTree(const node* np)
197 | {
198 | if(np != nullptr)
199 | {
200 | addTree(np->left);
201 | addTree(np->right);
202 | insert(np->value(), root);
203 | }
204 | }
205 |
206 | //--------------------------------------------------------------------
207 | // recursively finds the first occurance of a data item
208 | // pre: match must be set to nullptr
209 | //--------------------------------------------------------------------
210 | template
211 | void bst::findFirstOf(const T& d, node* &np, node* &match)
212 | {
213 | if(match != nullptr)
214 | return;
215 | if(np != nullptr)
216 | {
217 | findFirstOf(d, np->left, match);
218 | if(d == np->value())
219 | {
220 | match = np;
221 | parentptr = &np;
222 | return;
223 | }
224 | findFirstOf(d, np->right, match);
225 | }
226 | }
227 |
228 | //--------------------------------------------------------------------
229 | // inserts a new element
230 | // into the tree
231 | //--------------------------------------------------------------------
232 | template
233 | bool bst::insert(T d)
234 | {
235 | return insert(d, root);
236 | }
237 | //--------------------------------------------------------------------
238 | // inserts a new element
239 | // into the tree
240 | // throws bad_alloc
241 | //--------------------------------------------------------------------
242 | template
243 | bool bst::insert(T d, node* &cur)
244 | {
245 | if(cur == nullptr)
246 | {
247 | cur = new node(d);
248 | if(isempty())
249 | root = cur;
250 | }
251 | else
252 | {
253 | if (d < cur->value())
254 | insert(d, cur->left);
255 | else if (d > cur->value())
256 | insert(d, cur->right);
257 | else
258 | // MODIFY: Don't allow insert duplicate node.
259 | return false;
260 | if(root != nullptr)
261 | root->setHeight();
262 | }
263 | return true;
264 | }
265 |
266 | //--------------------------------------------------------------------
267 | // recursively prints out the tree inorder
268 | //--------------------------------------------------------------------
269 | template
270 | void bst::print(node* cur, ostream& out) const
271 | {
272 | if(cur != nullptr)
273 | {
274 | print(cur->left, out);
275 | out << cur->value() << "(" << cur->getHeight() << ") ";
276 | print(cur->right, out);
277 | }
278 | }
279 |
280 | //------------------------------------------------------------------------
281 | // recursuively sets levelVector to represent the specified level
282 | //------------------------------------------------------------------------
283 | template