-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST_ASSIGN.cpp
More file actions
155 lines (127 loc) · 2.92 KB
/
Copy pathBST_ASSIGN.cpp
File metadata and controls
155 lines (127 loc) · 2.92 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
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newnode(int data)
{
struct node * temp = (struct node *)malloc(sizeof(struct node));
temp->left = temp->right = NULL;
temp->data = data;
return temp;
}
struct node *insert_bst_Minimum_Height(int a[],int start ,int end,struct node* root)
{
if(start>end)
{
return NULL;
}
int middle = (start+end)/2;
root = newnode(a[middle]);
root->left = insert_bst_Minimum_Height(a,start,middle-1,root->left);
root->right = insert_bst_Minimum_Height(a,middle+1,end,root->right);
return root;
}
struct node *insert_bst_Maximum_Height(int data ,struct node* root)
{
if(root==NULL)
{
return newnode(data);
}
if(root->data > data)
{
root->left = insert_bst_Maximum_Height(data,root->left);
}
else
{
root->right = insert_bst_Maximum_Height(data,root->right);
}
return root;
}
void Inorder(struct node * root)
{
if(!root)
{
return;
}
Inorder(root->left);
printf("%d ",root->data);
Inorder(root->right);
}
void Preorder(struct node* root)
{
if(!root)
{
return;
}
printf("%d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
void Postorder(struct node *root)
{
if(!root)
{
return ;
}
Postorder(root->left);
Postorder(root->right);
printf("%d ",root->data);
}
void print_bst(struct node *root)
{
printf("Inorder of Maximum_Height Tree : ");
Inorder(root);
printf("\nPreorder of Maximum_Height Tree : ");
Preorder(root);
printf("\nPostorder of Maximum_Height Tree : ");
Postorder(root);
}
void delete_bst(struct node *root)
{
if(!root)
{
return;
}
delete_bst(root->left);
delete_bst(root->right);
free(root);
}
int height_bst(struct node * root) // height of single node is considered as 1 ;
{
if(root==NULL)
{
return 0;
}
return 1+max(height_bst(root->left),height_bst(root->right));
}
int main()
{
printf("Enter Number of nodes to Create BST : ");
int n;
cin >> n;
printf("Enter %d values to insert into BST : ",n);
int a[n];
struct node * root = NULL;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
root = insert_bst_Maximum_Height(a[i],root);
}
printf("\nPrinting Traversal for Maximum_Height_Bst(height : %d) :\n\n ",height_bst(root));
print_bst(root);
delete_bst(root); // frreing bst
root = NULL;
root = insert_bst_Minimum_Height(a,0,n-1,root);
printf("\n\nPrinting Traversal for Minimum_Height_Bst(height_bst : %d) : \n\n",height_bst(root));
print_bst(root);
delete_bst(root);
root = NULL;
}