Example #1
0
int main() {
	auto adam=Person::makePerson("Adam");
	adam->print(std::cout);
	auto eve=Person::makePerson("Eve");
	eve->print(std::cout);
	addson("Cain",adam, eve);
	addson("Abel",adam, eve);
	addson("Seth",adam, eve);
	adam->print(std::cout);
	eve->print(std::cout);
	// how to have Cain kill Abel?
	{
		auto abel=eve->findChild("Abel");
		eve->killChild(abel);
		adam->killChild(abel);
		abel->print(std::cout);
	}
	eve->print(std::cout);
	// how can we kill Adam and Eve?
	{
		adam->killMe();
		adam->print(std::cout);
		adam.reset();
	}
	eve->print(std::cout);
	auto cain=eve->findChild("Cain");
	if (cain) cain->print(std::cout);
	eve->killMe(); // avoid memory leak
	eve->print(std::cout);
	eve.reset();
}
Example #2
0
struct node23 *insert23(double dis, int a, int b, int c, struct node23 **root)
{
	struct node23 *newroot;
	struct node23 *oldroot;
	struct node23 *leaf;
	struct node23 *split;
	double minback;

	// NULL tree
	if (*root == NULL)
	{
		*root = avail23();
		(*root)->y1 = dis;
		// coordinates
		coor23((*root), a, b, c);
		return *root;
	}
	if ((*root)->son1 == NULL)
	{
		if ((*root)->y1 == dis)
			return NULL;
		newroot = avail23();
		leaf = avail23();
		leaf->y1 = dis;
		//coordinates
		coor23(leaf, a, b, c);

		if (dis > (*root)->y1)
		{
			newroot->son1 = *root;
			newroot->y1 = dis;
			newroot->son2 = leaf;
		}
		else
		{
			newroot->son1 = leaf;
			newroot->y1 = (*root)->y1;
			newroot->son2 = *root;
		}
		*root = newroot;
		return leaf;
	}

	// General Case

	leaf = addson(dis, a, b, c, *root, &split, &minback);
	if (leaf == NULL)
		return NULL;

	if (split != NULL)
	{
		oldroot = *root;
		*root = avail23();
		(*root)->son1 = oldroot;
		(*root)->son2 = split;
		(*root)->son3 = NULL;
		(*root)->y1 = minback;
	}
	return leaf;
}
Example #3
0
struct node23 *addson(double x, int a, int b, int c, struct node23 *node, struct node23 **split, double *low)
{
	struct node23 *leaf;
	struct node23 *nodeback;
	struct node23 *w;
	double lowback;
	int child;

	*split = NULL;

	if (node->son1 == NULL)
	{
		if (node->y1 == x)
			return NULL;
		*split = avail23();
		if (node->y1 <= x)
		{
			(*split)->y1 = x;
			*low = x;
			return *split;
		}
		else
		{
			(*split)->y1 = node->y1;
			(*split)->y2 = node->y2;
			node->y1 = x;
			*low = (*split)->y1;
			// coordinates
			coor23((*split), node->x, node->y, node->z);
			return node;
		}
	}

	if (x < node->y1)
	{
		child = 1;
		w = node->son1;
	}
	else
	{
		if (node->son3 == NULL || x < node->y2)
		{
			child = 2;
			w = node->son2;
		}
		else
		{
			child = 3;
			w = node->son3;
		}
	}

	leaf = addson(x, a, b, c, w, &nodeback, &lowback);
	if (leaf == NULL)
		return NULL;
	
	coor23(leaf, a, b, c);
	
	if (nodeback != NULL)
	{
		if (node->son3 == NULL)
		{
			if (child == 2)
			{
				node->son3 = nodeback;
				node->y2 = lowback;
			}
			else
			{
				node->son3 = node->son2;
				node->y2 = node->y1;
				node->son2 = nodeback;
				node->y1 = lowback;
			}
		}	
		else
		{
			(*split) = avail23();
			if (child == 3)
			{
				(*split)->son1 = node->son3;
				(*split)->son2 = nodeback;
				(*split)->son3 = NULL;
				(*split)->y1 = lowback;
				*low = node->y2;
				node->son3 = NULL;
			}
			else
			{
				(*split)->son2 = node->son3;
				(*split)->y1 = node->y2;
				(*split)->son3 = NULL;
				node->son3 = NULL;
			}
			if (child == 2)
			{
				(*split)->son1 = nodeback;
				*low = lowback;
			}
			if (child == 1)
			{
				(*split)->son1 = node->son2;
				*low = node->y1;
				node->son2 = nodeback;
				node->y1 = lowback;
			}
		}
	}

	return leaf;
}