First, I know that my class code is not acceptable, but I don't want to mess with a bunch of functions to access the variables when I'm trying to figure out how to make binary trees for the first time. Then why not use a struct? Because I forgot how to do a ctor initializer and I need to have some default values for my variables. Next, here's my code:
The problem is where the first set of loops is. When I set levelHeads to the address of stem, all goes as it should. But when I set current to the address of levelHeads (and stem), it doesn't appear to do anything when I print the value of current's stem member in the innermost for loop. The variable stem in levelHeads equals 1 and it apparently equals 0 in current. Please try to help me.Code:#include <iostream> using namespace std; class node { public: node(); int rowLen; node * levelLeft; node * levelRight; node * sibling; node * parent; node * childLeft; node * childRight; bool stem; }; node::node() { stem = 0; levelLeft = 0; levelRight = 0; sibling = 0; parent = 0; childLeft = 0; childRight = 0; } int main() { int levels; cout << "How many levels should the tree have?" << endl; cin >> levels; node * stem = new node; stem->stem = true; node * levelHeads = stem; cout << levelHeads->stem << endl; node * current; float lastCount = .5; float currentCount; for(int a = 0; a < levels; a++) { current = levelHeads; currentCount = lastCount*2; for(float b = 0; b < (lastCount*2)-1; b++) { cout << current->stem << endl; current->rowLen = currentCount; current->levelRight = new node; current = current->levelRight; } lastCount = lastCount*2; levelHeads->childLeft = new node; levelHeads = levelHeads->childLeft; } /* node * topRow = stem; node * bottomRow = stem->childLeft; node * topCurrent = topRow; node * bottomCurrent = bottomRow; for(int a = 0; a < levels-2; a++) { for(int b = 0; b < topRow->rowLen; b++) { for(int c = 0; c < 2; c++) { bottomCurrent->parent = topRow; if(c == 0) topRow->childLeft = bottomRow; else topRow->childRight = bottomRow; bottomCurrent = bottomCurrent->levelRight; } topCurrent = topCurrent->levelRight; } topRow = bottomRow; bottomRow = bottomRow->childLeft; }*/ return 0; }



LinkBack URL
About LinkBacks


