-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
85 lines (76 loc) · 2.23 KB
/
tree.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
#include <stdio.h>
#include <stdlib.h>
typedef struct element {
int VALUE;
struct element *FCHD; //First child Node pointer
struct element *NXSIB;//Next sibling Node pointer
struct element *PAR; //Parent Node pointer
} NODE;
NODE *startNode;
//Quick memory allocating function for "NODE" type
NODE *ndalloc() {
return (NODE*) malloc(sizeof(NODE));
}
//Create new empty Root node with a value
NODE *ndRoot(int rtVal) {
NODE *newRoot = ndalloc();
newRoot->VALUE = rtVal;
newRoot->PAR = NULL;
newRoot->NXSIB = NULL;
newRoot->FCHD = NULL;
return newRoot;
}
//Add children to Node
NODE *addChildrtoNode (NODE *targetNode,int childrNum) { //targetNode aka rootNode, parentNode
NODE firstChild;
firstChild.NXSIB = NULL;
NODE *currentNode = &firstChild;
//Create childrNum childrens for targetNode
for (int i=0;i<childrNum;i++) {
NODE *newNode = ndalloc();
//Input node value
printf ("Node no.%d: ",i);
int ndVal;scanf ("%d",&ndVal);
//Set relatives
newNode->VALUE = ndVal;
newNode->PAR = targetNode;
currentNode->NXSIB = newNode;
currentNode = newNode;
}
return firstChild.NXSIB; //Return the pointer to first child
}
//Create a Tree/Sub-tree, recursively
NODE *RcreateTree (NODE *targetNode,int childrNum) { //targetNode aka rootNode, parentNode
//If childrNum = 0 (no children will be created), return NULL
if (childrNum == 0) return NULL;
NODE firstChild; firstChild.NXSIB = NULL;
NODE *currentNode = &firstChild;
//Create childrNum children for targetNode
for (int i=0;i<childrNum;i++) {
NODE *newNode = ndalloc();
//Input node value
printf ("Node no.%d: ",i);
int ndVal;scanf ("%d",&ndVal);
//Set relatives
newNode->VALUE = ndVal;
newNode->PAR = targetNode;
currentNode->NXSIB = newNode;
currentNode = newNode;
}
//Create a new sub-tree rooted by children no.0 -> no. (childrNum-1)
currentNode = firstChild.NXSIB;
for (int i=0;i<childrNum;i++) {
printf ("Child no.%d:\n",i);
printf ("How many child(s): "); int ndChildr;scanf("%d",&ndChildr);
//Create Sub-tree for current node
RcreateTree (currentNode,ndChildr);
//Move to the next sibling node
currentNode = currentNode->NXSIB;
}
}
int main() {
//Initiate startNode
startNode = NULL;
printf ("\nEnd of Program. Exit now\n");
return 0;
}