This commit is contained in:
2024-12-05 15:46:08 +08:00
parent 08f6f4b0f4
commit 4ae4cf8554
26 changed files with 1385 additions and 1381 deletions
+42 -42
View File
@@ -1,42 +1,42 @@
/*
* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
*/
#include <iostream>
#include "BiTree.h"
using namespace std;
BiTree<int>* createTreeFromArray(const int* values, int size)
{
BiTree<int>* root = nullptr;
for (int i = 0; i < size; ++i)
{
BiTree<int>::insertNode(root, values[i]);
}
return root;
}
int main()
{
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
int size = sizeof(testValues) / sizeof(testValues[0]);
BiTree<int>* root = createTreeFromArray(testValues, size);
if (root)
{
cout << "树结构为:" << endl;
cout << root->toString() << endl;
cout << "叶子节点数据总和: " << BiTree<int>::sum_leaf(root) << endl;
root->destroy();
delete root;
}
else
{
cout << "树为空。" << endl;
}
return 0;
}
/*
* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
*/
#include <iostream>
#include "BiTree.h"
using namespace std;
BiTree<int>* createTreeFromArray(const int* values, int size)
{
BiTree<int>* root = nullptr;
for (int i = 0; i < size; ++i)
{
BiTree<int>::insertNode(root, values[i]);
}
return root;
}
int main()
{
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
int size = sizeof(testValues) / sizeof(testValues[0]);
BiTree<int>* root = createTreeFromArray(testValues, size);
if (root)
{
cout << "树结构为:" << endl;
cout << root->toString() << endl;
cout << "叶子节点数据总和: " << BiTree<int>::sum_leaf(root) << endl;
root->destroy();
delete root;
}
else
{
cout << "树为空。" << endl;
}
return 0;
}