struct element {
	int l;
	element* left, * right;
};

typedef element bt;
typedef element* btree;
typedef element* node;

#define ERR NULL

node ParentB(node n, btree T) {
	if(T==n) return ERR;
	if(T->left==n || T->right==n) return T;
	node p = NULL;
	if(T->left) p = ParentB(n, T->left);
	if(p) return p;
	if(T->right) p = ParentB(n, T->right);
	return p;
}

node LeftChildB(node n, btree T) {
	return n->left;
}

node RightChildB(node n, btree T) {
	return n->right;
}

int LabelB(node n, btree T) {
	return n->l;
}

void ChangeLabelB(int x, node n, btree T) {
	if(n!=NULL)
		n->l = x;
}

node RootB(btree T) {
	return T;
}

bool CreateLeftB(int x, node n, btree T) {
	if(n->left) return 0;
	n = n->left = new element;
	n->left = n->right = NULL;
	n->l = x;
	return 1;
}

bool CreateRightB(int x, node n, btree T) {
	if(n->right) return 0;
	n = n->right = new element;
	n->left = n->right = NULL;
	n->l = x;
	return 1;
}

void DeleteB(node n, btree T, bool fr=1) {
	if(fr==0) {
		if(n->left) DeleteB(n->left, T, 0);
		if(n->right) DeleteB(n->right, T, 0);
		delete n;
		return;
	}
	node p = ParentB(n, T);
	if(p!=NULL) {
		if(p->left == n)
			p->left = NULL;
		else
			p->right = NULL;
		DeleteB(n, T, 0);
	} else {
		DeleteB(n->left, T, 0);
		DeleteB(n->right, T, 0);
		n->left = n->right = NULL;
	}
}

btree InitB(int x, btree T) {
	T = new element;
	T->left = T->right = NULL;
	T->l = x;
	return T;
}