1. 程式人生 > >二叉樹的前序,中序,後序的遍歷的遞迴和非遞迴程式碼-C語言

二叉樹的前序,中序,後序的遍歷的遞迴和非遞迴程式碼-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 # # # */