Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE_RING 11
- const int ring[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- typedef struct tnode {
- int spectVector[SIZE_RING];
- struct tnode *left;
- struct tnode *right;
- }SpectrNode_t;
- int CompareIntArrays(int arr1[SIZE_RING], int arr2[SIZE_RING]);
- void generatePerms(int arr[SIZE_RING], SpectrNode_t **head);
- void countSpectVector(SpectrNode_t **head, int perm[SIZE_RING]);
- void find (SpectrNode_t **head, int newSpectr[SIZE_RING]);
- SpectrNode_t *addTree(SpectrNode_t *p, int spectrVector[SIZE_RING])
- {
- int i = 0;
- int cond = 0, tmp_rand = 0;
- srand(time(NULL));
- if (p == NULL) {
- p = (SpectrNode_t *)malloc(sizeof(SpectrNode_t));
- for (i = 0; i < SIZE_RING; i++)
- p->spectVector[i] = spectrVector[i];
- p->left = p->right = NULL;
- }
- else if (CompareIntArrays(p->spectVector,spectrVector) == 1) {
- tmp_rand = rand();
- if (tmp_rand % 2 == 0)
- p->left = addTree(p->left,spectrVector);
- if (tmp_rand % 2 == 1)
- p->right = addTree(p ->right,spectrVector);
- }
- return p;
- }
- int main()
- {
- SpectrNode_t *root = NULL;
- int i = 0, *perm;
- perm = (int*)malloc(SIZE_RING * sizeof(int));
- for (i = 0; i < SIZE_RING; i++)
- perm[i] = i;
- generatePerms(perm, &root);
- printf("Press any key...");
- getchar();
- }
- int CompareIntArrays(int arr1[SIZE_RING], int arr2[SIZE_RING])
- {
- int res = 0;
- register int i = 0;
- for (i = 0; i < SIZE_RING; i++)
- if (arr1[i] != arr2[i])
- {
- res = 1;
- break;
- }
- return res;
- }
- void generatePerms(int arr[SIZE_RING], SpectrNode_t **head)
- {
- int c[SIZE_RING];
- register int i;
- i = 0;
- memset(c, 0, SIZE_RING * sizeof(int));
- while (i < SIZE_RING) {
- if (c[i] < i) {
- if (i % 2 == 0)
- swap(&arr[0], &arr[i]);
- else{
- swap(&arr[c[i]], &arr[i]);
- }
- countSpectVector(head, arr);
- c[i] += 1;
- i = 0;
- }
- else {
- c[i] = 0;
- i += 1;
- }
- }
- }
- void countSpectVector(SpectrNode_t **head, int perm[SIZE_RING])
- {
- int intSpectr[SIZE_RING];
- register int i = 0, k = 0;
- register int diffTmp = 0, betta = 0;
- memset(intSpectr, 0, SIZE_RING * sizeof(int));
- for (i = 0; i < SIZE_RING; i++){
- betta = ring[i];
- diffTmp = perm[( 1 + betta ) % SIZE_RING] - perm[betta];
- if (diffTmp < 0)
- diffTmp += SIZE_RING;
- intSpectr[diffTmp]++;
- }
- find(head, intSpectr);
- }
- void find (SpectrNode_t **head, int newSpectr[SIZE_RING])
- {
- SpectrNode_t *cur = *head;
- int res = 0;
- while (cur != NULL) {
- if (CompareIntArrays(cur->spectVector, newSpectr) == 0) {
- res = 1; // такой элемент уже есть в списке
- break;
- }
- find(cur->left, newSpectr);
- find(cur->right, newSpectr);
- }
- cur = *head;
- if (res == 0) {
- addTree(head, newSpectr);
- }
- }
Add Comment
Please, Sign In to add comment