Zdravim, programujem binarny strom a rad by som dostal vypis vo forme grafu, cize
Kód:
10
5 15
1 4 11 20
Momentalne som tu niekde:
Kód:
typedef int T;
class Node {
private:
T value;
Node *left;
Node *right;
public:
Node(T value){
this->value = value;
left = NULL;
right = NULL;
}
T& get_value(){
return value;
}
Node* get_left(){
return left;
}
void set_left(Node* left){
this->left = left;
}
Node* get_right(){
return right;
}
void set_right(Node* right){
this->right = right;
}
};
class BinaryTree {
private:
Node* root;
void print_r(Node* left, int level){ //vypisuje zaradom, od najmensieho po najvecsi
if(left == root)
cout << left->get_value() << endl;
if(left == NULL)
return;
level = 0;
Node* right = left->get_right();
left = left->get_left();
if( left != NULL)
cout << left->get_value() << " ";
if( right != NULL)
cout << right->get_value();
if(left != NULL && right != NULL)
cout << endl;
if(level < 1)
print_r(left, level);
level++;
if(level < 2)
print_r(right, level);
}
void print(){
if (root != NULL)
print_r(root, 0);
cout << endl;
}
Problem je v tom, ze mi to odsadzuje po kazdej dvojici a neviem, aku podmienku mam dat pred endl, aby to robilo exponencialne...nejake napady prosim vas?
Zatial to robim bez medzier, medzery doplnim neskor.