-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBT_Creations2.cpp
70 lines (64 loc) · 1.26 KB
/
BT_Creations2.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
//method 2 for creating a bst that is filling all the left until -1 is given as input
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *left, *right;
Node(int value){
data=value;
left=right=NULL;
};
};
void PreOrder(Node*root){
if(root==NULL){
return;
}
cout<<root->data;
PreOrder(root->left);
PreOrder(root->right);
};
void InOrder(Node*root){
if(root==NULL){
return;
}
InOrder(root->left);
cout<<root->data;
InOrder(root->right);
};
void PostOrder(Node*root){
if(root==NULL){
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout<<root->data;
};
Node*BinaryTree(){
int x;
cin>>x;
if(x==-1){
return NULL;
}
Node*temp=new Node(x);
cout<<"Enter the left child of "<<x<<" : ";
temp->left=BinaryTree();
cout<<"Enter the right child of "<<x<<" : ";
temp->right=BinaryTree();
return temp;
}
int main()
{
//CODE FOR TREE CREATION
cout<<"Enter the root node: ";
Node*root;
root=BinaryTree();
//CODE FOR TRAVERSAL
cout<<"PreOrder: ";
PreOrder(root);
cout<<"\nPostOrder: ";
PostOrder(root);
cout<<"\nInOrder: ";
InOrder(root);
return 0;
}