0

为类编写程序并尝试调用函数“build”我一直收到错误消息,说不能将参数 1 从 'TreeNode *' 转换为 'std::string' Tree 和 TreeNode 的类写在其他头文件中文件,但包括在内 函数构建应该从排序的中序和预排序创建一棵树。主要部分是以我们应该编写函数的方式提供给我们的。不需要帮助来找出功能,但需要帮助来修复此错误。

template <class Comparable>
TreeNode<Comparable> *build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int   Ii , int Ij);

int main()
{

    string in[] = {"Apr", "Aug" ,"Dec", "Feb", "Jul", "Jun",  "Mar", "May", "Nov", "Oct",      "Sep"} ;
    string pre[] = {"Jul", "Feb", "Apr", "Aug", "Dec", "Mar", "Jun", "May", "Sep", "Oct", "Nov"} ;
    Tree<string> *tree = new Tree<string>(build(pre,in,0,10,0,10)) ;
}


TreeNode<Comparable> *build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii , int Ij)
{
    int preIndex = 0;
    TreeNode<Comparable> rNode = new TreeNode<Comparable>(prefix[preIndex]);
    if(Ii > Ij)
    {
        return NULL;
    }
    if(preIndex>=Pj)
    {
        return NULL;
    }
    int i =0;
    for (i=Ii; i<Ij;i++)
    {
        if(infix[i]==rNode.item)
        {
            break;
        }
    }
    rNode.left=build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii , int Ij-1);
    rNode.right=build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii+1, int Ij);*/
}

树节点类

#ifndef _TREENODE_H
#define _TREENODE_H

#include <iostream>
using namespace std;
template <class Comparable>

class TreeNode {

public:     
    Comparable  item;         // The data in this node.
    TreeNode *left;   // Pointer to the left subtree.
    TreeNode *right;  // Pointer to the right subtree.

    TreeNode  (Comparable value) 
    {
        item=value;
        left=NULL;
        right=NULL;
    };     // Constructor.  Make a node containing value.
    TreeNode  (Comparable value, TreeNode *leftTree, TreeNode *rightTree) 
    {
        item=value;
        left=leftTree;
        right=rightTree;
    };// Constructor. 
    template <class Comparable>
    friend class Tree ;
 };
 #endif

树类(未明确完成)

#ifndef _TREE_H
#define _TREE_H

#include <iostream>
#include "TreeNode.h"
using namespace std;

template <class Comparable>
class Tree{
public :
    TreeNode< Comparable> *root ;

    Tree () 
    {    // dedault constructor 
        root = NULL ;
    }

    Tree  (Comparable value)  // constructor ;create a single node tree
    {
        root = new TreeNode<Comparable>(value);
    };  // constructor        

