Sorry for all the compilation errors, the majority of it came from our textbook. After not being able to trace and understand most of the code, I rewrote it so that it could make a bit more sense for myself. (Which is why there is probably a few errors, but the majority of it is copied directly from the textbook)
@salem c Yeah I tried to retrace it to the best of my ability, after I posted this and some further debugging I fixed the first problem I was having, but then the same problem came up further down the compilation
@lastchance Thank you for trying your best to help out! It's really greatly appreciated. As for why the typename was there, it was because I did not want to have to type it out over and over again in that file. Mostly the iterator because I have some functions that return an iterator and I did not want to have to copy and paste that over and over again. It's mostly just so that my code doesn't look so cluttered. Also, I did not know where my problem may have been, that is why I just provided the whole project. Apologies for making it so inconvenient, I will try and put code I feel may be relevant next time.
I worked around the part about checking if it was a nullpointer by adding this to the start of the restructure function
1 2
|
if (x.isExternal())
binaryTree.expandExternal(x);
|
Since x would have been the grandchild, this problem would arise when it was the external node. I'm not sure if this is a good fix, but it worked for a while. After being able to go further down the compilation, the same exception would be thrown in the height function. I found that the height and setHeight functions wasn't doing what I was expecting it do, which was access the height and setHeight functions in my Entry.h file. I've been trying to find a way to do this but I can't seem to figure it out. In the textbook, it makes the tree a friend class, but when I do that I get an unexpected token(s) error for some reason.
And you're right, those two cases were wrong, thank you for pointing that out! I fixed them
Update
I've updated my code so that it will actually compile ( at least on visual studios ). I got past the second insertion, (which was when I would get the error ) and now I get the error on the third insertion after it attempts to restructure. I worked around the height problem by adding an int to keep track of height onto my node directly in BinaryTree.h , this seems to have fixed the problem of not being able to access an integer for my height. I got past my first two insertions, but now I get the error in the same height ( ) function in SearchTree.h . I get the null pointer error on my isExternal() function this time in BinaryTree.h ( Also, on a side note, I am not even sure if I am utilizing my Entry.h file at this point )
Here is some code I think would be relevant to fixing the problem,
in
SearchTree.h
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
|
int height(const TPos& currentPosition) const
{
return (currentPosition.isExternal() ? 0 : currentPosition.height());
}
void setHeight(TPos currentPosition)
{
int hl = height(currentPosition.left());
int hr = height(currentPosition.right());
currentPosition.setHeight(1 + std::max(hl, hr));
}
bool isBalanced(const TPos& v) const
{
int bal = height(v.left()) - height(v.right());
return ((-1 <= bal) && (bal <= 1));
}
void rebalance(const TPos& v)
{
TPos z = v;
while (!(z == root())) { // rebalance up to root
z = z.parent();
setHeight(z); // compute new height
if (!isBalanced(z)) { // restructuring needed
TPos x = tallGrandchild(z);
z = restructure(x); // trinode restructure
setHeight(z.left()); // update heights
setHeight(z.right());
setHeight(z);
}
}
}
TPos tallGrandchild(const TPos& z) const {
TPos zl = z.left();
TPos zr = z.right();
if (height(zl) >= height(zr)) // left child taller
if (height(zl.left()) >= height(zl.right()))
return zl.left();
else
return zl.right();
else // right child taller
if (height(zr.right()) >= height(zr.left()))
return zr.right();
else
return zr.left();
}
TPos restructure(const TPos x)
{
if (x.isExternal())
binaryTree.expandExternal(x);
TPos y = x.parent();
TPos z = y.parent();
TPos a, b, c, T0, T1, T2, T3;
if (z.left() == y && y.left() == x)
{
a = x;
b = y;
c = z;
T0 = a.left();
T1 = a.right();
T2 = b.right();
T3 = c.right();
}
else if (z.right() == y && y.right() == x)
{
a = z;
b = y;
c = x;
T0 = a.left();
T1 = b.left();
T2 = c.left();
T3 = c.right();
}
else if (z.left() == y && y.right() == x)
{
a = y;
b = x;
c = z;
T0 = a.left();
T1 = a.right();
T2 = c.left();
T3 = c.right();
}
else if (z.right() == y && y.left() == x)
{
a = z;
b = x;
c = y;
T0 = a.left();
T1 = a.right();
T2 = c.left();
T3 = c.right();
}
if (z.isRoot())
b = root();
b.left() = a;
b.right() = c;
a.parent() = b;
c.parent() = b;
a.left() = T0;
a.right() = T1;
T0.parent() = a;
T1.parent() = a;
c.left() = T2;
c.right() = T3;
T2.parent() = c;
T3.parent() = c;
return b;
}
|
in
BinaryTree.h
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
|
struct Node {
Elem element;
Node* parent;
Node* left;
Node* right;
int ht;
Node() : element(), parent(NULL), left(NULL), right(NULL), ht(0) { }
};
public:
class Position {
private:
Node* position;
public:
Position(Node* _pos = NULL) : position(_pos) { }
Elem& operator*() { return position->element; }
Position left() const { return Position(position->left); }
Position right() const { return Position(position->right); }
Position parent() const { return Position(position->parent); }
bool isRoot() { return position->parent == NULL; }
bool isExternal() const { return position->left == NULL && position->right == NULL; }
bool isInternal() const { return !isExternal(); }
bool operator==(const Position& comparedPosition) const { return position == comparedPosition.position; }
bool operator != (const Position comparedPosition) const { return !(*this == comparedPosition); }
int height() const { return position->ht; } // get height
void setHeight(int h) { position->ht = h; } // set height
friend class BinaryTree<Elem>;
};
void expandExternal(const Position& currentPosition)
{
Node* expandedNode = currentPosition.position;
expandedNode->left = new Node;
expandedNode->left->parent = expandedNode;
expandedNode->right = new Node;
expandedNode->right->parent = expandedNode;
totalNodes += 2;
}
|