完全模板化

This commit is contained in:
2024-11-22 18:09:17 +08:00
parent aff7b6b84e
commit 89152214b3
2 changed files with 73 additions and 10 deletions
+51 -1
View File
@@ -17,6 +17,37 @@ public:
{
}
static bool is_empty(BiTree* bt)
{
if (bt == nullptr)
{
return true;
}
return false;
}
static bool is_leaf(BiTree* bt)
{
if (bt->left == nullptr && bt->right == nullptr)
{
return true;
}
return false;
}
static T sum_leaf(BiTree* bt)
{
if (is_empty(bt))
{
return 0;
}
if (is_leaf(bt))
{
return bt->data;
}
return sum_leaf(bt->left) + sum_leaf(bt->right);
}
static BiTree* createNode(const T& value)
{
BiTree* node = new BiTree();
@@ -26,6 +57,25 @@ public:
return node;
}
static void insertNode(BiTree*& bt, T value)
{
if (bt == nullptr)
{
bt = createNode(value);
}
else
{
if (value < bt->data)
{
insertNode(bt->left, value);
}
if (value > bt->data)
{
insertNode(bt->right, value);
}
}
}
static BiTree* buildFromPreorderString(const std::string& preorder, size_t& index)
{
if (index >= preorder.size() || preorder[index] == '#')
@@ -67,7 +117,7 @@ public:
this->toStringHelper(oss, "", false, false);
return oss.str();
}
void destroy()
{
if (left)