Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- [Shazma Khimji] (20421561)
- CS136 Spring 2015
- Assignment 10, Problem 2b
- File: count.c
- **/
- #include "count.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <assert.h>
- struct node {
- int key;
- int cur_count;
- struct node *left;
- struct node *right;
- };
- struct count{
- struct node *root;
- };
- // see count.h for documentation
- Count create_Count(void){
- Count new = malloc(sizeof(struct count));
- new->root = NULL;
- return new;
- }
- void destroy_node(struct node *node) {
- if (NULL == node) {return;}
- destroy_node(node->left);
- destroy_node(node->right);
- free(node);
- }
- // see count.h for documentation
- void destroy_Count(Count c){
- destroy_node (c->root);
- free(c);
- }
- void insert (struct node *root, struct node *new_node) {
- if (new_node->key == root->key){
- root->cur_count +=1;
- free(new_node);
- }
- else if (new_node->key < root->key) {
- if (root->left == NULL) {
- root->left = new_node;
- } else {
- insert(root->left, new_node);
- }
- }
- else if (new_node->key > root->key) {
- if (root->right != NULL) {
- insert(root->right, new_node);
- }
- else {
- root->right = new_node;
- }
- }
- }
- // see count.h for documentation
- void next(Count c, int i){
- struct node *new = malloc(sizeof(struct node));
- new->left = NULL;
- new->right = NULL;
- new->key = i;
- new->cur_count = 1;
- if (c->root == NULL){
- c->root = new;
- }
- else {
- insert(c->root, new);
- }
- }
- int sum(struct node *a){
- if (a == NULL){
- return 0;
- }
- return sum(a->left) + a->cur_count + sum(a->right);
- }
- // see count.h for documentation
- int total(Count c){
- return sum(c->root);
- }
- int counter(struct node *a){
- int x = 0;
- if (a == NULL){
- return 0;
- }
- x++;
- return counter(a->left) + x + counter(a->right);
- }
- // see count.h for documentation
- int unique(Count c){
- return counter(c->root);
- }
- int finding(struct node *a, int i){
- if (a == NULL){
- return 0;
- }
- else if (a->key == i){
- return a->cur_count;
- }
- return finding(a->left, i) + finding(a->right, i);
- }
- // see count.h for documentation
- int count(Count c, int i){
- return finding(c->root,i);
- }
- int freqfind(struct node *a, int max, int maxposn){
- if (a == NULL){
- return 0;
- }
- else if (a->cur_count > max){
- maxposn = a->key;
- return maxposn;
- }
- else if (a->left != NULL){
- freqfind(a->left, max, maxposn);
- }
- else if (a->right != NULL){
- freqfind(a->left, max, maxposn);
- }
- return maxposn;
- }
- // see count.h for documentation
- int mostfreq(Count c){
- int max = 0;
- int maxposn = 0;
- return freqfind(c->root, max, maxposn);
- }
- void printer(struct node *a){
- if (a == NULL){
- return;
- }
- printer(a->left);
- printf("%d: %d\n", a->key, finding(a, a->key));
- printer(a->right);
- }
- // see count.h for documentation
- void stats(Count c){
- printer(c->root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement