/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdlib.h> #define ARRAYSIZE 1000000 #define Null -1 typedef int labeltype; struct element { labeltype label; int used; }; typedef struct bt { struct element elements[ARRAYSIZE]; }*btree; typedef int node; btree InitB (labeltype x){ btree myBTree; int i; myBTree=(btree)malloc(sizeof(struct bt)); for(i=1; i<ARRAYSIZE; i++){ if(i==1){ myBTree->elements[i].label=x; myBTree->elements[i].used=1; }else{ myBTree->elements[i].used=0; } } return myBTree; } node RootB(btree T){ return 1; } node ParrentB(node n, btree T){ if(n==1){ printf("Zatrazili ste roditelja korjenskog cvora\n"); return Null; } if(T->elements[n].used==0){ printf("Ne postoji taj cvor u stablu\n"); exit(1); } return (n/2); } node LeftChildB(node n, btree T){ node child; child=n*2; if (child > ARRAYSIZE-1){ printf("\nPolje nije toliko veliko\n"); return Null; //exit(1); } if(T->elements[child].used==0){ return Null; }else{ return child; } } node RightChildB(node n, btree T){ node child; child=(n*2)+1; if (child > ARRAYSIZE-1){ printf("\nPolje nije toliko veliko\n"); return Null; //exit(1); } if(T->elements[child].used==0){ return Null; }else{ return child; } } labeltype LabelB(node n, btree T){ if(T->elements[n].used==0){ printf("Nepostojeci cvor u stablu\n"); exit(1); }else{ return T->elements[n].label; } } void ChangeLabelB(labeltype x, node n, btree T){ if(T->elements[n].used==0){ printf("Nepostojeci cvor u stablu\n"); exit(1); }else{ T->elements[n].label=x; } } void CreateLeftB(labeltype x, node n, btree T){ node child; child=n*2; if (child > ARRAYSIZE-1){ printf("\nPolje nije toliko veliko\n"); return; //exit(1); } if(T->elements[child].used==1){ printf("Lijevo dijete ovog cvora vec postoji\n"); exit(1); }else{ T->elements[child].used=1; T->elements[child].label=x; } } void CreateRightB(labeltype x, node n, btree T){ node child; child=(n*2)+1; if (child > ARRAYSIZE-1){ printf("\nPolje nije toliko veliko\n"); return; //exit(1); } if(T->elements[child].used==1){ printf("Desno dijete ovog cvora vec postoji\n"); exit(1); }else{ T->elements[child].used=1; T->elements[child].label=x; } } void DeleteBRek(node n, btree T){ if(n==Null) return; DeleteBRek(LeftChildB(n, T), T); T->elements[n].used=0; DeleteBRek(RightChildB(n, T), T); } void DeleteB(node n, btree T ){ if(ParrentB(n, T)==Null){ printf("Ne mogu obrisati korjen\n"); exit(1); } DeleteBRek(n,T); }