• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

اشکال در برنامه

nafar_2konkor

Registered User
تاریخ عضویت
31 آگوست 2007
نوشته‌ها
95
لایک‌ها
1
سلام
من میخوام یه برنامه بنویسم که یه درخت دودویی رو بگیره و به نخی تبدیل کنه ولی توی گرفتن درخت مشکل دارم یه چیزایی نوشتم. یه قسمتیشو براتون میذارم. نمیدونم اصلا درسته یا خیلی پرت و زاغارته.لطفا بگید اشکالش چیه اگرم خیلی پرته لطفا راهنمایی کنید چه کار کنم .
ممنون

کد:
#include <iostream.h>
class btreenode
{friend class btree;

    public:

    btreenode(char , btreenode *, btreenode *);
    btreenode();
    private:
	  btreenode *lchild;
	  char data;
	  btreenode *rchild;
};
btreenode::btreenode(char ch, btreenode *Lefty, btreenode *Righty)

{
	data = ch;
	lchild= Lefty;
	rchild= Righty;
}
btreenode::btreenode()

{
	data = 0;
	lchild= rchild=0; 
}

//$$$$$$$$$$$$$$$$$$$$$$$$$
class btree{
public:
	btree();
	btree(btreenode *,char ,btreenode *);
	btreenode getroot();
	btreenode gettree(btreenode *);
private:
	btreenode *root;
};



btree::btree()
{root->data=0;
root->lchild=root->rchild=root;
}

btree::btree(btreenode *lc,char d,btreenode *rc)
{root->data=d;
root->lchild=lc;
root->rchild=rc;
}


btreenode btree::getroot(){
     int test;
	root=new btreenode;
    cout<<"enter root";
	cin>>root->data;
	cout<<"if root have lchild enter 1 else enter0 \n";
    cin>>test;
    if(test==1)
	{root->lchild=new btreenode;
	cout<<"enter root->leftchild \n";
	cin>>root->lchild;
	}


	cout<<"if root have rchild enter 1 else enter0 \n";
    cin>>test;
    if(test==1)
	{	root->rchild=new btreenode;
	cout<<"enter root->rftchild \n";
	cin>>root->rchild;
    gettree(root->lchild);
	gettree(root->rchild);
	}
	
	

}



