二叉树的前序,中序,后序的遍历的递归和非递归代码-C语言


#include <stdio.h>
#include<stdlib.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//二叉树的存储结构
typedef struct TreeNode
{
	char data;
	TreeNode* lchild;
	TreeNode* rchild;
}BinTree;
#define ElementType BinTree*
//栈的存储结构
typedef struct node
{
	ElementType data;
	struct node* next;
}Stack;
Stack* creatStack()//创建一个带头的栈
{
	Stack* s=(Stack*)malloc(sizeof(node));
	s->next=NULL;
	return s;
	
}
bool isEmpty(Stack* s)//判断栈是否为空
{
	if(s->next==NULL)	return true;
	return false;
}
void push(Stack* s,ElementType value)//入栈
{
	Stack* New=(Stack*)malloc(sizeof(node));
	New->data=value;
	New->next=s->next;
	s->next=New;
}
BinTree* pop(Stack* s)//出栈
{
	if(isEmpty(s)==true){
		printf("栈空,不能出栈!");
		return 0; 
	}
	else{
		Stack* p=(Stack*)malloc(sizeof(node));
		p=(s)->next;
		BinTree* root=p->data;
		(s)->next=p->next;
		free(p);
		return root;
		
	}
}

void CreateBinTree(BinTree** root)//创建一个二叉树
{
	char ch;
	scanf("%c\n",&ch);
	//ch=getchar();
	if (ch=='#'){
		
		*root=NULL;
	}	
	else{
		
			(*root)=(BinTree*)malloc(sizeof(TreeNode));
			(*root)->data=ch;
			CreateBinTree(&((*root)->lchild));
			CreateBinTree(&((*root)->rchild));                               
		
	}
		
}
void PreTraversal(BinTree* root)//前序遍历,递归
{
	if(root!=NULL){
		printf("%c ",root->data);
		PreTraversal(root->lchild);
		PreTraversal(root->rchild);
	}
 } 
void PreTraversal1(BinTree* root)//前序遍历,非递归
{
	Stack* stack=creatStack();
	while(!isEmpty(stack) || root){
		while(root){
			printf("%c ",root->data);
			push(stack,root);
			root=root->lchild;	
		}
		root=pop(stack);
		root=root->rchild;
	}
}
 void InOrderTraversal(BinTree* root)//中序遍历,递归
{
	if(root!=NULL){
		
		InOrderTraversal(root->lchild);
		printf("%c ",root->data);
		InOrderTraversal(root->rchild);
	}
 }
 void InOrderTraversal1(BinTree* root)//中序遍历,非递归
{
	Stack* stack=creatStack();
	while(!isEmpty(stack) || root){
		while(root){
			
			push(stack,root);
			root=root->lchild;	
		}
		root=pop(stack);
		printf("%c ",root->data);
		root=root->rchild;
	}
}
  void PostTraversal(BinTree* root)//后序遍历,递归
{
	if(root!=NULL){
		
		PostTraversal(root->lchild);
		PostTraversal(root->rchild);
		printf("%c ",root->data);
	}
 }
  void PostTraversal1(BinTree* root)//后序遍历,非递归
{
	BinTree* q=NULL;
	Stack* stack=creatStack();
	while(!isEmpty(stack) || root){
		while(root){
			
			push(stack,root);
			root=root->lchild;	
		}
		root=pop(stack);
		push(stack,root);
		if(root->rchild==NULL || root->rchild==q){
			root=pop(stack);
			printf("%c ",root->data);	
			q=root;
			root=NULL;
		}	
		else{
			root=root->rchild;
			//printf("%c ",root->data);
			
		}
		
	}
}
void PrintTree(BinTree* root,int h)//输出一颗二叉树
{
	if(root==NULL)	return;
	else{
		PrintTree(root->rchild,h+1);
		for(int i=0;i<h;i++)	printf("\t");
		printf("%c\n",root->data);
		PrintTree(root->lchild,h+1);
		
	}
}
//主函数
int main(int argc, char** argv) {
	BinTree* root;
	CreateBinTree(&root);
	PrintTree(root,1);
	PreTraversal(root);
	printf("\n");
	InOrderTraversal(root);
	printf("\n");
	PostTraversal(root);
	printf("\n");
	PreTraversal1(root);
	printf("\n");
	InOrderTraversal1(root);
	printf("\n");
	PostTraversal1(root);
	return 0;
}
/*输入样例
A
B
D
#
#
F
E
#
#
#
C
G
#
H
#
#
I
#
#
#
*/

猜你喜欢

转载自blog.csdn.net/qq_41386300/article/details/83420657