-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneral_binarytree.cpp
More file actions
197 lines (174 loc) · 3.89 KB
/
General_binarytree.cpp
File metadata and controls
197 lines (174 loc) · 3.89 KB
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
192
193
194
195
196
197
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node * treeptr;
typedef struct node
{
int level;
int key;
char Ckey[7];
treeptr leftC;
treeptr rightS;
};
typedef struct binary *Btree;
typedef struct binary
{
int key;
Btree L_child, R_child;
};
void binary(treeptr * root, int level, int key, char * Ckey);
void preorder(Btree ptr);
Btree copy(treeptr original);
treeptr search(treeptr tree, int level, int key);
/*
This program is to change general tree to binary tree.
Read a general tree from tree1,2 files ,then connecting node.
If same level, link rightchild (R_child).
After complete to make binary tree,then copy key, leftchild, rightchild
Finally print binary tree in preorder.
input: root's address, key, level and Childkey's address
output : not return value.
*/
void main()
{
treeptr root = NULL;
treeptr * rootptr = &root;
Btree btree;
int key, level;
char child[7];
FILE *fp;
fp = fopen("tree2.txt", "r");
if (fp == NULL)
{
printf(" Error !");
exit(1);
}
while (fscanf(fp, "%d %d %s", &level, &key, child) != EOF)
{
binary(rootptr, key, level, child);
}
btree = copy(root);
preorder(btree);
fclose(fp);
}
/*
This function is to change general tree to binary tree.
First, make new node(ptr) then insert general tree data in the new node.
Search whether the node is root or not.
If it is not root , then connecting leftchild(condition : same) or rightsibling(condition : another)
input : root's address, key, level and Childkey's address
output : not return value.
*/
void binary(treeptr * root, int key, int level, char * childkey)
{
treeptr ptr;
treeptr temp;
ptr = (treeptr)malloc(sizeof(*ptr));
if (ptr == NULL)
{
printf("Error : Not success malloc \n");
exit(1);
}
ptr->key = key;
ptr->level = level;
strcpy(ptr->Ckey, childkey);
ptr->leftC = ptr->rightS = NULL;
temp = search((*root), level, key);
if (temp != NULL)
{
if (temp->level + 1 == level && atoi(temp->Ckey) == key)
temp->leftC = ptr;
if (temp->level == level)
temp->rightS = ptr;
}
else
(*root) = ptr;
}
/*
This function is to display key in preorder.([order] = Parent key -> left key -> right key)
input : node's address
output :print data(key in preorder)
*/
void preorder(Btree ptr)
{
if (ptr)
{
printf("%d \n", ptr->key);
preorder(ptr->L_child);
preorder(ptr->R_child);
}
}
/*
This function is copying linked tree excepted childkey and level in binary tree and then return copied root's address.
input : original's tree root's address
output : return copied's tree root's address
*/
Btree copy(treeptr original)
{
Btree temp;
if (original != NULL)
{
temp = (Btree)malloc(sizeof(*temp));
temp->key = original->key;
temp->L_child = copy(original->leftC);
temp->R_child = copy(original->rightS);
return temp;
}
return NULL;
}
/*
This function is searching function.
With searching return node's address.
input : tree's address, key, level
output : traverse node and return node's address
*/
treeptr search(treeptr tree, int level, int key)
{
treeptr leftmove;
treeptr tmp;
if (level == 1)
{
if (tree == NULL)
return NULL;
else
return tree;
}
else
{
while (1)
{
if (tree->level == (level - 1))
{
if (atoi(tree->Ckey) == key)
return tree;
if (tree->leftC != NULL && tree->rightS != NULL)
leftmove = tree->leftC;
if (tree->rightS != NULL)
tree = tree->rightS;
else
{
if (tree->leftC == NULL)
{
tree = leftmove;
}
else
tree = tree->leftC;
}
}
else if (tree->level == level)
{
tmp = tree;
tree = tree->rightS;
if (tree == NULL)
return tmp;
}
else
{
if (tree->leftC != NULL)
tree = tree->leftC;
else
tree = tree->rightS;
}
}
}
}