If (tree null) return (-1)


Download 20.59 Kb.
Sana28.12.2022
Hajmi20.59 Kb.
#1009234
Bog'liq
8-amaliy mashgʻulot



O’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARNI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI QARSHI FILIALI
TELEKOMUNIKATSIYA TEXNOLOGIYALARI VA KASBIY TA’LIM” FAKULTETI
TT 11-21 guruh 2-bosqich talabasi
Sirojov Hasanning
Ma’lumotlar tuzilmasi va algoritmlari fanidan tayyorlagan

8-Amaliy ishi

#include


#include
using namespace std;
class node{
public: int info;
node *left;
node *right;
};
int k=0,Flag=1;
int height(node *tree){
int h1,h2;
if (tree==NULL) return (-1);
else {
h1 = height(tree->left);
h2 = height(tree->right);
if (h1>h2) return (1 + h1);
else return (1 + h2);
}
}
void vizual(node *tree,int l)
{ int i;
if(tree!=NULL) {
vizual(tree->right,l+1);
for (i=1; i<=l; i++) cout<<" ";
cout<info< vizual(tree->left,l+1);
}
}
int AVLtree (node *tree){
int t;
if (tree!=NULL){
t = height (tree->left) - height (tree->right);
if ((t<-1) || (t>1)) { Flag = 0; return Flag; }
AVLtree (tree->left); AVLtree (tree->right);
}
}
int GetFlag(){return Flag;}
int main()
{ int n,key,s; node *tree=NULL,*next=NULL;
cout<<"n="; cin>>n; int arr[n];
for(int i=0; inode *p=new node;
node *last=new node;
cin>>s;
p->info=s;
p->left=NULL;
p->right=NULL;
if(i==0){tree=p; next=tree; continue; }
next=tree;
while(1)
{ last=next;
if(p->infoinfo)next=next->left;
else next=next->right;
if(next==NULL)break; }
if(p->infoinfo)last->left=p;
else last->right=p;}
cout<cout<<"\nbinar daraxt:\n";
vizual(tree,0);
AVLtree(tree);
if(GetFlag()) cout<<"ha, muvozanatlangan daraxt"; else cout<<"yo'q, muvozanatlanmagan daraxt";cout<getch();
}

Download 20.59 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling