Идентификатор не найден - PullRequest
3 голосов
/ 24 ноября 2011

Итак, для моего назначения я должен реализовать класс Node, который просто содержит данные и указатели на его двух братьев и сестер, и BinaryTree, который читает эти узлы и создает из них двоичное дерево. Моя проблема, указывающая на корень дерева, похоже, не работает. Мы будем благодарны за любую помощь, которую вы можете оказать!

Примечание: ошибка обнаружена несколькими строками в методе addNode в файле BinaryTree.cpp, который можно найти в конце вопроса. Кроме того, я также не могу получить доступ к значению размера, поэтому я считаю, что это какая-то странная проблема, которую я не могу решить. Я также не могу использовать ключевое слово "this" в функции addNode.

Мне также не разрешено использовать структуры, согласно инструкции моей домашней работы.

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

Ответы [ 2 ]

3 голосов
/ 24 ноября 2011
void addNode(int data)

должно быть:

void Tree::addNode(int data)

, поскольку это функция-член класса Tree

1 голос
/ 24 ноября 2011
//Adds a node to the current Tree.
void addNode(int data)

Должно быть:

//Adds a node to the this Tree
void Tree::addNode(int data)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...