zoukankan      html  css  js  c++  java
  • 第五章总结

    关于头文件#include<queue>的一些用法介绍,当然也可以是基本数据类型去快速创建队列。

    ②以下是一个作业、两个实践的题目

    1 bool check[100] = {false};

    这个是bool类型的数组,用来做对应下标位置节点的“是否有双亲结点”的判断。

    题一:List Leaves

    buildTree之后接着就是output(要求自上而下、从左至右的输出叶子结点):

     1 void output(node a[], int root)
     2 {
     3     int tmp, i = 0, c[100];
     4     queue<int>q;
     5     q.push(root);//根入队
     6     while(!q.empty())
     7     {
     8         tmp = q.front();
     9         q.pop();
    10         if(tmp != -1)
    11         {
    12             if(a[tmp].lchild == -1 && a[tmp].rchild == -1)
    13             {
    14                 c[i] = tmp;
    15                 i++;
    16             }
    17             q.push(a[tmp].lchild);
    18             q.push(a[tmp].rchild);
    19         }
    20      }
    21     for(int j = 0; j < i-1; j++)
    22     {
    23         cout << c[j] << " ";
    24      } 
    25     cout << c[i-1];
    26 
    27 }
    output代码

    题二:树的同构

    buildTree与上一个相似,难点在于判断的代码

    首先、明确三点:

    ①我把顺序树的数组定义成全局变量。

    ②root和R1、R2都是这个数组的下标(即数字、不是结点代表的字母)

    ③NuLL是 -1

     1 int Isomorphic(int R1, int R2)
     2 {
     3     if ((R1 == NuLL) && (R2 == NuLL))//若都等于-1就是到了叶子结点 
     4         return 1;
     5  
     6     if (((R1 == NuLL) && (R2 != NuLL)) || ((R1 != NuLL) && (R2 == NuLL)))//有棵树突出辽 
     7         return 0;
     8  
     9     if (a[R1].name != b[R2].name)//或是结点不对 
    10         return 0;
    11  
    12     if ((a[R1].Left == NuLL) && (b[R2].Left == NuLL))//左边为空就去右边看看 
    13         return Isomorphic(a[R1].Right, b[R2].Right);
    14  
    15     if (((a[R1].Left != NuLL) && (b[R2].Left != NuLL)) && ((a[a[R1].Left].name) == (b[b[R2].Left].name)))//可是如果左边不空,先看名字 
    16         return (Isomorphic(a[R1].Left, b[R2].Left) && Isomorphic(a[R1].Right, b[R2].Right));//再往下走看看 
    17     else
    18         return (Isomorphic(a[R1].Left, b[R2].Right) && Isomorphic(a[R1].Right, b[R2].Left));
    19 }
    Isomorphic代码

    前三个还比较好理解~~就还没用到递归

    题三:深入虎穴

     1 for(int i = 1; i < num_rooms + 1; i++)
     2     {
     3         cin >> maze[i].doors;//记录这个房间有几个门(度) 
     4         if(maze[i].doors != 0)
     5         {
     6             maze[i].p = new int[maze[i].doors];
     7             for(int j = 0; j < maze[i].doors; j++)
     8             {
     9                 cin >> maze[i].p[j];
    10                 if_leaf[maze[i].p[j]] = true;
    11             }
    12         }        
    13     }
    在房间中动态创建门的个数

    以上是唯一需要注意的点。(若用二维数组申请,那么内存可能会爆)

    ③可以说,这几次的目标都没能很好的完成。还是再定个小目标吧,好好学图。

  • 相关阅读:
    cordova build android get Execution failed for task ':dexArmv7Debug'
    brew install memcache get Error: Formulae found in multiple taps
    快速激活JetBrains系列产品
    NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
    `libsass` bindings not found. Try reinstalling `node-sass`?
    Cordova 快速入门记录
    perl: warning: Setting locale failed.
    httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接
    httpclient源码分析之MainClientExec
    fastjson从1.1.41升级到1.2.28的坑
  • 原文地址:https://www.cnblogs.com/Winston-wmj/p/10807927.html
Copyright © 2011-2022 走看看