Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- struct node
- {
- float data;
- int count;
- struct node *left;
- struct node *right;
- };
- struct node* newNode(float data)
- {
- struct node* node = (struct node*)malloc(sizeof(struct node));
- node->data = data;
- node->count = 1;
- node->left = NULL;
- node->right = NULL;
- return node;
- }
- int addNode(struct node *root, struct node *add)
- {
- struct node *currentNode = root;
- while(1)
- {
- if(abs(add->data - currentNode->data) < 0.0000001)
- {
- currentNode->count += 1;
- free(add);
- add = NULL;
- return 0;
- }
- else if(add->data > currentNode->data)
- {
- if(currentNode->right == NULL)
- {
- currentNode->right = add;
- return 1;
- }
- else
- {
- currentNode = currentNode->right;
- continue;
- }
- }
- else if(add->data < currentNode->data)
- {
- if(currentNode->left == NULL)
- {
- currentNode->left = add;
- return 1;
- }
- else
- {
- currentNode = currentNode->left;
- continue;
- }
- }
- }
- }
- struct node* removeNode(struct node *root, float removeData)
- {
- if(abs(root->data - removeData) < 0.000001)
- {
- root->count -= 1;
- if(root->count < 1)
- {
- struct node *left = root->left;
- struct node *right = root->right;
- if(left == NULL && right != NULL)
- {
- free(root);
- root = NULL;
- return right;
- }
- else if(right == NULL && left != NULL)
- {
- free(root);
- root = NULL;
- return left;
- }
- else if(right != NULL && left != NULL)
- {
- free(root);
- root = NULL;
- addNode(left, right);
- return left;
- }
- else
- {
- free(root);
- root = NULL;
- return NULL;
- }
- }
- else
- {
- return root;
- }
- }
- else if((root->data < removeData) && (root->right != NULL))
- {
- root->right = removeNode(root->right, removeData);
- return root;
- }
- else if((root->data > removeData) && (root->left != NULL))
- {
- root->left = removeNode(root->left, removeData);
- return root;
- }
- else
- {
- return root;
- }
- }
- void displayTree(struct node *root)
- {
- if(root->left != NULL && root->right != NULL)
- {
- displayTree(root->left);
- printf("Node Value: %f Node Count: %d\n", root->data, root->count);
- displayTree(root->right);
- }
- else if(root->right != NULL)
- {
- printf("Node Value: %f Node Count: %d\n", root->data, root->count);
- displayTree(root->right);
- }
- else if(root->left != NULL)
- {
- displayTree(root->left);
- printf("Node Value: %f Node Count: %d\n", root->data, root->count);
- }
- else
- {
- printf("Node Value: %f Node Count: %d\n", root->data, root->count);
- }
- return;
- }
- void deleteTree(struct node *root)
- {
- if(root->left != NULL)
- {
- deleteTree(root->left);
- }
- if(root->right != NULL)
- {
- deleteTree(root->right);
- }
- free(root);
- root = NULL;
- return;
- }
- int main(const int argc, const char *argv[])
- {
- srand(time(NULL));
- float randMax = 1000;
- struct node* root = newNode(0.0);
- for(int i = -10000 ; i <= 10000 ; i++ )
- {
- addNode(root, newNode(((float)rand()/(float)(RAND_MAX)) * randMax - 500.0));
- }
- int test = addNode(root, newNode(1.0));
- displayTree(root);
- root = removeNode(root, 1.0);
- printf("\n\n");
- for(int i = -5000; i <= 5000 ; i++)
- {
- root = removeNode(root, ((float)rand()/(float)(RAND_MAX)) * randMax - 500.0);
- }
- displayTree(root);
- deleteTree(root);
- }
Add Comment
Please, Sign In to add comment