Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "header.h"
- TreeNode * create_tree_node(value data){ // returns dynamically allocated node pointer with value key = data
- TreeNode *new_node;
- new_node = (TreeNode *) malloc(sizeof(TreeNode)); //Allocates memoery for new tree node
- if (!new_node) {
- puts("Error mallocing");
- exit(0);
- }
- else {
- new_node->key = data;
- new_node->left = NULL;
- new_node->right = NULL;
- return new_node; // returns initialized tree node pointer
- }
- }
- int insert_node_by_value(TreeNode ** node, TreeNode *new_node, int (*comp_func)(value,value))
- // inserts new_node in the correct available location (left = smaller, right = larger) in tree root, uses comp_func for value comparison
- {
- if (*node == NULL){ //if tree doesn't have a root yet
- *node = new_node; //assign new node as tree root
- return 1;
- }
- else
- {
- if (comp_func(new_node->key,(*node)->key) == 0) { //if new_node key equal to the node key
- puts("ERROR: The key you entered already exists in the tree!");
- return 0;
- }
- if (comp_func(new_node->key,(*node)->key) < 0) //if new_node key is smaller
- insert_node_by_value(&((*node))->left,new_node,comp_func); //insert it on the left of current node
- else
- insert_node_by_value(&((*node))->right,new_node,comp_func); //insert on right of current node
- return 1;
- }
- }
- void print_pre_order(TreeNode *node, void (*print_func)(value)){
- if (!node) return;
- else{
- print_func(node->key); //print current node
- print_pre_order(node->left,print_func); //recursively print left side of this node
- print_pre_order(node->right,print_func); //recursively print right side of this node
- }
- }
- int depth_by_key(TreeNode *node, value data, int (*comp_func)(value,value), int level){
- int ret_value=0;
- if (!node) return -1; // if data cannot be found at all, return -1
- if (comp_func(node->key, data) == 0) return level; // if data == current key, return the level number of current key
- else {
- (comp_func(node->key, data) > 0) ? // is data smaller than current key?
- (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
- (ret_value = depth_by_key(node->right,data,comp_func,level+1)); //no, check the right side
- if (ret_value)
- return ret_value;
- else
- return 0;
- }
- }
- int print_k_smallest(TreeNode *node, int k, void (*print_func)(value)){
- if (node){
- if (k=print_k_smallest(node->left,k,print_func)){
- print_func(node->key);
- k--;
- }
- k = print_k_smallest(node->right,k,print_func);
- }
- return k;
- }
- void print_menu(){
- puts("**Welcome to an implementation of ADT binary tree**");
- puts("Please choose an option from the following menu:");
- puts("\t1. Add node to tree");
- puts("\t2. Print tree (pre-order method)");
- puts("\t3. Calculate specific value depth");
- puts("\t4. Print k smallest keys");
- puts("\t9. Print menu again");
- puts("\tx. Exit program");
- }
- void free_tree(TreeNode *root){
- TreeNode *cur;
- if (!root) return;
- else{
- cur = root;
- free_tree(cur->left);
- free_tree(cur->right);
- free(cur->key); //NOT WORKING!
- free(cur);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement