一、二叉树概念
二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
性质1:在二叉树的第i层上至多有个结点。
性质2:深度为k的二叉树至多有个结点。
性质3:对任何一棵二叉树T,如果其终端结点数为n0,度位2的结点数为n2,则n0=n2+1。
满二叉树:一棵深度为k,且有2k-1个节点称之为满二叉树;
完全二叉树:深度为k,有n个节点的二叉树,当且仅当其每一个节点都与深度为k的满二叉树中,序号为1至n的节点对应时,称之为完全二叉树。
平衡二叉树:平衡二叉树又被称为AVL树(区别于AVL算法),它是一棵二叉排序树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
二叉排序树:(Binary Sort Tree)又称二叉查找树(二叉搜索树)。 它或者是一棵空树;或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4) 中序遍历结果为有序数列。
(5)左<=根<右 中序遍历(左<根<右)
二、二叉树遍历
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
//二叉树的节点定义
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
};
//从数组的某个位置的元素开始生成树
TreeNode* createTree(vector<int> list, int start){
if (list[start] == '#') {
return NULL;
}
TreeNode* root = new TreeNode(list[start]);
int lnode = 2*start + 1;
int rnode = 2*start + 2;
if ( lnode > list.size() -1) {
root -> left = NULL;
}else{
root -> left = createTree(list, lnode);
}
if (rnode > list.size() -1) {
root -> right =NULL;
}else{
root -> right = createTree(list, rnode);
}
return root;
}
//前序遍历
void preorder(TreeNode *root, vector<int> &path)
{
if(root != NULL)
{
path.push_back(root->val);
preorder(root->left, path);
preorder(root->right, path);
}
}
//中序遍历
void inorder(TreeNode *root, vector<int> &path)
{
if(root != NULL)
{
inorder(root->left, path);
path.push_back(root->val);
inorder(root->right, path);
}
}
//后续遍历
void postorder(TreeNode *root, vector<int> &path)
{
if(root != NULL)
{
postorder(root->left, path);
postorder(root->right, path);
path.push_back(root->val);
}
}
//非递归前序遍历
void preorderTraversal(TreeNode *root, vector<int> &path)
{
stack<TreeNode *> s;
TreeNode *p = root;
while(p != NULL || !s.empty())
{
while(p != NULL)
{
path.push_back(p->val);
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
p = p->right;
}
}
}
//非递归中序遍历
void inorderTraversal(TreeNode *root, vector<int> &path)
{
stack<TreeNode *> s;
TreeNode *p = root;
while(p != NULL || !s.empty())
{
while(p != NULL)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
path.push_back(p->val);
s.pop();
p = p->right;
}
}
}
//非递归后序遍历-迭代
void postorderTraversal(TreeNode *root, vector<int> &path)
{
stack<TreeNode*> s{{root}};
TreeNode *head = root;
while (!s.empty()) {
TreeNode *t = s.top();
if ((!t->left && !t->right) || t->left == head || t->right == head) {
path.push_back(t->val);
s.pop();
head = t;
}
else {
if (t->right) s.push(t->right);
if (t->left) s.push(t->left);
}
}
}
void PostOrder2(TreeNode *t, vector<int> &path)
{
stack<TreeNode *> s;
TreeNode *p = t;
TreeNode *tmp = nullptr;
//tmp 指针是为了保存节点上一次访问的状态
while (p != nullptr || !s.empty())
{
while (p != nullptr)
{
s.push(p);
p = p->left;//左
}
if(!s.empty())
{
//每次取栈定元素
//判断栈顶元素的右孩子是否为空,
//如果不为空,查看之前访问的节点是不是该栈顶元素的右孩子
p = s.top();
if (p->right != nullptr && p->right != tmp)
{
p = p->right;//右
}
else
{
path.push_back(p->val);
s.pop();
//每次访问节点之后,需要将缓存上一次访问的节点,并且将指针置空
tmp = p;
p = nullptr;
}
}
}
}
//更简单的非递归前序遍历
void preorderTraversalNew(TreeNode *root, vector<int> &path)
{
stack< pair<TreeNode *, bool> > s;
s.push(make_pair(root, false));
bool visited;
while(!s.empty())
{
root = s.top().first;
visited = s.top().second;
s.pop();
if(root == NULL)
continue;
if(visited)
{
path.push_back(root->val);
}
else
{
s.push(make_pair(root->right, false));
s.push(make_pair(root->left, false));
s.push(make_pair(root, true));
}
}
}
//更简单的非递归中序遍历
void inorderTraversalNew(TreeNode *root, vector<int> &path)
{
stack< pair<TreeNode *, bool> > s;
s.push(make_pair(root, false));
bool visited;
while(!s.empty())
{
root = s.top().first;
visited = s.top().second;
s.pop();
if(root == NULL)
continue;
if(visited)
{
path.push_back(root->val);
}
else
{
s.push(make_pair(root->right, false));
s.push(make_pair(root, true));
s.push(make_pair(root->left, false));
}
}
}
//更简单的非递归后序遍历
void postorderTraversalNew(TreeNode *root, vector<int> &path)
{
stack< pair<TreeNode *, bool> > s;
s.push(make_pair(root, false));
bool visited;
while(!s.empty())
{
root = s.top().first;
visited = s.top().second;
s.pop();
if(root == NULL)
continue;
if(visited)
{
path.push_back(root->val);
}
else
{
s.push(make_pair(root, true));
s.push(make_pair(root->right, false));
s.push(make_pair(root->left, false));
}
}
}
void Iterative_sequence_traversal(TreeNode *root, vector<vector<int>>& res){
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
vector<int> oneLevel;
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode *node = q.front();
q.pop();
oneLevel.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(oneLevel);
}
}
void level_order(TreeNode* root, int level, vector<vector<int>>& res){
if(!root)
return;
if(res.size() == level)
res.push_back({});
res[level].push_back(root->val);
if(root->left)
level_order(root->left, level+1, res);
if(root->right)
level_order(root->right, level+1, res);
}
void Recursive_sequence_traversal(TreeNode *root, vector<vector<int>>& res){
level_order(root, 0, res);
}
int main()
{
vector<int> datanum = {1,2,3,4,5,'#',6,'#','#',7,8};
//1,2,3,4,5,'#',6,'#','#',7,8,'#','#','#','#' 后面的#可省略
TreeNode *t;
t = createTree(datanum, 0);
vector<int> res;
// preorder(t, res);
// preorderTraversal(t, res);
// preorderTraversalNew(t, res);
// inorder(t, res);
// inorderTraversal(t, res);
// inorderTraversalNew(t, res);
// postorder(t, res);
postorderTraversal(t, res);
// postorderTraversalNew(t, res);
for(auto num : res){
cout << num;
}
cout << endl;
{
vector<vector<int>> res;
Iterative_sequence_traversal(t, res);
for(auto& r1 : res){
for(auto& r2 : r1){
cout << r2;
}
}
}
return 0;
}