第九章17题power by spirit_only

原创 2006年06月02日 15:42:00

// 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;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

GDCPC2015题解 by lby@SYSU_Spirit_Moon

第13届广东省大学生程序设计竞赛(GDCPC2015)非官方题解

Fake Ninja 2.7 by Spirit

  • 2007-06-21 17:36
  • 278KB
  • 下载

17、Power Query-户口信息统计

本节知识点:填充的妙用——户口信息统计 案例:比如有大量的户口信息,几千几万条,我们要快速的按照户主统计出一户人家有多少男丁,多少女丁,如下图所示。 效果如下: ...

Caused by: java.sql.SQLException: The MySQL server is running with the --read-only option so it cann

1、错误描述### Cause: java.sql.SQLException: The MySQL server is running with the --read-only option so i...

解决macOS下mysql的sql_mode=only_full_group_by问题

解决macOS下mysql的sql_mode=only_full_group_by问题原本在win10上运行的好好的sql语句,在Mac下执行带group by的命令时出现xxx……this is i...

Libgdx Caused by: java.io.IOException: couldn't load pixmap 8bit only 问题

好久没用过Libgdx这个引擎了,感觉都忘得差不多了,

MySQL Error this is incompatible with sql_mode=only_full_group_by

本次问题发生在我将WampServer工具里从 Version 2.2 升级到 Version 3.0.6 64bit        同时也把MySQL从5.5.20升级到5.7.14版本,然...

Deposit Promotion 30 Free 50 by iBET Malaysia Only(deposit bonus, Deposit Promotion, Free Register i

iBET Online Casino Malaysia provides the best promotion,just deposit RM30 and you will get RM50 for ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)