zoukankan      html  css  js  c++  java
  • 软件设计师2006年11月下午试题5(C语言 树及其孩子-兄弟表示)

    [说明]
      一般的树结构常采用孩子-兄弟表示法表示,即用二叉链表作树的存储结构,链表中结点的两个链域分别指向该结点的第一个孩子结点和下一个兄弟结点。例如,图 5-1(a) 所示的树的孩子-兄弟表示如图 5-1(b)示。

    图 5-1 树及其孩子-兄弟表示示意图


      函数 LevelTraverse()的功能是对给定树进行层序遍历。例如,对图 5-1 所示的树进 行层序遍历时,结点的访问次序为:D B A E F P C 。
      对树进行层序遍历时使用了队列结构,实现队列基本操作的函数原型如下表所示:

     

    函数原型

    说明

    void InitQueue(Queue *Q)

    初始化队列

    Bool IsEmpty(Queue Q)

    判断队列是否为空,若是则返回 TRUE,否则返回 FALSE

    void EnQueue(Queue *Q,TreeNode p)

    元素入队列

    void DeQueue(Queue *Q,TreeNode *p)

    元素出队列

    Bool、Status 类型定义如下:
       typedef enum {FALSE = 0,TRUE = 1} Bool;
       typedef enum {OVERFLOW = -2,UNDERFLOW = -1,ERROR = 0,OK = 1} Status;

      树的二叉链表结点定义如下:
       typedef struct Node {
        char data;
        struct Node *firstchild,*nextbrother;
       }Node,*TreeNode;

    [函数]
      Status LevelTraverse(TreeNode root)
      { /*层序遍历树,树采用孩子-兄弟表示法,root 是树根结点的指针*/
       Queue tempQ;
       TreeNode ptr,brotherptr;
       if (!root)
        return ERROR;
       InitQueue(&tempQ);
       EnQueue(&tempQ,root) ;
       brotherptr = root -> nextbrother;
       while (brotherptr){ EnQueue(&tempQ,brotherptr);
         brotherptr=brotherptr->nextbrother;
       } /*end-while*/
       while ( !IsEmpty(tempQ) ) {
         DeQueue(&temp,&ptr);
        printf("%c\t",ptr->data);
        if (!ptr->firstchild) continue;
         EnQueue(&tempQ,ptr->firstchild);
        brotherptr = ptr->firstchild->nextbrother;
        while (brotherptr){ EnQueue(&tempQ,brotherptr);
          brotherptr=brotherptr->nextbrother;
        } /*end-while*/
       } /*end-while*/
       return OK;
      }/*LevelTraverse*/

  • 相关阅读:
    DVWA的安装过程
    《论美国的民主》读后感
    《C专家编程》读书笔记(三)
    vue中插槽(slot)的使用
    element-ui中el-table表格的使用(如何取到当前列的所有数据)
    element-ui遮罩层el-dialog的使用
    移动端开发网页时,有部分字体无故变大或变小
    Meathill的博客地址
    css让文字,字母折行
    vue-element-admin平时使用归纳
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2093730.html
Copyright © 2011-2022 走看看