## Posted By

javert on 01/19/15

# tree_array.h

/ Published in: C++

Implementacija opcenitog stabla, polje

`struct oelement {	int value;		int fc, ns;};struct otree {	oelement el[10000];	int korijen;};typedef otree* oTree;typedef int onode;  onode FirstChildT(onode n, oTree T) {	return T->el[n].fc;} onode NextSiblingT(onode n, oTree T) {	return T->el[n].ns;} int LabelT(onode n, oTree T) {	return T->el[n].value;} void ChangeLabelT(int x, onode n, oTree T) {	T->el[n].value = x;} onode RootT(oTree T) {	return T->korijen;}  onode ParentT(onode n, oTree T) {	for(int m=0; m<10000; m++) { 		if(n == T->el[m].fc) return m; 		if(n == T->el[m].ns) return ParentT(m, T);	} 	return -1;}  void CreateT(onode x, onode n, oTree T) {	if(T->el[x].value!=0) return;	T->el[x].fc = -1;	T->el[x].ns = -1;	T->el[x].value = rand() % 99+1; 	if(T->el[n].fc==-1) {		T->el[n].fc = x;		return;	}	n = FirstChildT(n, T);	while(NextSiblingT(n, T)!=-1) n=NextSiblingT(n, T);	T->el[n].ns = x;}    oTree InitT(onode x, oTree T) {	T = new otree;	for(int i=0; i<10000; i++) {		T->el[i].fc = -1;		T->el[i].ns = -1;		T->el[i].value = 0;	}	T->korijen = x;	return T;}   void DeleteT(onode n, oTree T, bool m=1) {	if(m) {		for(int i=0; i<10000; i++) {			if(T->el[i].fc==n) {				T->el[i].fc = NextSiblingT(n, T);				T->el[n].ns = -1;				break;			}			else if(T->el[i].ns==n) {				T->el[i].ns = NextSiblingT(n, T);				T->el[n].ns = -1;				break;			}		}		DeleteT(n, T, 0);		return;	}	if(FirstChildT(n, T)!=-1) DeleteT(FirstChildT(n, T), T, 0);	for(onode n2=NextSiblingT(n, T); n2!=-1; n2=NextSiblingT(n2, T) )		DeleteT(n2, T, 0);	T->el[n].value = 0;	T->el[n].ns = -1;	T->el[n].fc = -1;}`