-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-creation.cpp
More file actions
165 lines (133 loc) · 3.16 KB
/
Copy pathbinary-tree-creation.cpp
File metadata and controls
165 lines (133 loc) · 3.16 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<queue>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
node* buildTree()
{
int data;
cout<<"Enter value of data: "<<endl;
cin>>data;
// Base Case
if (data==-1)
return NULL;
node* newNode = new node(data);
cout<<"Enter value of left child: "<<endl;
newNode->left=buildTree();
cout<<"Enter value of right child: "<<endl;
newNode->right=buildTree();
return newNode;
}
void preOrder(node* &root)
{
// Preorder traversal follows NLR(Node-Left-Right) approach
// Base Case
if(root==NULL)
return;
// N
cout<<root->data<<" ";
// L
preOrder(root->left);
// R
preOrder(root->right);
}
void inOrder(node* &root)
{
// Inorder traversal follows LNR(Left-Node-Right) approach
// Base Case
if(root==NULL)
return;
// L
inOrder(root->left);
// N
cout<<root->data<<" ";
// R
inOrder(root->right);
}
void postOrder(node* &root)
{
// Postorder traversal follows LRN(Left-Right-Node) approach
// Base Case
if(root==NULL)
return;
// L
postOrder(root->left);
// R
postOrder(root->right);
// N
cout<<root->data<<" ";
}
void lvlOrderTraversal(node* root)
{
queue<node*> q;
q.push(root);
// change no.1
node* seperator= NULL;
q.push(seperator);
while (!q.empty())
{
// Tu Nikal
node* front = q.front();
q.pop();
// change no.2
if(front==NULL)
{
cout<<endl;
// idhar h ek catch jo dhyan dena h (child nodes ke level ko bhi check krna h)
if(!q.empty()) // still elements are present
q.push(seperator);
}
else
{
cout<<front->data<<" ";
// Or bacche chor jaana
if(front->left!=NULL)
q.push(front->left);
if(front->right!=NULL)
q.push(front->right);
}
}
}
int height(node* root) // count the height through number of edges
{
// Base Case
if(root==NULL)
return 0;
// Leaf node
if (root->left==NULL && root->right==NULL)
return 0;
int leftAns=height(root->left);
int rightAns=height(root->right);
return 1+max(leftAns,rightAns);
}
int main()
{
node* root = buildTree();
node* root = NULL;
root=buildTree();
cout<<"Printing using Pre-order Traversal:"<<endl;
preOrder(root);
cout<<endl;
cout<<"Printing using In-order Traversal:"<<endl;
inOrder(root);
cout<<endl;
cout<<"Printing using Post-order Traversal:"<<endl;
postOrder(root);
cout<<endl;
cout<<"Printing using Level-order Traversal:"<<endl;
lvlOrderTraversal(root);
cout<<endl;
cout<<"Height of tree:"<<height(root)<<endl;
return 0;
}