-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.c
74 lines (65 loc) · 1.57 KB
/
BinaryTree.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
#include "BinaryTree.h"
#include <stdlib.h>
#include <stdio.h>
BinaryTreeNode* new_BinaryTreeNode(Book elem, BinaryTreeNode* l, BinaryTreeNode* r){
BinaryTreeNode* node = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
node->parent = NULL;
node->data = elem;
node->left = l;
node->right = r;
if(l != NULL){
l->parent = node;
}
if(r != NULL){
r->parent = node;
}
return node;
}
void preorder_traversal(BinaryTreeNode* t){
if(t == NULL){
return;
}
printf("\t%-8d\t", t->data.key);
printf("%-20s\t", t->data.title);
printf("%-20s\t", t->data.author);
printf(" %-9d\t", t->data.existing_stocks);
printf("\n");
preorder_traversal(t->left);
preorder_traversal(t->right);
}
void inorder_traversal(BinaryTreeNode* t){
if(t == NULL){
return;
}
inorder_traversal(t->left);
printf("%d ", t->data.key);
inorder_traversal(t->right);
}
void postorder_traversal(BinaryTreeNode* t){
if(t == NULL){
return;
}
postorder_traversal(t->left);
postorder_traversal(t->right);
printf("%d ", t->data.key);
}
int BinaryTree_depth(BinaryTreeNode* t){
if(t == NULL){
return -1;
}
int left_depth = BinaryTree_depth(t->left);
int right_depth = BinaryTree_depth(t->right);
if(left_depth > right_depth){
return left_depth + 1;
}else{
return right_depth + 1;
}
}
void delete_BinaryTree(BinaryTreeNode* t){
if(t == NULL){
return;
}
delete_BinaryTree(t->left);
delete_BinaryTree(t->right);
free(t);
}