May 9, 2013 at 1:21am UTC
For my BST-tree based map, whenever I load in my data from a file, it reads everything in, but the only thing that actually stays in the tree is the most recent element read. I don't know why it's doing this, as when I was dealing with integers, it read in and balanced them correctly. Can anyone identify the problem?
File I'm reading in:
http://www.june29.com/IDP/files/Spanish.txt
Code for reading in the file:
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
void Map<K,V>::loadFromFile(string file) {
ifstream tmp;
K tmp1;
V tmp2;
tmp.open(file.c_str());
string discard;
string junk;
tmp >> discard;
cout << discard << endl;
while (true ) {
getline(tmp,junk);
if (junk == discard)
break ;
}
int i = 0;
while (!tmp.eof()) {
if (i == 5)
break ;
tmp >> tmp1;
getline(tmp, tmp2);
insert(tmp1, tmp2);
++i;
}
tmp.close();
}
Insertion:
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
template <typename K, typename V>
void Map<K,V>::insert(const K& k, const V& v) {
if (key !="" ) {
key = k;
value = v;
return ;
}
if (k == key)
return ;
if (k < key)
if (left != NULL)
left->insert(k, v);
else {
left = new Map<K,V>;
left->key = k;
left->value = v;
left->left = NULL;
left->right =NULL;
}
else
if (right != NULL)
right->insert(k,v);
else {
right = new Map<K,V>;
right->key = k;
right->value = v;
right->left = NULL;
right->right =NULL;
}
balance();
}
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
Left Rotation + Right Rotation + Balance function:
template <typename K, typename V>
void Map<K,V>::shiftLeft() {
//so much work to maintain access to the root node
if (!left)
return ;
Map* tmp1 = right;
Map* tmp2 = left->right;
right = NULL;
left->right = NULL;
swap(left);
right = left;
left = left->left;
right->right = tmp1;
right->left = tmp2;
}
template <typename K, typename V>
void Map<K,V>::shiftRight() {
if (!right)
return ;
Map* tmp1 = right->left;
Map* tmp2 =left;
right->left = NULL;
left = NULL;
swap(right);
left = right;
right = right-> right;
right->left = tmp2;
right->right = tmp1;
}
template <typename K, typename V>
void Map<K,V>::balance() {
if (!this )
return ;
if (balanceFactor() == -2)
if (right->balanceFactor() == -1)
shiftLeft();
else if (right->balanceFactor() == 1) {
right->shiftRight();
shiftLeft();
}
else if (right->balanceFactor() == -2)
right->balance();
else if (right->balanceFactor() == 2)
left->balance();
else
cout << "YOU DUN GOOFED" << endl;
else if (balanceFactor() == 2)
if (left->balanceFactor() == 1)
shiftRight();
else if (left->balanceFactor() == -1) {
left->shiftLeft();
shiftRight();
}
else if (left->balanceFactor() == 2)
left->balance();
else if (left->balanceFactor() == -2)
right->balance();
else
cout << "YOU DUN GOOFED" << endl;
//allows me to recursively balance the tree;
left->balance();
right->balance();
}
What is displayed when I run the program:
aardvarks osos hormigueros
Why am I deleting everything else?
Last edited on May 9, 2013 at 1:22am UTC
May 9, 2013 at 2:09am UTC
swap() has an implicit parameter, and I don't know how to use a debugger.
May 9, 2013 at 3:03am UTC
Please describe how your swap() works then.
Learn to use one. It will help you a lot.
May 9, 2013 at 3:11am UTC
Let's say MapA has:
key = a
value = b
MapB:
key = c
value = d
when I do MapA.swap(MapB);
I have a temp variable to hold the key, and another to hold the value of Map B.
MapB->key = a;
MapB->value = b;
key = the temp key var;
value = the temp value var;
Because I'm calling key and value inside a function where the implicit parameter is MapA, the compiler knows that I'm calling key and value on it.