long long BinaryTree::nodeCounts() { std::queue<BinaryNode*> collections; // Find first leaf and push it to stack collections.push(root_); long long count = 1; // Iterate the cell from root to its childrens to destory // binary tree BinaryNode* cell = NULL; while (!collections.empty()) { cell = collections.front(); collections.pop(); // Push the children to stack if (!cell->isLeaf()) { BinaryNode *leftChild = cell->leftChild(); BinaryNode *rightChild = cell->rightChild(); if (leftChild != NULL) collections.push(leftChild); if (rightChild != NULL) collections.push(rightChild); } // Count the current node count++; } return count; }
void BinaryTree::deallocate() { std::queue<BinaryNode*> del_collections; // Find first leaf and push it to stack del_collections.push(root_); BinaryNode* cell = NULL; // Iterate the cell from root to its childrens to destory // binary tree while (!del_collections.empty()) { cell = del_collections.front(); del_collections.pop(); // Push the children to stack if (!cell->isLeaf()) { BinaryNode *leftChild = cell->leftChild(); BinaryNode *rightChild = cell->rightChild(); if (leftChild != NULL) del_collections.push(leftChild); if (rightChild != NULL) del_collections.push(rightChild); } // Delete current octree cell delete cell; cell = NULL; } this->root_ = NULL; }