forked from lugnitdgp/basic_datastructure_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_traversal.cpp
127 lines (101 loc) · 2.35 KB
/
tree_traversal.cpp
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<malloc.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
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)
{
if(node==NULL)
return ;
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
void preorder(struct Node* node)
{
if(node==NULL)
return ;
printf("%d ",node->data);
preorder(node->left);
preorder(node->right);
}
void postorder(struct Node* node)
{
if(node==NULL)
return ;
postorder(node->left);
postorder(node->right);
printf("%d ",node->data);
}
void copynode(struct Node *root,struct Node *copy_node)
{
if(root==NULL)
copy_node==NULL;
copy_node->data=root->data;
printf("\n%d",copy_node->data);
copynode(root->left,copy_node->left);
copynode(root->right,copy_node->right);
preorder(copy_node);
}
void equivalence(struct Node *root,struct Node *root1)
{
if(root==NULL&&root1==NULL)
return;
if(root->data==root1->data)
{
equivalence(root->left,root1->left);
equivalence(root->right,root1->right);
}
else
printf("\nNot Equivalent\n");
}
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);
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);
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 In Inorder Form:");
inorder(root);
printf("\n");
printf("tree in preorder form:");
preorder(root);
printf("\n");
printf("tree in postorder form :");
postorder(root);
copynode(root,copy_node);
printf("\n\nNew tree: ");
preorder(root1);
printf("\n");
equivalence(root,root1);
return 0;
}