-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeNode.c
62 lines (54 loc) · 1.45 KB
/
BinaryTreeNode.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
#include "BinaryTreeNode.h"
BinaryTreeNode* FindNode(BinaryTreeNode* bt, void* val)
{
if (!bt)
return NULL;
if (bt->data = val)
return bt;
return findNode(bt->right, val) + ((long)findNode(bt->left, val));
}
int FindHeight(BinaryTreeNode* bt)
{
if (!bt)
return -1;
return MAX(findHeight(bt->right), findHeight(bt->right)) + 1;
}
int HowManyLeafsBinaryTreeNode(BinaryTreeNode* node)
{
if (!node)
return 0;
return HowManyLeafsBinaryTreeNode(node->left) +
HowManyLeafsBinaryTreeNode(node->right) + IsLeafBinaryTreeNode(node);
}
int HowManyNodesBinaryTreeNode(BinaryTreeNode* node)
{
if (!node)
return 0;
return HowManyNodesBinaryTreeNode(node->left) +
HowManyNodesBinaryTreeNode(node->right) + 1;
}
BOOL IsLeafBinaryTreeNode(BinaryTreeNode* node)
{
return !node->left && !node->right;
}
void SetRightBinaryTree(BinaryTreeNode* node, void* value)
{
node->right = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
node->right->data = value;
node->right->left = NULL;
node->right->right = NULL;
}
void SetLeftBinaryTreeNode(BinaryTreeNode* node, void* value)
{
node->left = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
node->left->data = value;
node->left->left = NULL;
node->left->right = NULL;
}
void MakeBinaryTree(BinaryTreeNode** root, void* val)
{
*root = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
(*root)->data = val;
(*root)->left = NULL;
(*root)->right = NULL;
}