怎么硕呢?
之前学的二叉树基本上忘得差不多了
今天大概复习了一下,今天结合考研的书(王道)重新写了一遍。
包括:递归前中后序,非递归前中后序,层序遍历以及二叉树的构建。
参考了书中的代码,所以很接近考研的思路和习惯,废话不多说了,上代码。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
typedef struct BiTNode{
struct BiTNode *lchild,*rchild;
int data;
} ;
struct BiTNode *T;
struct BiTNode *creat_BiTNode(struct BiTNode *s){
s = (struct BiTNode *)malloc(sizeof(struct BiTNode));
s->lchild = NULL; s->rchild = NULL;
}
struct BiTNode *creat_BiTNode_by_zero(struct BiTNode *T){//0表示空来创建二叉树
int x ; scanf("%d",&x);
if(x==0) return NULL;
T = creat_BiTNode(T);
T->data = x;
T->lchild = creat_BiTNode_by_zero(T->lchild);
T->rchild = creat_BiTNode_by_zero(T->rchild);
return T;
}
struct BiTNode *creat_BiTNode_by_level(struct BiTNode *T){//一层一层建立二叉树
int x ; scanf("%d",&x);;
if(x==0) return NULL;
T = creat_BiTNode(T);
T->data = x;
queue<struct BiTNode*> q;
q.push(T);
while(!q.empty()){
struct BiTNode *s;
s = q.front(); q.pop();
scanf("%d",&x);;//左子树建立
if(x==0) s->lchild = NULL;
else{
s->lchild = creat_BiTNode(s->lchild);
s->lchild->data = x;
q.push(s->lchild);
}
scanf("%d",&x);;//右子树建立
if(x==0) s->rchild = NULL;
else{
s->rchild = creat_BiTNode(s->rchild);
s->rchild->data = x;
q.push(s->rchild);
}
}
return T;
}
void preorderTree(struct BiTNode *T){//递归前序遍历
if(T!=NULL){
printf("%d ",T->data);//vist(T)
preorderTree(T->lchild);
preorderTree(T->rchild);
}
}
void inorderTree(struct BiTNode *T){//递归中序遍历
if(T!=NULL){
inorderTree(T->lchild);
printf("%d ",T->data);//vist(T)
inorderTree(T->rchild);
}
}
void postorderTree(struct BiTNode *T){//递归后序遍历
if(T!=NULL){
postorderTree(T->lchild);
postorderTree(T->rchild);
printf("%d ",T->data);//vist(T)
}
}
void preorderWithoutRecursion(struct BiTNode *T){//非递归前序遍历
if(T==NULL) return ;
BiTNode *p = T;
stack<struct BiTNode *> s;
while(p||!s.empty()){
if(p){
printf("%d ",p->data);
s.push(p);
p = p->lchild;
}
else{
p = s.top();
s.pop();
p = p->rchild;
}
}
printf("\n");
}
void inorderWithoutRecursion(struct BiTNode *T){//非递归中序遍历
if(T==NULL) return ;
BiTNode *p = T;
stack<struct BiTNode *> s;
while(p||!s.empty()){
if(p){
s.push(p);
p = p->lchild;
}
else{
p = s.top();
s.pop();
printf("%d ",p->data);
p = p->rchild;
}
}
printf("\n");
}
void postorderWithoutRecursion(struct BiTNode *T){//非递归后序遍历
if(T==NULL) return ;
BiTNode *p = T;
stack<struct BiTNode *> s;
BiTNode * r = NULL;
while(p||!s.empty()){
if(p){//向左走
s.push(p);
p = p->lchild;
}else{
p = s.top();//取出最外部的节点
if(p->rchild&&p->rchild!=r){//如果右子树存在且未被访问过
p = p->rchild;
s.push(p);
p=p->lchild;
}else{
s.pop();
printf("%d ",p->data);
r = p;
p = NULL;
}
}
}
printf("\n");
}
void levelordertree(struct BiTNode *T){
queue<struct BiTNode *> q;
q.push(T);
while(!q.empty()){
struct BiTNode *s = q.front();q.pop();
printf("%d ",s->data);
if(s->lchild) q.push(s->lchild);
if(s->rchild) q.push(s->rchild);
}
}
int main(){
struct BiTNode *T;
// T = creat_BiTNode_by_zero(T);// 1 2 4 0 0 5 7 0 0 8 0 0 3 0 6 0 0
T = creat_BiTNode_by_level(T);//1 2 3 4 5 0 6 0 0 7 8 0 0 0 0 0 0
//递归遍历
printf("递归前序遍历\n "); preorderTree(T); printf("\n");
printf("递归中序遍历\n "); inorderTree(T); printf("\n");
printf("递归后序遍历\n "); postorderTree(T);printf("\n");
//非递归遍历
printf("非递归前序遍历\n "); preorderWithoutRecursion(T);
printf("非递归中序遍历\n "); inorderWithoutRecursion(T);
printf("非递归后序遍历\n "); postorderWithoutRecursion(T);
//层序遍历
printf("层序遍历\n "); levelordertree(T); printf("\n");
}