写了个tostring
This commit is contained in:
+29
-9
@@ -1,38 +1,39 @@
|
|||||||
#include "BiTree.h"
|
#include "BiTree.h"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
bool BiTree::is_empty(BiTree *bt) {
|
bool BiTree::is_empty(BiTree *bt) {
|
||||||
if(bt==nullptr) {
|
if (bt == nullptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BiTree::is_leaf(BiTree *bt) {
|
bool BiTree::is_leaf(BiTree *bt) {
|
||||||
if(bt->left==nullptr && bt->right==nullptr) {
|
if (bt->left == nullptr && bt->right == nullptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BiTree::sum_leaf(BiTree *bt) {
|
int BiTree::sum_leaf(BiTree *bt) {
|
||||||
if(is_empty(bt)) {
|
if (is_empty(bt)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(is_leaf(bt)) {
|
if (is_leaf(bt)) {
|
||||||
return bt->data;
|
return bt->data;
|
||||||
}
|
}
|
||||||
return sum_leaf(bt->left)+sum_leaf(bt->right);
|
return sum_leaf(bt->left) + sum_leaf(bt->right);
|
||||||
}
|
}
|
||||||
|
|
||||||
BiTree* BiTree::createNode(int data) {
|
BiTree *BiTree::createNode(int data) {
|
||||||
BiTree* newNode = new BiTree();
|
BiTree *newNode = new BiTree();
|
||||||
newNode->data = data;
|
newNode->data = data;
|
||||||
newNode->left = nullptr;
|
newNode->left = nullptr;
|
||||||
newNode->right = nullptr;
|
newNode->right = nullptr;
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BiTree::insertNode(BiTree*& bt, int value) {
|
void BiTree::insertNode(BiTree *&bt, int value) {
|
||||||
if (bt == nullptr) {
|
if (bt == nullptr) {
|
||||||
bt = createNode(value);
|
bt = createNode(value);
|
||||||
} else {
|
} else {
|
||||||
@@ -42,4 +43,23 @@ void BiTree::insertNode(BiTree*& bt, int value) {
|
|||||||
insertNode(bt->right, value);
|
insertNode(bt->right, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void toStringHelper(const BiTree *node, std::ostringstream &oss, const std::string &prefix, bool isLeft) {
|
||||||
|
if (node != nullptr) {
|
||||||
|
oss << prefix;
|
||||||
|
|
||||||
|
oss << (isLeft ? "|--" : "|--") << node->data << "\n";
|
||||||
|
|
||||||
|
std::string newPrefix = prefix + (isLeft ? "| " : " ");
|
||||||
|
toStringHelper(node->left, oss, newPrefix, true);
|
||||||
|
toStringHelper(node->right, oss, newPrefix, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string BiTree::toString() {
|
||||||
|
std::ostringstream oss;
|
||||||
|
toStringHelper(this, oss, "", false);
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|||||||
+9
-3
@@ -1,6 +1,7 @@
|
|||||||
#ifndef BITREE_H
|
#ifndef BITREE_H
|
||||||
#define BITREE_H
|
#define BITREE_H
|
||||||
|
|
||||||
|
#include<sstream>
|
||||||
|
|
||||||
|
|
||||||
class BiTree {
|
class BiTree {
|
||||||
@@ -10,12 +11,17 @@ public:
|
|||||||
BiTree *right;
|
BiTree *right;
|
||||||
|
|
||||||
static bool is_empty(BiTree *bt);
|
static bool is_empty(BiTree *bt);
|
||||||
|
|
||||||
static bool is_leaf(BiTree *bt);
|
static bool is_leaf(BiTree *bt);
|
||||||
|
|
||||||
static int sum_leaf(BiTree *bt);
|
static int sum_leaf(BiTree *bt);
|
||||||
static BiTree* createNode(int data);
|
|
||||||
static void insertNode(BiTree*& bt, int value);
|
static BiTree *createNode(int data);
|
||||||
|
|
||||||
|
static void insertNode(BiTree *&bt, int value);
|
||||||
|
|
||||||
|
std::string toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //BITREE_H
|
#endif //BITREE_H
|
||||||
|
|||||||
+7
-7
@@ -1,14 +1,13 @@
|
|||||||
/*
|
/*
|
||||||
*二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
|
*二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
|
||||||
*/
|
*/
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include"BiTree.h"
|
#include"BiTree.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
BiTree *createTreeFromArray(const int *values, int size) {
|
||||||
BiTree* createTreeFromArray(const int* values, int size) {
|
BiTree *root = nullptr;
|
||||||
BiTree* root = nullptr;
|
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
BiTree::insertNode(root, values[i]);
|
BiTree::insertNode(root, values[i]);
|
||||||
}
|
}
|
||||||
@@ -19,8 +18,9 @@ int main() {
|
|||||||
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
|
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
|
||||||
int size = sizeof(testValues) / sizeof(testValues[0]);
|
int size = sizeof(testValues) / sizeof(testValues[0]);
|
||||||
|
|
||||||
BiTree* root = createTreeFromArray(testValues, size);
|
BiTree *root = createTreeFromArray(testValues, size);
|
||||||
|
|
||||||
cout << "Sum of leaf nodes: " << BiTree::sum_leaf(root) << endl;
|
cout << "树结构为:" << endl << root->toString() << endl;
|
||||||
|
cout << "叶子节点之和为: " << BiTree::sum_leaf(root) << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user