-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
89 lines (77 loc) · 1.64 KB
/
utility.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "utility.h"
#include <stdio.h>
/***********************************************/
/* Local function prototypes */
/***********************************************/
bNode* CreatebNode(int data)
{
bNode *newbNode = (bNode*)malloc(sizeof(bNode));
if(newbNode == NULL)
{
printf("Error: Unable to allocate memory for new bNode.\n");
exit(1);
}
newbNode->data = data;
newbNode->right = NULL;
newbNode->left = NULL;
newbNode->nextParent = NULL;
return newbNode;
}
bool MaxMin(int a, int b, bool returnMax)
{
if (returnMax == true)
{
return a > b;
}
else
{
return a < b;
}
/*
* bool result = a < b
* return returnMax^result;
* The above two lines of code will not work for the case a = 6, b = 6
* It is supposed to return false, but returns true
*/
}
#ifdef DEBUG_HEAP
static int call = 1;
#endif
// Destroy binary tree
void destroybTree(bNode *root)
{
if (root == NULL)
{
return;
}
destroybTree(root->left);
destroybTree(root->right);
#ifdef DEBUG_HEAP
printf("%.2d : %.2d\n", call++, root->data);
#endif
free(root);
}
bRootNode* CreatebRootNode(int num)
{
bRootNode* root = (bRootNode*)malloc(sizeof(bRootNode));
root->left = NULL;
root->right = NULL;
root->lastParent = NULL;
root->level = 1;
root->col = 0;
root->data = num;
return root;
}
int power(int base, int exponent)
{
if (exponent == 0)
{
return 1;
}
int result = base;
for (int i = 1; i < exponent; i++)
{
result *= base;
}
return result;
}