-
Notifications
You must be signed in to change notification settings - Fork 6
/
mirrornarraytree
100 lines (96 loc) · 2.37 KB
/
mirrornarraytree
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
#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);
}
void mirror(struct Node *root, struct Node *parentSib,struct Node *parent)
{
if(root == NULL)return;
mirror(root->child,NULL,root);
mirror(root->siblings,root,parent);
if(root->siblings == NULL && parent)
{
parent->child=root;
}
if(parentSib)
{
root->siblings=parentSib;
}
else
{
root->siblings = NULL;
}
}
void main()
{
struct Node *root = createNArrayTree();
printNode(root);
printf("\n\n\n");
mirror(root,NULL,NULL);
printNode(root);
}