@@ -23,6 +23,8 @@ void doInOrder();
23
23
struct node * newNode (int value ,struct node * parent );
24
24
struct node * insertN (int value ,struct node * root ,struct node * parent );
25
25
26
+ // Printing
27
+ void inOrder (struct node * root );
26
28
27
29
int main (){
28
30
// Initialize
@@ -86,6 +88,8 @@ void doDelete(){
86
88
}
87
89
88
90
void doSearch (){
91
+ int toFind ;
92
+
89
93
system ("cls" );
90
94
printf ("--- Search ---\n" );
91
95
@@ -100,7 +104,9 @@ void doInOrder(){
100
104
system ("cls" );
101
105
printf ("--- Printing ---\n" );
102
106
103
- for (i = 0 ; i < tRoot -> keyCount - 1 ; i ++ ) printf ("%d " , tRoot -> value [i ]);
107
+ //for (i = 0; i < tRoot->keyCount - 1; i++) printf("%d ", tRoot->value[i]);
108
+ if (tRoot != NULL ) inOrder (tRoot );
109
+ else printf ("\nThe tree is empty!\n" );
104
110
105
111
printf ("\n\n\nPress any key to continue...\n" );
106
112
getch ();
@@ -115,7 +121,10 @@ struct node* newNode(int value,struct node *parent){
115
121
root -> parent = parent ;
116
122
117
123
int i ;
118
- for (i = 1 ; i < MAX ; i ++ ) root -> value [i ] = (int )NULL ;
124
+ for (i = 1 ; i < MAX ; i ++ ) {
125
+ root -> value [i ] = (int )NULL ; // Typcast and wrap NULL into an int for equality purposes
126
+ root -> keys [i ] = NULL ;
127
+ }
119
128
120
129
return root ;
121
130
}
@@ -126,7 +135,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
126
135
if (root == NULL ){ // If the tree doesn't have a value yet
127
136
return newNode (value ,parent );
128
137
} else {
129
- while (1 ){
138
+ while (1 ){ // Loop for inserting data in a node rect
130
139
if ( (void * )root -> value [i ] != NULL ){ // If there is a value in the current box
131
140
if (value == root -> value [i ]){ // If the value is already inserted
132
141
printf ("Data Already inserted!\n" );
@@ -152,8 +161,26 @@ struct node* insertN(int value,struct node *root,struct node *parent){
152
161
153
162
i ++ ;
154
163
}
164
+
165
+ if (root -> keyCount > treeOrder ){ // Overflow
166
+ printf ("Overflow! Splitting and Promoting...\n" );
167
+ struct node * leftHalf = (struct node * )malloc (sizeof (struct node ));
168
+ struct node * rightHalf = (struct node * )malloc (sizeof (struct node ));
169
+ }
155
170
}
156
171
157
172
return root ;
158
173
}
159
174
175
+ // Printing
176
+ void inOrder (struct node * root ){
177
+ int i ;
178
+ if (root == NULL ) return ;
179
+ else {
180
+ for (i = 0 ; i < root -> keyCount - 1 ; i ++ ){ // -1 since left and right key of every data box
181
+ inOrder (root -> keys [i ]);
182
+ printf ("~%d~\n" ,root -> value [i ]);
183
+ }
184
+ }
185
+ }
186
+
0 commit comments