Once I was doing a job assignment after an interview, something like a test assignment, but not at home. There was the Internet and I googled the C code and found some code that contained errors, because of this I could not complete the task for which an hour was allocated. Why did it happen I will only say that in terms of programming everything was bad for me to a minimum and I did not have my own code for trees and for graphs either.
And so let's start with the basics:
Μ Μ β , (). , , . .[1]
// #1 ,
#include <iostream>
#include <conio.h>
using namespace std;
//
struct node
{
int info; //
node *l, *r; //
};
node *tree = NULL; // ,
/* */
void push(int a, node **t)
{
if ((*t) == NULL) //
{
(*t) = new node; //
(*t)->info = a; // a
(*t)->l = (*t)->r = NULL; //
return; // ,
}
//
if (a > (*t)->info) push(a, &(*t)->r); // ,
else push(a, &(*t)->l); //
}
/* */
void print(node *t, int u)
{
if (t == NULL) return; // , ,
else //
{
print(t->l, ++u); //
for (int i = 0; i < u; ++i) cout << "|";
cout << t->info << endl; //
u--;
}
print(t->r, ++u); //
}
int sum(node *node_) {
if (node_ == 0) return 0;
return node_->info + sum(node_->l) + sum(node_->r);
}
int main()
{
int n = 16; //
int s; //,
for (int i = 0; i < n; ++i)
{
s = -5 + rand() % 10; //
push(s, &tree); //
}
cout << " \n";
print(tree, 0);
cout << "\n"<<
sum(tree) << endl;
cin.ignore().get();
}
. .
- , .
!