#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string.h>
using namespace std;
int counter = 0 ;
class Node
{
char d;
Node * lft;
Node * mdl;
Node * rgt;
public :
//static int counter;
Node( )
{
lft = 0 ;
mdl = 0 ;
rgt = 0 ;
cout << "M" << endl;
}
~Node( )
{
if ( lft) delete lft;
if ( mdl) delete mdl;
if ( rgt) delete rgt;
}
friend class Tree;
} ;
class Tree
{
Node * root;
char num, maxnum;
int maxrow, offset;
char ** SCREEN;
void clrscr( ) ;
Node * MakeNode( int depth) ;
void OutNodes( Node * v, int r, int c) ;
Tree( const Tree & ) ;
Tree( Tree && ) ;
Tree operator = ( const Tree& ) const ;
Tree operator = ( Tree && ) const ;
public :
Tree( char nm, char mnm, int mxr) :
num( nm) , maxnum( mnm) , maxrow( mxr) , offset( 40 ) , root( NULL )
{
SCREEN = new char * [ maxrow] ;
for ( int i = 0 ; i < maxrow; i++ )
SCREEN[ i] = new char [ 80 ] ;
}
~Tree( )
{
for ( int i = 0 ; i < maxrow; i++ )
delete [ ] SCREEN[ i] ;
delete [ ] SCREEN;
delete root;
}
void MakeTree( )
{
root = MakeNode( 0 ) ;
}
bool exist( )
{
return root ! = NULL ;
}
int DFS( ) ;
int BFS( ) ;
void OutTree( ) ;
} ;
Node * Tree:: MakeNode ( int depth)
{
Node * v = NULL ;
int Y = ( depth < rand ( ) % 6 + 1 ) && ( num >= 'a' ) && ( num <= 'z' ) ;
if ( Y)
{
v = new Node;
v- > d = num++ ;
v- > lft = MakeNode( depth + 1 ) ;
v- > mdl = MakeNode( depth + 1 ) ;
v- > rgt = MakeNode( depth + 1 ) ;
if ( v- > lft || v- > mdl || v- > rgt)
counter++ ;
}
return v;
}
void Tree:: OutTree ( )
{
clrscr( ) ;
OutNodes( root, 1 , offset) ;
for ( int i = 0 ; i < maxrow; i++ )
{
SCREEN[ i] [ 79 ] = 0 ;
cout << '\n ' << SCREEN[ i] ;
}
cout << '\n ' ;
}
void Tree:: clrscr ( )
{
for ( int i = 0 ; i < maxrow; i++ )
memset ( SCREEN[ i] , '.' , 80 ) ;
}
void Tree:: OutNodes ( Node * v, int r, int c)
{
if ( r && c && ( c < 80 ) ) SCREEN[ r - 1 ] [ c - 1 ] = v- > d;
if ( r < maxrow)
{
if ( v- > lft) OutNodes( v- > lft, r + 1 , c - ( offset >> r) ) ;
if ( v- > mdl) OutNodes( v- > mdl, r + 1 , c) ;
if ( v- > rgt) OutNodes( v- > rgt, r + 1 , c + ( offset >> r) ) ;
}
}
template < class Item> class QUEUE
{
Item * Q;
int h, t, N;
public :
QUEUE( int maxQ) : h( 0 ) , t( 0 ) , N( maxQ) { Q = new Item[ maxQ + 1 ] ; }
int empty( ) const { return ( h % N) == t; }
void put( Item item) { Q[ t++ ] = item; t % = N; }
Item get( ) { h % = N; return Q[ h++ ] ; }
} ;
int Tree:: BFS ( )
{
const int MaxQ = 20 ;
int count = 0 ;
QUEUE < Node * > Q( MaxQ) ;
Q.put ( root) ;
while ( ! Q.empty ( ) )
{
Node * v = Q.get ( ) ;
cout << v- > d << '_' ;
count++ ;
if ( v- > lft) Q.put ( v- > lft) ;
if ( v- > mdl) Q.put ( v- > mdl) ;
if ( v- > rgt) Q.put ( v- > rgt) ;
}
return count;
}
int main( )
{
setlocale( 0 , "" ) ;
int n = 0 ;
Tree Tr( 'a' , 'z' , 4 ) ;
srand ( time ( nullptr) ) ;
Tr.MakeTree ( ) ;
if ( Tr.exist ( ) )
{
Tr.OutTree ( ) ;
cout << endl << "Обход в ширину: " ;
n = Tr.BFS ( ) ;
cout << endl << "Пройдено узлов: " << n << endl;
cout << "Количество узлов с хотя бы 1 потомком: " << counter << endl;
}
else cout << "Дерево пусто." ;
cout << endl << "Конец." << endl;
system ( "pause" ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPHRpbWUuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGNvdW50ZXIgPSAwOwpjbGFzcyBOb2RlCnsKICAgICAgICBjaGFyIGQ7CiAgICAgICAgTm9kZSAqbGZ0OwogICAgICAgIE5vZGUgKm1kbDsKICAgICAgICBOb2RlICpyZ3Q7CiAgICAgICAKcHVibGljOgogICAgICAgIC8vc3RhdGljIGludCBjb3VudGVyOwogICAgICAgIE5vZGUoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIGxmdCA9IDA7CiAgICAgICAgICAgICAgICBtZGwgPSAwOwogICAgICAgICAgICAgICAgcmd0ID0gMDsgICAgICAgCiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgY291dCA8PCAiTSIgPDwgZW5kbDsKICAgICAgICB9CiAgICAgICAgfk5vZGUoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChsZnQpIGRlbGV0ZSBsZnQ7CiAgICAgICAgICAgICAgICBpZiAobWRsKSBkZWxldGUgbWRsOwogICAgICAgICAgICAgICAgaWYgKHJndCkgZGVsZXRlIHJndDsKICAgICAgICB9CiAgICAgICAgZnJpZW5kIGNsYXNzIFRyZWU7Cn07CiAKY2xhc3MgVHJlZQp7CiAgICAgICAgTm9kZSAqcm9vdDsKICAgICAgIAogICAgICAgIGNoYXIgbnVtLCBtYXhudW07CiAgICAgICAgaW50IG1heHJvdywgb2Zmc2V0OwogICAgICAgIGNoYXIgKipTQ1JFRU47CiAgICAgICAgdm9pZCBjbHJzY3IoKTsKICAgICAgICBOb2RlICpNYWtlTm9kZShpbnQgZGVwdGgpOwogICAgICAgIHZvaWQgT3V0Tm9kZXMoTm9kZSAqdiwgaW50IHIsIGludCBjKTsKICAgICAgICBUcmVlKGNvbnN0IFRyZWUgJik7CiAgICAgICAgVHJlZShUcmVlICYmKTsKICAgICAgICBUcmVlIG9wZXJhdG9yID0gKGNvbnN0IFRyZWUmKSBjb25zdDsKICAgICAgICBUcmVlIG9wZXJhdG9yID0gKFRyZWUgJiYpIGNvbnN0OwpwdWJsaWM6CiAgICAgICAKICAgICAgICBUcmVlKGNoYXIgbm0sIGNoYXIgbW5tLCBpbnQgbXhyKSA6CiAgICAgICAgICAgICAgICBudW0obm0pLCBtYXhudW0obW5tKSwgbWF4cm93KG14ciksIG9mZnNldCg0MCksIHJvb3QoTlVMTCkKICAgICAgICB7CiAgICAgICAgICAgICAgICBTQ1JFRU4gPSBuZXcgY2hhciAqW21heHJvd107CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG1heHJvdzsgaSsrKQogICAgICAgICAgICAgICAgICAgICAgICBTQ1JFRU5baV0gPSBuZXcgY2hhcls4MF07CiAgICAgICAgfQogCiAgICAgICAgflRyZWUoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbWF4cm93OyBpKyspCiAgICAgICAgICAgICAgICAgICAgICAgIGRlbGV0ZVtdU0NSRUVOW2ldOwogICAgICAgICAgICAgICAgZGVsZXRlW11TQ1JFRU47CiAgICAgICAgICAgICAgICBkZWxldGUgcm9vdDsKICAgICAgICB9CiAgICAgICAgdm9pZCBNYWtlVHJlZSgpCiAgICAgICAgewogICAgICAgICAgICAgICAgcm9vdCA9IE1ha2VOb2RlKDApOwogICAgICAgIH0KICAgICAgICBib29sIGV4aXN0KCkKICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4gcm9vdCAhPSBOVUxMOwogICAgICAgIH0KICAgICAgICBpbnQgREZTKCk7CiAgICAgICAgaW50IEJGUygpOwogICAgICAgIHZvaWQgT3V0VHJlZSgpOwp9OwogCk5vZGUgKiBUcmVlOjpNYWtlTm9kZShpbnQgZGVwdGgpCnsKICAgICAgICBOb2RlICogdiA9IE5VTEw7CiAgICAgICAgaW50IFkgPSAoZGVwdGggPCByYW5kKCkgJSA2ICsgMSkgJiYgKG51bSA+PSAnYScpICYmIChudW0gPD0gJ3onKTsKICAgICAgICBpZiAoWSkKICAgICAgICB7CiAgICAgICAgICAgICAgICB2ID0gbmV3IE5vZGU7CiAgICAgICAgICAgICAgICB2LT5kID0gbnVtKys7CiAgICAgICAgICAgICAgICB2LT5sZnQgPSBNYWtlTm9kZShkZXB0aCArIDEpOwogICAgICAgICAgICAgICAgdi0+bWRsID0gTWFrZU5vZGUoZGVwdGggKyAxKTsKICAgICAgICAgICAgICAgIHYtPnJndCA9IE1ha2VOb2RlKGRlcHRoICsgMSk7CiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGlmICh2LT5sZnQgfHwgdi0+bWRsIHx8IHYtPnJndCkKICAgICAgICAgICAgICAgICAgICAgICAgY291bnRlcisrOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdjsKfQogCnZvaWQgVHJlZTo6T3V0VHJlZSgpCnsKICAgICAgICBjbHJzY3IoKTsKICAgICAgICBPdXROb2Rlcyhyb290LCAxLCBvZmZzZXQpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbWF4cm93OyBpKyspCiAgICAgICAgewogICAgICAgICAgICAgICAgU0NSRUVOW2ldWzc5XSA9IDA7CiAgICAgICAgICAgICAgICBjb3V0IDw8ICdcbicgPDwgU0NSRUVOW2ldOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICdcbic7Cn0KIAp2b2lkIFRyZWU6OmNscnNjcigpCnsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG1heHJvdzsgaSsrKQogICAgICAgICAgICAgICAgbWVtc2V0KFNDUkVFTltpXSwgJy4nLCA4MCk7Cn0KIAp2b2lkIFRyZWU6Ok91dE5vZGVzKE5vZGUgKiB2LCBpbnQgciwgaW50IGMpCnsKICAgICAgICBpZiAociAmJiBjICYmIChjIDwgODApKSBTQ1JFRU5bciAtIDFdW2MgLSAxXSA9IHYtPmQ7CiAgICAgICAgaWYgKHIgPCBtYXhyb3cpCiAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKHYtPmxmdCkgT3V0Tm9kZXModi0+bGZ0LCByICsgMSwgYyAtIChvZmZzZXQgPj4gcikpOwogICAgICAgICAgICAgICAgaWYgKHYtPm1kbCkgT3V0Tm9kZXModi0+bWRsLCByICsgMSwgYyk7CiAgICAgICAgICAgICAgICBpZiAodi0+cmd0KSBPdXROb2Rlcyh2LT5yZ3QsIHIgKyAxLCBjICsgKG9mZnNldCA+PiByKSk7CiAgICAgICAgfQp9CiAKdGVtcGxhdGUgPGNsYXNzIEl0ZW0+IGNsYXNzIFFVRVVFCnsKICAgICAgICBJdGVtICogUTsKICAgICAgICBpbnQgaCwgdCwgTjsKcHVibGljOgogICAgICAgIFFVRVVFKGludCBtYXhRKSA6IGgoMCksIHQoMCksIE4obWF4USkgeyBRID0gbmV3IEl0ZW1bbWF4USArIDFdOyB9CiAgICAgICAgaW50IGVtcHR5KCkgY29uc3QgeyByZXR1cm4gKGggJSBOKSA9PSB0OyB9CiAgICAgICAgdm9pZCBwdXQoSXRlbSBpdGVtKSB7IFFbdCsrXSA9IGl0ZW07IHQgJT0gTjsgfQogICAgICAgIEl0ZW0gZ2V0KCkgeyBoICU9IE47IHJldHVybiBRW2grK107IH0KfTsKIAppbnQgVHJlZTo6QkZTKCkKewogICAgICAgIGNvbnN0IGludCBNYXhRID0gMjA7CiAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICBRVUVVRSA8IE5vZGUgKiA+IFEoTWF4USk7CiAgICAgICAgUS5wdXQocm9vdCk7CiAgICAgICAgd2hpbGUgKCFRLmVtcHR5KCkpCiAgICAgICAgewogICAgICAgICAgICAgICAgTm9kZSAqIHYgPSBRLmdldCgpOwogICAgICAgICAgICAgICAgY291dCA8PCB2LT5kIDw8ICdfJzsKICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICBpZiAodi0+bGZ0KSBRLnB1dCh2LT5sZnQpOwogICAgICAgICAgICAgICAgaWYgKHYtPm1kbCkgUS5wdXQodi0+bWRsKTsKICAgICAgICAgICAgICAgIGlmICh2LT5yZ3QpIFEucHV0KHYtPnJndCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBjb3VudDsKfQogCmludCBtYWluKCkKewogICAgICAgIHNldGxvY2FsZSgwLCAiIik7CiAgICAgICAgaW50IG4gPSAwOwogICAgICAgIFRyZWUgVHIoJ2EnLCAneicsIDQpOwogICAgICAgIHNyYW5kKHRpbWUobnVsbHB0cikpOwogICAgICAgIFRyLk1ha2VUcmVlKCk7CiAgICAgICAgaWYgKFRyLmV4aXN0KCkpCiAgICAgICAgewogICAgICAgICAgICAgICAgVHIuT3V0VHJlZSgpOwogICAgICAgICAgICAgICAgY291dCA8PCBlbmRsIDw8ICLQntCx0YXQvtC0INCyINGI0LjRgNC40L3RgzogIjsKICAgICAgICAgICAgICAgIG4gPSBUci5CRlMoKTsKICAgICAgICAgICAgICAgIGNvdXQgPDwgZW5kbCA8PCAi0J/RgNC+0LnQtNC10L3QviDRg9C30LvQvtCyOiAiIDw8IG4gPDwgZW5kbDsKICAgICAgICAgICAgICAgIGNvdXQgPDwgItCa0L7Qu9C40YfQtdGB0YLQstC+INGD0LfQu9C+0LIg0YEg0YXQvtGC0Y8g0LHRiyAxINC/0L7RgtC+0LzQutC+0Lw6ICIgPDwgY291bnRlciA8PCBlbmRsOwogICAgICAgIH0KICAgICAgICBlbHNlIGNvdXQgPDwgItCU0LXRgNC10LLQviDQv9GD0YHRgtC+LiI7CiAgICAgICAgY291dCA8PCBlbmRsIDw8ICLQmtC+0L3QtdGGLiIgPDwgZW5kbDsKICAgICAgICBzeXN0ZW0oInBhdXNlIik7CiAgICAgICAgcmV0dXJuIDA7Cn0=