Здравствуйте, уважаемые форумчане, у меня к вам вот какой вопрос... как по коду в Си разобраться где входные данные, где выходные данные для функций... И написать спецификацию к этой программе, т.е. документ, который для каждой функции будет определять ВХОД ВЫХОД и ОПИСАНИЕ?
А вот в принципе и сама программа. #include "stdafx.h"
Код:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <stdio.h>
using namespace std;
struct node {
int value;
node *left;
node *right;
};
class tree {
node *head;
node* find_node (node *root, int x); //поиск заданного элемента (функция возвращает адрес узла с найденным элементом; если элемент в дереве не найден, возвращается nil)
int delete_min (node *root, node *r);
node* add_node (node *root, int x); //добавление узла в дерево
node* remove_node (node *root, int x); //удаление узла из дерева
void print_tree (int k, node *root);
node* create_node (node *p, int n); //создание узла дерева
public:
tree() {head=0;}; //инициализация дерева
~tree() {};
void find (int x);
void add (int x);
int get_node (node *root); //значение текущего элемента
void remove (int x);
void del (); //уничтожение деревеа
void print (); //печать дерева
};
node* tree::create_node (node *p, int n) {
p = new node;
(*p).value = n;
(*p).left = 0;
(*p).right = 0;
return p;
}
node* tree::add_node(node *root, int x) {
if (root == 0) { //создаем новый узел дерева
return create_node (root, x);
} else {
if ((*root).value < x) {
(*root).right = add_node ((*root).right, x);
} else {
if ((*root).value > x) {
(*root).left = add_node ((*root).left, x);
} else {
cout << "key can't repeat" << endl;
}
}
}
return root;
}
void tree::add (int x) {
head = add_node (head, x);
}
int tree::get_node (node *root) {
if (root == 0) {
cout << "empty tree" << endl;
return 0;
} else {
return (*root).value;
}
}
node* tree::find_node (node *root, int x) {
if (root == 0) {
return 0;
} else {
if (x == (*root).value) {
return root;
} else {
if (x < (*root).value) {
return find_node ((*root).left, x);
} else {
return find_node ((*root).right, x);
}
}
}
}
void tree::find (int x) {
cout << find_node (head, x) << endl;
}
int tree::delete_min(node *root, node *r)
{
node *right_root;
node *was_root;
int val;
if ((*root).left == 0)
{
val=(*root).value;
was_root=root;
right_root = (*root).right;
delete was_root; //удаляем бывший корень
root = right_root;
(*r).right = right_root;
return val;
}
else //узел root имеет левого сына
{ return delete_min((*root).left, r); }
}
node* tree::remove_node(node *root, int x)
{
node *was_next;
if (root != 0)
{
if (x < (*root).value) { (*root).left = remove_node((*root).left,x); }
else
{
if (x > (*root).value) { (*root).right = remove_node((*root).right,x); }
else
{
if (((*root).left == 0) && ((*root).right == 0)){ delete root; root=0; }
// нет сыновей, удаляем узел, на который указывает root
else
{
if ((*root).left == 0)
{
was_next = (*root).right;
delete root;
root = was_next;
}
else
{
if ((*root).right == 0)
{
was_next = (*root).left;
delete root;
root = was_next;
}
else //у удаляемого элемента есть оба сына
{ (*root).value = delete_min((*root).right, root);}
}
}
}
}
}
return root;
}
void tree::remove(int x)
{
if (head == 0) { cout << "derevo pusto";}
head = remove_node(head,x);
}
void tree::print_tree (int k, node *root) {
int i;
if (root != 0) {
print_tree (k+3, (*root).left);
for (i = 1; i <= k; i++)
cout << " ";
cout << (*root).value << endl;
print_tree (k+3, (*root).right);
}
}
void tree::print () {
if (head == 0)
cout << "Derevo pusto" << endl;
else
print_tree (4, head);
}
int main() {
tree derevo; int i;
derevo.print();
for (int i=0;i<20;i++)
{
derevo.add(i);
derevo.add(20-i);
}
derevo.print();
cin>>i;
for (int i=-1;i<22;i=i+1)
derevo.remove(i);
derevo.print();
derevo.find(29);
system("Pause");
return 0;
}
Писала не сама и язык Си для меня абсолютная загадка. Help!