PDA

View Full Version : مبتدی: selection trees (درختهای انتخابی)



Arcsinos
یک شنبه 28 آذر 1389, 13:59 عصر
سلام علیکم و رحمت الله
دوستان میخواستم الگوریتم درخت های انتخابی رو پیاده کنم یا همون برنامشو بنویسم ، به یه مشکلی بر خوردم . سوالم اینه که اگه ما مثلا 8 تا آرایه داشته باشیم در هنگام شروع برنامه باید 15 تا node داشته باشیم ؟؟؟؟؟؟
خیلی مسخره میشه که . دوستان اگه میتونن راهنمایی کنن .

Arcsinos
سه شنبه 30 آذر 1389, 08:41 صبح
دوستان نمیدونم این برنامه درسته یا نه ولی کار میکنه : این برنامه اعداد 8 تا آرایه رو میگیره و مرتب شده همشونو تو خروجی مینویسه :


// list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "string.h"
using namespace std;
struct node
{
int info;
int index;
};
/*struct student
{
string name;
int id;
};*/
struct university
{
int num;
int mem[10];
};
/*node *getnode()
{
node* q;
q= new struct node;
return q;
}*/
void sort(int p[],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n-i-1;j++)
if(p[j]>p[j+1])
{
int temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
node max(node p,node q)
{
if(p.info >= q.info)
return p;
else
return q;
}
int _tmain(int argc, _TCHAR* argv[])
{
university file[8];
int min=0;
for(int i=0;i<8;i++)
{
cout<<"\nPlease enter the number of Student of "<<i+1<<"th university : ";
cin>>file[i].num ;
cout<<"\nplease enter "<<i+1<<"th array's number : ";
for(int j=0;j<file[i].num;j++)
{
cin>>file[i].mem[j];
if(file[i].mem[j]<min)
min=file[i].mem[j];
}
sort(file[i].mem,file[i].num);
}
node tree[15];
for(int i=0;i<8;i++)
{
tree[i+7].info=file[i].mem[file[i].num-1];
tree[i+7].index=i;
}
bool flag=true;
while(flag)
{
for(int i=6;i>=0;i--)
tree[i]=max(tree[2*i+1],tree[2*i+2]);
cout<<tree[0].info<<"\n";
--file[tree[0].index].num;
if(file[tree[0].index].num!=0)
tree[tree[0].index+7].info=file[tree[0].index].mem[file[tree[0].index].num - 1];
else
tree[tree[0].index+7].info=min;
flag=false;
for(int i=0;i<8;i++)
if(tree[i+7].info!=min)
{
flag=true;
break;
}
}
getch();
return 0;
}