Return to Snippet

Revision: 38719
at January 6, 2011 09:11 by drmarinko


Initial Code
struct element
{
       labeltype label;
       int used;
};

typedef struct bt
{
       element elements[10000];
} *btree;

typedef int node;

node RootB(btree b_stablo)
{
	if (b_stablo->elements[1].used == 1) 
		return 1;
	else
		return 0;
}

void InitB(labeltype v, btree b_stablo)
{
	for (int i = 0; i<10000; i++) 
		b_stablo->elements[i].used = 0; 
	b_stablo->elements[1].label = v; 
	b_stablo->elements[1].used = 1; 
}

labeltype LabelB(node n, btree b_stablo)
{
	return b_stablo->elements[n].label; 
}

void ChangeLabelB(labeltype v, node n, btree b_stablo)
{
	b_stablo->elements[n].label = v; 
}

int ExistsLeftChild(node n, btree b_stablo)
{
	if (b_stablo->elements[2*n].used == 1) 
		return 1;
	else
		return 0;
}

int ExistsRightChild(node n, btree b_stablo)
{
	if (b_stablo->elements[(2*n)+1].used == 1) 
		return 1;
	else
		return 0;
}

void CreateLeftB(labeltype v, node n, btree b_stablo)
{
	if (ExistsLeftChild(n, b_stablo)) 
		cout << "Lijevo dijete vec postoji\n";
	else 
	{
		b_stablo->elements[2*n].used = 1; 
		b_stablo->elements[2*n].label = v; 
	}
}

void CreateRightB(labeltype v, node n, btree b_stablo)
{
	if (ExistsRightChild(n, b_stablo))
		cout << "Desno dijete vec postoji\n";
	else 
	{
		b_stablo->elements[(2*n)+1].used = 1; 
		b_stablo->elements[(2*n)+1].label = v; 
	}
}

node LeftChildB(node n, btree b_stablo)
{
	if (ExistsLeftChild(n, b_stablo)) 
		return 2*n; 
	else
		return 0;
}

node RightChildB(node n, btree b_stablo)
{
	if (ExistsRightChild(n, b_stablo)) 
		return (2*n)+1; 
	else
		return 0;
}

node ParentB(node n, btree b_stablo)
{
     if(n == 1) 
		 return 0;
	 if(n%2) 
		 n--; 
     return n/2; 
}


void DeleteB(node n, btree b_stablo)
{
	if (ExistsLeftChild(n, b_stablo)) 
		DeleteB(LeftChildB(n, b_stablo), b_stablo); 
	if (ExistsRightChild(n, b_stablo)) 
		DeleteB(RightChildB(n, b_stablo), b_stablo);
	b_stablo->elements[n].used = 0; 
}

Initial URL

                                

Initial Description

                                

Initial Title
bstablo_polje.h

Initial Tags

                                

Initial Language
C++