btreenode btree::gettree(btreenode *b)
{int n;
	cout<<"enter the node number:\n";
cin>>n;
	/*if n=1
		return root;
  else*/
	  for(int i=1;i<n+1;n++)
	  {
	btreenode *temp=b;
temp=new btreenode;
cout<<"enter data\n";
cin >>temp->data;
cout<<"enter lchild\n";
cin>>temp->lchild;
temp=temp->lchild;
gettree(temp);

cout<<"enter rchild\n";
cin>>temp->rchild;
temp=temp->rchild;
gettree(temp);
	

return *temp;
	  }
من فقط تا جمعه وقت دارم . لطفا دریغ نکنید.
 

nafar_2konkor

Registered User
تاریخ عضویت
31 آگوست 2007
نوشته‌ها
95
لایک‌ها
1
یعنی هیچ کس بلد نیست اشکال این برنامه رو بگیره.بابا پس برای چی اصلا این تاپیک ایجاد

شده . من خودم برنامه رو تقریبا تکمیل کردم . لااقل همینو درست کنید برام.این برنامه

باید یه درخت رو به صورت دودویی بگیره و به نخی تبدیل کنه . ولی وقتی براش

main مینویسم ارور میده . اگه مکمکنه یکی اشکالشو رفع کنه.
کد:
#include <iostream.h>
class threadnode{
friend class threadtree;
public:
/*	threadnode(bool lth,threadnode * lch,int dat ,threadnode * rch,bool rth)
	{lthread=lth;
    lchild=lch;
	data=dat;
	rchild=rch;
	rthread=rth;
	}


	threadnode(char ch) { data = ch;};

   threadnode(threadnode * lch,int dat ,threadnode * rch)
	{lchild=lch;
	data=dat;
	rchild=rch;
	};
*/

private:
	bool lthread;
    threadnode *lchild;
	char data;
    threadnode *rchild;
	bool rthread;

};


////////////////////////////////////////////////////////////////////////

class threadtree{
public:
//void gettree();
threadtree(int th)
	{
    nn=th;
	r=root=new threadnode[nn];}

threadnode* change(threadnode *t);
threadnode* next(threadnode *a);
threadnode* before(threadnode *a);
threadnode* InorderSucc(threadnode *current);
void InsertRight(threadnode *s, char ch);
threadnode* Inordercuss(threadnode *current);
void Insertleft(threadnode *s, char ch);
void creat();


private:
	threadnode *root;
threadnode *r;

	int nn;//nn=node number
};
//////////////////////////////////////////


threadnode* threadtree::next(threadnode *a)
{threadtree t(nn);
t.root=a;
 
	threadnode *temp=a->rchild;
	if(!a->rthread)
		while(!temp->lthread)
			temp=temp->lchild;
		a=temp;
		if(a==t.root)
		{cout<<"tree has just one node \n";
		 return root;
		}

	else
		return a;
};

//////////////////////////////////////////

threadnode* threadtree::before(threadnode *a)
{threadtree t(nn);
t.root=a;
threadnode *temp=a->lchild;
         if(!a->lthread)
           while(!temp->rthread)
			   temp=temp->rchild;
        	a=temp;
                if(a==t.root)
				{cout<<"tree has just one node \n";
		            return root;
				}
                else
			return a;

		
};

///////////////////////////////////////////

threadnode* threadtree::InorderSucc(threadnode *current)
{
    threadnode*temp = current->rchild;
    if (! current->rthread)
       while (! temp->lthread) temp = temp->lchild;
    return temp;
};
/////////////////////////////////////////
void threadtree::InsertRight(threadnode *s, char ch)

{
   threadnode *r = new threadnode;
   r->data=ch;
    r->rchild = s->rchild;
    r->rthread = s->rthread;
    r->lchild = s;
    r->lthread = true; 
    s->rchild = r; 
    s->rthread = false;
    if (! r->rthread) {
	threadnode *temp = InorderSucc(r);
	temp->lchild = r;
    }
};
/////////////////////////////////////


threadnode* threadtree::Inordercuss(threadnode *current)
{
    threadnode *temp = current->lchild;
    if (! current->lthread)
       while (! temp->rthread) temp = temp->rchild;
    return temp;
};

//////////////////////////////////////
void threadtree::Insertleft(threadnode *s, char ch)

{
    threadnode *r = new threadnode;
	r->data=ch;

    r->lchild = s->lchild;
    r->lthread = s->lthread;
    r->rchild = s;
    r->rthread = true; 
    s->lchild = r; 
    s->lthread = false;
    if (! r->lthread) {
	threadnode *temp = Inordercuss(r);
	temp->rchild = r;
    }
};

////////////////////////////////////////////
void threadtree::creat()
{
//	cout<<"entre the number of tree";
//	cin>>nn;

	/////////
	int m=0;
	char d,lch,rch;
     cout<<"enter  root data \n";
	 cin>>d;
	 r=root=new threadnode;
	 r->data=root->data=d;
	// cout<<"enter date of lch and rch";
	 //cin>>lch>>rch;
	 //threadnode *r=root;

    for (int i=0;i<nn;i++)
	{   
		cout<<"enter data"<<i<<"om \n";
        cin>> r[i].data;

        cout<<"enter lch & rch:\n";
		cin>>rch>>lch;

		if(lch!=0)
		{threadnode *temp=new threadnode;
			temp->lchild=temp->rchild=0;
			r[i].lchild=temp;
         r[++m]=*temp;

		}

      else
      r[i].lchild=0;
	  if(rch!=0)
	  {
       threadnode *temp=new threadnode;
       temp->lchild=temp->rchild=0;
        r[i].rchild=temp;
       r[++m]=*temp;
	  }
        else
         r[i].rchild=0;
	}
};



threadnode* threadtree::change(threadnode *t)
{    for(int i=0;i<nn;i++)
{     if(r[i].lchild)
          r[i].lthread=false ;
       else
	   {r[i].lthread=true;
        r[i].lchild=before(&r[i]);
	   }
}

return t;
};
//////////////////////////////////////////////////////
void main()
{threadtree t;
t.creat();

}
 
بالا