第二题初步完成
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user