tree.h:
#pragma once
#include<vector>
#include<iostream>
using namespace std;
typedef struct BiTree {
int key;
BiTree* left;
BiTree* right;
BiTree* parent;
}BiTree;
BiTree* createBiNode();
BiTree* &Insert(BiTree* &root, BiTree* node);
int depth(BiTree* root);
int nodeNums(BiTree* root);
int twoChildNodes(BiTree* root);
int oneChildNodes(BiTree* root);
int leaves(BiTree* root);
void swap(int& a, int& b);
void Perm(vector<int> vec, int b, int e);
BiTree* createBiNodeTest(int value);
int factorial(int n);
void testCal();
tree.cpp:
#include"tree.h"
#include<iostream>
#include<stack>
#include<Windows.h>
#include<conio.h>
using namespace std;
BiTree* createBiNode() {
BiTree* node = new BiTree;
node->left = node->right = node->parent = NULL;
cout << "输入新结点key值:";
cin >> node->key;
return node;
}
BiTree* &Insert(BiTree* &root, BiTree* node) {
BiTree* parent = NULL;
BiTree* child = root;
while (child) {
parent = child;
child = node->key < child->key ? child->left : child->right;
}
node->parent = parent;
if (!parent)
root = node;
else if (node->key < parent->key)
parent->left = node;
else
parent->right = node;
return root;
}
int depth(BiTree* root) {
if (!root)
return 0;
else {
int left_depth = depth(root->left);
int right_depth = depth(root->right);
return left_depth > right_depth ? (left_depth + 1) : (right_depth + 1);
}
}
int nodeNums(BiTree* root) {
if (!root)
return 0;
else
return nodeNums(root->left) + nodeNums(root->right) + 1;
}
int twoChildNodes(BiTree* root) {
if (!root)
return 0;
if (root->left && root->right)
return twoChildNodes(root->left) + twoChildNodes(root->right) + 1;
else
return twoChildNodes(root->left) + twoChildNodes(root->right);
}
int oneChildNodes(BiTree* root) {
if (!root)
return 0;
bool only_left = root->left && !root->right;
bool only_right = root->right && !root->left;
if (only_left)
return 1 + oneChildNodes(root->left);
else if (only_right)
return 1 + oneChildNodes(root->right);
else
return oneChildNodes(root->left) + oneChildNodes(root->right);
}
int leaves(BiTree* root) {
if (!root)
return 0;
if (!root->left && !root->right)
return 1;
return leaves(root->left) + leaves(root->right);
}
void swap(int& c1, int& c2)
{
char temp;
temp = c1;
c1 = c2;
c2 = temp;
}
BiTree* createBiNodeTest(int value) {
BiTree* node = new BiTree;
node->left = node->right = node->parent = NULL;
node->key = value;
return node;
}
void Perm(vector<int> p, int b, int e)
{
static int num = 1;
if (b == e) {
BiTree* testRoot = NULL;
for (int i = 0; i <= e; i++) {
BiTree* temp = createBiNodeTest(p[i]);
Insert(testRoot, temp);
}
cout << "--- 测试组" << num++ << "---" << endl;
cout << "插入顺序:";
for (int i = 0; i <= e; i++)
cout << p[i] << " ";
cout << endl << endl;
int two = twoChildNodes(testRoot);
int one = oneChildNodes(testRoot);
int leaf = leaves(testRoot);
int all = nodeNums(testRoot);
cout << "度数为0:" << leaf << endl;
cout << "度数为1:" << one << endl;
cout << "度数为2:" << two << endl;
cout << "总结点数:" << all << endl;
if ((all == one + two + leaf) && leaf == two + 1)
cout << "测试通过!" << endl;
else {
cout << "程序逻辑出现错误" << endl;
_getch();
exit(-1);
}
cout << "--- 测试组 ---" << endl << endl;
}
else
{
for (int i = b; i <= e; i++)
{
swap(p[b], p[i]);
Perm(p, b + 1, e);
swap(p[b], p[i]);
}
}
}
int factorial(int n) {
if (n == 1)
return 1;
return n * factorial(n - 1);
}
void testCal() {
int n;
cout << "接下来生成由[0, n - 1)中整数构成的测试用例,将这n个数的全排列中每一种排列数都作为生成二叉排序树的新方式,总测试数应为n!。" << endl << endl;
cout << "【注意】本测试程序没有测试计算二叉树的深度。" << endl;
cout << "输入n:";
cin >> n;
cout << "--------------------------------------------------------------" << endl;
cout << "\t\t\t" << "下面测试删除结点正确性" << endl;
vector<int> p;
for (int i = 0; i < n; i++)
p.push_back(i);
Perm(p, 0, n - 1);
cout << "共有 " << n << "! = " << factorial(n) << " 组测试数据进行测试,总测试通过组数为 " << n << "! = " << factorial(n) << " 组。" << endl;
cout << "--- 测试通过!---" << endl;
cout << "--------------------------------------------------------------" << endl;
}
main.cpp:
#include"tree.h"
#include<iostream>
#include<conio.h>
using namespace std;
int main(void) {
testCal();
cout << endl;
_getch();
return 0;
}