#include<iostream>
#include<vector>
#include<queue>
using namespace std;
// 定义二叉链表节点类型
template<class T>
struct Node
{
T data;
Node *lchild;
Node *rchild;
};
// 二叉树链表类
template<class T>
class Binary_Tree
{
private:
Node<T> *BT; // 二叉链表的根节点
public:
Binary_Tree(){BT = NULL; return;} // 二叉链表初始化
void Creat_Binary_Tree(T); // 生成二叉链表类
void Pretrav_Binary_Tree(); // 二叉树前序遍历
void Intrav_Binary_Tree(); // 二叉树中序遍历
void Postrav_Binary_Tree();
void Print_By_Level(); // 层次遍历
void Print_By_Level_1(); // 层次遍历
};
// 生成二叉链表
template<class T>
void Binary_Tree<T>::Creat_Binary_Tree(T end) // end 为结束符标志
{
Node<T> *p;
T x;
cin>>x;
if(x == end)return;
p = new Node<T>;
p->data = x;
p->lchild = NULL;
p->rchild = NULL;
BT = p;
Creat(p, 1, end);
Creat(p, 2, end);
}
template<class T>
int Creat(Node<T> *p, int k, T end)
{
Node<T> *q;
T x;
cin>>x;
if(x != end)
{
q = new Node<T>;
q->data = x;
q->lchild = NULL;
q->rchild = NULL;
if(k == 1)p->lchild = q;
if(k == 2)p->rchild = q;
Creat(q, 1, end);
Creat(q, 2, end);
}
return 0;
}
template<class T>
void Binary_Tree<T>::Pretrav_Binary_Tree() // 二叉树前序遍历
{
Node<T> *p;
p = BT;
pretrav(p);
cout<<endl;
return;
}
template<class T>
int pretrav(Node<T> *p)
{
if(p != NULL)
{
cout<<p->data<<" ";
pretrav(p->lchild);
pretrav(p->rchild);
}
return 0;
}
template<class T>
void Binary_Tree<T>::Intrav_Binary_Tree() // 二叉树中序遍历
{
Node<T> *p;
p = BT;
intrav(p);
cout<<endl;
return;
}
template<class T>
int intrav(Node<T> *p)
{
if(p != NULL)
{
intrav(p->lchild);
cout<<p->data<<" ";
intrav(p->rchild);
}
return 0;
}
template<class T>
void Binary_Tree<T>::Postrav_Binary_Tree() // 二叉树的后序遍历
{
Node<T> *p;
p = BT;
postrav(p);
cout<<endl;
return;
}
template<class T>
int postrav(Node<T> *p)
{
if(p != NULL)
{
postrav(p->lchild);
postrav(p->rchild);
cout<<p->data<<" ";
}
return 0;
}
// 二叉树的层次遍历
// 1, 设置两个标志 一个用来记录该层 需要新增的结点数,一个就是,下一层的数组添加数
// 2 当遍历该层的时候,若子结点有,就+1 ,并将该结点添加到数组中;
// 3 把该结点新添加的
template<class T>
void Binary_Tree<T>::Print_By_Level()
{
Node<T> *pHead = BT;
vector<Node<T>*> vec;
vec.push_back(pHead);
int cur = 0;
int end = 1;
while(cur < vec.size())
{
end = vec.size();
while(cur < end)
{
cout<<vec[cur]->data<<" ";
if(vec[cur]->lchild)
vec.push_back(vec[cur]->lchild);
if(vec[cur]->rchild)
vec.push_back(vec[cur]->rchild);
cur++;
}
cout<<endl;
}
}
/*
二叉树的层次遍历2
在遍历当前层的时候,保存下一层的结点数,只需要每次插入一个结点的时候childSize++ ,这样就知道了下一层
的结点数了,让后把 childSize 赋值给parentSize ,开始新的一层遍历,从队列中取出parentSize 个结点后,一盒就知道这一层遍历完了。
*/
template<class T>
void Binary_Tree<T>::Print_By_Level_1()
{
int parentSize = 1, childSize = 0;
Node<T> *temp;
queue<Node<T> *> ivec;
ivec.push(BT);
do
{
temp = ivec.front();
cout<<temp->data<<' ';
ivec.pop();
if(temp->lchild)
{
ivec.push(temp->lchild);
childSize++;
}
if(temp->rchild)
{
ivec.push(temp->rchild);
childSize++;
}
parentSize--;
if(parentSize == 0)
{
parentSize = childSize;
childSize = 0;
cout<<endl;
}
}while(!ivec.empty());
}
int main()
{
Binary_Tree<int> b;
cout<<"输入各结点数值(-1为结束符值)"<<endl;
b.Creat_Binary_Tree(-1);
cout<<"前序遍历:"<<endl;
b.Pretrav_Binary_Tree();
cout<<"中序遍历:"<<endl;
b.Intrav_Binary_Tree();
cout<<"后序遍历:"<<endl;
b.Postrav_Binary_Tree();
cout<<"层次遍历:"<<endl;
//b.Print_By_Level();
b.Print_By_Level_1();
//
system("pause");
return 0;
}
//
//void print_by_level_3(Tree T) {
// vector<tree_node_t*> vec;
// vec.push_back(T);
// int cur = 0;
// int end = 1;
// while (cur < vec.size()) {
// end = vec.size();
// while (cur < end) {
// cout << vec[cur]->data << " ";
// if (vec[cur]->lchild)
// vec.push_back(vec[cur]->lchild);
// if (vec[cur]->rchild)
// vec.push_back(vec[cur]->rchild);
// cur++;
// }
// cout << endl;
// }
//}
c++ 二叉树的构建 前序遍历 中序遍历 后续遍历 层次遍历
最新推荐文章于 2023-03-10 16:56:03 发布