-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
160 lines (150 loc) · 4.01 KB
/
bst.cpp
File metadata and controls
160 lines (150 loc) · 4.01 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
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
#include "bst.h"
#include <queue>
#include <iostream>
#include <fstream>
node *BinarySearchTree::insert(int k, node *n, int &cost) {
cost++;
if (n == nullptr) {
numNodes++;
n = new node(k);
n->search_cost = cost;
cout << "Key = " << k << " Search Cost = " << cost << endl;
}
else if (k < n->key)
n->left = insert(k, n->left, cost);
else
n->right = insert(k, n->right, cost);
return n;
}
node *BinarySearchTree::remove_min(node *n) {
if (n->left != nullptr)
n->left = remove_min(n->left);
else {
node *node = n;
n = n->right;
delete node;
}
return n;
}
node *BinarySearchTree::remove(int x, node *n) {
if (n == nullptr) return n;
if (x < n->key)
n->left = remove(x, n->left);
else if (x > n->key)
n->right = remove(x, n->right);
else if (n->left != nullptr && n->right != nullptr) {
n->key = find_min(n->right)->key;
n->right = remove_min(n->right);
numNodes--;
}
else {
node *node = n;
if (n->left != nullptr) {
n = n->left;
}
else {
n = n->right;
}
numNodes--;
delete node;
}
new_cost(root, 1);
return n;
}
void BinarySearchTree::new_cost(node *n, int i) {
if (n == nullptr) return;
new_cost(n->left, i+1);
new_cost(n->right, i+1);
n->search_cost = i;
}
void BinarySearchTree::total_cost(node *root, int &total) {
total = total + root->search_cost;
if (root->get_left() != NULL)
total_cost(root->get_left(), total);
if (root->get_right() != NULL)
total_cost(root->get_right(), total);
}
double BinarySearchTree::avg_cost(BinarySearchTree t) {
int total = 0;
total_cost(t.get_root(), total);
double node_count = numNodes;
return total / node_count;
}
void BinarySearchTree::InOrderTraversal(node *n) {
if (n->left != nullptr)
InOrderTraversal(n->left);
if (numNodes < 17) {
cout << n->key << "[" << n->search_cost << "] ";
}
if (n->right != nullptr)
InOrderTraversal(n->right);
}
void BinarySearchTree::PreOrderTraversal(node *n) {
if (numNodes < 17) {
cout << n->key << "[" << n->search_cost << "] ";
}
if (n->left != nullptr)
PreOrderTraversal(n->left);
if (n->right != nullptr)
PreOrderTraversal(n->right);
}
void BinarySearchTree::PostOrderTraversal(node *n) {
if (n->left != nullptr)
PostOrderTraversal(n->left);
if (n->right != nullptr)
PostOrderTraversal(n->right);
if (numNodes < 17) {
cout << n->key << "[" << n->search_cost << "] ";
}
}
void BinarySearchTree::OutputTree(string filename) {
ofstream ofs(filename);
if (root == nullptr) return;
queue<node *> nq;
vector<int> t;
int lvl = 1;
int itr = 0;
node *x = new node(-1, 0, nullptr, nullptr);
bool flag = true;
nq.push(root);
while (nq) {
node *current = nq.front();
nq.pop();
lvl--;
if (current) {
t.push_back(current->key);
if (current->left == nullptr) {
nq.push(x);
}
else {
flag = false;
nq.push(current->left);
}
if (current->right == nullptr) {
nq.push(x);
}
else {
flag = false;
nq.push(current->right);
}
itr += 2;
}
if (lvl == 0) {
for (int i = 0; i < t.size(); i++) {
if (t[i] == -1) {
ofs << "X ";
}
else {
ofs << t[i] << " ";
}
}
ofs << endl;
if (flag) return;
flag = true;
lvl = itr;
itr = 0;
}
t.clear();
}
delete x;
}