Good day! There is a non-recursive function that performs a symmetric tree walk. Instead of a stack used deck.
struct BinaryTree { int Data; BinaryTree* Left; BinaryTree* Right; }; //создание бинарного дерева void Make_Binary_Tree(BinaryTree** Node, int n) { BinaryTree** ptr; //вспомогательный указатель srand(time(NULL) * 1000); while (n > 0) { ptr = Node; while (*ptr != NULL) { if ((double)rand() / RAND_MAX < 0.5) ptr = &((*ptr)->Left); else ptr = &((*ptr)->Right); } (*ptr) = new BinaryTree(); cout << "Введите значение "; cin >> (*ptr)->Data; n--; } } // функция нерекурсивного обхода void SymmetricOrder_BinaryTree_Loop(BinaryTree* Node) { deque <BinaryTree> stack; do { while (Node != NULL) { stack.push_back(*Node); Node = Node->Left; } if (stack.size() > 0) { *Node = stack.back(); stack.pop_back(); printf("%3ld", Node->Data); Node = Node->Right; } } while (stack.size() > 0); } int main() { struct BinaryTree *nodes = { 0 } ; struct BinaryTree *root = nodes; Make_Binary_Tree(&root, 8); SymmetricOrder_BinaryTree_Loop(root); system("pause"); return 0; } The problem is that it gives an error:
"Caused an exception: write access violation. Node was nullptr."
That is, the loop goes outside the tree and points to an empty cell. According to the algorithm, this is how it should be, but apparently somehow implemented in a different way. How can I fix it to work?