Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <windows.h>
- typedef struct Node {
- int value;
- struct Node *left;
- struct Node *right;
- struct Node *ware;
- } Node;
- typedef struct ListNode {
- Node *node;
- struct ListNode *next;
- } ListNode;
- Node *createTree(int rootValue) {
- Node* root = malloc(sizeof(Node));
- root->value = rootValue;
- root->left = NULL;
- root->right = NULL;
- root->ware = NULL;
- return root;
- }
- void findPlace(Node** nodep, Node* newNode) {
- Node *node = *nodep;
- if (newNode->value > node->value)
- if (node->right != NULL)
- findPlace(&(node->right), newNode);
- else
- node->right = newNode;
- else if (node->left != NULL)
- findPlace(&(node->left), newNode);
- else
- node->left = newNode;
- }
- bool isValueInTree(int value, Node *root) {
- if (root != NULL) {
- Node *temp;
- temp = root;
- while (temp != NULL) {
- if (value == temp->value) {
- printf("Вершина уже есть в дереве!\n");
- return true;
- }
- else if (value > temp->value)
- temp = temp->right;
- else
- temp = temp->left;
- }
- }
- return false;
- }
- void addNode(int value, Node** root) {
- if (isValueInTree(value, *root)) return;
- if (*root == NULL) {
- *root = createTree(value);
- return;
- }
- Node* newNode = malloc(sizeof(Node));
- newNode->value = value;
- newNode->left = NULL;
- newNode->right = NULL;
- newNode->ware = NULL;
- findPlace(root, newNode);
- }
- void destroyTree(Node **pnode) {
- Node *node = *pnode;
- if (node->right != NULL)
- destroyTree(&(node->right));
- if (node->right == NULL) {
- if (node->left != NULL)
- destroyTree(&(node->left));
- if (node->left == NULL) {
- free(node);
- node = NULL;
- }
- }
- }
- void addValuesToTree(Node **root) {
- int value = 100;
- printf("Введите значение (-99..99) для добавления в дерево, или введите 100 для прекращения ввода: ");
- scanf("%d", &value);
- while (value != 100) {
- addNode(value, root);
- printf("Введите значение (-99..99) для добавления в дерево, или введите 100 для прекращения ввода: ");
- scanf("%d", &value);
- }
- }
- void outputTree(Node* node, int offset, char nodeType) {
- for (int i = 0; i < offset; i++) {
- printf(" ");
- }
- if (nodeType != 'W') {
- printf("[%c] %d\n", nodeType, node->value);
- }
- else {
- printf("[%c](%d)\n", 'R', node->value);
- return;
- }
- if (node->left != NULL)
- outputTree(node->left, offset + 2, 'L');
- if (node->right != NULL)
- outputTree(node->right, offset + 2, 'R');
- if (node->ware != NULL)
- outputTree(node->ware, offset + 2, 'W');
- }
- void getRAB(Node *root) {
- if (root == NULL) {
- printf("%d ", 0);
- return;
- }
- printf("(%d) ", root->value);
- getRAB(root->left);
- printf("%d ", root->value);
- getRAB(root->right);
- printf("%d ", root->value);
- }
- void getARB(Node *root, ListNode **list) {
- if (root == NULL) {
- printf("%d ", 0);
- return;
- }
- printf("%d ", root->value);
- getARB(root->left, list);
- printf("(%d) ", root->value);
- ListNode *listNode = malloc(sizeof(ListNode));
- listNode->node = root;
- listNode->next = NULL;
- if (*list == NULL) {
- *list = listNode;
- }
- else {
- ListNode *temp = *list;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- temp->next = listNode;
- }
- getARB(root->right, list);
- printf("%d ", root->value);
- }
- void getARBNoPrint(Node *root, ListNode **list) {
- if (root == NULL) {
- return;
- }
- getARBNoPrint(root->left, list);
- ListNode *listNode = malloc(sizeof(ListNode));
- listNode->node = root;
- listNode->next = NULL;
- if (*list == NULL) {
- *list = listNode;
- }
- else {
- ListNode *temp = *list;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- temp->next = listNode;
- }
- getARBNoPrint(root->right, list);
- }
- void getABR(Node *root) {
- if (root == NULL) {
- printf("%d ", 0);
- return;
- }
- printf("%d ", root->value);
- getABR(root->left);
- printf("%d ", root->value);
- getABR(root->right);
- printf("(%d) ", root->value);
- }
- ListNode *getTraversals(Node *root) {
- printf("-----------------------------------------\nОбходы:");
- printf("\nRAB: ");
- getRAB(root);
- printf("\n\nARB: ");
- ListNode *ARB = NULL;
- getARB(root, &ARB);
- printf("\n\nABR: ");
- getABR(root);
- return ARB;
- }
- void getRightSymmWare(ListNode *ARB, Node *root) {
- ListNode *temp = ARB;
- while (ARB->next != NULL) {
- if (ARB->node->right == NULL) {
- ARB->node->ware = ARB->next->node;
- }
- ARB = ARB->next;
- }
- ARB->node->ware = root;
- }
- Node *search(Node *root, int value) {
- if ((root->left != NULL && root->left->value == value) || (root->right != NULL && root->right->value == value)) {
- return root;
- }
- else if (root->value > value) {
- return search(root->left, value);
- }
- else {
- return search(root->right, value);
- }
- }
- Node *getLeftLeaf(Node *node) {
- if (node->left == NULL) {
- return node;
- }
- else {
- return getLeftLeaf(node->left);
- }
- }
- Node *replace(Node *root) {
- if (root->left == NULL) {
- return root->right;
- }
- else if (root->right == NULL) {
- return root->left;
- }
- else {
- Node *node = getLeftLeaf(root->right);
- node->left = root->left;
- if (node->right == NULL) {
- node->right = root->right;
- Node *temp = search(root, node->value);
- temp->left = NULL;
- }
- return node;
- }
- }
- Node *delete(Node *root, int value) {
- if (root->value == value) {
- root = replace(root);
- }
- else {
- Node *node = search(root, value);
- if (node->left != NULL && node->left->value == value) {
- node->left = replace(node->left);
- }
- else {
- node->right = replace(node->right);
- }
- }
- return root;
- }
- Node *deleteNode(Node *root) {
- int value = 0;
- printf("Введите значение вершины, которую нужно удалить:");
- scanf("%d", &value);
- return delete(root, value);
- }
- void deleteWare(Node *root) {
- if (root == NULL) return;
- root->ware = NULL;
- deleteWare(root->left);
- deleteWare(root->right);
- }
- int main(void) {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- Node* root = NULL;
- addValuesToTree(&root);
- printf("-----------------------------------------\nДерево:\n");
- outputTree(root, 0, '-');
- ListNode *ARB = getTraversals(root);
- getRightSymmWare(ARB, root);
- printf("\n-----------------------------------------\nДерево после правой симметричной прошивки:\n");
- outputTree(root, 0, '-');
- while (ARB != NULL) {
- ListNode *temp = ARB;
- ARB = ARB->next;
- free(temp);
- }
- root = deleteNode(root);
- getARBNoPrint(root, &ARB);
- deleteWare(root);
- getRightSymmWare(ARB, root);
- printf("-----------------------------------------\nПерепрошитое дерево после удаления:\n");
- outputTree(root, 0, '-');
- while (ARB != NULL) {
- ListNode *temp = ARB;
- ARB = ARB->next;
- free(temp);
- }
- destroyTree(&root);
- int a;
- scanf("%d", a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement