Return to Snippet

Revision: 68553
at January 19, 2015 09:21 by marfioren


Initial Code
struct belement {
	int v;
	belement *lijevo;
	belement *desno;
};

typedef belement* binStablo;
typedef belement* binNode;

binStablo InitB(int x, binStablo t) {
	t = new belement;
	t->v = x;
	t->lijevo = 0;
	t->desno = 0;
	
	return t;
}

binNode RootB(binStablo t) {
	return t;
}

binNode ParentB(binNode n, binStablo t) {
	binNode r = 0;
	
	if(n==RootB(t))return 0;
	if(t->lijevo==n || t->desno==n)
		return t;
	
	if(t->lijevo)
		r=ParentB(n, t->lijevo);
	if(r)return r;
	if(t->desno)
		r=ParentB(n, t->desno);
	if(r)return r;
}

binNode LeftChildB(binNode n, binStablo t) { return n->lijevo;}
binNode RightChildB(binNode n, binStablo t) { return n->desno;}

int LabelB(binNode n, binStablo t) {
	return n->v;
}
void ChangeLabelB(int x, binNode n, binStablo t) {
	if(n!=0)
		n->v = x;
}

void CreateLeftB(int x, binNode n, binStablo t) {
	binNode nel = new belement;
	
	nel->v = x; nel->lijevo = 0; nel->desno = 0;
	n->lijevo = nel;
}

void CreateRightB(int x, binNode n, binStablo t) {
	binNode nel = new belement;
	
	nel->v = x; nel->lijevo = 0; nel->desno = 0;
	n->desno = nel;
}

void DeleteB(binNode n, binStablo t) {
	if(t!=0) {
		if(ParentB(n, t)==0) return;
		
		binNode r = ParentB(n, t);
		if(r->lijevo==n)r->lijevo=0;
		if(r->desno==n)r->desno=0;
		
		DeleteB(n, 0);
	}
	else {
		if(n->lijevo!=0)DeleteB(n->lijevo, 0);
		if(n->desno!=0)DeleteB(n->desno, 0);
		
		delete n;
	}
}

Initial URL

                                

Initial Description
binarno stablo-pokazivaci

Initial Title
binarno stablo-pokazivaci

Initial Tags

                                

Initial Language
C++