-
Notifications
You must be signed in to change notification settings - Fork 6
/
hangingnarytree
127 lines (121 loc) · 3.05 KB
/
hangingnarytree
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node
{
int data;
struct Node *child,*siblings;
};
struct Node *createNode(int value)
{
struct Node *node= (struct Node *)malloc(sizeof(struct Node));
node->data = value;
node->child = NULL;
node->siblings = NULL;
return node;
}
struct Node *createNArrayTree()
{
struct Node *root,*temp,*curr;
curr = root = createNode(1);
//children of 29
curr->child = createNode(2);
curr->child->siblings = createNode(3);
curr->child->siblings->siblings = createNode(4);
//children of 13
curr = root->child;
curr->child = createNode(5);
curr->child->siblings = createNode(6);
//children of 70
curr = root->child->child;
curr->child = createNode(11);
curr->child->siblings = createNode(12);
curr->child->siblings->siblings = createNode(13);
//children of 10
curr = root->child->child->child;
curr->child = createNode(20);
curr->child->siblings = createNode(21);
//children of 57
curr = root->child->child->siblings;
curr->child = createNode(14);
/////////////////////////////////////////////////////////////////////
//children of 75
curr = root->child->siblings;
curr->child = createNode(7);
curr->child->siblings = createNode(8);
//children of 45
curr = root->child->siblings->child;
curr->child = createNode(15);
//children of 78
curr = root->child->siblings->child->siblings;
curr->child = createNode(16);
curr->child->siblings = createNode(17);
////////////////////////////////////////////////////////////////////////////
//children of 43
curr = root->child->siblings->siblings;
curr->child = createNode(9);
curr->child->siblings = createNode(10);
//children of 55
curr = root->child->siblings->siblings->child->siblings;
curr->child = createNode(18);
curr->child->siblings = createNode(19);
return root;
}
void printNode(struct Node *root)
{
if(root == NULL)
{
return;
}
printf("%d\n",root->data);
printNode(root->child);
printNode(root->siblings);
}
struct Node *newroot;
struct Node * changeRelations(struct Node *root,struct Node *parent,struct Node *sibParent)
{
struct Node *node,*pre;
if(root == NULL)return;
if(sibParent)
sibParent->siblings=root->siblings;
else if(parent)
parent->child=root->siblings;
for(pre = node = root->child;node!=NULL;node=node->siblings)
pre = node;
if(pre == NULL)
root->child=parent;
else
pre->siblings=parent;
root->siblings=NULL;
return parent;
}
struct Node * changeRoot(struct Node *root,int value,struct Node *parent,struct Node *sibParent)
{
struct Node *pre = NULL,*sibChild;
if(root == NULL)return;
if(root->data == value)
{
newroot = root;
return changeRelations(root,parent,sibParent);
}
pre = changeRoot(root->child,value,root,NULL);
if(pre == root)
{
return changeRelations(root,parent,sibParent);
}
if(pre)
return pre;
return changeRoot(root->siblings,value,parent,root);
}
void main()
{
int findingValue=21;
struct Node *root;
root = createNArrayTree();
printf("Please Enter the ")
scanf("%d",&findingValue);
changeRoot(root,findingValue,NULL,NULL);
if(newroot)
root = newroot;
printNode(root);
}