-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaryTree.c
141 lines (121 loc) · 3.14 KB
/
NaryTree.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
#include "NaryTree.h"
int forwarder;
void init_nodes(node *n[], int num_nodes) {
int i;
int j = 1;
for (i = 0; i < num_nodes; i++) {
// itera en la direccion del array
n[i] = new_node(j++);
};
}
node *new_node(int id) {
node *new_node = malloc(sizeof(node));
if (new_node) {
new_node->sibling = NULL;
new_node->child = NULL;
new_node->id = id;
}
return new_node;
}
node *add_sibling(node *n, node *new_n) {
if (n == NULL)
return NULL;
while (n->sibling) {
// Recorre la lista de hermanos
if (new_n->id == n->sibling->id) {
// Si encuentra el id del nuevo nodo en el siguiente hermano
// se valida si el siguiente nodo tiene hermanos
// if(n->sibling->sibling != NULL) {
//}
printf("SE REEMPLAZA NODO EXISTENTE");
new_n->sibling = n->sibling->sibling; // Se agrega el hermano actual al nuevo nodo
return (n->sibling = new_n); // Reemplaza el nodo existente por el nuevo
}
n = n->sibling;
}
return (n->sibling = new_n); // new_node()
}
node *add_child(node *n, node *new_n) {
if (n == NULL)
return NULL;
if (n->child) {
// Si hay un hijo
// if(n->id == 1) { // si es raiz entonces
// return add_sibling(n->child, new_n); //n->child
// } else {
return add_sibling(n, new_n); // n->child
//}
} else {
return (n->child = new_n); // new_node()
}
}
void print_node_decendents(node *n, int level) {
/*
if (n->sibling != NULL) { // Recorre la lista de hermanos
printf("Sibling: %d\n",n->sibling->id); //Node: %d\n ,n->child->id
print_node_decendents(n->sibling);
}
if(n->child != NULL) { // Recorre la lista de hijos
printf("Children: %d\n",n->child->id); //Node: %d\n ,n->child->id
print_node_decendents(n->child);
}
*/
while (n != NULL) {
print_tabs(level);
printf("Node: %d\n", n->id);
if (n->child != NULL) {
// Si tiene hijos
print_tabs(level);
printf("Children:\n"); // Node: %d\n ,n->child->id
print_node_decendents(n->child, level + 1);
}
n = n->sibling;
}
}
/*
int search_forwarder(node *n, int id_dest) {
while (n != NULL) {
while(n != NULL){
n = n->child;
printf("ESTOY EN EL NODO: %d",n->id);
forwarder= search_forwarder(n, id_dest);
if(forwarder != 0){
return n->id;
}
}
n = n->sibling;
}
}
*/
int search_forwarder(node *n, int id_dest) {
while (n != NULL) {
//printf("Node: %d\n", n->id);
if ( (n->child != NULL && n->child->id == id_dest) || (n->sibling != NULL && n->sibling->id == id_dest)) { // si el hermano o el hijo del nodo actual es el destino entonces retorne el id actual
// SI encontramos el nodo entonces
//printf("Forwarder: %d\n", n->id);
forwarder = n->id;
break;
}
if (n->child != NULL) {
// Si tiene hijos
//printf("Children:\n"); // Node: %d\n ,n->child->id
//forwarder = n;
search_forwarder(n->child, id_dest);
}
//printf("Node ORIGINAL: %d\n", n->id);
//printf("Forwarder: %d\n", forwarder);
if (n->child != NULL && n->sibling != NULL && forwarder!=0 ) {
search_forwarder(n, forwarder);
break;
}
n = n->sibling;
}
//printf("Node FUERA DEL WHILE: %d\n", n->id);
return forwarder;
}
void print_tabs(int count) {
int i;
for (i = 0; i < count; i++) {
putchar('\t');
}
}