第九章17题power by spirit_only

// 9_17_main.cpp : 二叉树的非递归先序遍历

//

#include "stdafx.h"

#include <conio.h>

#include <stdlib.h>

//数据结构定义及全局常量定义

int const MAXSIZE = 50;

typedef char datatype;

struct node

{

         datatype data;

         struct node *lchild,*rchild;

};

typedef node bitree;

//建立二叉树函数,返回指向根节点的指针

bitree *CreatTree()

{

         //变量定义及初始化

         char ch;

         bitree *Q[MAXSIZE];

         int front,rear;

         bitree *root,*currentnode;

         root = NULL;

         front = 1; rear = 0;

 

         //二叉树的构造过程

         printf("请输入根节点字符:(‘#’结束,‘@’为虚节点)/t");

         ch = getche();

         while (ch != '#')

         {

                  currentnode = NULL;

                   if (ch != '@')

                   {

                            currentnode = (node *)malloc(sizeof(node));

 

                            if (currentnode == NULL)

                            {

                                     printf("分配内存时出错!");

                                     return NULL;

                            }

                            else

                            {

                                     currentnode->data = ch;

                                    

                                     currentnode->lchild = NULL;

                                     currentnode->rchild = NULL;

                            }

                   }

 

                   rear++;

                   Q[rear] = currentnode;

 

                   if (rear == 1)

                   {

                            root = currentnode;

                   }

                   else

                   {

                            if (currentnode && Q[front])

                            {

                                     if (rear%2 == 0)

                                     {

                                              Q[front]->lchild = currentnode;

 

                                     }

                                     else

                                              Q[front]->rchild = currentnode;

                                    

                            }        

                            //下面有可能出错

                            if (rear%2 == 1)

                            {

                                     front++;

                            }

 

                   }

 

                  printf("/n请输入节点字符:(‘#’结束,‘@’为虚节点)/t");

                   ch = getche();

         }

         return root;

}

//递归的先序遍历二叉树程序

void PreOrder_Recursion(bitree *root)

{

         if (root != NULL)

         {

                   {

                            printf("%c/t",root->data);

                            PreOrder_Recursion(root->lchild);

                            PreOrder_Recursion(root->rchild);

                   }

         }

         return;

}

//非递归的先序遍历二叉树程序

void PreOrder(bitree *root)

{

         bitree *stack[MAXSIZE];                   //用栈来模拟递归函数的调用

         int top;                                            //指向栈顶

         bitree *tnode;

 

         if (root != NULL)

         {

                   top = -1;

                   tnode = root;

                   while ((top != -1) || (tnode != NULL))

                   {

                            while (tnode != NULL)

                            {

                                     if (top == MAXSIZE-1)

                                     {

                                               printf("栈溢出……");

                                               return;

                                     }

                                     else

                                     {

                                              printf("%c/t",tnode->data);

                                               top++;

                                              stack[top] = tnode;

                                               tnode = tnode->lchild;

                                     }

                            }

                            tnode = stack[top];

                            top--;

                            tnode = tnode->rchild;

                   }

                   return;

         }

}

int _tmain(int argc, _TCHAR* argv[])

{

         bitree *root;

         root = CreatTree();

         if (root != NULL)

         {

                  printf("/n以下显示的是递归调用的二叉树遍历:/n");

                  PreOrder_Recursion(root);

                  printf("/n以下显示的是非递归调用的二叉树遍历:/n");

                  PreOrder(root);

         }

         printf("/n请按任意键继续……");

         getche();

         return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值