-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.h
More file actions
68 lines (59 loc) · 1.79 KB
/
binarySearchTree.h
File metadata and controls
68 lines (59 loc) · 1.79 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
#include <stdio.h>
#include <stdlib.h>
struct TreeNode{
int val;
struct TreeNode* left;
struct TreeNode* right;
}TreeNode;
void insert(struct TreeNode* root, int num){
if (num <= root->val){
if (root->left){
insert(root->left, num);
}else{
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(TreeNode));
newNode->val = num;
newNode->left = NULL;
newNode->right = NULL;
root->left = newNode;
}
}else{
if (root->right){
insert(root->right, num);
}else{
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(TreeNode));
newNode->val = num;
newNode->left = NULL;
newNode->right = NULL;
root->right =newNode;
}
}
}
struct TreeNode* binarySearchTree(struct TreeNode* root, int* array, int sizeOfArray){
int index = 0;
if (!root){
root = (struct TreeNode*)malloc(sizeof(TreeNode));
root->val = array[index];
root->left = NULL;
root->right = NULL;
index++;
}
for (int i = index; i<sizeOfArray; i++){
insert(root, array[i]);
}
return root;
}
void inOrderTransversal(struct TreeNode* node){
if (node->left) inOrderTransversal(node->left);
printf("%d, ", node->val);
if (node->right) inOrderTransversal(node->right);
}
// int main(){
// struct TreeNode* root = NULL;
// int num[] = {2,6,2,1,5,7,8,3};
// int sizeofNum = sizeof(num)/sizeof(num[0]);
// struct TreeNode* bst = binarySearchTree(root, num, sizeofNum);
// int num2[] = {4,0,9};
// int sizeofNum2 = sizeof(num2)/sizeof(num2[0]);
// struct TreeNode* bst2 = binarySearchTree(bst, num2, sizeofNum2);
// inOrderTransversal(bst2);
// }