为类编写程序并尝试调用函数“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<<” “<<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
编辑:添加树/树节点类