функция для построения дерева из симметричного и PREORDERне в состоянии понять последний шаг в этом коде
struct treenode * construct(struct listnode *inptr,struct listnode * preptr ,int num)
{
struct treenode *temp;
struct listnode *q;
int i,j;
if(num==0)
return NULL;
temp=(struct treenode *)malloc(sizeof(struct treenode));
temp->info=preptr->info;
temp->left=NULL;
temp->right=NULL;
if(num==1)/*if only one node in tree*/
return temp;
q=inptr;
for(i=q;q->info!=preptr->info;i++) q=q->next;
/*now q points to root node in inorder list and the number of nodes in its left tree is i*/
/*for left subtree*/
temp->lchild=construct(inptr,preptr->next,i);
/*for right subtree*/
for(j=1;j<=i+1;j++) preptr=preptr->next;
temp->rchild=construct(q->next,preptr,num-i-1);//unbale to understand this step as after node G construction value of i and num would be same this would result in -1
return temp;
}/*end of construct */
Вопрос: Я не могу понять, что мы хотим достичь с помощью temp->rchild=construct(q->next,preptr,num-i-1);
Что именно вы не понимаете? Синтаксис? Логика? Пожалуйста, будьте более конкретными. – Maroun
Он вызывает эту функцию рекурсивно. –
Логика @MarounMaroun не понятна, поскольку после ручной отладки я обнаружил, что значение i и num будет одинаковым на каком-то этапе, и там не будет базового случая для завершения. Я нашел этот код в книге структуры данных. – yogeshmanjhi