2011-11-24 105 views
3

因此,對於我的任務,我應該實現一個Node類,該類僅包含指向其兩個兄弟的數據和指針,以及讀取這些節點並從中創建二進制樹的BinaryTree。我的問題是指向樹的根似乎沒有工作。任何幫助,您可以提供將不勝感激!標識符未找到

注意:錯誤發現在BinaryTree.cpp文件的addNode方法中可以找到,它可以在問題的最後找到。另外,我也無法訪問大小的值,所以我認爲這是我無法解決的一些奇怪的範圍問題。我也不能在addNode函數中使用「this」關鍵字。

根據我的作業指導,我也不允許使用結構。

Node.H

#include <iomanip> 

using namespace std; 

class Node 
{ 

    public: 
     int data; 
     Node* leftChild; 
     Node* rightChild; 
     Node(int data, Node* leftChild, Node* rightChild); 

}; 

Node.cpp

#include <iomanip> 
#include <iostream> 
#include "Node.h" 

using namespace std; 

Node::Node(int data, Node* leftChild, Node* rightChild) 
{ 
    this->data = data; 
    this->leftChild = leftChild; 
    this->rightChild = rightChild; 
} 

BinaryTree.H

#include <iomanip> 
#include "Node.h" 

using namespace std; 

class Tree 
{ 

public: 
    Tree(int data); 
    void addNode(int data); 
    void inOrder(Node* N); 

protected: 
    Node* root; 
    int size; 
    int data; 

private: 
    int printNode(Node* N); 

}; 

BinaryTree.cpp

#include <iostream> 
#include <iomanip> 
#include "BinaryTree.h" 

using namespace std; 

//Tree constructor. Sets the values of data, size, and root. 

Tree::Tree(int data) 
{ 
    this->data = data; 
    this->size = 0; 
    this->root = new Node(data, NULL, NULL); 
} 

//Adds a node to the current Tree. 
void addNode(int data) 
{ 

    Node* tempNode = new Node(data, NULL, NULL); 
    Node* current = root; //THIS IS THE ERROR LINE. 

    while(current!=NULL) 
    { 
     //If the data we are trying to add is already in the Tree 
     if(current->data == tempNode->data) 
     { 
      cout << "Data already in the Tree."; 
     } 

     //If the data for the new node is larger than the old 
     else if(current->data < tempNode->data) 
     { 
      //See if the right child is null. If so, add the tree node there. 
      if(current->rightChild == NULL) 
      { 
       current->rightChild = tempNode; 

       return; 
      } 

      //Otherwise, traverse down the right tree. 
      else 
      { 
       current = current->rightChild; 
      } 
     } 

     //The data is smaller than the current node 
     else 
     { 
      //See if the left child is null. If so, add the tree node there. 
      if(current->leftChild == NULL) 
      { 
       current->leftChild = tempNode; 
       return; 
      } 

      //Otherwise, traverse down the left tree 
      else 
      { 
       current = current->leftChild; 
      } 
     }//End of leftChild Else 

    }//End of while 

}//End of addNode 
+0

什麼是 「當前的樹」? –

+0

這是不相關的,因此我發佈這個評論而不是答案的原因;但是根據您在此發佈的代碼,您不需要在.h文件中包含「」標頭,因爲它們將它們包含在.cpp文件中。避免在頭文件中包含文件通常是一個好主意,因爲這會迫使任何使用你的類的人包含這些文件。 –

+0

@DavidSchwartz,currentTree應該是addNode方法試圖添加新節點的當前節點。對不起,我沒有說清楚。 – jtrevag

回答

3
void addNode(int data) 

應該是:

void Tree::addNode(int data) 

,因爲它是Tree

+0

天哪,我做出了多麼愚蠢的錯誤......我不能相信我錯過了這一點。非常感謝! – jtrevag

1
//Adds a node to the current Tree. 
void addNode(int data) 

應該是類的成員函數:

//Adds a node to the this Tree 
void Tree::addNode(int data) 
+0

真棒,不敢相信我錯過了。謝謝! – jtrevag