BinarySearchTree insert(ElementType e, BinarySearchTree root) { if(!root) {// find the node with its left or right being NULL root = createBinarySearchTree(e); if(root) return root; else return NULL; } if(e > root->value) // the tree node with value e inserting into right child of the parent root->right = insert(e, root->right); else if(e < root->value) // the tree node withe value e inserting into left child of the parent root->left = insert(e, root->left); else Error(" you cannot insert the node into the tree for its value equals to one in the tree"); return root; // dont't forget this line ! }
int main() { BinarySearchTree bst; int value[] = {2, 8, 1, 5, 3, 4}; int i; printf("\n ====== test for preordering the BinarySearchTree ====== \n"); printf("\n test for creating a binary search tree with root value 6 \n"); bst = createBinarySearchTree(6); printPreorder(1, bst); printf("\n test for insertint 6 nodes, that's 2, 8, 1, 5, 3 and 4 in turn \n"); for(i = 0; i < 6; i++) insert(value[i], bst); printPreorder(1, bst); printf("\n test for find minimum \n"); printf(" the minimum is %2d, in this binary search tree! \n", findMin(bst)->value); printf("\n test for find maximum \n"); printf(" the maximum is %2d, in this binary search tree! \n", findMax(bst)->value); printf("\n test for deleting node '2' with two nodes from the binary search tree \n"); deleteBinarySearchTree(2, bst); printPreorder(1, bst); printf("\n test for deleting node '5' with one node from the binary search tree \n"); deleteBinarySearchTree(5, bst); printPreorder(1, bst); printf("\n test for deleting node '8' with zeron node from the binary search tree \n"); deleteBinarySearchTree(8, bst); printPreorder(1, bst); printf("\n test for inserting '8', '5' and '2' into the binary search tree \n"); insert(8, bst); insert(5, bst); insert(2, bst); printPreorder(1, bst); return 0; }
int main() { BinarySearchTree *tree = createBinarySearchTree(); printf("Тестирование двоичного дерева поиска\n"); while (true) { printf("Возможные действия:\n"); printf("0 - выход\n"); printf("1 - добавить элемент в дерево\n"); printf("2 - удалить элемент из дерева\n"); printf("3 - проверить, есть ли элемент в дереве\n"); printf("4 - вывести элементы в порядке возрастания\n"); printf("5 - вывести элементы в порядке убывания\n"); printf("Введите номер действия: "); int operationNumber = 0; scanf("%d", &operationNumber); switch (operationNumber) { case 0: { deleteBinarySearchTree(tree); return 0; } case 1: { printf("Введите элемент для добавления: "); int value = 0; scanf("%d", &value); addToBinarySearchTree(tree, value); break; } case 2: { printf("Введите элемент для удаления: "); int value = 0; scanf("%d", &value); removeFromBinarySearchTree(tree, value); break; } case 3: { printf("Введите элемент для проверки: "); int value = 0; scanf("%d", &value); if (existsInBinarySearchTree(tree, value)) printf("Элемент найден\n"); else printf("Элемент не найден\n"); break; } case 4: { printLowToHigh(tree); break; } case 5: { printHighToLow(tree); break; } default: { printf("Некорректный номер действия\n"); break; } } } }