PDA

View Full Version : کمک در اشکال یابی یه برنامه در C



Mr.Keivan
یک شنبه 09 تیر 1387, 23:55 عصر
با سلام
خدمت اساتید واقعاً استاد

خدمتتون عرض کنم کد زیر به نظرم داریای چند خطا و اشکال میباشد اگه همراه با توضیح برام بنویسید که اشکال من در کجا و چه بوده و کد درست رو هم بنویسید بی نهایت ممنونتون میشم
و کلی دعاتون میکنم


برنامه پيمايش پسوندي يك درخت به صورت غير بازگشتي با استفاده از پشته با زبان c

در اين برنامه من چند تابع تعريف كردم

Insert : تابعي كه يك درخت دودويي را از ورودي مي گيرد.

Postorder : اين تابع در تابع insert فراخواني مي شود تا درختت دودويي كه مي خواهد از ورودي بگيرد پيمايش كند.

پس از گرفتن درخت از ورودي حال ما مي خواهيم با استفاده از توابع زير اين درخت را به صورت پسوندي پيمايش كند و در خروجي نيز نشان دهد.

makePostorder :تابعي كه با فراخواني ؟آن درخت به صورت پسوندي پيمايش شود. و درون آن توابع زير فراخواني مي شود:

push : مقداري را در داخل پشته قرار مي دهد(فرزندان راست درخت را با علامت منفي و چپ را با علامت مثبت)

Pop : مقداري را از داخل پشته بر ميدارد (البته پس از پيمابش فرزندان چپ node)

Top : تابع كه مثل pop است فقط عنصر بالاي پشته را برمي گرداند بدون اينكه آن را از داخل پشته حذف كند.

chengeTop : اگر گره اي فرزند راست دارد آن را منفي كن و در داخل پشته قرار بده.








#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define max 100
#define true 1;
void push(int);
int pop ( );
int top ();
void changeTop(struct tree*);
void postorder (struct tree*)
int makePostorder(struct tree *);
struct tree
{
struct tree *lchild;
int data;
struct tree *rchild;
};



int *p1; // pointer to stack 's memory
int *tos; // pointer to upper stack.
int *bos; // pointer to lower stack.

struct tree *insert(struct tree *p,int val)
{
struct tree *temp1,*temp2;
if(p == NULL)
{
p = (struct tree *) malloc(sizeof(struct tree));
/* insert the new node as root node*/
if(p == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
p->data = val;
p->lchild=p->rchild=NULL;
}
else
{
temp1 = p;
/* traverse the tree to get a pointer to that node whose child will be the newly created node*/
while(temp1 != NULL)
{
temp2 = temp1;
if( temp1 ->data > val)
temp1 = temp1->lchild;
else
temp1 = temp1->rchild;
}
if( temp2->data > val)
{
temp2->lchild = (struct tree*)malloc(sizeof(struct tree));
/*inserts the newly created node as left child*/
temp2 = temp2->lchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
else
{
temp2->rchild = (struct tree*)malloc(sizeof(struct tree));

/*inserts the newly created node as left child*/
temp2 = temp2->rchild;
if(temp2 == NULL)
{
printf("Cannot allocate\n");
exit(0);
}
temp2->data = val;
temp2->lchild=temp2->rchild = NULL;
}
}
return(p);
}
//*******************************
int main ()
{
/* saved memory for stack.*/
p1 = (int*) malloc(max * sizeof(int));
if (!p1)
{
printf("\n allocation failure.");
exit(1);
}
tos = p1;
bos = p1+max-1;
struct tree* root=NULL;
int n,x;
printf("Enter the number of nodes\n");
scanf("%d",&n);
while( n != 0)
{
printf("Enter the data value\n");
scanf("%d",&x);
root = insert(root,x);
}
printf("The created tree is :\n");
postorder(root);
printf ("The created tree after postorder procces is:\n" );
makePostorder(root);

}
//**********************************
/* a function to binary tree in inorder */
void postorder(struct tree *p)
{
if(p != NULL)
{
postorder(p->lchild);
postorder(p->rchild);
printf("%d\t",p->data);

}
}

//*************************
/* a function that pushed information in stack*/
void push (int x)
{
if (p1 > bos)
{
printf("\n stack is full.");
return 0;
}
*p1=x;
p1++;
}
//*************************
/* a function that poped information of stack*/
int pop ()
{
p1--;
if (p1 < tos)
{
printf("\n stack is empty.");
return 0 ;
}
return *p1;
}
//******************
/* a function that it's simple function pop but didn't remove information of stack and pointer upper stack. */
int top ()
{
if (p1 < tos)
{
printf("\n stack is empty.");
return 0 ;
}
return *p1;
}
//************************
/* a function changed information in stach*/
void changeTop(struct Tree *root)
{
struct tree *cn=root;
/*if node didn't have rchild then poped information of stack until node had rchild.*/
if (tos->rchild == NULL)
while(tos->rchild == NULL )
{
cn->data=pop();
printf("%d",cn->data);
cn++;
}
/*if node had rchild then gived sign negative to rchild*/
else
cn->rchild =-(cn->rchild);
return ;
}

//***********************
/*a function that maked nodes of binary tree to postorder and showed nodes in output */

int makePostorder(struct Tree *cn)
{
if ( &cn == NULL )
{
printf("empty tree. ");
return 0 ;
}
while (1)
{
/*until didn't meet null in tree, pushed root and lchild to stack.*/
while ( &cn != NULL)
{
push(cn->data);
*cn = cn -> lchild;
}
/*while node was negative in stack,therefore poped rchild of stack and showed in output.*/
cn.data=top();
while (cn->data < 0)
{
cn =pop( );
printf("%d",cn->data);
cn=top();
if ( cn->data == NULL)
return 0;
}
cn = cn -> rchild;
/*go to function changetop.*/
changeTop (cn );
return 0;
}
}

Mr.Keivan
چهارشنبه 12 تیر 1387, 19:54 عصر
هنوز یه استادی نتونسته اینو نگاه کنه؟

Mr.Keivan
چهارشنبه 12 تیر 1387, 22:17 عصر
کمک نکنین
دستتون درد نکنه
ممنونم

Mr.Keivan
شنبه 15 تیر 1387, 19:05 عصر
help me plz