1
+ //https://thehuxley.com/problem/546?quizId=6236
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+ #include <math.h>
6
+ #include <ctype.h>
7
+ #define DEBUG if(0)
8
+ typedef struct Node
9
+ {
10
+ int data ;
11
+ int height ;
12
+ struct Node * left ;
13
+ struct Node * right ;
14
+ }binaryTree ;
15
+
16
+ binaryTree * iniT ()
17
+ {
18
+ return NULL ;
19
+ }
20
+ binaryTree * createBinTree (int item , binaryTree * left , binaryTree * right )
21
+ {
22
+ binaryTree * new = (binaryTree * ) malloc (sizeof (binaryTree ));
23
+ new -> data = item ;
24
+ new -> left = left ;
25
+ new -> right = right ;
26
+ new -> height = 0 ;
27
+ return new ;
28
+ }
29
+ int max (int a , int b )
30
+ {
31
+ return (a > b ) ? a : b ;
32
+ }
33
+ int height (binaryTree * bt )
34
+ {
35
+ if (bt == NULL ) return -1 ;
36
+ else {
37
+ return 1 + max (height (bt -> left ),height (bt -> right ));
38
+ }
39
+ }
40
+ binaryTree * add (int item , binaryTree * bt )
41
+ {
42
+ if (bt == NULL )
43
+ {
44
+ bt = createBinTree (item ,NULL ,NULL );
45
+ }
46
+ else if (item < bt -> data ) //11>2
47
+ {
48
+ bt -> left = add (item , bt -> left );
49
+ }
50
+ else
51
+ {
52
+ bt -> right = add (item , bt -> right );
53
+ }
54
+
55
+ bt -> height = height (bt );
56
+ return bt ;
57
+ }
58
+ int isEmpty (binaryTree * bt )
59
+ {
60
+ return (bt == NULL );
61
+ }
62
+ void preOrder (binaryTree * bt ,int num [], int * count , char out [])
63
+ {
64
+ char buffer [30 ];
65
+ if (!(isEmpty (bt )))
66
+ {
67
+ //sprintf gets what would be printed
68
+ int n = sprintf (buffer , "(%d" , bt -> data );
69
+ strcat (out ,buffer );
70
+ //adds bt->data to num[]
71
+
72
+ num [* count ] = bt -> data ;
73
+ * count += 1 ;
74
+
75
+ preOrder (bt -> left ,num ,count ,out );
76
+ preOrder (bt -> right ,num ,count ,out );
77
+
78
+ //cleans buffer and adds ) to out[]
79
+ memset (buffer ,0 ,strlen (buffer ));
80
+ sprintf (buffer ,")" );
81
+ strcat (out ,buffer );
82
+ memset (buffer ,0 ,strlen (buffer ));
83
+ }
84
+ else
85
+ {
86
+ //cleans buffer and adds () to out[]
87
+ sprintf (buffer ,"()" );
88
+ strcat (out ,buffer );
89
+ }
90
+ }
91
+ binaryTree * search (binaryTree * root , int target )
92
+ {
93
+ if (root == NULL || root -> data == target )
94
+ {
95
+ DEBUG printf ("-- %d --\n" ,root -> data );
96
+ return root ;
97
+ }
98
+ else if (root -> data > target )
99
+ {
100
+ return search (root -> left ,target );
101
+ }
102
+ else
103
+ {
104
+ return search (root -> right ,target );
105
+ }
106
+ }
107
+ int main ()
108
+ {
109
+ int linesOfInput ;
110
+ scanf ("%d" ,& linesOfInput );
111
+ binaryTree * bT = iniT ();
112
+ int listOfValuesOfNodes [linesOfInput ];
113
+ int
114
+ for (int i = 0 ; i < linesOfInput ; i ++ )
115
+ {
116
+ int valueOfiNode , left , right ;
117
+ scanf ("%d" ,& valueOfiNode );
118
+ scanf ("%d" ,& left );
119
+ scanf ("%d" ,& right );
120
+ bT = adde (i ,value , left ,right ,bT );
121
+ }
122
+
123
+ return 0 ;
124
+ }
0 commit comments