クラスのプログラムを作成し、関数「ビルド」を呼び出そうとしています パラメータ 1 を 'TreeNode *' から 'std::string' に変換できないというエラーが表示され続けます Tree および TreeNode のクラスは他のヘッダーに書き込まれますファイルが含まれていますが、関数ビルドは、並べ替えられた inorder および preorder からツリーを作成することになっています。メインセクションは、関数を書くことになっていた方法で提供されました。関数を理解するのに助けは必要ありませんが、このエラーを修正するのに助けが必要です.
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);*/
}
TreeNode クラス
#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<<” “<<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<<” “<<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<<” “<<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
編集: tree/treenode クラスを追加