diff --git a/homework4/BiTree.cpp b/homework4/BiTree.cpp deleted file mode 100644 index fca0d70..0000000 --- a/homework4/BiTree.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include "BiTree.h" -#include - -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(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(); - } -} - diff --git a/homework4/BiTree.h b/homework4/BiTree.h index 5967e8b..2ef41ac 100644 --- a/homework4/BiTree.h +++ b/homework4/BiTree.h @@ -1,24 +1,86 @@ #ifndef BITREE_H #define BITREE_H -#include +#include +#include +#include + +template class BiTree { public: - int data; + T data; BiTree* left; BiTree* right; - static bool is_empty(BiTree* bt); - 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* build_from_preorder_string(const std::string& preorder, size_t& index); - void inOrder(std::ostringstream& oss) const; - std::string toString(); - void destory(); + BiTree() : left(nullptr), right(nullptr) + { + } + + static BiTree* createNode(const T& value) + { + BiTree* node = new BiTree(); + node->data = value; + 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 diff --git a/homework4/CMakeLists.txt b/homework4/CMakeLists.txt index 786c5ad..4e4da73 100644 --- a/homework4/CMakeLists.txt +++ b/homework4/CMakeLists.txt @@ -3,10 +3,6 @@ project(homework4) set(CMAKE_CXX_STANDARD 20) -add_executable(test1 test1.cpp - BiTree.cpp - BiTree.h) -add_executable(test2 test2.cpp - BiTree.cpp - BiTree.h -) +add_executable(test1 BiTree.h + test1.cpp) +add_executable(test2 test2.cpp BiTree.h) diff --git a/homework4/test1.cpp b/homework4/test1.cpp index 2e81d16..0ba525a 100644 --- a/homework4/test1.cpp +++ b/homework4/test1.cpp @@ -1,5 +1,5 @@ /* - * 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和 +* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和 */ #include #include"BiTree.h" diff --git a/homework4/test2.cpp b/homework4/test2.cpp index 9a7617f..8cdd4be 100644 --- a/homework4/test2.cpp +++ b/homework4/test2.cpp @@ -11,8 +11,9 @@ * CBEGDFA */ #include - +#include #include "BiTree.h" + using namespace std; int main() @@ -22,23 +23,27 @@ int main() cin >> input; size_t index = 0; - BiTree* root = BiTree::build_from_preorder_string(input, index); + BiTree* root = BiTree::buildFromPreorderString(input, index); // 构建字符型二叉树 ostringstream oss; if (root) { - root->inOrder(oss); - cout << "中序遍历结果:" << oss.str() << endl;; - root->destory(); + // 中序遍历 + root->inorderTraversal(oss); + cout << "中序遍历结果:" << oss.str() << endl; + + // 打印树结构 + cout << "该树结构为:" << endl; + cout << root->toString(); + + // 销毁树,释放内存 + root->destroy(); delete root; } else { - cout << "输入的二叉树为空" << endl; + cout << "输入的二叉树为空。" << endl; } - //cout << "该树结构为:" << endl; - //cout << root->toString() << endl; - return 0; }