![](/user_photo/1144_wzNgE.jpg)
Формирование и балансировка:
n»ї#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
typedef unsigned short uShort;
using namespace std;
struct tItem //treeItem
{
int cData; //childData
tItem* lChild; //leftChild
tItem* rChild; //rightChild
};
tItem* tRoot; //treeRoot
void initTree()
{
tRoot = new tItem();
tRoot->lChild = NULL;
tRoot->rChild = NULL;
}
ifstream iFile; //input file var
struct retChild //return R
{
bool sFilled; //is record filled
uShort direction; //direction
uShort level; //is brunch the last
int cData; //child data
};
retChild loadTree( tItem* tTree, uShort lvl ) //ifile = inputFile; tTree = treeTree
{
retChild res;
if( iFile.good() )
{
iFile >> res.direction >> res.level >> res.cData;
res.sFilled=true;
}else
{
res.sFilled=false;
}
while( res.sFilled )
{
if( res.level > lvl )
{
tItem* tmp;
tmp = new tItem();
tmp->lChild = NULL;
tmp->rChild = NULL;
tmp->cData = res.cData;
if( res.direction == 0 )
{
tTree->lChild = tmp;
}else
{
tTree->rChild = tmp;
}
res = loadTree( tmp, res.level );
}else
{
break;
}
}
return res;
}
void printTree( tItem* item, uShort lvl ) //item = tree item, lvl = level
{
if( item != NULL )
{
string tmp;
tmp.clear();
for( int i = 1; i < lvl; i++ )
tmp += '-';
cout << tmp << item->cData << "\n";
printTree( item->lChild, lvl+1 );
printTree( item->rChild, lvl+1 );
}
}
void bTree(tItem *dr)
{
struct stek
{
tItem *d;
stek *s;
} *st,*st1=NULL;
tItem *dr1;
dr1=dr;
int pr=1,i=0;
for(i=0;i<2;i++)
{
st=(stek*)calloc(1,sizeof(stek));
st->d=dr1;
st->s=st1;
st1=st;
}
printf("%d vstrech\n",dr1->cData);
while(st)
{
do
{
if(pr && dr1->lChild) dr1=dr1->lChild;
else if(dr1->rChild) dr1=dr1->rChild;
pr=1;
if(dr1->lChild && dr1->rChild)
{
st=(stek*)malloc(sizeof(stek));
st->d=dr1;
st->s=st1;
st1=st;
}
printf("%d vstrech\n",dr1->cData);
} while(dr1->lChild || dr1->rChild);
dr1=st->d;
st1=st->s;
free(st);
st=st1;
if(dr1->rChild) pr=0;
}
}
int height(tItem* t){
if(t==NULL)
return 0;
if(t->lChild==NULL)
return height(t->rChild)+1;
else
if(t->rChild==NULL)
return height(t->lChild)+1;
else
if(height(t->lChild)>height(t->rChild))
return height(t->lChild)+1;
else
return height(t->rChild)+1;
}
int treeBalance(tItem* r,tItem* p)
{
if(( height(r->lChild) - height(r->rChild) )==2)
{
if(height(r->lChild->lChild)>height(r->lChild->rChild)) treeTurn_m_on(r, p);
}
if(( height(r->lChild) - height(r->rChild) )==-2)
{
if(height(r->rChild->lChild)<height(r->rChild->rChild)) treeTurn_m_op(r, p);
}
p=r;
if(r->lChild!=NULL)
do
{
}while(!treeBalance(r->lChild,p));
if(r->rChild!=NULL)
do
{
}while(!treeBalance(r->rChild,p));
return 1;
}
void treeTurn_m_on(tItem* r,tItem* p)
{
tItem* t=r->lChild;
r->lChild=t->rChild
t->rChild=r;
if(p==NULL) {return;}
if(p->cData<=t->cData)p->rChild=t;
else p->lChild=t;
}
void treeTurn_m_op(tItem*r,tItem*p)
{
tItem* t=r->rChild;
r->rChild=t->lChild;
t->lChild=r;
if(p==NULL) {return;}
if(p->cData<=t->cData)p->rChild=t;
else p->lChild=t;
}
void treeTurn_b_on(tItem*r, tItem*p)
{
tItem* t=r->lChild->rChild;
r->lChild->rChild=t->lChild;
t->lChild=r->lChild;
r->lChild=t->rChild;
t->rChild=r;
if(p==NULL) {return;}
if(p->cData<=t->cData)p->rChild=t;
else p->lChild=t;
}
void treeTurn_b_op(tItem*r, tItem*p)
{
tItem* t=r->rChild->lChild;
r->rChild->lChild=t->rChild;
t->rChild=r->rChild;
r->rChild=t->lChild;
t->lChild=r;
if(p==NULL) {return;}
if(p->cData<=t->cData)p->rChild=t;
else p->lChild=t;
}
int main()
{
initTree();
iFile.open( "tree.in" );
uShort level;
uShort cData;
iFile >> level >> cData;
tRoot->cData = cData;
loadTree( tRoot, level );
iFile.close();
printTree( tRoot, 1 );
//bTree( tRoot);
treeBalance(tRoot, NULL);
printTree(tRoot, 1);
return 0;
}
Результат действия программы:
Задача 2
Реализовать класс String для работы со строками символов. Память под строку выделять динамически. Постройте класс string так, чтобы для присваивания, передачи параметров и т.п. он имел формат по значению, который обеспечивает копирование полного представления данных строки, а не просто управляющей структуры данных класса String. В функции main привести примеры реализации класса String.
Код программы:
#include <iostream>
using namespace std;
class String{
public:
char* str;
int l;
int uid;
String(){
str = new char[1];
uid = rand() % 10000 + 1;
strcpy(str, "");
}
String(const char* s){
uid = rand() % 10000 + 1;
l=strlen(s)+1;
str = new char[l+1];
strcpy(str, s);
}
String& String::operator=(const char* s){
delete[] str;
l=strlen(s)+1;
str = new char[l+1];
strcpy(str, s);
return *this;
}
String& String::operator=(const String& s){
l=s.l;
delete[] str;
str = new char[l];
strcpy(str, s.str);
return *this;
}
friend ostream& operator<< (ostream& os, const String& s){
os << s.str;
return os;
}
friend istream& operator>> (istream& is, String& s){
char* c = new char;
is >> c;
s.l = strlen(c)+1;
s.str = new char[s.l];
strcpy(s.str, c);
return is;
}
~String(){
delete[] str;
}
};
void main()
{
String s1, s2;
cin >> s1;
s2 = s1;
cout << s2 << "\n";
s2 = "abcde";
cout << s2 << "\n";
cout << s1;
}