-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBSTDriver.cpp
More file actions
121 lines (82 loc) · 2.48 KB
/
BSTDriver.cpp
File metadata and controls
121 lines (82 loc) · 2.48 KB
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
#include "BinarySearchTree.h"
#include "BinaryTreeIterator.h"
#include "ListArray.h"
using CSC2110::ListArray;
#include "ListArrayIterator.h"
using CSC2110::ListArrayIterator;
#include "CD.h"
using CSC2110::CD;
#include <iostream>
using namespace std;
void deleteCDs(ListArray<CD>* list)
{
ListArrayIterator<CD>* iter = list->iterator();
while(iter->hasNext())
{
CD* cd = iter->next();
delete cd;
}
delete iter;
}
int main()
{
//the unsorted ListArray of cds
ListArray<CD>* cds = CD::readCDs("cds.txt");
int numItems = cds->size();
cout << numItems << endl;
cout << endl;
//test the binary search tree
//insert all of the cds
ListArrayIterator<CD>* iter = cds->iterator();
BinarySearchTree<CD>* bst = new BinarySearchTree<CD>(&CD::compare_items, &CD::compare_keys);
while(iter->hasNext())
{
CD* cd = iter->next();
bst->insert(cd);
}
delete iter;
BinaryTreeIterator<CD>* bst_iter = bst->iterator();
bst_iter->setInorder(); //takes a snapshot of the data
while(bst_iter->hasNext())
{
CD* cd = bst_iter->next();
cd->displayCD();
}
delete bst_iter;
//DO THIS
//display the height of the binary search tree (not minimum height)
//display whether the binary search tree is balanced (should not be balanced)
//create a minimum height binary search tree
BinarySearchTree<CD>* min_bst = bst->minimize();
bst_iter = min_bst->iterator();
//make sure that an inorder traversal gives sorted order
bst_iter->setInorder(); //takes a snapshot of the data
while(bst_iter->hasNext())
{
CD* cd = bst_iter->next();
cd->displayCD();
}
delete bst_iter;
//DO THIS
//display the height of the binary search tree (should be minimum height)
//display whether the binary search tree is balanced (should be balanced)
//create a complete binary search tree
BinarySearchTree<CD>* complete_bst = bst->minimizeComplete();
delete bst;
//make sure that an inorder traversal gives sorted order
bst_iter = complete_bst->iterator();
bst_iter->setInorder(); //takes a snapshot of the data
while(bst_iter->hasNext())
{
CD* cd = bst_iter->next();
cd->displayCD();
}
delete bst_iter;
//DO THIS
//display the height of the complete binary search tree (should be minimum height)
//display whether the binary search tree is balanced (should be balanced)
delete complete_bst;
deleteCDs(cds);
delete cds;
return 0;
}