-3
我添加葉子函數有一些錯誤。我已經聲明瞭一個添加函數和一個調用添加函數的函數。我在第52行和第63行有一些錯誤。我將如何從無效轉換或我將不得不使用其他東西?二元搜索樹 - 添加葉子
錯誤2錯誤C2440: '=':不能從 '無效' 轉換爲 'SDI :: BST ::節點*'
頭文件:
#ifndef SDI_BT
#define SDI_BT
namespace SDI
{
class BST
{
class Node
{
public:
int data; //data element
Node* left; //pointers to left and right of data element
Node* right;
private:
};
private:
Node* root; //node for top of tree, starting node
Node* newLeaf(int data); //creates new leaf node
void leafSort(int data, Node* ptr);
void printSort(Node* ptr);
public:
BST(); //constructor
~BST(); //destructor
void addLeaf(int data); //functions that access nodes
void print();
};
}
#endif
CPP文件:
#include <iostream>
#include <string>
#include <fstream>
#include "BinaryTree.h"
using namespace SDI;
int main()
{
system("pause");
return 0;
}
BST::BST()
{
root = NULL; //assign root to nothing so it is at start
}
BST::Node* BST::newLeaf(int data)
{
Node* newNode = new Node; //node pointer for returning
newNode->data = data;
newNode->left = NULL; //assign left and right to nothing to start
newNode->left = NULL;
return newNode;
}
BST::~BST()
{
}
void BST::leafSort(int data, Node* ptr)
{
if (root == NULL) //if tree is empty
{
root = newLeaf(data); //create leaf with stuff passed into data
}
else if (data < ptr->data) //find out if pointing to left child
{
if (ptr->left != NULL) //if left node is pointing to something, if something is there
{
leafSort(data, ptr->left); //move down left pointer
}
else
{
**ptr->left = addLeaf(data);** //add new leaf
}
}
else if (data < ptr->data) //find out if pointing to right child
{
if (ptr->right != NULL) //if right node is pointing to something, if something is there
{
leafSort(data, ptr->right); //move down right pointer
}
else
{
**ptr->right = addLeaf(data);** //add new leaf
}
}
else
{
std::cout << data << "is in list already!" << std::endl;
}
}
void BST::addLeaf(int data)
{
leafSort(data, root); //call from privateLeaf function as that worries about where to add leaf etc.
}
void BST::printSort(Node* ptr)
{
if (root != NULL) //something in tree
{
if (ptr->left != NULL) //if left pointer is pointing to something
{
printSort(ptr->left); //traverse down nodes
}
std::cout << ptr->data << " "; //prints data
if (ptr->right != NULL) //if right pointer pointing to something
{
printSort(ptr->right); //traverse down nodes
}
std::cout << ptr->data << " ";
}
else
{
std::cout << "tree empty" << std::endl;
}
}
void BST::print()
{
printSort(root); //call from private print function as that worries about how to print etc.
}
我們沒有行號,請顯示(標記)找到錯誤的行。 – Arkadiy
你的'addleaf'函數不返回任何東西,這就是它被聲明爲'void'的原因。對於正在破譯的assigment語句,您需要讓'addleaf'返回一個'Node *'。 – Katie
糟糕的代碼設計 - 太多無法修復。 – Sadique