    Tree(Comparable value , Tree left, Tree right) 
    {
        root = new TreeNode<Comparable>(value, left, right);
    };
    Tree build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii , int Ij)
    {
    }
    Tree (Tree  &other)    // copy constructor
    {
        copyTree(other.root, root);
    };

    /*Tree(TreeNode *r )   //  constructor taking a pointer to a tree node
    {
        root=new TreeNode<Comparable>(r->item,r->left,r->right);
    };*/
    Tree & operator = (const Tree &rhs)         // overload assignment operator
    {
        copyTree(rhs.root,root);
    };

    ~Tree()  // destructor
    {
        delete left;
        delete right;
    }



    void preorder()
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            cout<<root->item<<” “;
            leftTemp=leftTemp->left;
            preorder();
            rightTemp = rightTemp->right;
            preorder();
        }

    }
    void postorder()
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            leftTemp=leftTemp->left;
            postorder(root->left);
            rightTemp = rightTemp->right;
            postorder(root->right);
            cout<<root->item<<” “;
        } 

    }
    void inorder()
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            leftTemp=leftTemp->left;
            inorder();
            cout<<root->item<<” “;
            rightTemp = rightTemp->right;
            inorder();
        }
    }

       // the following recursive functions that print the tree node and its level #

    void preorder(int level) 
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            cout<<root->item<<” “&lt;<level<<” “;
            leftTemp=leftTemp->left;
            preorder(level+1);
            rightTemp = rightTemp->right;
            preorder(level+1);
        }
    }
    void postorder(int level)
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            leftTemp=leftTemp->left;
            postorder(level+1);
            rightTemp = rightTemp->right;
            postorder(level+1);
            cout<<root->item<<” “&lt;<level<<” “;
        } 
    }
    void inorder(int level )
    {
        if(root != NULL)
        {
            leftTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            rightTemp = new TreeNode<Comparable>(root->item,root->left,root->right);
            leftTemp=leftTemp->left;
            inorder(level+1);
            cout<<root->item<<” “&lt;<level<<” “;
            rightTemp = rightTemp->right;
            inorder(level+1);
        }
    }

    // the following recursive function prints the  tree node with its parent and level number 

    /*void preorder(TreeNode< Comparable> *p , int level) ;  // recursive preorder with level #

    void postorder(TreeNode< Comparable> *p , int level) ;  // recursive postorder with level #

    void inorder(TreeNode< Comparable> *p , int level) ;  // recursive inorder with level #

    void byLevel();  // print the tree by level , use of STL queue class

    /*int weight() ;   // returns the total number of nodes in the tree

    int height();    //  returns the height of the tree


    // the following three are non-recursive version use of STL stack class

    /*void pre() ;        // non-recursive preorder   
    void in() ;         // non-recursive inorder() 
    void post() ;       // non-recursive postorder() 


    // static function build2Tree build a nearly perfect balanced binary tree: detail will discuss in class
    static Tree *build2Tree( Comparable arr [] , int n);  */
    void copyTree(TreeNode<Comparable>* &copiedTreeRoot, TreeNode<Comparable>* otherTreeRoot)
    {
        if(otherTreeRoot == NULL)
        {
            copiedTreeRoot = NULL;
        }
        else
        {
            copiedTreeRoot = new nodeType<Comparable>;
            copiedTreeRoot->info = otherTreeRoot->info;
            copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
            copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
        }
    }//end copyTree

} ;
#endif

编辑:添加树/树节点类

4

2 回答 2

0

你的错误在这里:

TreeNode<Comparable> rNode = new TreeNode<Comparable>(prefix[preIndex]);

这应该是

TreeNode<Comparable> * rNode = ...
                     ^
                     note star

现在让我们解释错误:

在这一行中,您尝试从 type 的表达式构造 ,但它似乎有构造函数,未标记为,接受。喜欢TreeNode<Comparable>TreeNode<Comparable>*TreeNode<Comparable>explicitstd::string

template <typename T>
struct TreeNode
{
TreeNode(const std::string&) {}
};

编译器解释代码的唯一可能方法是强制TreeNode<Comparable>转换std::string为调用该构造函数。但它不能。所以你会得到这样的错误。

现在,你必须rNode使用->insread of进行评估.,因为它变成了一个指针


另外,这段代码

rNode.left=build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii , int Ij-1);
    rNode.right=build(Comparable prefix[], Comparable infix[] , int Pi , int Pj , int Ii+1, int Ij);*/

看起来无效。它应该是

rNode->left=build(prefix, infix, Pi, Pj, Ii, Ij-1);
rNode->right=build(prefix, infix, Pi, Pj, Ii+1, Ij);

(我认为这是错字)


当您尝试在Tree此处创建时,会发生与第一点相同的事情:

Tree<string> *tree = new Tree<string>(build(pre,in,0,10,0,10)) ;

build返回你TreeNoed<std::string>,但你Tree<std::string>有一个构造函数接受std::string(即Comparable

您可能需要将新的构造函数添加到Tree

Tree(TreeNode< Comparable>* r):root(r){}
于 2012-10-21T21:25:27.670 回答
0
Tree  (Comparable value)  // constructor ;create a single node tree
    {
        root = new TreeNode<Comparable>(value);
    }

这是你的错误。它期待一个“字符串”,你传递了一个指向它的指针。

和这里

TreeNode  (Comparable value) 
    {
        item=value;
        left=NULL;
        right=NULL;
    }

同样的事情,期待一个 Comparable(这是一个字符串)并得到一个 TreeNode *

还有一些函数后面的分号是怎么回事?是的,那个接受 TreeNode * 的注释块。取消注释以了解:-)

于 2012-10-21T21:48:53.977 回答