Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef int Data;
- struct Node {
- Data val; // данные в узле
- struct Node * left; // левый ребенок
- size_t freq;
- struct Node * right; // правый ребенок
- };
- struct queue {
- struct Node * Data;
- size_t size;
- size_t start;
- size_t capacity;
- };
- struct queue Q;
- size_t N = 0;
- void Q_construct (struct queue * Q);
- void Q_destruct (struct queue * Q);
- void Q_realloc (struct queue * Q);
- void Q_push (struct queue * Q, struct Node t);
- struct Node Q_pop (struct queue * Q);
- void tree_floors (struct Node * tree);
- struct Node * tree_add (struct Node * tree, Data x);
- void tree_print (struct Node * tree);
- void tree_destroy (struct Node * tree);
- size_t tree_height (struct Node * tree, size_t curHeight);
- void tree_freq (struct Node * tree);
- int main()
- {
- Q_construct (&Q);
- struct Node * tree=NULL;
- Data tmp = 0;
- scanf ("%d", &tmp);
- while (tmp != 0) {
- N++;
- tree = tree_add (tree, tmp);
- scanf ("%d", &tmp);
- }
- tree_floors (tree);
- tree_destroy (tree);
- Q_destruct (&Q);
- return 0;
- }
- struct Node * tree_add (struct Node * tree, Data x)
- {
- if (tree == NULL) {
- tree = (struct Node *) calloc (1, sizeof (struct Node));
- tree->val = x;
- tree->left = NULL;
- tree->right = NULL;
- tree->freq = 1;
- return tree;
- }
- if (x > tree->val) {
- tree->right = tree_add (tree->right, x);
- }
- if (x < tree->val) {
- tree->left = tree_add (tree->left, x);
- }
- if (x == tree->val) {
- tree->freq += 1;
- N--;
- }
- return tree;
- }
- void tree_print (struct Node * tree)
- {
- if (tree == NULL)
- return;
- tree_print (tree->left);
- printf ("%d ", tree->val);
- tree_print (tree->right);
- }
- void tree_destroy (struct Node * tree)
- {
- if (tree == NULL)
- return;
- tree_destroy (tree->left);
- tree_destroy (tree->right);
- free (tree);
- }
- size_t tree_height (struct Node * tree, size_t curHeight)
- {
- if (tree == NULL)
- return curHeight;
- size_t H1, H2;
- H1 = tree_height (tree->left, curHeight + 1);
- H2 = tree_height (tree->right, curHeight + 1);
- if (H1 > H2)
- return H1;
- return H2;
- }
- void tree_freq (struct Node * tree)
- {
- if (tree == NULL)
- return;
- tree_freq (tree->left);
- printf ("%d %ld\n", tree->val, tree->freq);
- tree_freq (tree->right);
- }
- void tree_floors (struct Node * tree)
- {
- Q_push (&Q, *tree);
- while (N) {
- struct Node TR = Q_pop (&Q);
- printf ("%d ", TR.val);
- if (TR.left != NULL)
- Q_push (&Q, *(TR.left));
- if (TR.right != NULL)
- Q_push (&Q, *(TR.right));
- N--;
- }
- return;
- }
- void Q_construct (struct queue * Q)
- {
- Q->Data = (struct Node *) calloc (1000, sizeof (struct Node));
- Q->capacity = 1000;
- Q->size = 0;
- Q->start = 0;
- }
- void Q_destruct (struct queue * Q)
- {
- free(Q->Data);
- }
- void Q_realloc (struct queue * Q)
- {
- size_t New_Size = Q->capacity + 1000;
- struct Node * New_Data = (struct Node *) calloc (New_Size, sizeof (struct Node));
- for (size_t i = Q->start; i < Q->capacity; i++) {
- *(New_Data + i - Q->start) = *(Q->Data + i);
- }
- free (Q->Data);
- Q->Data = New_Data;
- Q->capacity = New_Size;
- Q->start = 0;
- Q->size -= Q->start;
- return 0;
- }
- void Q_push (struct queue * Q, struct Node t)
- {
- if (Q->size >= Q->capacity - 1)
- Q_realloc (Q);
- *(Q->Data + Q->size) = t;
- Q->size++;
- }
- struct Node Q_pop (struct queue * Q)
- {
- Q->start++;
- return (*(Q->Data + Q->start - 1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement