mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
work in progress...1
This commit is contained in:
Executable
BIN
Binary file not shown.
+83
-53
@@ -2,12 +2,12 @@
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
tree *init_root(tree *root, int field) // Выделение памяти под корень дереваnode
|
||||
tree *init_root(tree *root, int field) // Выделение памяти под корень дерева
|
||||
{
|
||||
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
|
||||
tmp -> field = field;// Присваивание указателю на родителя значения NULL
|
||||
tmp -> parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp -> left = tmp -> right = NULL;
|
||||
tmp->field = field; // Присваивание указателю на родителя значения NULL
|
||||
tmp->parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp->left = tmp->right = NULL;
|
||||
root = tmp;
|
||||
return root;
|
||||
}
|
||||
@@ -15,39 +15,42 @@ tree *init_root(tree *root, int field) // Выделение памяти под
|
||||
tree *add_node(tree *root, int field)
|
||||
{
|
||||
tree *root2 = root, *root3 = NULL; // Выделение памяти под узел дерева
|
||||
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
|
||||
tmp -> field = field;
|
||||
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
|
||||
tmp->field = field;
|
||||
while (root2 != NULL)
|
||||
{
|
||||
root3 = root2;
|
||||
if (field < root2 -> field)
|
||||
root2 = root2 -> left;
|
||||
if (field < root2->field)
|
||||
root2 = root2->left;
|
||||
else
|
||||
root2 = root2 -> right;
|
||||
root2 = root2->right;
|
||||
}
|
||||
tmp -> parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp -> left = NULL;
|
||||
tmp -> right = NULL;
|
||||
if (field < root3 -> field) root3 -> left = tmp;
|
||||
else root3 -> right = tmp;
|
||||
tmp->parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
if (field < root3->field)
|
||||
root3->left = tmp;
|
||||
else
|
||||
root3->right = tmp;
|
||||
return root;
|
||||
}
|
||||
|
||||
tree *find_node(tree * root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень
|
||||
tree *find_node(tree *root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень
|
||||
{
|
||||
if ((root == NULL) || (root -> field == field))
|
||||
if ((root == NULL) || (root->field == field))
|
||||
return root; // Поиск нужного узла
|
||||
if (field < root -> field)
|
||||
return find_node(root -> left, field);
|
||||
else return find_node(root -> right, field);
|
||||
if (field < root->field)
|
||||
return find_node(root->left, field);
|
||||
else
|
||||
return find_node(root->right, field);
|
||||
}
|
||||
|
||||
// Минимальный элемент дерева
|
||||
tree *minim(tree *root)
|
||||
{
|
||||
tree *lef = root;
|
||||
while (lef -> left != NULL)
|
||||
lef = lef -> left;
|
||||
while (lef->left != NULL)
|
||||
lef = lef->left;
|
||||
return lef;
|
||||
}
|
||||
|
||||
@@ -55,73 +58,100 @@ tree *minim(tree *root)
|
||||
tree *maxim(tree *root)
|
||||
{
|
||||
tree *rig = root;
|
||||
while (rig -> right != NULL)
|
||||
rig = rig -> right;
|
||||
while (rig->right != NULL)
|
||||
rig = rig->right;
|
||||
return rig;
|
||||
}
|
||||
|
||||
tree *findForDel(tree *root)
|
||||
{
|
||||
tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве
|
||||
if (rig -> right != NULL)
|
||||
return minim(rig -> right);
|
||||
lef = rig -> parent;
|
||||
while ((lef != NULL) && (rig == lef -> right))
|
||||
if (rig->right != NULL)
|
||||
return minim(rig->right);
|
||||
lef = rig->parent;
|
||||
while ((lef != NULL) && (rig == lef->right))
|
||||
{
|
||||
rig = lef;
|
||||
lef = lef -> parent;
|
||||
lef = lef->parent;
|
||||
}
|
||||
return lef;
|
||||
}
|
||||
|
||||
tree *del_node(tree *root, int field)
|
||||
{
|
||||
// Поиск удаляемого узла по ключу
|
||||
// Поиск удаляемого узла по ключу
|
||||
tree *rig = root, *lef = NULL, *tmp = NULL;
|
||||
lef = find_node(root, field);
|
||||
// 1 случай: у узла нет потомков
|
||||
if ((lef -> left == NULL) && (lef -> right == NULL))
|
||||
// 1 случай: у узла нет потомков
|
||||
if ((lef->left == NULL) && (lef->right == NULL))
|
||||
{
|
||||
tmp = lef -> parent;
|
||||
if (lef == tmp -> right) tmp -> right = NULL;
|
||||
else tmp -> left = NULL;
|
||||
tmp = lef->parent;
|
||||
if (lef == tmp->right)
|
||||
tmp->right = NULL;
|
||||
else
|
||||
tmp->left = NULL;
|
||||
free(lef);
|
||||
}
|
||||
// 2 случай: потомок один - поддерево справа
|
||||
if ((lef -> left == NULL) && (lef -> right != NULL))
|
||||
// 2 случай: потомок один - поддерево справа
|
||||
if ((lef->left == NULL) && (lef->right != NULL))
|
||||
{
|
||||
tmp = lef -> parent;
|
||||
if (lef == tmp -> right) tmp -> right = lef -> right;
|
||||
else tmp -> left = lef -> right;
|
||||
tmp = lef->parent;
|
||||
if (lef == tmp->right)
|
||||
tmp->right = lef->right;
|
||||
else
|
||||
tmp->left = lef->right;
|
||||
free(lef);
|
||||
}
|
||||
// 2 случай: потомок один - поддерево слева
|
||||
if ((lef -> left != NULL) && (lef -> right == NULL))
|
||||
// 2 случай: потомок один - поддерево слева
|
||||
if ((lef->left != NULL) && (lef->right == NULL))
|
||||
{
|
||||
tmp = lef -> parent;
|
||||
if (lef == tmp -> right) tmp -> right = lef -> left;
|
||||
else tmp -> left = lef -> left;
|
||||
tmp = lef->parent;
|
||||
if (lef == tmp->right)
|
||||
tmp->right = lef->left;
|
||||
else
|
||||
tmp->left = lef->left;
|
||||
free(lef);
|
||||
}
|
||||
// 3 случай: оба потомка
|
||||
if ((lef -> left != NULL) && (lef -> right != NULL))
|
||||
// 3 случай: оба потомка
|
||||
if ((lef->left != NULL) && (lef->right != NULL))
|
||||
{
|
||||
tmp = findForDel(lef);
|
||||
lef -> field = tmp -> field;
|
||||
if (tmp -> right == NULL)
|
||||
tmp -> parent -> left = NULL;
|
||||
else tmp -> parent -> left = tmp -> right;
|
||||
lef->field = tmp->field;
|
||||
if (tmp->right == NULL)
|
||||
tmp->parent->left = NULL;
|
||||
else
|
||||
tmp->parent->left = tmp->right;
|
||||
free(tmp);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void print_tree(tree *root)
|
||||
void preorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
print_tree(root -> left);
|
||||
print_tree(root -> right);
|
||||
preorder(root -> left);
|
||||
preorder(root -> right);
|
||||
}
|
||||
|
||||
void postorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
postorder(root -> left);
|
||||
postorder(root -> right);
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
}
|
||||
|
||||
void inorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
inorder(root -> left);
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
inorder(root -> right);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
typedef struct tnode {
|
||||
typedef struct tnode
|
||||
{
|
||||
int field;
|
||||
struct tnode *left;
|
||||
struct tnode *right;
|
||||
@@ -7,9 +8,11 @@ typedef struct tnode {
|
||||
|
||||
tree *init_root(tree *root, int field);
|
||||
tree *add_node(tree *root, int field);
|
||||
tree *find_node(tree * root, int field);
|
||||
tree *find_node(tree *root, int field);
|
||||
tree *minim(tree *root);
|
||||
tree *maxim(tree *root);
|
||||
tree *findForDel(tree *root);
|
||||
tree *del_node(tree *root, int field);
|
||||
void print_tree(tree *root);
|
||||
void preorder(tree *root);
|
||||
void inorder(tree *root);
|
||||
void postorder(tree *root);
|
||||
+24
-18
@@ -1,25 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
tree *root=NULL;
|
||||
root = init_root(root, 65);
|
||||
print_tree(root);
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
// Дерево улетает в SF
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
tree *root = NULL;
|
||||
root = init_root(root, 'A');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
root=del_node(root, 65);
|
||||
print_tree(root);
|
||||
root = del_node(root, 'A');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
root = add_node(root, 70);
|
||||
root = add_node(root, 66);
|
||||
root = add_node(root, 68);
|
||||
root = add_node(root, 71);
|
||||
root = add_node(root, 67);
|
||||
root = add_node(root, 72);
|
||||
print_tree(root);
|
||||
root = add_node(root, 'F');
|
||||
root = add_node(root, 'B');
|
||||
root = add_node(root, 'D');
|
||||
root = add_node(root, 'G');
|
||||
root = add_node(root, 'C');
|
||||
root = add_node(root, 'H');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
root = del_node(root, 66);
|
||||
print_tree(root);
|
||||
postorder(root);
|
||||
printf("\n");
|
||||
inorder(root);
|
||||
printf("\n");
|
||||
root = del_node(root, 'B');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user