二叉树遍历
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define M 100
typedef struct node
{
int data;
int count;
struct node *lchild;
struct node *rchild;
}binaryTree;
typedef struct stack
{
binaryTree *elements[M];
int top;
}sequenceStack;
typedef struct queue {
binaryTree *queueNum[M];
int front;
int rear;
}sequenceQueue;
binaryTree *root;
sequenceStack s;
sequenceQueue q;
void setNull()
{
s.top =0;
}
void push(binaryTree *temp)
{
s.elements[s.top++] = temp;
}
binaryTree *pop()
{
return s.elements[--s.top];
}
int empty()
{
return s.top == 0;
}
void initialQueue() {
q.front = 0;
q.rear = 0;
}
void pushElement(binaryTree * temp) {
q.queueNum[q.rear++] = temp;
}
binaryTree *popElement() {
return q.queueNum[q.front++];
}
int emptyQueue() {
return q.front == q.rear;
}
binaryTree *creatBinaryTree(){
binaryTree *t;
int x;
scanf("%d",&x);
if(x==0){
t=NULL;
} else {
t=(binaryTree*)malloc(sizeof(binaryTree));
t->data=x;
t->lchild=creatBinaryTree();
t->rchild=creatBinaryTree();
}
return t;
}
void preOrderWithoutRecursion(binaryTree *t)
{
binaryTree *temp = t;
while(temp != NULL || s.top != 0)
{
while(temp != NULL)
{
printf("%5d",temp->data);
push(temp);
temp = temp->lchild;
}
if(s.top != 0)
{
temp = pop();
temp = temp->rchild;
}
}
printf("\n");
}
void inOrderWithoutRecursion(binaryTree *t)
{
binaryTree *temp = t;
while(temp != NULL||s.top != 0)
{
while(temp != NULL)
{
push(temp);
temp = temp->lchild;
}
if(s.top != 0)
{
temp = pop();
printf("%5d",temp->data);
temp = temp->rchild;
}
}
printf("\n");
}
void postOrderWithoutRecursion(binaryTree *root)
{
binaryTree *temp = root;
while(temp!=NULL||s.top!=0) {
while(temp!= NULL) {
temp->count=1;
push(temp);
temp=temp->lchild;
}
if(s.top!=0) {
temp=pop();
if(temp->count == 1) {
temp->count++;
push(temp);
temp=temp->rchild;
}
else {
if(temp->count==2) {
printf("%5d",temp->data);
temp=NULL;
}
}
}
}
printf("\n");
}
void preOrderWithRecursion(binaryTree *t) {
if (t == NULL) {
return;
}
printf("%5d\t",t->data);
preOrderWithRecursion(t->lchild);
preOrderWithRecursion(t->rchild);
}
void inOrderWithRecursion(binaryTree *t) {
if (t == NULL) {
return;
}
inOrderWithRecursion(t->lchild);
printf("%5d\t",t->data);
inOrderWithRecursion(t->rchild);
}
void postOrderWithRecursion(binaryTree *t){
if (t == NULL) {
return;
}
postOrderWithRecursion(t->lchild);
postOrderWithRecursion(t->rchild);
printf("%5d\t",t->data);
}
void levelOrderWithoutRecursion(binaryTree *t) {
binaryTree *temp;
pushElement(t);
while (!emptyQueue()) {
temp = popElement();
printf("%5d\t", temp->data);
if(temp->lchild) {
pushElement(temp->lchild);
}
if(temp->rchild) {
pushElement(temp->rchild);
}
}
}
int main()
{
binaryTree *root;
setNull();
root=creatBinaryTree();
printf("非递归前序遍历:\n");
preOrderWithoutRecursion(root);
printf("非递归中序遍历:\n");
inOrderWithoutRecursion(root);
printf("非递归后序遍历:\n");
postOrderWithoutRecursion(root);
printf("**********************************************************************");
printf("\n");
printf("递归前序遍历:\n");
preOrderWithRecursion(root);
printf("\n递归中序遍历:\n");
inOrderWithRecursion(root);
printf("\n递归后序遍历:\n");
postOrderWithRecursion(root);
printf("\n**********************************************************************");
printf("\n层次序非递归遍历结果:\n");
levelOrderWithoutRecursion(root);
return 0;
}