引自百度百科:在计算机科学中,二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
一棵深度为k,且有2^k-1个节点的二叉树,称为满二叉树。这种树的特点是每一层上的节点数都是最大节点数。而在一棵二叉树中,除最后一层外,若其余层都是满的,并且最后一层或者是满的,或者是在右边缺少连续若干节点,则此二叉树为完全二叉树。具有n个节点的完全二叉树的深度为floor(log2n)+1。深度为k的完全二叉树,至少有2k-1个叶子节点,至多有2k-1个节点。
平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等。 最小二叉平衡树的节点总数的公式如下 F(n)=F(n-1)+F(n-2)+1 这个类似于一个递归的数列,可以参考Fibonacci(斐波那契)数列,1是根节点,F(n-1)是左子树的节点数量,F(n-2)是右子树的节点数量。
注意:
二叉树的先序遍历、中序遍历、后序遍历的区别在于根节点对应左右子树的顺序,其中根节点-->左子树-->右子树这样根节点在前面的称为先序遍历或前序遍历,相应的:左子树->根节点--->右子树称为中序遍历,左子树->右子树->根节点称为后序遍历。
#include "pch.h"
#include <iostream>
#include<iomanip>
#define INF (0x3f3f3f3f)
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//将二叉树镜像并返回
TreeNode* mirro(TreeNode* root) {
if (root == NULL)
return root;
TreeNode* mid = mirro(root->left);
root->left = mirro(root->right);
root->right = mid;
return root;
}
// 获取树的最大直径
class Solution {
public:
int L_max = 0;
int Get_height(TreeNode *t)
{
if (t == NULL)
return 0;
int L_H = Get_height(t->left);
int R_H = Get_height(t->right);
if (R_H + L_H > 0 && R_H + L_H > L_max)
L_max = R_H + L_H;
return L_H > R_H ? L_H + 1 : R_H + 1;
}
int diameterOfBinaryTree(TreeNode* root) {
cout << Get_height(root);
return L_max;
}
};
TreeNode * Tree_Set(int val, int higth, int num) {
TreeNode *root;
root = new TreeNode(val);
if (higth != num)
{
root->left = Tree_Set(2 * root->val, higth + 1, num);
root->right = Tree_Set(2 * root->val + 1, higth + 1, num);
}
else
{
root->left = NULL;
root->right = NULL;
}
return root;
}
//递归先序遍历
void Pre_Find(TreeNode *t)
{
//注意跳出条件
if (t != NULL)
{
//注意访问语句顺序
cout << t->val;
if (t->left)
cout << '(';
Pre_Find(t->left);
if (t->right)
cout << ',';
Pre_Find(t->right);
if (t->right)
cout << ')';
}
}
//递归中序遍历
void Mid_Find(TreeNode *t)
{
//注意跳出条件
if (t != NULL)
{
//注意访问语句顺序
cout << '(';
Mid_Find(t->left);
if (t->right)
cout << ',';
cout << t->val;
if (t->right) cout << ',';
Mid_Find(t->right);
cout << ')';
}
}
//递归后序遍历
void Back_Find(TreeNode *t)
{
//注意跳出条件
if (t != NULL)
{
//注意访问语句顺序
cout << '(';
Back_Find(t->left);
if (t->right)
cout << ',';
if (t->right) cout << ',';
Back_Find(t->right);
cout << t->val;
cout << ')';
}
}
// 迭代先序
void pre_find(TreeNode* root) {
if (root == nullptr)
return;
stack<TreeNode*>room;
room.push(root);
while (!room.empty()) {
TreeNode* p = room.top();
room.pop();
res.push_back(p->val);
if (p->right != nullptr)
room.push(p->right);
if (p->left != nullptr)
room.push(p->left);
}
}
// 迭代中序
void mid_find(TreeNode* root) {
stack<TreeNode*>room;
TreeNode* p = root;
while (!room.empty() || p != nullptr) {
while (p != nullptr) {
room.push(p);
p = p->left;
}
p = room.top();
room.pop();
res.push_back(p->val);
p = p->right;
}
}
// 迭代后序
void back_find(TreeNode* root) {
stack<TreeNode*>room;
TreeNode* p = root;
TreeNode* pre = nullptr;
while (!room.empty() || p != nullptr) {
while (p != nullptr) { //所有左节点入栈
room.push(p);
p = p->left;
}
p = room.top();
room.pop();
if (p->right == nullptr || p->right == pre) { //左右节点都遍历过了
res.push_back(p->val);
pre = p;
p = nullptr;
}
else { //遍历右子树
room.push(p);
p = p->right;
}
}
}
//层次遍历
void Level_Find(TreeNode *t)
{
if (t == NULL)
return;
queue <TreeNode* >room;
room.push(t);
while (!room.empty())
{
TreeNode* save = room.front();
room.pop();
cout << save->val << ' ';
if (save->left)
room.push(save->left);
if (save->right)
room.push(save->right);
}
}
//遍历叶子节点
void Find_Leaf(TreeNode *t)
{
if(t == NULL)
return;
if (t->left == NULL && t->right == NULL)
cout << t->val << ' ';
else
{
Find_Leaf(t->left);
Find_Leaf(t->right);
}
}
//找到指定值所在的节点,返回指针
TreeNode* Find_note(int x, TreeNode* t)
{
TreeNode* k;
queue <TreeNode* >room;
room.push(t);
while (!room.empty())
{
TreeNode* save = room.front();
if (save->val == x)
return save;
room.pop();
if (save->left)
room.push(save->left);
if (save->right)
room.push(save->right);
}
return NULL;
}
//获取树包含的结点个数,输入为根节点
int Find_Res(TreeNode* t)
{
if (t == NULL)
return 0;
return Find_Res(t->left) + Find_Res(t->right) + 1;
}
//获取二叉树高度
int Get_height(TreeNode *t)
{
if(t == NULL)
return 0;
int L_H = Get_height(t->left);
int R_H = Get_height(t->right);
return L_H > R_H ? L_H + 1 : R_H + 1;
}
int main()
{
int N, T;
TreeNode *t;
t = Tree_Set(1, 1, 5);
cout << t->val << ' ' << t->left->val << ' ' << t->right->val << endl;;
Pre_Find(t);
cout << endl;
Mid_Find(t);
cout << endl;
Back_Find(t);
cout << endl;
Level_Find(t);
cout << endl;
Find_Leaf(t);
cout << endl;
cout << "height = " << Get_height(t) << endl;
/*Solution SO;
SO.longestDecomposition(a);*/
return 0;
}
输出结果:
1 2 3
1(2(4(8(16,17),9(18,19)),5(10(20,21),11(22,23))),3(6(12(24,25),13(26,27)),7(14(28,29),15(30,31))))
(((((16),8,(17)),4,((18),9,(19))),2,(((20),10,(21)),5,((22),11,(23)))),1,((((24),12,(25)),6,((26),13,(27))),3,(((28),14,(29)),7,((30),15,(31)))))
(((((16),,(17)8),,((18),,(19)9)4),,(((20),,(21)10),,((22),,(23)11)5)2),,((((24),,(25)12),,((26),,(27)13)6),,(((28),,(29)14),,((30),,(31)15)7)3)1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
height = 5