改成模板类了

This commit is contained in:
2024-11-22 17:55:29 +08:00
parent 7037512838
commit aff7b6b84e
5 changed files with 93 additions and 152 deletions
-122
View File
@@ -1,122 +0,0 @@
#include "BiTree.h"
#include <sstream>
bool BiTree::is_empty(BiTree* bt)
{
if (bt == nullptr)
{
return true;
}
return false;
}
bool BiTree::is_leaf(BiTree* bt)
{
if (bt->left == nullptr && bt->right == nullptr)
{
return true;
}
return false;
}
int BiTree::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);
}
BiTree* BiTree::createNode(int data)
{
BiTree* newNode = new BiTree();
newNode->data = data;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
void BiTree::insertNode(BiTree*& bt, int value)
{
if (bt == nullptr)
{
bt = createNode(value);
}
else
{
if (value < bt->data)
{
insertNode(bt->left, value);
}
else if (value > bt->data)
{
insertNode(bt->right, 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)
{
if (node != nullptr)
{
oss << prefix;
oss << (isLeft ? (hasSibling ? "├─" : "└─") : (hasSibling ? "├─" : "└─")) << node->data << "\n";
std::string newPrefix = prefix + (isLeft ? (hasSibling ? "" : " ") : " ");
bool hasLeftSibling = (node->left != nullptr && node->right != nullptr);
toStringHelper(node->left, oss, newPrefix, true, hasLeftSibling);
toStringHelper(node->right, oss, newPrefix, false, false);
}
}
std::string BiTree::toString()
{
std::ostringstream oss;
toStringHelper(this, oss, "", false, false);
return oss.str();
}
void BiTree::destory()
{
if (left)
{
left->destory();
}
if (right)
{
right->destory();
}
}
+74 -12
View File
@@ -1,24 +1,86 @@
#ifndef BITREE_H #ifndef BITREE_H
#define BITREE_H #define BITREE_H
#include<sstream>
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
class BiTree class BiTree
{ {
public: public:
int data; T data;
BiTree* left; BiTree* left;
BiTree* right; BiTree* right;
static bool is_empty(BiTree* bt); BiTree() : left(nullptr), right(nullptr)
static bool is_leaf(BiTree* bt); {
static int sum_leaf(BiTree* bt); }
static BiTree* createNode(int data);
static void insertNode(BiTree*& bt, int value); static BiTree* createNode(const T& value)
static BiTree* build_from_preorder_string(const std::string& preorder, size_t& index); {
void inOrder(std::ostringstream& oss) const; BiTree* node = new BiTree();
std::string toString(); node->data = value;
void destory(); node->left = nullptr;
node->right = nullptr;
return node;
}
static BiTree* buildFromPreorderString(const std::string& preorder, size_t& index)
{
if (index >= preorder.size() || preorder[index] == '#')
{
index++;
return nullptr;
}
BiTree* node = createNode(preorder[index++]);
node->left = buildFromPreorderString(preorder, index);
node->right = buildFromPreorderString(preorder, index);
return node;
}
void inorderTraversal(std::ostringstream& oss) const
{
if (left) left->inorderTraversal(oss);
oss << data;
if (right) right->inorderTraversal(oss);
}
void toStringHelper(std::ostringstream& oss, const std::string& prefix, bool isLeft, bool hasSibling) const
{
if (this != nullptr)
{
oss << prefix;
oss << (isLeft ? (hasSibling ? "├─" : "└─") : (hasSibling ? "├─" : "└─")) << data << "\n";
std::string newPrefix = prefix + (isLeft ? (hasSibling ? "" : " ") : " ");
bool hasLeftSibling = (left != nullptr && right != nullptr);
if (left) left->toStringHelper(oss, newPrefix, true, hasLeftSibling);
if (right) right->toStringHelper(oss, newPrefix, false, false);
}
}
std::string toString() const
{
std::ostringstream oss;
this->toStringHelper(oss, "", false, false);
return oss.str();
}
void destroy()
{
if (left)
{
left->destroy();
delete left;
}
if (right)
{
right->destroy();
delete right;
}
}
}; };
#endif // BITREE_H #endif // BITREE_H
+3 -7
View File
@@ -3,10 +3,6 @@ project(homework4)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
add_executable(test1 test1.cpp add_executable(test1 BiTree.h
BiTree.cpp test1.cpp)
BiTree.h) add_executable(test2 test2.cpp BiTree.h)
add_executable(test2 test2.cpp
BiTree.cpp
BiTree.h
)
+14 -9
View File
@@ -11,8 +11,9 @@
* CBEGDFA * CBEGDFA
*/ */
#include <iostream> #include <iostream>
#include <sstream>
#include "BiTree.h" #include "BiTree.h"
using namespace std; using namespace std;
int main() int main()
@@ -22,23 +23,27 @@ int main()
cin >> input; cin >> input;
size_t index = 0; size_t index = 0;
BiTree* root = BiTree::build_from_preorder_string(input, index); BiTree<char>* root = BiTree<char>::buildFromPreorderString(input, index); // 构建字符型二叉树
ostringstream oss; ostringstream oss;
if (root) if (root)
{ {
root->inOrder(oss); // 中序遍历
cout << "中序遍历结果:" << oss.str() << endl;; root->inorderTraversal(oss);
root->destory(); cout << "中序遍历结果:" << oss.str() << endl;
// 打印树结构
cout << "该树结构为:" << endl;
cout << root->toString();
// 销毁树,释放内存
root->destroy();
delete root; delete root;
} }
else else
{ {
cout << "输入的二叉树为空" << endl; cout << "输入的二叉树为空" << endl;
} }
//cout << "该树结构为:" << endl;
//cout << root->toString() << endl;
return 0; return 0;
} }