-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraversing_bianry_tree.cpp
82 lines (66 loc) · 1.4 KB
/
traversing_bianry_tree.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
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *leftnode,*rightnode;
};
struct node * create()
{
struct node *newnode;
newnode=(struct node *) malloc(sizeof(struct node));
int x;
cout<<"Enter data (-1 for no data)"<<endl;
cin>>x;
if(x==-1)
{
return 0;
}
newnode->data=x;
cout<<"Enter the Left node of "<<x<<endl;
newnode->leftnode=create();
cout<<"Enter the Right node of "<<x<<endl;
newnode->rightnode=create();
return newnode;
}
void preorder(struct node *root)
{
if(root==0) return;
cout<<root->data<<" ";
preorder(root->leftnode);
preorder(root->rightnode);
}
void inorder(struct node *root)
{
if(root==0) return;
inorder(root->leftnode);
cout<<root->data<<" ";
inorder(root->rightnode);
}
void mirror(node* node) {
if(!node)return;
swap(node->leftnode,node->rightnode);
mirror(node->leftnode);
mirror(node->rightnode);
//swap(node->leftnode,node->rightnode);
}
void postorder(struct node *root)
{
if(root==0) return;
postorder(root->leftnode);
postorder(root->rightnode);
cout<<root->data<<" ";
}
int main()
{
struct node *root;
root=create();
cout<<"Display in preorder"<<endl;
preorder(root);
cout<<"Display in inorder"<<endl;
inorder(root);
// cout<<"Display in postorder"<<endl;
mirror(root);
cout<<"Display in inorder"<<endl;
inorder(root);
}