简单的二叉搜索树建树过程
#include<stdio.h> #include<string.h> int tree1[1500], tree2[1500]; char s[1000], k[1000]; void build1() { int i; tree1[1] = s[0] - ‘0‘; int y = strlen(s); for (i = 1; i < y; i++) { int now = 1; while (1) { if (tree1[now] == -1) break; else { if (tree1[now] < s[i] - ‘0‘)now = now * 2 + 1; else now = now * 2; } } tree1[now] = s[i] - ‘0‘; } } void build2() { int i; tree2[1] = k[0] - ‘0‘; int y = strlen(k); for (i = 1; i < y; i++) { int now = 1; while (1) { if (tree2[now] == -1) break; else { if (tree2[now] < k[i] - ‘0‘) now = now * 2 + 1; else now = now * 2; } } tree2[now] = k[i] - ‘0‘; } } int main() { int n, i, j, g; while (~scanf("%d", &n)) { if (n == 0) break; scanf("%s", s); for (i = 0; i < 1350; i++) tree1[i] = -1; build1(); for (i = 0; i < n; i++) { for (g = 0; g < 1350; g++) tree2[g] = -1; scanf("%s", k); build2(); for (j = 1; j < 1400; j++) { if (tree1[j] != tree2[j]) break; } if (j == 1400) puts("YES"); else puts("NO"); } } return 0; }
时间: 2024-10-05 09:04:50