-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion21.c
65 lines (52 loc) · 1.66 KB
/
question21.c
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
/*
Form a balanced binary search tree from a given sorted array
Since a sorted array will always give a skewed tree if done directly,
METHOD1:
To avoid skewed tree, if we check for every insertion if the tree needs to be rotated in order to be
balanced. For each element we will have to check logn level for thats
Time complexity: O(nlogn) //n is the total number of elements in the array
Space complexity: O(1) //if iterative
METHOD2:
We can simply pick the middle element and elements to the left of it will be LST and to the right of it
will be RST, then again from elements to the left we pick middle elemenet and keep repeating the process.
Time complexity T(n) = 1+T(n/2)+T(n/2) = O(n) //picking middle + doing the same process on LST and RST
Space complexity = O(1) // iterative
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printTree(struct node *root){
if(root){
printf("%d\n", root->data);
printTree(root->left);
printTree(root->right);
}
}
struct node *makeBBST(int *arr,struct node *root, int start,int end){
if(start > end){
return NULL;
}
int middle = start+floor((end-start)/2);
root = newNode(arr[middle]);
root->left = makeBBST(arr,root,start,middle-1);
root->right = makeBBST(arr,root,middle+1,end);
return root;
}
int main(){
struct node *root = NULL;
int arr[] = {10,20,30,40,50,60,70};
int size = sizeof(arr)/sizeof(arr[0]);
root = makeBBST(arr, root,0,size-1);
printTree(root);
}