-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtreeverticalsum.c
152 lines (126 loc) · 3.31 KB
/
btreeverticalsum.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* Find the vertical sum of a binary tree. Assume that the left and right child
* of a node makes 45 degree angle with the parent.
*/
#include "btree.h"
#include "llist.h"
#include <stdio.h>
#include <stdlib.h>
struct VerticalSum {
struct ListNode *llist;
struct ListNode *rlist;
};
void traverseVerticalSumCallback(struct TreeNode *node, int pos, int axis,
int *stop, void *data) {
struct VerticalSum *pData;
struct ListNode *lnode;
pData = data;
if (axis >= 0) {
lnode = findNthListNode(pData->rlist, axis);
if (NULL == lnode)
enQueueInt(node->val, &((pData)->rlist));
else
lnode->data.val += node->val;
} else {
lnode = findNthListNode(pData->llist, (axis * -1) - 1);
if (NULL == lnode)
enQueueInt(node->val, &((pData)->llist));
else
lnode->data.val += node->val;
}
}
struct ListNode *calculateBTreeVerticalSum(struct TreeNode *root) {
struct VerticalSum data;
struct ListNode *list;
struct ListNode *node;
data.llist = NULL;
data.rlist = NULL;
traverseTreeNodeInVerticalOrderTopDown(root, traverseVerticalSumCallback,
&data);
list = data.llist;
if (NULL == list)
list = data.rlist;
else {
list = reverseQueue(list);
node = list;
while (NULL != node->next)
node = node->next;
node->next = data.rlist;
}
return list;
}
/*
*/
int main(int argc, char *argv[]) {
struct TreeNode *root;
struct TreeNode *other;
struct ListNode *resultlist;
struct ListNode *listIter;
root = NULL;
root = malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = NULL;
root->right = NULL;
other = malloc(sizeof(struct TreeNode));
other->val = 2;
other->left = NULL;
other->right = NULL;
root->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 3;
other->left = NULL;
other->right = NULL;
root->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 6;
other->left = NULL;
other->right = NULL;
root->right->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 5;
other->left = NULL;
other->right = NULL;
root->right->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 7;
other->left = NULL;
other->right = NULL;
root->right->left->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 8;
other->left = NULL;
other->right = NULL;
root->right->left->right = other;
printf("The tree topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
printf("\n");
printf("The tree in aligned vertical axis\n");
printf("\n");
printf("\t\t1\n");
printf("\n");
printf("\t2\t\t3\n");
printf("\n");
printf("\t\t5\t\t6\n");
printf("\n");
printf("\t7\t\t8\n");
printf("\n");
printf("\n");
printf("Vertical node sum:\n");
printf("\n");
resultlist = calculateBTreeVerticalSum(root);
listIter = resultlist;
while (NULL != listIter) {
printf("\t%d", listIter->data.val);
listIter = listIter->next;
}
freeQueue(&resultlist);
printf("\n");
printf("\n");
printf("\n");
// I do not care about freeing malloced memory, OS will take care of freeing
// heap that is part of process for this one off program.
return 0;
}