二叉树计算问题完整代码

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);
//计算该二叉树中度数为2的结点总数
int twoChildNodes(BiTree* root);
//计算该二叉树中度数为1的结点
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>																													//注意这里用到了stl库中的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是实际遍历的结点,parent用来保存应该插入位置的父结点
		child = node->key < child->key ? child->left : child->right;
	}
	node->parent = parent;	//当root是空的时候,令node->parent = parent即赋NULL。当root不是空的时候,令node->parent = parent即链接到父结点。
	if (!parent)																														//如果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;
}
//计算度数为2的结点总数
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);
}
//计算度数为1的结点总数
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;
		//Sleep(100);
	}
	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) {
	//int n;
	//cout << "要创建多少结点:";
	//cin >> n;

	声明根结点
	//BiTree* root = NULL;
	创建二叉树
	//for (int i = 0; i < n; i++) {
	//	BiTree* temp = createBiNode();
	//	Insert(root, temp);
	//}
	//cout << endl;
	//cout << "------------- 二叉树的计算问题 -------------" << endl;
	//cout << "深度:\t\t\t" << depth(root) << endl << endl;

	//cout << "总结点个数:\t\t" << nodeNums(root) << endl << endl;

	//cout << "度数为2的结点个数:\t" << twoChildNodes(root) << endl << endl;

	//cout << "度数为1的结点个数:\t" << oneChildNodes(root) << endl << endl;

	//cout << "叶子结点个数:\t\t" << leaves(root) << endl << endl;
	//cout << "------------- 二叉树的计算问题 -------------" << endl;

	testCal();

	cout << endl;
	_getch();
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值