2-3 tree Algorism bug(memory allocation)

i am trying to bulid 2-3 tree insertion function,it seem successfully in the beginning(from 1 to 14),but when i input 15,the output will beocme infinite and the program crashed.I used 3days to debug but still cannot find out the problem(maybe memory allocation?),thx!!
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<stdio.h>
#include<stdlib.h>
#define m 3 //n=number of data,m=max child,rounding up((m-1)/2)-1=<n<=m-1

typedef struct bNode{
	int indegree;
	int data[m];/*data store at most store 2 data and store 3 child;when 3 child ,it will split*/
	struct bNode *pointer[m+1];
	struct bNode **parent;//use 2pointer to store the address of the B;
}bNode;

bNode **B=(bNode**)malloc(sizeof(bNode*));

void printbtree(bNode *B){
	
	if(B==NULL){
	printf("\n");
	return;	
	}
	
int i=0;

while(i<B->indegree){
printf("%d\n",B->data[i]);
i++;	
}

for(i=0;i<B->indegree+1;i++){
printf("%d:",i);
printbtree(B->pointer[i]);	
}

}

void list_Init(int *a){
	int i;
	for(i=0;i<m;i++){
		a[i]=INT_MAX;
	}
}

void AInsertion(bNode **cur,bNode **pre);

void list_Insert(bNode *cur,int *a,bNode **b,int data,bNode **pre){
	int i,j;	
	for(i=0;i<m;i++){
		if(data<=a[i])
		break;
	}
	for(j=m-1;j>i;j--){
		a[j]=a[j-1];
		b[j+1]=b[j];
	}

	if(!pre)
	cur->indegree++;
	a[i]=data;
	

	b[i]=cur->pointer[0];
	b[i+1]=cur->pointer[1];
	bNode *q=cur->pointer[0];


	if(q){
		q->parent=pre;
		
	}
	q=cur->pointer[1];
		if(q){
		q->parent=pre;
	}
	

}
void InitBNode(bNode **N){
(*N)=(bNode*)malloc(sizeof(bNode));

int i;

(*N)->indegree=0;

for(i=0;i<m+1;i++){
(*N)->pointer[i]=NULL;	
}

(*N)->parent=NULL;

list_Init((*N)->data);
}

bNode** B_Tree_Search(bNode **B,bNode*** pre,int ele){
int i=0;

if((*B)==NULL){
	return NULL;
}

while(i<(*B)->indegree){
	if(ele>(*B)->data[i]){
		i++;
	}
	else if (ele<(*B)->data[i]&&(*B)->pointer[i]!=NULL){
		*pre=B;
	
		return B_Tree_Search(&(*B)->pointer[i],pre,ele);
	}
	else{
		break;
	}
}

if(i==m-1&&(*B)->pointer[i]!=NULL||(*B)->pointer[i]!=NULL){//if the pointer is not null,then can go to the next stage,otherwise stay at the same stage
	*pre=B;

	return B_Tree_Search(&(*B)->pointer[i],pre,ele);
}
else{
	return B;
}

}

void split(bNode **cur){//e,g, [6,7,8] spilie to (left)<6>,(root)<7>,<8>(right)
bNode *a=NULL;

InitBNode(&a);
a->data[0]=(*cur)->data[0];
a->pointer[0]=(*cur)->pointer[0];
a->pointer[1]=(*cur)->pointer[1];
a->indegree++;

bNode *c=NULL;
InitBNode(&c);
c->data[0]=(*cur)->data[2];
c->pointer[0]=(*cur)->pointer[2];
c->pointer[1]=(*cur)->pointer[3];
c->indegree++;

bNode **b=(bNode**)malloc(sizeof(bNode*));
*b=NULL;
InitBNode(b);
(*b)->data[0]=(*cur)->data[1];
(*b)->pointer[0]=a;
(*b)->pointer[1]=c;
(*b)->indegree++;

(*b)->parent=(*cur)->parent;
c->parent=b;
a->parent=b;



free(*cur);
(*cur)=*b;



if((*cur)->parent){

AInsertion(cur,(*cur)->parent);

}


}

void AInsertion(bNode **cur,bNode **pre){//******
	(*pre)->indegree++;

	list_Insert(*cur,(*pre)->data,(*pre)->pointer,(*cur)->data[0],pre);//current node,previous data array,previoud pointer array,previous node



	if((*pre)->indegree==m){
		split(pre);
	}


}

void Insertion(bNode **pre,bNode **cur,int data){
int i,n;

	list_Insert((*cur),(*cur)->data,(*cur)->pointer,data,NULL);//current node,current data array,previous pointer,data,previous node

	if(pre){
	(*cur)->parent=pre;	
	}	
	else{
	(*cur)->parent=NULL;	

	}
			
	

	
	if((*cur)->indegree==m){
		split(cur);
	}

	
}

void B_Tree_Insertion(bNode **B){//this B not equal to main B
	int data;
	bNode **pre=NULL,**cur=NULL;
	int i; 

	for(i=1;i<15;i++){//<--15 chagne to 16,problem will occur
		pre=NULL;
	cur= B_Tree_Search(B,&pre,i);//pre to store the address of B
	Insertion(pre,cur,i);	



	}
}



int main(){
int choice;
InitBNode(B);


B_Tree_Insertion(B);
 printbtree(*B);
	

}
Last edited on
Put some spaces in line 113 and take a close look at it.
I don't know if that's the source of your problem, but it can't be correct.
(gcc complains that you should use some parens to explicitly indicate your grouping, which is why that line jumped out at me so easily.)

Also, you're storing 4 pointers per node, but it seems to me that you would only need 3. Maybe I'm not thinking about it correctly.
Last edited on
Duplicate of http://www.cplusplus.com/forum/windows/245900/

Please DON'T make multiple threads for the same question.
thx all of u, i solve it finally.
Topic archived. No new replies allowed.