2017-06-15 7 views
2

私はツリーを印刷するために使用した表示機能は、最初の要素を印刷するように見えます。再帰なしで使用した挿入関数が原因である可能性があると思われる理由はわかりませんが、どこが間違っているのか分からないようです。どのように修正するか、コードがどこで失敗するかについての説明は役に立ちます。ありがとう。このツリー表示機能が最初の要素だけを印刷するのはなぜですか?

#include <stdio.h> 
#include<stdlib.h> 

void insert(int data_add,struct tree *temp); 
void display(struct tree *temp); 

struct tree 
{ 
    int data; 
    struct tree *left; 
    struct tree *right; 
} *root = NULL; 

int main() 
{ 
    int data_add,n; 
    while(1) 
    { 

    printf("\n\n1.Add\n2.Display\n4.Exit\n"); 
    scanf("%d",&n); 
    switch(n) 
    { 
     case 1: printf("\nEnter the element to add "); 
       scanf("%d",&data_add); 
       insert(data_add,root); 
       break; 
     case 2: printf("The nos are: "); 
       display(root); 
       break; 

     /*case 3: printf("The nos are: "); 
       reversedisplay(root);*/ 
     case 4: exit(1); 
       break; 
     default: printf("\nChoose a appropriate option"); 
    } 
    } 
} 

void insert(int data,struct tree *temp) 
{ 
    struct tree *current; 
    current = (struct tree*) malloc(sizeof(struct tree)); 
    current->data = data; 

    if(root == NULL) 
    { 
    root = current; 
    current->left = NULL; 
    current->right = NULL; 
    } 
    else 
    { 
    while(temp!=NULL) 
    { 
     if(data<temp->data) 
     { 
     temp = temp->left; 
     } 
     else 
     { 
     temp = temp->right; 
     } 
    } 

    temp = current; 
    current->left = NULL; 
    current->right = NULL; 
    } 

} 


void display(struct tree *temp) 
{ 
    if(temp == NULL) 
    return; 

    display(temp->right); 
    display(temp->left); 

    printf("%d",temp->data); 
} 
+0

問題は、要素を挿入するときに、他のノードの左または右の子として新しく挿入された要素を割り当てていないことです。新しい要素がツリーに追加されないため、要素を挿入しようとするたびに、挿入中にツリーをたどるだけです。 –

+0

しかし、私は現在のスペースを割り振りました。そして、トラバースした後、現在の温度にtempを割り当てていますか?それで、ツリーに新しい要素が追加されることはありませんか?だからそれを達成する方法は? –

+0

私の答えをチェックしてください。 –

答えて

0

あなたのコード内の問題は、ノードを挿入しながら、その新しいノードが実際にあなたのツリーに挿入されていないので、あなたは、任意のノードの左または右の子として新しい挿入されたノードを作っていないことです。物事がうまくいかない あなたのコード - ループから出てきた後

temp = current; 
current->left = NULL; 
current->right = NULL; 

は、tempは今currenttempに割り当てられている、NULLであるが、内の他のノードから新しいノードに到達する方法はありませんツリー内の任意のノードの左/右の子ではないためです。
ここで私はinsert関数のための正しいコード提示 -
printf("%d ",temp->data); - にあなたのprintfステートメントを変更、また

void insert(int data,struct tree *temp) 
{ 
    struct tree *current; 
    current = (struct tree*) malloc(sizeof(struct tree)); 
    current->data = data; 
    current->left = NULL; 
    current->right = NULL; 

    if(root == NULL) 
    { 
    root = current; 
    current->left = NULL; 
    current->right = NULL; 
    } 
    else 
    { 
     struct tree *par=temp; //par is used to keep track of node whose left 
          //or right child will be the new node. 
     while(temp!=NULL) 
     { 
     par=temp; 
     if(data<temp->data) 
     temp=temp->left; 
     else temp=temp->right; 
     } 
     // The below part is used to make the new node as left or right child 
     // of the appropriate node. 
     if(data<par->data) 
      par->left=current; 
     else 
     par->right=current; 
    } 

} 

、あなたのdisplay機能のわずかな変化を。 以前のバージョンでは、すべてのノード値がスペースなしで印刷され、1つの数字だけが印字されるという印象を与えました。

+0

ちょうど1つの質問は、コードが素晴らしい作品と私​​は間違って理解しても変数のパーがそこには何を理解するが、parとtempが同じものである場合、tempはどのようにtempが変わっている間も既存のツリーをどのように指しているか。ポインターが同じように等しい場合、ポインターは変更されたときに同じ場所を指すだけですか?多分私はノブの質問を知っているかもしれませんが、明確にしてください。 –

0

問題はinsert機能にあります。新しいノードを古いノードにリンクすることはありません。ポインタを使用して、右または左のノードのアドレスを保持する必要があります。その代わりに、現在のノードのアドレスをローカルtemp変数にコピーするだけです。

... 
    else 
    { 
    t = &temp; 
    while(*t!=NULL) 
    { 
     if(data<(*t)->data) 
     { 
     t = &(*t)->left; 
     } 
     else 
     { 
     t = &(*t)->right; 
     } 
    } 

    *t = current; // actually copy current in right of left of last node 
    current->left = NULL; 
    current->right = NULL; 
    } 

しかし、それがすべてではありません、順序で要素を表示するためには、あなたがに表示を変更する必要があります:あなたのコードは、あるべきwhileループ以下の時間では

void display(struct tree *temp) 
{ 
    if(temp == NULL) 
    return; 

    display(temp->left);  // left side first 
    printf("%d",temp->data); // then current 
    display(temp->right);  // finally right side 
} 
0

tempが保証され、終了はNULLとなります。次にtemp = currentを実行すると、ローカルポインタtempcurrentを指すようになります。あなたがやろうとしたことはしません。

while(temp!=NULL) 
{ 
    if(data < temp->data) 
     temp = temp->left; 
    else 
     temp = temp->right; 
} 
temp = current; 

以下のように変更してください。

while(true) 
{ 
    if(data <= temp->data) 
    { 
     if (temp->left == NULL){ 
      temp->left = current; 
      return; 
     } 
     else 
      temp = temp->left; 
    } 
    else 
    { 
     if (temp->right == NULL){ 
      temp->right = current; 
      return; 
     } 
     else 
      temp = temp->right; 
    } 
} 

提案

  • あなたは引数が冗長であるとして、それを渡して、グローバル変数としてrootノードを宣言してきたように。トラバースするローカル変数としてtempを宣言します。
関連する問題