改成模板类了
This commit is contained in:
@@ -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
@@ -1,24 +1,86 @@
|
||||
#ifndef BITREE_H
|
||||
#define BITREE_H
|
||||
#include<sstream>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
+14
-9
@@ -11,8 +11,9 @@
|
||||
* CBEGDFA
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
#include <sstream>
|
||||
#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<char>* root = BiTree<char>::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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user