forked from lugnitdgp/basic_datastructure_programs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_traversal.c
152 lines (118 loc) · 3.01 KB
/
tree_traversal.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
142
143
144
145
146
147
148
149
150
151
152
//Tree Traversal
//C++ Implementation
#include<stdio.h>
#include<malloc.h>
struct Node //Node Initialization
{
int data; //Node data
struct Node *left; //Pointer to left child node
struct Node *right; //Pointer to right child node
};
struct Node *newNode(int data)
{
struct Node *node=(struct Node*)malloc(sizeof(struct Node));
node->data=data;
node->left=NULL;
node->right=NULL;
};
void inorder(struct Node* node)//recursive function to traverse nodes using inorder method
{
if(node==NULL)
return ;
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
void preorder(struct Node* node)//recursive function to traverse nodes using preorder method
{
if(node==NULL)
return ;
printf("%d ",node->data);
preorder(node->left);
preorder(node->right);
}
void postorder(struct Node* node)//recursive function to traverse nodes using postorder method
{
if(node==NULL)
return ;
postorder(node->left);
postorder(node->right);
printf("%d ",node->data);
}
void copynode(struct Node *root,struct Node *copy_node) //Function to create a copy of tree
{
if(root==NULL){
copy_node=NULL;
return ;
}
copy_node->data=root->data;
printf(" %d",copy_node->data);
// 1st recur
copynode(root->left,copy_node->left);
// 2nd recur
copynode(root->right,copy_node->right);
}
void equivalence(struct Node *root,struct Node *root1) //Function to check if two trees are equal
{
if(root==NULL&&root1==NULL)
return;
if(root->data==root1->data)
{
equivalence(root->left,root1->left);
equivalence(root->right,root1->right);
}
else
printf("\nRoot and Root1 are Not Equivalent\n");
}
//Driver Function
int main()
{
struct Node *copy_node;
copy_node=(struct Node*)malloc(sizeof(struct Node));
copy_node=newNode(0);
copy_node->left=newNode(0);
copy_node->right=newNode(0);
copy_node->left->left=newNode(0);
copy_node->left->right=newNode(0);
copy_node->right->left=newNode(0);
copy_node->right->right=newNode(0);
struct Node *root=newNode(1);//Initialization of tree 'Root'
root->left=newNode(2);
root->right=newNode(3);
root->left->left=newNode(5);
root->left->right=newNode(4);
root->right->left=newNode(6);
root->right->right=newNode(7);
struct Node *root1=newNode(1);//Initialization of tree 'Root1'
root1->left=newNode(2);
root1->right=newNode(3);
root1->left->left=newNode(15);
root1->left->right=newNode(4);
root1->right->left=newNode(6);
root1->right->right=newNode(7);
printf("Tree Name: Root");
printf("\n");
printf("\n");
printf("In inorder form: ");
inorder(root);
printf("\n");
printf("In preorder form: ");
preorder(root);
printf("\n");
printf("In postorder form: ");
postorder(root);
printf("\n");
printf("\nCopy of binary tree 'Root' \n") ;
printf("\nIn preorder form: ");
copynode(root,copy_node);
printf("\nIn inorder form: ");
inorder(copy_node);
printf("\nIn postorder form: ");
postorder(copy_node);
printf("\n\nNew tree (Root1): ");
preorder(root1);
printf("\n");
//comparison of Root & Root1
equivalence(root,root1);
return 0;
}