目录
二叉树知识点总结
一、二叉树的建立
#include <iostream>
#include <queue>
using namespace std;
string str;
struct node
{
int data;
node *left,*right;
};
node* creat(int i)
{
if(str[i] == '.'||i >= str.size())
{
return NULL;
}
node *root = new node;
root->data = str[i];
root->left = creat(2*i+1);
root->right = creat(2*i+2);
return root;
}
int main()
{
str = "ABCDEFGHI";
node *root = creat(0);
/*
cout << endl << "前序遍历:" << endl;
Preorder(root);
cout << endl << "中序遍历:" << endl;
Inorder(root);
cout << endl << "后序遍历:" << endl;
Postorder(root);
cout << endl << "层序遍历:" << endl;
LeverOrder(root);
cout << endl << "二叉树的高度:" << endl;
cout<<getHigh(root);
*/
}
二、二叉树的遍历
(1)先序遍历
先序遍历也叫做先根遍历、前序遍历,可记做根左右(二叉树父结点向下先左后右)。
首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树,如果二叉树为空则返回。
例如,下图所示二叉树的遍历结果是:ABDECF
void Preorder(node* root)
{
if(root != NULL)
{
cout << root->data << ' ';
Preorder(root->left);
Preorder(root->right);
}
}
(2)中序遍历
中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树。若二叉树为空则结束返回,否则:
(1)中序遍历左子树
(2)访问根结点
(3)中序遍历右子树
如下图所示二叉树,中序遍历结果:DBEAFC
void Inorder(node* root)
{
if(root != NULL)
{
Inorder(root->left);
cout << root->data << ' ';
Inorder(root->right);
}
}
(3)后序遍历
后序遍历首先遍历左子树,然后遍历右子树,最后访问根结点,在遍历左、右子树时,仍然先遍历左子树,然后遍历右子树,最后遍历根结点。即:
若二叉树为空则结束返回,
否则:
(1)后序遍历左子树
(2)后序遍历右子树
(3)访问根结点
如下图所示二叉树,后序遍历结果:DEBFCA
void Postorder(node* root)
{
if(root != NULL)
{
Postorder(root->left);
Postorder(root->right);
cout << root->data << ' ';
}
}
(4)层序遍历
void LeverOrder(node* root)
{
queue<node*> que;
que.push(root);
while (!que.empty())
{
root = que.front();
cout << root->data << ' ';
que.pop();
if(root->left)
que.push(root->left);
if(root->right)
que.push(root->right);
}
}
三、二叉树的高度
int getHigh(node* root)
{
if(root == NULL)
return 0;
int lh = getHigh(root->left);
int rh = getHigh(root->right);
return max(lh,rh)+1;
}
四、其他建树方式
(1)已知先序和中序,建立二叉树
学习于:https://blog.csdn.net/qq_31726419/article/details/78364858
https://www.cnblogs.com/deltadeblog/p/9296469.html
https://blog.csdn.net/imissyoualwalys/article/details/89503203
令先序序列和中序序列在数组中连续存放。
设先序序列第一个字母的数组中的位置为preL,最后一个字母的数组中的位置为preR,
中序序列第一个字母的位置为inL,最后一个字母的位置为inR
从中序序列中,能找到一个节点将当前二叉树分为左子树与右子树,设此节点位于中序序列的 i 位置
左子树节点个数num = i - inL
所以左子树的先序序列区间[preL+1,preL+num];中序区间[inL,i-1]
右子树的先序区间[preL+num+1,preR],中序区间[i+1,inR]
题目链接: https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784
#include <iostream>
#include <queue>
#include <stdlib.h>
using namespace std;
int m,preorder[50],inorder[50];
struct node
{
int data;
node *left,*right;
};
node* creat(int preL,int preR,int inL,int inR)
{
if(preL > preR)
return NULL;
node* root = new node;
root->data = preorder[preL];
int i;
for (i = inL; i < inR+1; i++)
{
if (inorder[i] == root->data)
break;
}
int leftsize = i - inL;
root->left = creat(preL+1,preL+leftsize,inL,i-1);
root->right = creat(preL+1+leftsize,preR,i+1,inR);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m;
for(int i=0;i<m;i++)
cin >> inorder[i];
for (int i = 0; i < m; i++)
cin >> preorder[i];
node* root = creat(0,m-1,0,m-1);
//Leverorder(root);
}
(2)已知后序和中序,建立二叉树
学习于:https://www.cnblogs.com/-citywall123/p/11950877.html
https://www.cnblogs.com/three-year-old/p/10571105.html
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456
#include <iostream>
#include <queue>
#include <stdlib.h>
using namespace std;
int m,postorder[50],inorder[50];
struct node
{
int data;
node *left,*right;
};
node* creat(int postL,int postR,int inL,int inR)
{
if(postL > postR)
return NULL;
node* root = new node;
root->data = postorder[postR];
int i;
for (i = inL; i <= inR; i++)
{
if (inorder[i] == root->data)
break;
}
int leftsize = i - inL;
root->left = creat(postL,postL+leftsize-1,inL,i-1);
root->right = creat(postL+leftsize,postR-1,i+1,inR);
return root;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m;
for(int i=0;i<m;i++)
cin >> postorder[i];
for (int i = 0; i < m; i++)
cin >> inorder[i];
node* root = creat(0,m-1,0,m-1);
//Leverorder(root);
}