-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelSearch.c
124 lines (118 loc) · 2.54 KB
/
levelSearch.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
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
//https://thehuxley.com/problem/546?quizId=6236
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define DEBUG if(0)
typedef struct Node
{
int data;
int height;
struct Node* left;
struct Node* right;
}binaryTree;
binaryTree* iniT()
{
return NULL;
}
binaryTree* createBinTree(int item, binaryTree* left, binaryTree* right)
{
binaryTree* new = (binaryTree*) malloc(sizeof(binaryTree));
new->data = item;
new->left = left;
new->right = right;
new->height = 0;
return new;
}
int max (int a, int b)
{
return (a>b) ? a : b;
}
int height (binaryTree* bt)
{
if (bt == NULL) return -1;
else{
return 1 + max(height(bt->left),height(bt->right));
}
}
binaryTree* add(int item, binaryTree* bt)
{
if (bt == NULL)
{
bt = createBinTree(item,NULL,NULL);
}
else if (item < bt->data) //11>2
{
bt->left = add(item, bt->left);
}
else
{
bt->right = add(item, bt->right);
}
bt->height = height(bt);
return bt;
}
int isEmpty(binaryTree* bt)
{
return (bt == NULL);
}
void preOrder(binaryTree* bt,int num[], int *count, char out[])
{
char buffer[30];
if (!(isEmpty(bt)))
{
//sprintf gets what would be printed
int n = sprintf(buffer, "(%d", bt->data);
strcat(out,buffer);
//adds bt->data to num[]
num[*count] = bt->data;
*count += 1;
preOrder(bt->left,num,count,out);
preOrder(bt->right,num,count,out);
//cleans buffer and adds ) to out[]
memset(buffer,0,strlen(buffer));
sprintf(buffer,")");
strcat(out,buffer);
memset(buffer,0,strlen(buffer));
}
else
{
//cleans buffer and adds () to out[]
sprintf(buffer,"()");
strcat(out,buffer);
}
}
binaryTree* search(binaryTree* root, int target)
{
if (root == NULL || root->data == target)
{
DEBUG printf("-- %d --\n",root->data);
return root;
}
else if (root->data > target)
{
return search(root->left,target);
}
else
{
return search(root->right,target);
}
}
int main()
{
int linesOfInput;
scanf("%d",&linesOfInput);
binaryTree* bT = iniT();
int listOfValuesOfNodes[linesOfInput];
int
for (int i = 0; i < linesOfInput; i++)
{
int valueOfiNode, left, right;
scanf("%d",&valueOfiNode);
scanf("%d",&left);
scanf("%d",&right);
bT = adde(i,value, left,right,bT);
}
return 0;
}