forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree Traversal.c
More file actions
51 lines (51 loc) · 1.21 KB
/
Tree Traversal.c
File metadata and controls
51 lines (51 loc) · 1.21 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
#include <malloc.h>
#include <stdio.h>
typedef struct treenode
{
int data;
struct treenode* lchild;
struct treenode* rchild;
} node;
node* newnode(int data)
{
node* newnode = (node*)malloc(sizeof(node));
newnode->data = data;
newnode->lchild = NULL;
newnode->rchild = NULL;
return newnode;
}
void inorder(node* root)
{
if (root == NULL)
return;
inorder(root->lchild);
printf("%d ", root->data);
inorder(root->rchild);
}
void Mirrorify(node* root, node** mirror)
{
if (root == NULL) {
mirror = NULL;
return;
}
*mirror = newnode(root->data);
Mirrorify(root->lchild, &((*mirror)->rchild));
Mirrorify(root->rchild, &((*mirror)->lchild));
}
int main()
{
printf("\n\n\t\t\t19BCT0117\n\n");
node* tree = newnode(10);
tree->lchild = newnode(15);
tree->rchild = newnode(29);
tree->lchild->lchild = newnode(13);
tree->lchild->rchild = newnode(5);
printf("\nInorder traversal of original tree:\n");
inorder(tree);
node* mirror = NULL;
Mirrorify(tree, &mirror);
printf("\nInorder traversal of mirror tree:\n");
inorder(mirror);
printf("\n");
return 0;
}