Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. /**
  2.  [Shazma Khimji] (20421561)
  3.  CS136 Spring 2015
  4.  Assignment 10, Problem 2b
  5.  File: count.c
  6. **/
  7.  
  8. #include "count.h"
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12. #include <assert.h>
  13.  
  14. struct node {
  15.   int key;
  16.   int cur_count;
  17.   struct node *left;
  18.   struct node *right;
  19. };
  20.  
  21. struct count{
  22.   struct node *root;
  23. };
  24.  
  25. // see count.h for documentation
  26. Count create_Count(void){
  27.   Count new = malloc(sizeof(struct count));
  28.   new->root = NULL;
  29.   return new;
  30. }
  31.  
  32. void destroy_node(struct node *node) {
  33.   if (NULL == node) {return;}
  34.   destroy_node(node->left);
  35.   destroy_node(node->right);
  36.   free(node);
  37. }
  38.  
  39.  
  40. // see count.h for documentation
  41. void destroy_Count(Count c){
  42.   destroy_node (c->root);
  43.   free(c);
  44. }
  45.  
  46. void insert (struct node *root, struct node *new_node) {
  47.   if (new_node->key == root->key){
  48.     root->cur_count +=1;
  49.     free(new_node);
  50.   }
  51.   else if (new_node->key < root->key) {
  52.     if (root->left == NULL) {
  53.       root->left = new_node;
  54.     } else {
  55.       insert(root->left, new_node);
  56.     }
  57.   }
  58.   else if (new_node->key > root->key) {
  59.     if (root->right != NULL) {
  60.       insert(root->right, new_node);
  61.     }
  62.     else {
  63.       root->right = new_node;
  64.     }
  65.   }
  66. }
  67. // see count.h for documentation
  68. void next(Count c, int i){
  69.   struct node *new = malloc(sizeof(struct node));
  70.   new->left = NULL;
  71.   new->right = NULL;
  72.   new->key = i;
  73.   new->cur_count = 1;
  74.   if (c->root == NULL){
  75.     c->root = new;
  76.   }
  77.   else {
  78.     insert(c->root, new);
  79.   }
  80. }
  81.  
  82. int sum(struct node *a){
  83.   if (a == NULL){
  84.     return 0;
  85.   }
  86.   return sum(a->left) + a->cur_count + sum(a->right);
  87. }
  88.  
  89.  
  90. // see count.h for documentation
  91. int total(Count c){
  92.   return sum(c->root);
  93. }
  94.  
  95. int counter(struct node *a){
  96.   int x = 0;
  97.   if (a == NULL){
  98.     return 0;
  99.   }
  100.   x++;
  101.   return counter(a->left) + x + counter(a->right);
  102. }
  103. // see count.h for documentation
  104. int unique(Count c){
  105.   return counter(c->root);
  106. }  
  107.  
  108. int finding(struct node *a, int i){
  109.   if (a == NULL){
  110.     return 0;
  111.   }
  112.   else if (a->key == i){
  113.     return a->cur_count;
  114.   }
  115.   return finding(a->left, i) + finding(a->right, i);
  116. }
  117.  
  118.  
  119. // see count.h for documentation
  120. int count(Count c, int i){
  121.   return finding(c->root,i);
  122. }
  123.  
  124.  
  125. int freqfind(struct node *a, int max, int maxposn){
  126.   if (a == NULL){
  127.     return 0;
  128.   }
  129.   else if (a->cur_count > max){
  130.     maxposn = a->key;
  131.     return maxposn;
  132.   }
  133.   else if (a->left != NULL){
  134.     freqfind(a->left, max, maxposn);
  135.   }
  136.   else if (a->right != NULL){
  137.     freqfind(a->left, max, maxposn);
  138.   }
  139.   return maxposn;
  140. }
  141.  
  142.  
  143. // see count.h for documentation
  144. int mostfreq(Count c){
  145.   int max = 0;
  146.   int maxposn = 0;
  147.     return freqfind(c->root, max, maxposn);
  148. }
  149.      
  150.  
  151. void printer(struct node *a){
  152.   if (a == NULL){
  153.     return;
  154.   }
  155.   printer(a->left);
  156.   printf("%d: %d\n", a->key, finding(a, a->key));
  157.   printer(a->right);
  158. }
  159.  
  160. // see count.h for documentation
  161. void stats(Count c){
  162.   printer(c->root);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement