第二题初步完成

This commit is contained in:
2024-11-22 17:16:02 +08:00
parent f8b03bad55
commit 7037512838
4 changed files with 86 additions and 2 deletions
+35
View File
@@ -60,6 +60,29 @@ void BiTree::insertNode(BiTree*& bt, int value)
}
}
BiTree* BiTree::build_from_preorder_string(const std::string& preorder, size_t& index)
{
if (index >= preorder.size() || preorder[index] == '#')
{
index++;
return nullptr;
}
BiTree* node = createNode(preorder[index++] - 'A' + 1);
node->left = build_from_preorder_string(preorder, index);
node->right = build_from_preorder_string(preorder, index);
return node;
}
void BiTree::inOrder(std::ostringstream& oss) const
{
if (this == nullptr) return;
if (left)left->inOrder(oss);
oss << static_cast<char>(data + 'A' - 1);
if (right)right->inOrder(oss);
}
void toStringHelper(const BiTree* node, std::ostringstream& oss, const std::string& prefix, bool isLeft,
bool hasSibling)
@@ -85,3 +108,15 @@ std::string BiTree::toString()
return oss.str();
}
void BiTree::destory()
{
if (left)
{
left->destory();
}
if (right)
{
right->destory();
}
}