递归、栈、非递归非栈实现二叉树的遍历

基于迎春花开365天利用栈实现二叉树的先序、中序、后序遍历的非递归操作

以及  Alvin-Qu非递归,不用栈实现二叉树中序遍历  的算法,本人对二叉树的遍历做出系统的总结(C++实现),包含先序、中序、后序的递归、栈实现、非递归非栈实现。其中以双栈作为辅助数据结构的后序遍历算法,来源于jimolangge123二叉树的后序遍历实现(基于两个栈实现)


先序遍历的三种算法:

//递归实现先序遍历
void preorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	else{
		cout<getElement();//print the value
		preorderTree(root->left());
		preorderTree(root->right());
	}
}

//非递归实现先序遍历(使用栈作为辅助数据结构)
void preorderTree(binNode* root){
	stack*> Stack;
	if(root==NULL){
		return;
	}
	while(root||!Stack.empty()){
		while(root){
			Stack.push(root);
			cout<getElement();//遍历
			root=root->left();
		}
		root=Stack.top();
		Stack.pop();
		root=root->right();
	}
}


//不使用栈,非递归先序遍历BST
/*
 *每个节点有个parent指针进行回溯
*/

void preorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	while(root->left()){
		cout<getElement();//遍历
		root=root->left();
	}
	while(root!=NULL){
		if(root->right(){//该节点有右孩子
			root=root->right();
			cout<getElement();//遍历
			while(root->left()){//指向该子树的最左端
				root=root->left();
				cout<getElement();//遍历
			}
		}
		else{//没有右孩子
		    binNode* temp=NULL;
	        do{//溯回
				temp=root;
				root=root->parent();
			}while(root!=NULL&&temp==root->right());
		}
	}
}


中序遍历:

//递归实现中序遍历
void inorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	else{
		inorderTree(root->left());
		cout<getElement();//print the value
		inorderTree(root->right());
	}
}

//非递归实现中序遍历(使用栈作为辅助数据结构)
void inorderTree(binNode* root){
	stack*> Stack;
	if(root==NULL){
		return;
	}
	while(root||!Stack.empty()){
		while(root){
			Stack.push(root);
			root=root->left();
		}
		root=Stack.top();
		Stack.pop();
		cout<getElement();
		root=root->right();
	}
}


//不使用栈,非递归中序遍历BST
/*
 *每个节点有个parent指针进行回溯
*/

void inorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	while(root->left()){
		root=root->left();
	}
	while(root!=NULL){
		cout<getElement();//遍历
		if(root->right(){//该节点有右孩子
			root=root->right();
			while(root->left()){//指向该子树的最左端
				root=root->left();
			}
		}
		else{//没有右孩子
		    binNode* temp=NULL;
	        do{//溯回
				temp=root;
				root=root->parent();
			}while(root!=NULL&&temp==root->right());
		}
	}
}

后序遍历:

//递归实现后序遍历
void postorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	else{
		inorderTree(root->left());
		inorderTree(root->right());
		cout<getElement();//print the value
	}
}

//非递归实现后序遍历
/*
*使用双栈作为辅助数据结构
*/
void postorderTree(binNode* root){
	stack*> Stack1;
	stack*> Stack2;
	if(root==NULL){
		return;
	}
	Stack1.push(root);
	while(!Stack1.empty()){
		root=Stack1.top();
		Stack2.push(root);
		Stack1.pop();
		if(root->left()) Stack1.push(root->left());
		if(root->right()) Stack1.push(root->right());
	}
	while(!Stack2.empty()){
		root=Stack2.top();
		Stack2.pop();
		cout<getElement();
	}
}


//不使用栈,非递归后序序遍历BST
/*
 *每个节点有个parent指针进行回溯
*/

void postorderTree(binNode* root){
	if(root==NULL){
		return;
	}
	while(root->left()){
		root=root->left();
	}
	while(root!=NULL){
		if(root->right()){//该节点有右孩子
			root=root->right();
			while(root->left()){//指向该子树的最左端
				root=root->left();
			}
		}
		else{//没有右孩子
		     //溯回操作
		    binNode* temp=root;
			root=root->parent();
			if(temp==root->left()){
				cout<getElement();
			}			
	        while(temp==root->right()||root){
				cout<getElement();
				temp=root;
				root=root->parent();
			}
		}
	}
}


你可能感兴趣的:(数据结构与算法)