【数据结构 06】二叉树

一、原理

二叉树算法核心思维:递归

满二叉树:二叉树的层数为K,节点数为2^K - 1

完全二叉树:二叉树的层数为K,前K-1层是满的,第K层是连续的

满二叉树是完全二叉树的子集。

任意二叉树:若叶子节点的个数是n_o,设度为2(有2个子节点)的节点个数是n_2,则n_0 = n_2 + 1

二叉树的第i层上最多有2^{i-1}个节点,第n个节点的满二叉树深度h = \lg (n+1)

二叉树可以顺序存储或链式存储。

【数据结构 06】二叉树_第1张图片

二、BinaryTree.h

#define _CRT_SECURE_NO_WARNINGS 1

#pragma
#include 
#include 
#include 
using namespace std;

typedef char DataType;

struct BinaryTreeNode
{
	DataType data;
	BinaryTreeNode* left;
	BinaryTreeNode* right;

	BinaryTreeNode(DataType x)
		: data(x), left(nullptr), right(nullptr)
	{}
};

class BinaryTree
{
	typedef BinaryTreeNode Node;
public:
	// 构建,指定构造数据序列
	void Create(const DataType* datas, int& i)
	{
		_root = _Create(datas, i);
	}

	// 先序遍历(根、左、右)
	void PreOrder()
	{
		_PreOrder(_root);
		cout << ",先序遍历结束" << endl;
	}

	// 中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << ",中序遍历结束" << endl;
	}

	// 后序遍历
	void PastOrder()
	{
		_PastOrder(_root);
		cout << ",后序遍历结束" << endl;
	}

	// 总节点数
	int Size()
	{
		int num = 0;
		_Size(_root, num);
		return num;
	}

	// 叶子节点数
	int LeafSize()
	{
		int leafNum = 0;
		_LeafSize(_root, leafNum);
		return leafNum;
	}

	// 深度(层数)
	int Depth()
	{
		return _Depth(_root);
	}

	// 子树数量
	int TreeSize()
	{
		return _TreeSize(_root);
	}

	// 销毁
	void Destroy()
	{
		_Destroy(_root);
		_root = nullptr;
		cout << "二叉树销毁成功" << endl;
	}

private:
	Node* _Create(const DataType* datas, int& i)
	{
		if (datas[i] == '#')
			return nullptr;

		Node* root = new Node(datas[i]);
		root->left = _Create(datas, ++i);
		root->right = _Create(datas, ++i);
		return root;
	}

	void _PreOrder(Node* root)
	{
		if (root == nullptr)
		{
			cout << "# ";
			return;
		}

		cout << root->data << ' ';
		_PreOrder(root->left);
		_PreOrder(root->right);
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			cout << "# ";
			return;
		}

		_InOrder(root->left);
		cout << root->data << ' ';
		_InOrder(root->right);
	}

	void _PastOrder(Node* root)
	{
		if (root == nullptr)
		{
			cout << "# ";
			return;
		}

		_PastOrder(root->left);
		_PastOrder(root->right);
		cout << root->data << ' ';
	}

	void _Size(Node* root, int& num)
	{
		if (root == nullptr)
			return;

		++num;
		_Size(root->left, num);
		_Size(root->right, num);
	}

	void _LeafSize(Node* root, int& leafNum)
	{
		if (root == nullptr)
			return;

		if (root->left == nullptr && root->right == nullptr)
			leafNum++;
		_LeafSize(root->left, leafNum);
		_LeafSize(root->right, leafNum);
	}

	int _Depth(Node* root)
	{
		if (root == nullptr)
			return 0;

		int leftDepth = _Depth(root->left);
		int rightDepth = _Depth(root->right);

		return (leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1);
	}

	int _TreeSize(Node* root)
	{
		if (root == nullptr)
			return 0;

		return (root->left == nullptr && root->right == nullptr) ? 0 :
			_TreeSize(root->left) + _TreeSize(root->right) + 1;
	}

	void _Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		
		_Destroy(root->left);
		_Destroy(root->right);
		cout << root->data << "销毁,";
		delete root;
	}

private:
	Node* _root;
};

三、test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "BinaryTree.h"

int main()
{
	cout << "--Test1---" << endl << "数据序列: 123###45##6##" << endl;
	BinaryTree bt;

	// 构建二叉树
	int i = 0;
	bt.Create("123###45##6##", i);
	bt.PreOrder();
	bt.InOrder();
	bt.PastOrder();

	// 计算二叉树节点总数
	cout << "二叉树节点总数为:" << bt.Size() << endl; // 6

	// 计算二叉树叶子结点数
	cout << "二叉树叶子结点数为:" << bt.LeafSize() << endl; // 3

	// 计算二叉树深度
	cout << "二叉树深度:" << bt.Depth() << endl; // 3

	// 计算二叉树子树数量
	cout << "二叉树子树数量:" << bt.TreeSize() << endl; // 3

	// 销毁二叉树
	bt.Destroy(); // 3销毁,2销毁,5销毁,6销毁,4销毁,1销毁,二叉树销毁成功

	// 计算二叉树节点总数
	cout << "二叉树节点总数为:" << bt.Size() << endl;

	return 0;
}

你可能感兴趣的:(数据结构,数据结构,算法)