2016-08-18 7 views
0

バイナリツリーが与えられたので、深度ごとに要素のリンクリストを作成しようとしています。それは深さDのDリストを作成します。私は非再帰的な実装を行い、私のC++コードでレベルオーバトラバーサルを使用しています。コンパイル時にエラーは表示されませんが、頭がポインタの配列に格納されていないことがわかります。私のコードを見てください。私がデータ構造を初めて知っているので、どんな助けや提案も素晴らしいだろう。ありがとう!C++でLinkedListの配列の配列を格納する方法は?

#include <iostream> 
#include <queue> 

using namespace std; 

struct BTnode 
{ 
    int data; 
    BTnode* left; 
    BTnode* right; 
}; 

struct LLnode 
{ 
    int data; 
    LLnode* next; 
}; 

BTnode* newNode(int data) 
{ 
    BTnode* node = new BTnode; 
    node->data = data; 
    node->left = NULL; 
    node->right = NULL; 
    return node; 
} 

void MakeLL(LLnode* &head, int data) 
{ 
    LLnode* temp = new LLnode; 
    temp->data = data; 

    if (head == NULL) 
    { 
     head = temp; 
     temp->next = NULL; 
    } 
    else 
    { 
     temp->next = head; 
     head = temp; 
    } 
} 

LLnode** LevelElementsLinkedlist(BTnode* &root) 
{ 
    if (root == NULL) 
     return NULL; 

    queue<BTnode*>nodeQ; 
    nodeQ.push(root); 

    int level = 0; 
    LLnode **arr; 

    while(1) 
    { 
     int count = nodeQ.size(); 

     if (count == 0) 
      break; 

     LLnode* head = NULL; 
     arr[level] = head; 
     level ++; 

     while (count > 0) 
     { 
      BTnode* node = nodeQ.front(); 

      MakeLL(head,node->data); 

      nodeQ.pop(); 

      if (node->left) 
       nodeQ.push(node->left); 
      if (node->right) 
       nodeQ.push(node->right); 

      count--; 
     } 
    } 

    return arr; 
} 

    void printLL(LLnode* head) 
    { 
     if (head == NULL) 
     return; 
     LLnode* temp = head; 

     while (temp != NULL) 
     { 
     cout << temp->data << " "; 
     temp = temp->next; 
     } 
    } 

int main() 
{ 
    BTnode* root = newNode(1); 
    root->left  = newNode(2); 
    root->right  = newNode(3); 
    root->left->left = newNode(40); 
    root->left->right = newNode(5); 
    root->right->left = newNode(60); 
    root->right->right = newNode(7); 
    root->left->left->left = newNode(8); 
    root->left->left->right = newNode(9); 
    root->left->right->left = newNode(10); 
    root->left->right->right = newNode(11); 
    root->right->left->left = newNode(12); 
    root->right->left->right = newNode(13); 
    root->right->right->left = newNode(14); 
    root->right->right->right = newNode(15); 

    LLnode** arr = LevelElementsLinkedlist(root); 

    int n = sizeof(arr)/sizeof(arr[0]); 
    cout << n; 

    for (int i = 0; i < n; i++) 
    { 
     printLL(arr[i]); 
     cout << endl; 
    } 

    cout << endl; 
    return 0; 
} 
+0

なぜノードのベクトルを使用しないのですか? –

答えて

1

あなたのコードに気づいたことはいくつか問題があります。

LevelElementsLinkedlistの機能では、arrの領域を割り当てていません。それはLLnode *arr = new LLnode *[100]である必要があります。

LLnode* head = NULL; 
arr[level] = head; 

あなたが頭にNULLを割り当て、その配列に追加されます。

を使用すると、以下のように書かれている行を確認してください。あなたの全体の配列にはNULL以外のものは含まれません。

これらの問題を修正して、コードをもう一度実行してみてください。

+0

アレイの初期化中にメモリを割り当てていなかったことを指摘してくれてありがとう@Ankit。以来、私は配列が持つ要素の数をあらかじめ割り当てたいとは思わなかった。私は自分のアプローチを変更し、代わりにリストのリストを作成しました。これは完全に正常に動作します。 –

関連する問題