关于树的问题

dreamsdark 2008-01-30 10:21:45
主要功能:创建二叉排序树,中序遍历,结点取3的模,0,1,2个各输出不同的值。vs.net 2003.
// mlyyytree.cpp : Defines the entry point for the console application

#include "stdafx.h"
#include <iostream>
#define MAX 100
using namespace std;

typedef struct _ml_YYY_Node
{
int data;
struct _ml_YYY_Node * lc;
struct _ml_YYY_Node * rc;
}ml_YYY_BTree;//定义树结点的结构体


//二叉排序树的类定义
class ml_YYY_CreateTree
{
public:
ml_YYY_CreateTree(int r[],int n);

virtual ~ml_YYY_CreateTree();
void ml_YYY_InSert(ml_YYY_BTree * root , ml_YYY_BTree * s);//在二叉树中插入结点
void mlYYYinOrder(ml_YYY_BTree * root);//中序遍历
private:
ml_YYY_BTree * root;
ml_YYY_BTree * s; //二叉排序树的根指针
};


//插入操作
void ml_YYY_CreateTree:: ml_YYY_InSert(ml_YYY_BTree * root , ml_YYY_BTree * s)
{
if(root == NULL)
{
root = s ;
}
else if(s-> data < root-> data)
{
ml_YYY_InSert(root-> lc , s);
}
else
{
ml_YYY_InSert(root-> rc , s);
}
}


//二叉排序树的构造算法
ml_YYY_CreateTree:: ml_YYY_CreateTree(int r[],int n)
{
ml_YYY_BTree * root = NULL;
for(int i = 0;i < n;i++)
{
ml_YYY_BTree * s=(ml_YYY_BTree*)malloc(sizeof(ml_YYY_BTree));
if(s == NULL)
{
cout < <"Not Enough Memory!\n" ;
}
else
{
s-> data = r[i];
s-> lc = NULL;
s-> rc = NULL;
ml_YYY_InSert( root , s);
}
}
}


//多态输出结果

class mlYYYshow
{
public:
virtual void disPlay(int n);
private:
int n;
};

class mlYYYcircleShow:public mlYYYshow
{
public:
void disPlay(int n)
{
if(n%3 == 0)
{
cout < <"Circle-" < <endl;
}
}
};

class mlYYYtriangleShow:public mlYYYshow
{
public:
void disPlay(int n)
{
if(n%3 == 1)
{
cout < <"Triangle-" < <endl;
}
}
};

class mlYYYrectangleShow:public mlYYYshow
{
public:
void disPlay(int n)
{
if(n%3 == 2)
{
cout < <"Rectangle-" < <endl;
}
}
};


//树的中序遍历算法
void mlYYYinOrder(ml_YYY_BTree * root)
{
if(root == NULL)
{
cout < <"the tree is null." < <endl;
}
else
{
mlYYYshow show;
mlYYYinOrder(root-> lc);
show.disPlay(root-> data);
mlYYYinOrder(root-> rc);

}
}


void main()
{
int i,n;
int a[MAX];
cout < <"intput the number of the ver " < <endl;
cin > > n;
cout < <"请输入数字构造二叉树:" < <endl;
for(i=0;i <n;i++)
{
cin > > a[i] ;
if(a[i] < 0 ¦ ¦ !cin)
{
cout < <"输入有误,请重新输入数字:" < <endl;
cin.clear(); //清除错误标记
cin.get(); //清除输入缓冲区
}
}
ml_YYY_BTree * Tree ;
Tree = ml_YYY_CreateTree(a, n);
cout < <"中序遍历树,输出如下:" < <endl;
mlYYYinOrder(Tree);
}

编译错误如下:
d:\Program Files\Microsoft Visual Studio .NET 2003\pro\2\2.cpp(166): error C2440: '=' : cannot convert from 'ml_YYY_CreateTree' to 'ml_YYY_BTree *'

我真的不知道该怎么办,时间很紧。
...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouyh12345 2008-01-30
  • 打赏
  • 举报
回复
很明显,对象不能转化成指针,从错误提示的字面意义上来说,解决方法是用&取对象的地址
没看代码
paula2008 2008-01-30
  • 打赏
  • 举报
回复
up

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