Exemplo n.º 1
0
/* free all the memory being used by the tree, and set the root to TREE_EMPTY */
static void treeDestroy(struct node **root) {
	if (*root) { /*if the root exists*/
		treeDestroy(&(*root)->child[LEFT]); /*recursively call on left*/
		treeDestroy(&(*root)->child[RIGHT]); /*recursively call on right*/
		free((*root)->key); /*free malloced mem for string in each node*/
		free(*root); /*free memory for each node*/
		*root = TREE_EMPTY; /*set root to empty*/
	}
}
Exemplo n.º 2
0
void treeDestroy(Node **node)
{
	if (*node == NULL)
		return;

	if ((*node)->_left != NULL)
		treeDestroy(&(*node)->_left);

	if ((*node)->_right != NULL)
		treeDestroy(&(*node)->_right);

	free(*node);

	*node = NULL;
}
Exemplo n.º 3
0
Node *treeMakeNotMinus(Node **node)
{
	Node *tmpNode = treeCopy(&(*node)->_right);

	treeDestroy(node);

	return tmpNode;
}
Exemplo n.º 4
0
int main(void)
{
	int i, maxBFS;
	char cmd[255], arg;
	TreeNode *root = NULL, *tmpNode = NULL;
	Vector v;

	do
	{
		printf("Введите команду (h - справка):\n");
		scanf("%s", cmd);

		if (cmd[0] == '+')
		{
			scanf(" %c", &arg);

			if (cmd[1] == 'r')
			{
				if (root == NULL)
				{
					if (arg >= 'A' && arg <= 'Z')
					{
						treeAddNode(&root, arg - 'A');

						printf("Корень %c создан\n", arg);
					}
					else
						printf("Ошибка. Введена недопустимая буква\n");
				}
				else
					printf("Корень уже существует\n");
			}
			else if (root == NULL)
				printf("Корень не создан\n");
			else
			{
				tmpNode = root;

				if (cmd[1] != '\0')
					tmpNode = getNodeByPath(&root, &cmd[1]);

				if (tmpNode == NULL)
					printf("Ошибка. Такого пути не существует\n");
				else if (arg >= 'A' && arg <= 'Z')
				{
					if (treeAddNode(&tmpNode, arg - 'A') != NULL)
						printf("Узел %c добавлен к узлу %c\n", arg, tmpNode->_data + 'A');
				}
				else
					printf("Ошибка. Введена недопустимая буква\n");
			}
		}
		else if (cmd[0] == '-')
		{
			scanf(" %c", &arg);

			if (arg >= 'A' && arg <= 'Z')
			{
				if (treeRemoveNode(&root, arg - 'A'))
					printf("Узел %c удален\n", arg);
				else
					printf("Узел %c не найден\n", arg);
			}
			else
				printf("Ошибка. Введена недопустимая буква\n");
		}
		else if (cmd[0] == 'p')
		{
			KLP(&root, 0);
		}
		else if (cmd[0] == 't')
		{
			if (root != NULL)
			{
				vectorCreate(&v, treeDFS(&root));

				for (i = 0; i < vectorSize(&v); i++)
					vectorSave(&v, i, 0);

				countNodesOnLevels(&root, &v, 0);

				maxBFS = 0;

				for (i = 0; i < vectorSize(&v); i++)
					if (vectorLoad(&v, i) > maxBFS)
						maxBFS = vectorLoad(&v, i);

				printf("Ширина дерева: %d\n", maxBFS);

				vectorDestroy(&v);
			}
			else
				printf("Дерево пусто\n");
		}
		else if (cmd[0] == 'h')
		{
			printf("================================\n");
			printf("Список команд:\n");
			printf("+r CHAR - создать корень CHAR (A, B, ..., Z)\n");
			printf("+ CHAR - добавить сына CHAR к корню\n");
			printf("+PATH CHAR - добавить CHAR узел по заданому пути (s - сын, b - брат)\n");
			printf("- CHAR - удалить первый найденный узел CHAR и его поддерево\n");
			printf("p - распечатать дерево\n");
			printf("t - выполнить задание над деревом\n");
			printf("q - завершить программу\n");
			printf("================================\n");
		}
		else if (cmd[0] != 'q')
		{
			printf("Неизвестная команда\n");
		}
	}
	while (cmd[0] != 'q');

	treeDestroy(&root);

	return 0;
}
Exemplo n.º 5
0
int main(void)
{
	int action;
	char expr[255];
	Node *root = NULL, *root2 = NULL;
	Stack stPost;

	while (1)
	{
		printf("Меню:\n");
		printf("1) Ввести выражение\n");
		printf("2) Печать исходного выражения\n");
		printf("3) Печать преобразованного выражения\n");
		printf("4) Печать исходного дерева\n");
		printf("5) Печать преобразованного дерева\n");
		printf("6) Выход\n");
		printf("Выберите действие: ");
		scanf("%d", &action);

		switch (action)
		{
			case 1:
			{
				printf("Введите выражение: ");
				scanf("%s", expr);

				treeDestroy(&root);
				treeDestroy(&root2);
				stackCreate(&stPost);
				postOrder(expr, &stPost);
				treeBuild(&root, &stPost);
				stackDestroy(&stPost);
				
				root2 = treeCopy(&root);
				
				treeMoveMinus(&root2);

				break;
			}

			case 2:
			{
				printf("Исходное выражение: %s\n", expr);

				break;
			}

			case 3:
			{
				LKP(&root2);
				printf("\n");

				break;
			}

			case 4:
			{
				if (root != NULL)
				{
					printf("Дерево исходного выражения\n");
					PKL(&root, 0);
				}
				else
					printf("Дерево исходного выражения пусто\n");

				break;
			}

			case 5:
			{
				if (root2 != NULL)
				{
					printf("Дерево преобразованного выражения\n");
					PKL(&root2, 0);
				}
				else
					printf("Дерево преобразованного выражения пусто\n");

				break;
			}

			case 6: break;

			default:
			{
				printf("Ошибка. Такого пункта меню не существует\n");

				break;
			}
		}

		if (action == 6)
			break;
	}

	treeDestroy(&root);
	treeDestroy(&root2);

	return 0;	
}
Exemplo n.º 6
0
/*this function takes a pointer to an ordered set and frees all the memory of the tree that the ordered set has a pointer to
and the memory the orderedSet struct uses itself*/
void orderedSetDestroy (struct orderedSet *s) {
	treeDestroy(&(s->set)); /*free tree in orderedSet struct*/
	free(s); /*freeSet*/
}