第九章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;

}

JAVA语言程序设计(基础篇)第九章答案

习题9.1 public class SimpleRectangle { double width; double height; SimpleRectangle(){ width = 1;...
  • zhenkeshawn
  • zhenkeshawn
  • 2017年03月06日 23:48
  • 1972

C++ Primer Plus第六版 第九章 编程练习答案

//第一题 //golf.h const int Len = 40; struct golf{ char fullname[Len]; int handicap; }; void setgol...
  • zhaoshu01234
  • zhaoshu01234
  • 2015年08月07日 09:35
  • 1803

java语言程序设计基础篇第九章编程练习题

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ Rectangle...
  • gyhguoge01234
  • gyhguoge01234
  • 2016年07月11日 21:23
  • 2794

GDCPC2015题解 by lby@SYSU_Spirit_Moon

第13届广东省大学生程序设计竞赛(GDCPC2015)非官方题解
  • u010929036
  • u010929036
  • 2015年05月25日 23:14
  • 3541

Fake Ninja 2.7 by Spirit

  • 2007年06月21日 17:36
  • 278KB
  • 下载

WinRAR.v3.92.KeyfileMaker.Only.(stolen.by.CORE)-FFF.zip

  • 2013年09月22日 21:34
  • 254KB
  • 下载

Multi-Resonant-Activation for a System Only Driven by Multi-State Noise

  • 2012年03月22日 11:16
  • 281KB
  • 下载

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 ...
  • ibet2u2u
  • ibet2u2u
  • 2017年03月14日 15:04
  • 110

The resource identified by this request is only capable of generating responses with characteristics

今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常: The resource identified by this request is only cap...
  • subuser
  • subuser
  • 2014年02月24日 20:23
  • 39644

JS倒计时结束关闭页面:Scripts may close only the windows that were opened by it.

在应用中,经常看见支付成功之后,会有倒计时关闭网页。去年我写过一个关闭网页的小demo HTML5S后关闭此页面 JSfunction task(){ var n=$('.autoClose'...
  • qq_31164127
  • qq_31164127
  • 2017年05月05日 09:05
  • 1321
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:第九章17题power by spirit_only
举报原因:
原因补充:

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