-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree2btreebuild.c
191 lines (154 loc) · 4.31 KB
/
tree2btreebuild.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* Build binary tree from an none binary tree which # of child can be more
* than 2.
*/
#include "btree.h"
#include "btreetraverse.h"
#include <stdio.h>
#include <stdlib.h>
struct NTreeNode {
int val;
int numberOfChild;
struct NTreeNode *child[50]; // let give it an artificial limit
};
void printNTreeInMultiLevel(struct NTreeNode *list[], int listCnt, int level) {
int i;
int j;
if (0 == listCnt)
return;
for (i = listCnt - 1; i >= 0; i--) {
for (j = 0; j < level * 5; j++)
putchar(' ');
printf("%d\n", (list[i])->val);
printNTreeInMultiLevel((list[i])->child, (list[i])->numberOfChild,
level + 1);
}
}
void printNTree(struct NTreeNode *root) { printNTreeInMultiLevel(&root, 1, 0); }
struct TreeNode *ntree2btreeInternal(struct TreeNode *broot,
struct NTreeNode *list[], int listCnt) {
int i;
struct TreeNode *node = NULL;
struct TreeNode *prev = NULL;
for (i = 0; i < listCnt; i++) {
node = malloc(sizeof(struct TreeNode));
node->val = list[i]->val;
if (NULL == broot) {
broot = node;
} else {
if (NULL == prev) {
broot->left = node;
} else {
prev->right = node;
}
prev = node;
}
ntree2btreeInternal(node, list[i]->child, list[i]->numberOfChild);
}
return broot;
}
struct TreeNode *ntree2btree(struct NTreeNode *root) {
struct TreeNode *broot = NULL;
return ntree2btreeInternal(broot, &root, 1);
}
struct TreeNode *ntree2bstreeInternal(struct NTreeNode *root,
struct TreeNode *bsroot) {
int i;
if (NULL == root)
return bsroot;
bsroot = addTreeNodeAndRebalanceTree(bsroot, root->val);
for (i = 0; i < root->numberOfChild; i++)
bsroot = ntree2bstreeInternal(root->child[i], bsroot);
return bsroot;
}
struct TreeNode *ntree2bstree(struct NTreeNode *root) {
struct TreeNode *bsroot = NULL;
return ntree2bstreeInternal(root, bsroot);
}
/* The logic to convert a generic sorted tree into a binary tree is that:
*
* - right sibling of the current node is added as right branch node of the
* current node.
* - child of the current node is added as left branch nodes of the current
* node.
* - repeat above two steps for each node recursively.
*
*
* 1
* |
* +--------+--------+
* 10 20 30
* |
* +------+-----+
* 100 101 102
*
*
* binary tree:
*
* 1
* |
* +-----------+
* 10 //
* |
* +-----------------+
* 100 20
* | |
* -----+ +-----+
* 101 30
* |
* -----+
* 102
*
* in order = 100, 101, 102, 10, 20, 30, 1
*/
int main(int argc, char *argv[]) {
struct NTreeNode *root;
struct NTreeNode *other;
struct TreeNode *broot;
int n;
root = malloc(sizeof(struct NTreeNode));
root->val = 1;
root->numberOfChild = 0;
n = 0;
other = malloc(sizeof(struct NTreeNode));
other->val = 10;
other->numberOfChild = 0;
root->child[n++] = other;
other = malloc(sizeof(struct NTreeNode));
other->val = 20;
other->numberOfChild = 0;
root->child[n++] = other;
other = malloc(sizeof(struct NTreeNode));
other->val = 30;
other->numberOfChild = 0;
root->child[n++] = other;
root->numberOfChild = n;
n = 0;
other = malloc(sizeof(struct NTreeNode));
other->val = 100;
other->numberOfChild = 0;
root->child[0]->child[n++] = other;
other = malloc(sizeof(struct NTreeNode));
other->val = 101;
other->numberOfChild = 0;
root->child[0]->child[n++] = other;
other = malloc(sizeof(struct NTreeNode));
other->val = 102;
other->numberOfChild = 0;
root->child[0]->child[n++] = other;
root->child[0]->numberOfChild = n;
printf("Nary tree topology:\n");
printf("\n");
printNTree(root);
printf("\n");
printf("\n");
broot = ntree2bstree(root);
printf("Build a binary search tree, the tree topology:\n");
printf("\n");
printTreeNodeInTreeTopology(broot);
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;
}