zoukankan      html  css  js  c++  java
  • 数据结构实验5——二叉树

    学校给了代码让填空…………非常简单

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cwchar>
      4 #include <windows.h>
      5 using namespace std;
      6 #define ok 1
      7 #define error 0
      8 
      9 //二叉链表定义
     10 typedef struct BiNode
     11 {
     12     char data;
     13     struct BiNode *lchild,*rchild;
     14 }BiTNode,*BiTree;
     15 
     16 typedef BiTree QElemType;
     17 //链队列的定义 
     18 typedef struct QNode
     19 {
     20     QElemType data;
     21     QNode *next;
     22 }*QueuePtr;
     23 
     24 typedef struct
     25 {
     26     QueuePtr front,rear;// 队头、队尾指针
     27 
     28 }LinkQueue;
     29 
     30 //初始化队列 
     31 int InitQueue(LinkQueue &Q)
     32 { // 构造一个空队列Q
     33     Q.front = Q.rear = new QNode;
     34     Q.front->next = nullptr;
     35     return ok;
     36 }
     37 //入队 
     38 int EnQueue(LinkQueue &Q,QElemType e)
     39 { // 插入元素e为Q的新的队尾元素
     40     auto p = new QNode;
     41     p->data = e;
     42     p->next = nullptr;
     43     Q.rear->next = p;
     44     Q.rear = p;
     45     return ok;
     46 }
     47 //判断队空 
     48 int QueueEmpty(LinkQueue Q)
     49 { // 若Q为空队列,则返回TRUE,否则返回FALSE
     50     if(Q.front->next==nullptr)
     51         return ok;
     52     else
     53         return error;
     54 }
     55 //出队 
     56 int DeQueue(LinkQueue &Q,QElemType &e)
     57 { // 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
     58     if(Q.front->next != nullptr){
     59         e = Q.front->data;
     60         Q.front = Q.front->next;
     61         return ok;
     62     }
     63     else
     64         return error;
     65 }
     66 
     67 void InitBiTree(BiTree &T)
     68 { // 操作结果:构造空二叉树T
     69     T=nullptr;
     70 }
     71 
     72 int BiTreeEmpty(BiTree T)
     73 { // 初始条件:二叉树T存在。操作结果:若T为空二叉树,则返回TRUE,否则FALSE
     74     if(T)
     75         return error;
     76     else
     77         return ok;
     78 }
     79 
     80 void DestroyBiTree(BiTree T)
     81 { // 初始条件:二叉树T存在。操作结果:销毁二叉树T
     82     if(T) // 非空树
     83     {
     84         if(T->lchild) // 有左孩子
     85             DestroyBiTree(T->lchild); // 销毁左孩子子树
     86         if(T->rchild) // 有右孩子
     87             DestroyBiTree(T->rchild); // 销毁右孩子子树
     88         free(T); // 释放根结点
     89         T=nullptr; // 空指针赋0
     90     }
     91 }
     92 
     93 
     94 BiTree Point(BiTree T, char s)
     95 { // 返回二叉树T中指向元素值为s的结点的指针。另加
     96     LinkQueue q;
     97     BiTree a;
     98 
     99     if(T) // 非空树
    100     {
    101         InitQueue(q); // 初始化队列
    102         EnQueue(q,T); // 根指针入队
    103         while(!QueueEmpty(q)) // 队不空
    104         {
    105             DeQueue(q,a); // 出队,队列元素赋给a
    106             if(a->data==s)
    107                 return a;
    108             if(a->lchild) // 有左孩子
    109                 EnQueue(q,a->lchild); // 入队左孩子
    110             if(a->rchild) // 有右孩子
    111                 EnQueue(q,a->rchild); // 入队右孩子
    112         }
    113     }
    114     //  cout<<a->data<<endl;
    115     return nullptr;
    116 }
    117 
    118 void Assign(BiTree p, char value)
    119 { // 给p所指结点赋值为value
    120     p->data=value;
    121 }
    122 
    123 char LeftChild(BiTree T, char e)
    124 { // 初始条件:二叉树T存在,e是T中某个结点。操作结果:返回e的左孩子。若e无左孩子,则返回"空"
    125     BiTree a;
    126     if(T) // 非空树
    127     {
    128         a=Point(T,e); // a是结点e的指针
    129         if(a&&a->lchild) // T中存在结点e且e存在左孩子
    130             return  a->lchild->data; // 返回e的左孩子的值
    131     }
    132     return ' '; // 其余情况返回空
    133 }
    134 
    135 char RightChild(BiTree T, char e)
    136 { // 初始条件:二叉树T存在,e是T中某个结点。操作结果:返回e的右孩子。若e无右孩子,则返回"空"
    137     BiTree a;
    138     if(T) // 非空树
    139     {
    140         a=Point(T,e); // a是结点e的指针
    141         if(a&&a->rchild) // T中存在结点e且e存在右孩子
    142             return a->rchild->data; // 返回e的右孩子的值
    143     }
    144     return ' '; // 其余情况返回空
    145 }
    146 
    147 
    148 
    149 char Parent(BiTree T, char e)
    150 { // 初始条件:二叉树T存在,e是T中某个结点
    151     // 操作结果:若e是T的非根结点,则返回它的双亲,否则返回"空"
    152     LinkQueue q;
    153     QElemType a; //BiTree a;
    154     if(T) // 非空树
    155     {
    156         InitQueue(q); // 初始化队列
    157         EnQueue(q,T); // 树根指针入队
    158         while(!QueueEmpty(q)) // 队不空
    159         {
    160             DeQueue(q,a); // 出队,队列元素赋给a
    161             if(a->lchild&&a->lchild->data==e||a->rchild&&a->rchild->data==e) // 找到e(是其左或右孩子)
    162                 return a->data; // 返回e的双亲的值
    163             else // 没找到e,则入队其左右孩子指针(如果非空)
    164             {
    165                 if(a->lchild)
    166                     EnQueue(q,a->lchild);
    167                 if(a->rchild)
    168                     EnQueue(q,a->rchild);
    169             }
    170         }
    171     }
    172     return ' '; // 树空或没找到e
    173 }
    174 
    175 //用算法5.3 先序遍历的顺序建立二叉链表
    176 void  CreateBiTree(BiTree &T)
    177 {
    178     //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
    179     char ch;
    180     cin>>ch;
    181     if(ch=='#')  T=nullptr;    //递归结束,建空树
    182     else
    183     {
    184         T = new BiTNode;
    185         T->data = ch;
    186         CreateBiTree(T->lchild);
    187         CreateBiTree(T->rchild);
    188     }
    189 }                                    //CreateBiTree
    190 
    191 void InOrderTraverse(BiTree T)
    192 {
    193     //中序遍历二叉树T的递归算法
    194     if(T){
    195         InOrderTraverse(T->lchild);
    196         cout<<T->data;
    197         InOrderTraverse(T->rchild);
    198     }
    199 }
    200 
    201 void PreOrderTraverse(BiTree T)
    202 {
    203     //先序遍历二叉树T的递归算法
    204     if(T){
    205         cout<<T->data;
    206         InOrderTraverse(T->lchild);
    207         InOrderTraverse(T->rchild);
    208     }
    209 }
    210 
    211 void PostOrderTraverse(BiTree T)
    212 {
    213     //后序遍历二叉树T的递归算法
    214     if(T){
    215         InOrderTraverse(T->lchild);
    216         InOrderTraverse(T->rchild);
    217         cout<<T->data;
    218     }
    219 }
    220 
    221 
    222 int BiTreeDepth(BiTree T)
    223 {
    224     int m,n;
    225     if(T == nullptr ) return 0; //如果是空树,深度为0,递归结束
    226     else
    227     {
    228         m = BiTreeDepth(T->lchild);
    229         n = BiTreeDepth(T->rchild);
    230         if(m > n) return  m + 1;
    231         else return n + 1;
    232 
    233     }
    234 }
    235 
    236 int LeadCount(BiTree T)
    237 {//求叶子数 
    238     if(T->rchild || T->lchild) return 0;
    239     else return LeadCount(T->lchild) + 1 + LeadCount(T->rchild);
    240 }
    241 
    242 int NodeCount(BiTree T)
    243 {//求结点数 
    244     if(T == nullptr) return 0;
    245     else return NodeCount(T->lchild) + 1 + NodeCount(T->rchild);
    246 }
    247 
    248 
    249 char Root(BiTree T)
    250 {//返回根结点值 
    251     if(T)
    252         return T->data;
    253     else return ' ';
    254 }
    255 
    256 int main()
    257 {
    258     BiTree T,p;
    259     char e1,e2;
    260     int islelect;
    261     SetConsoleOutputCP(65001);
    262     InitBiTree(T);
    263     cout<<"------------------------------------------------------
    ";
    264     printf("构造空二叉树后,树空否?%d(1:是 0:否)
    树的深度=%d
    ",BiTreeEmpty(T),BiTreeDepth(T));
    265     e1=Root(T);
    266     if(e1!=' ')
    267         printf("二叉树的根为: %c
    ",e1);
    268     else
    269         printf("树空,无根
    ");
    270     cout<<"------------------------------------------------------
    ";
    271 
    272     printf("请先序输入二叉树(P121图5.10(b): ABC##DE#G##F###)
    ");
    273     CreateBiTree(T);
    274     printf("建立二叉树后,树空否?%d(1:是 0:否)",BiTreeEmpty(T));
    275 
    276     e1=Root(T);
    277     if(e1!=' ')
    278         printf("二叉树的根为: %c
    ",e1);
    279     else
    280         printf("树空,无根
    ");
    281     cout<<"------------------------------------------------------
    ";
    282     cout<<" 树的叶子数="<<LeadCount(T)<<endl;
    283     cout<<" 树的结点数="<<NodeCount(T)<<endl;
    284     cout<<" 树的叶深度="<<BiTreeDepth(T)<<endl;
    285 
    286     printf("
    先序递归遍历二叉树:
    ");
    287     PreOrderTraverse(T);
    288     printf("
    中序递归遍历二叉树:
    ");
    289     InOrderTraverse(T);
    290     printf("
    后序递归遍历二叉树:
    ");
    291     PostOrderTraverse(T);
    292     cout<<"
    ------------------------------------------------------
    ";
    293 
    294     printf("
    请输入一个结点的值: ");
    295     cin>>e1;
    296     // cout<<e1;
    297     p=Point(T,e1); // p为e1的指针
    298     if(p)
    299         printf("结点的值为%c
    ",p->data);
    300     else
    301         cout<<"输入结点值错误!
    ";
    302     cout<<"
    ------------------------------------------------------
    ";
    303 
    304     printf("欲改变此结点的值,请输入新值: ");
    305     cin>>e2;
    306     Assign(p,e2);
    307     cout<<"
    ------------------------------------------------------
    ";
    308     printf("
    先序递归遍历二叉树:
    ");
    309     PreOrderTraverse(T);
    310     printf("
    中序递归遍历二叉树:
    ");
    311     InOrderTraverse(T);
    312     printf("
    后序递归遍历二叉树:
    ");
    313     PostOrderTraverse(T);
    314     cout<<"
    ------------------------------------------------------
    ";
    315 
    316     e1=Parent(T,e2);
    317     if(e1!=' ')
    318         printf("
    %c的双亲是%c",e2,e1);
    319     else
    320         printf("
    %c没有双亲
    ",e2);
    321 
    322     e1=LeftChild(T,e2);
    323     if(e1!=' ')
    324         printf("
    %c的左孩子是%c
    ",e2,e1);
    325     else
    326         printf("
    %c没有左孩子
    ",e2);
    327 
    328     e1=RightChild(T,e2);
    329     if(e1!=' ')
    330         printf("
    %c的右孩子是%c
    ",e2,e1);
    331     else
    332         printf("
    %c没有右孩子
    ",e2);
    333     cout<<"
    ------------------------------------------------------
    ";
    334 
    335     DestroyBiTree(T);
    336     system("pause");
    337     return 0;
    338 }
  • 相关阅读:
    .Net环境下的缓存技术介绍
    JavaScript 全局对象
    JavaScript escape() 函数
    实现DIV拖动
    巧用Ajax的beforeSend 提高用户体验
    js中ie8下不识别indexOf的解决办法
    页面弹窗效果HTML
    让html页面中的文字不可选中
    MVC路由规则
    C# Math.Round
  • 原文地址:https://www.cnblogs.com/moomight/p/11739544.html
Copyright © 2011-2022 走看看