-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab#10__Farhanulhaq__Task#6.cpp
More file actions
62 lines (58 loc) · 1.51 KB
/
Copy pathLab#10__Farhanulhaq__Task#6.cpp
File metadata and controls
62 lines (58 loc) · 1.51 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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
Node* insert(Node* root, int value) {
if (root ==nullptr) {
root =new Node();
root->data=value;
root->left=root->right = nullptr;
} else if (value<root->data) {
root->left=insert(root->left, value);
} else {
root->right=insert(root->right, value);
}
return root;
}
void inOrderTraversal(Node* root) {
if (root==nullptr) return;
inOrderTraversal(root->left);
cout<<root->data << " ";
inOrderTraversal(root->right);
}
void preOrderTraversal(Node* root) {
if (root == nullptr) return;
cout<<root->data << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
void postOrderTraversal(Node* root) {
if (root==nullptr) return;
postOrderTraversal(root->left);
postOrderTraversal(root->right);
cout<<root->data << " ";
}
void sortUsingBST(int arr[], int size) {
Node* root =nullptr;
for (int i=0;i < size;i++) {
root =insert(root, arr[i]);
}
cout<<"In-order traversal (Sorted array): ";
inOrderTraversal(root);
cout<<endl;
cout<<"Pre-order traversal: ";
preOrderTraversal(root);
cout<<endl;
cout<<"Post-order traversal: ";
postOrderTraversal(root);
cout<<endl;
}
int main() {
int arr[] = {50,30,20,40,70,60,80};
int size = sizeof(arr) / sizeof(arr[0]);
sortUsingBST(arr, size);
return 0;
}