Return to Snippet

Revision: 68481
at January 19, 2015 05:14 by ivan_uzarevic


Initial Code
#ifndef bstablo_polje
#define bstablo_polje

#include <iostream>
using namespace std;

struct elem {
	int labela;
	int iskoristen;
};

struct bstablo {
	elem cvor[10000];
};

typedef int vrijed;
bstablo *stablo = new bstablo;

vrijed parentB (vrijed poz, bstablo *stablo) {
	if (poz == 1)
		return -1;
	return (poz/2);
}

vrijed leftchildB(vrijed poz, bstablo *stablo) {
	if (stablo->cvor[poz].iskoristen == -1)
		return 0;
	if (stablo->cvor[poz*2].iskoristen == -1)
		return 0;
	return poz * 2;
}

vrijed rightchildB(vrijed poz, bstablo *stablo){
	if (stablo->cvor[poz].iskoristen == -1)
		return 0;
	if (stablo->cvor[poz*2+1].iskoristen == -1)
		return 0;
	return (poz * 2 + 1);
}

int labelB(vrijed poz, bstablo *stablo) {
	if (stablo->cvor[poz].iskoristen == -1)
		return -1;
	if (poz == -1)
		return -1;
	return stablo->cvor[poz].labela;
}

void changelabelB(int x, vrijed poz, bstablo *stablo) {
	if (stablo->cvor[poz].iskoristen == -1)
		return;
	stablo->cvor[poz].labela = x;
	return;
}

vrijed rootB(bstablo *stablo) {
	if (stablo->cvor[1].iskoristen == 1)
		return 1;
	else
		return -1;
}

void createleftB (int x, vrijed poz, bstablo *stablo) {
	if (stablo->cvor[poz].iskoristen == -1) {
		cout << "Pogreska. Cvor uopce ne postoji" << endl;
		return;
	}
	if (stablo->cvor[poz * 2].iskoristen == 1) {
		cout << "Pogresaka. Lijevo dijete vec postoji" << endl;
		return;
	}
	stablo->cvor[poz*2].labela = x;
	stablo->cvor[poz*2].iskoristen = 1;
	return;	
}

void createrightB(int x, vrijed poz, bstablo *stablo) {
	if (stablo->cvor[poz].iskoristen == -1) {
		cout << "Pogreska. Cvor uopce ne postoji" << endl;
		return;
	}
	if (stablo->cvor[poz*2+1].iskoristen == 1) {
		cout << "Pogresaka. Desno dijete vec postoji" << endl;
		return;
	}
	stablo->cvor[poz*2+1].labela = x;
	stablo->cvor[poz*2+1].iskoristen = 1;
	return;		
}

void deleteB(vrijed poz, bstablo *stablo) {
	int lijevo, desno;
	if (stablo->cvor[poz].iskoristen == 1) {
		lijevo = leftchildB(poz,stablo);
		if(stablo->cvor[lijevo].iskoristen == 1)
			deleteB(lijevo,stablo);
		desno = rightchildB(poz,stablo);
		if (stablo->cvor[desno].iskoristen == 1)
			deleteB(desno,stablo);
		stablo->cvor[poz].iskoristen = -1;
		stablo->cvor[poz].labela = -1;
	}
	else
		cout << "Cvor odabran za brisanje ne postoji" << endl;
}

void initB (int x, bstablo *stablo) {
	for (int i = 0; i < 10000; i++) {
		stablo->cvor[i].iskoristen = -1;
		stablo->cvor[i].labela = -1;
	}
	stablo->cvor[1].iskoristen = 1;
	stablo->cvor[1].labela = x;
}

vrijed provjera (vrijed poz) {
	if (stablo->cvor[poz].iskoristen == 1)
		return 1;
	else
		return 0;
}
#endif

Initial URL


Initial Description
Zaglavlje sa funkcijama, izvedenih pomoću polja, za rad sa binarnim stablom

Initial Title
bstablo_polje.h

Initial Tags
data, array, c++

Initial Language
C++