Advertisement
Guest User

functions.c

a guest
Jan 19th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "header.h"
  5.  
  6. TreeNode * create_tree_node(value data){ // returns dynamically allocated node pointer with value key = data
  7.     TreeNode *new_node;
  8.    
  9.     new_node = (TreeNode *) malloc(sizeof(TreeNode)); //Allocates memoery for new tree node
  10.  
  11.     if (!new_node) {
  12.         puts("Error mallocing");
  13.         exit(0);
  14.     }
  15.     else {
  16.         new_node->key = data;
  17.         new_node->left = NULL;
  18.         new_node->right = NULL;
  19.         return new_node; // returns initialized tree node pointer
  20.     }
  21.  
  22. }
  23.  
  24. int insert_node_by_value(TreeNode ** node, TreeNode *new_node, int (*comp_func)(value,value))
  25. //  inserts new_node in the correct available location (left = smaller, right = larger) in tree root, uses comp_func for value comparison
  26. {
  27.     if (*node == NULL){                             //if tree doesn't have a root yet
  28.         *node = new_node;                           //assign new node as tree root
  29.         return 1;
  30.     }
  31.     else   
  32.     {
  33.         if (comp_func(new_node->key,(*node)->key) == 0) { //if new_node key equal to the node key
  34.             puts("ERROR: The key you entered already exists in the tree!");
  35.             return 0;
  36.         }
  37.        
  38.         if (comp_func(new_node->key,(*node)->key) < 0) //if new_node key is smaller
  39.             insert_node_by_value(&((*node))->left,new_node,comp_func); //insert it on the left of current node
  40.         else
  41.             insert_node_by_value(&((*node))->right,new_node,comp_func); //insert on right of current node
  42.         return 1;
  43.     }
  44.  
  45. }
  46.  
  47. void print_pre_order(TreeNode *node, void (*print_func)(value)){
  48.     if (!node) return;
  49.     else{
  50.         print_func(node->key); //print current node
  51.         print_pre_order(node->left,print_func); //recursively print left side of this node
  52.         print_pre_order(node->right,print_func); //recursively print right side of this node
  53.     }
  54. }
  55.  
  56. int depth_by_key(TreeNode *node, value data, int (*comp_func)(value,value), int level){
  57.     int ret_value=0;
  58.     if (!node) return -1; // if data cannot be found at all, return -1
  59.     if (comp_func(node->key, data) == 0) return level; // if data == current key, return the level number of current key
  60.     else {
  61.         (comp_func(node->key, data) > 0) ? // is data smaller than current key?
  62.             (ret_value = depth_by_key(node->left,data,comp_func,level+1)): //yes, it's smaller, so check if it's on the left side
  63.             (ret_value = depth_by_key(node->right,data,comp_func,level+1)); //no, check the right side
  64.            
  65.         if (ret_value)
  66.             return ret_value;
  67.         else
  68.             return 0;
  69.     }
  70. }
  71.  
  72. int print_k_smallest(TreeNode *node, int k, void (*print_func)(value)){
  73.     if (node){
  74.         if (k=print_k_smallest(node->left,k,print_func)){
  75.             print_func(node->key);
  76.             k--;
  77.         }
  78.         k = print_k_smallest(node->right,k,print_func);
  79.     }
  80.     return k;
  81. }
  82.  
  83. void print_menu(){
  84.     puts("**Welcome to an implementation of ADT binary tree**");
  85.     puts("Please choose an option from the following menu:");
  86.     puts("\t1. Add node to tree");
  87.     puts("\t2. Print tree (pre-order method)");
  88.     puts("\t3. Calculate specific value depth");
  89.     puts("\t4. Print k smallest keys");
  90.     puts("\t9. Print menu again");
  91.     puts("\tx. Exit program");
  92. }
  93.  
  94. void free_tree(TreeNode *root){
  95.     TreeNode *cur;
  96.     if (!root) return;
  97.     else{
  98.         cur = root;
  99.         free_tree(cur->left);
  100.         free_tree(cur->right);
  101.         free(cur->key); //NOT WORKING!
  102.         free(cur);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement