完全模板化
This commit is contained in:
+22
-9
@@ -1,17 +1,17 @@
|
||||
/*
|
||||
* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
|
||||
*/
|
||||
#include<iostream>
|
||||
#include"BiTree.h"
|
||||
#include <iostream>
|
||||
#include "BiTree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
BiTree* createTreeFromArray(const int* values, int size)
|
||||
BiTree<int>* createTreeFromArray(const int* values, int size)
|
||||
{
|
||||
BiTree* root = nullptr;
|
||||
BiTree<int>* root = nullptr;
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
BiTree::insertNode(root, values[i]);
|
||||
BiTree<int>::insertNode(root, values[i]);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
@@ -21,9 +21,22 @@ int main()
|
||||
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
|
||||
int size = sizeof(testValues) / sizeof(testValues[0]);
|
||||
|
||||
BiTree* root = createTreeFromArray(testValues, size);
|
||||
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;
|
||||
}
|
||||
|
||||
cout << "树结构为" << endl << root->toString() << endl;
|
||||
cout << "叶子节点数据总和: " << BiTree::sum_leaf(root) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user