zoukankan      html  css  js  c++  java
  • 7-31 笛卡尔树 (25分)--判断二叉搜索树,小顶堆

    先初步判断是否满足二叉搜索树和小顶堆(针对每一颗最小的子树),如果都满足,进一步判断整棵树是否满足。

      1 #include <iostream>
      2 #include <string>
      3 #include <cstring>
      4 using namespace std;
      5 typedef struct node
      6 {
      7     int K1;
      8     int K2;
      9     int L;
     10     int R;
     11 }node_arr[1001];
     12 node_arr s;
     13 int n;
     14 int f[1001];//存放前驱结点,判断是否为小顶堆的时候用到
     15 int f_is_BST1 = 0;
     16 int f_greater1 = 0;
     17 int f_greater2 = 0;
     18 void is_BST1(int i)//二叉搜索树的初步判断
     19 {
     20     if (s[i].L != -1)
     21     {
     22         if (s[i].K1 > s[s[i].L].K1)
     23         {
     24             is_BST1(s[i].L);
     25         }
     26         else
     27         {
     28             f_is_BST1 = 1;
     29             return;
     30         }
     31     }
     32     if (s[i].R != -1)
     33     {
     34         if (s[i].K1 <= s[s[i].R].K1)
     35         {
     36             is_BST1(s[i].R);
     37         }
     38         else
     39         {
     40             f_is_BST1 = 1;
     41             return;
     42         }
     43     }
     44 }
     45 void is_greater1(int i)//小顶堆的初步判断
     46 {
     47     if (s[i].L != -1)
     48     {
     49         if (s[i].K2 <= s[s[i].L].K2)
     50         {
     51             is_greater1(s[i].L);
     52         }
     53         else
     54         {
     55             f_greater1 = 1;
     56             return;
     57         }
     58     }
     59     if (s[i].R != -1)
     60     {
     61         if (s[i].K2 <= s[s[i].R].K2)
     62         {
     63             is_greater1(s[i].R);
     64         }
     65         else
     66         {
     67             f_greater1 = 1;
     68             return;
     69         }
     70     }
     71 }
     72 void is_greater2()//小顶堆进一步判断(整棵树)
     73 {
     74     for (int i = 0; i < n; i++)
     75     {
     76         int j = i;
     77         while (f[j] != -1)
     78         {
     79             if (s[f[j]].K2 > s[j].K2)
     80                 f_greater2 = 1;
     81             j = f[j];
     82         }
     83     }
     84 }
     85 bool is_BST2(int x, int min, int max)//搜索树进一步判断(整棵树)
     86 {
     87     if (x == -1)return true;
     88     if (s[x].K1 > max || s[x].K1 < min) return false;
     89     return (is_BST2(s[x].L, min, s[x].K1 - 1) && is_BST2(s[x].R, s[x].K1, max));
     90 }
     91 int main()
     92 {
     93     int r[1001] = { 0 };
     94     cin >> n;
     95     for (int i = 0; i < n; i++)f[i] = -1;
     96     for (int i = 0; i < n; i++)
     97     {
     98         struct node temp;
     99         cin >> temp.K1 >> temp.K2 >> temp.L >> temp.R;
    100         if (temp.L != -1)
    101         {
    102             f[temp.L] = i;
    103             r[temp.L] = 1;
    104         }
    105         if (temp.R != -1)
    106         {
    107             f[temp.R] = i;
    108             r[temp.R] = 1;
    109         }
    110         s[i] = temp;
    111     }
    112     int root;
    113     for (int i = 0; i < n; i++)
    114     {
    115         if (r[i] == 0)root = i;
    116     }
    117     for (int i = 0; i < n; i++)
    118         is_BST1(i);
    119     is_greater1(0);
    120     is_greater2();
    121     if (f_is_BST1 == 0 && f_greater1 == 0)
    122         if (f_greater2 == 1 || is_BST2(root, -32767, 32767) == false)
    123             cout << "NO";
    124         else
    125             cout << "YES";
    126     else
    127     {
    128         cout << "NO";
    129     }
    130     return 0;
    131 }
  • 相关阅读:
    根据第三方库spire.pdf使用指定打印机打印pdf文件
    大批量GPS坐标转百度坐标
    maven settings.xml
    linux 权限
    hyper-v 创建ubuntu虚拟机设置静态ip
    mysql 复制
    nginx
    python函数定义
    Mysql索引浅析
    Mysql 数据库锁机制浅析
  • 原文地址:https://www.cnblogs.com/2020R/p/12590987.html
Copyright © 2011-2022 走看看