考研複習之二叉樹

怎麼碩呢?

之前學的二叉樹基本上忘得差不多了

今天大概複習了一下,今天結合考研的書(王道)重新寫了一遍。

包括:遞歸前中後序,非遞歸前中後序,層序遍歷以及二叉樹的構建。

參考了書中的代碼,所以很接近考研的思路和習慣,廢話不多說了,上代碼。

#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");
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章