社区
C++ 语言
帖子详情
請大家幫忙。
orjava
2003-11-30 10:17:27
我剛學完C++﹐想拿一些源碼作參考﹐望各位大哥能提供一些﹐
部分也可﹐先謝謝各位了﹗﹗
...全文
37
2
打赏
收藏
請大家幫忙。
我剛學完C++﹐想拿一些源碼作參考﹐望各位大哥能提供一些﹐ 部分也可﹐先謝謝各位了﹗﹗
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
brisk
2003-11-30
打赏
举报
回复
给你一个二叉树的代码吧,有一点小问题,看看能不能解决,呵呵
#include <iostream.h>
template <class Type> class BinaryTree; //前视节点的类定义
template <class Type> class BinTreeNode { //二叉树节点类的定义
friend class BinaryTree<Type>;
public:
BinTreeNode ( ) : leftChild (NULL), rightChild (NULL) { } //无参构造函数
BinTreeNode ( Type item, BinTreeNode<Type> *left = NULL,
BinTreeNode<Type> *right = NULL ) :
data(item), leftChild(left), rightChild(right){}
Type GetData ( ) const { return data; }
BinTreeNode<Type> *GetLeft ( ) const
{ return leftChild; }
BinTreeNode<Type> *GetRight ( ) const
{ return rightChild; }
void SetData ( const Type & item )
{ data = item; }
void SetLeft ( BinTreeNode <Type> *L )
{ leftChild = L; }
void SetRight ( BinTreeNode <Type> *R )
{ rightChild = R; }
private:
BinTreeNode<Type> *leftChild, *rightChild;
Type data;
};
template <class Type> class BinaryTree {
public:
BinaryTree():root(NULL) { }
BinaryTree(Type value):RefValue(value), root(NULL){}
virtual ~BinaryTree() { destroy ( root ); }
virtual int IsEmpty() { return root == NULL ? 1 : 0; }
// virtual BinTreeNode <Type> *Parent(BinTreeNode <Type> *current)
// { return root == NULL || root==current? NULL : Parent ( root, current ); }
virtual BinTreeNode <Type> *LeftChild (BinTreeNode <Type> *current )
{ return root != NULL ?current->leftChild : NULL; }
virtual BinTreeNode <Type> *RightChild (BinTreeNode <Type> *current )
{ return root != NULL ?current->rightChild : NULL; }
// virtual int Insert ( const Type & item);
// virtual int Find ( const Type &item ) const;
void CreateBiTree(BinTreeNode<Type> *bitr);
const BinTreeNode <Type> *GetRoot ( ) const
{ return root; }
friend istream &operator >> ( istream &in, BinaryTree<Type> &Tree );
friend ostream &operator << ( ostream &out, BinaryTree <Type> &Tree );
private:
BinTreeNode <Type> *root;
Type RefValue;
// BinTreeNode <Type> * Parent ( BinTreeNode <Type> *start,
// BinTreeNode <Type> *current );
int Insert ( BinTreeNode<Type> * ¤t,
const Type &item );
void Traverse ( BinTreeNode<Type> *current, ostream &out ) const;
int Find ( BinTreeNode<Type> *current, const Type &item ) const ;
void destroy(BinTreeNode<Type> *current);
};
template <class Type> void BinaryTree<Type>::destroy(BinTreeNode<Type> *current)
//私有函数:若指针current不为空,则删除跟为current的子树
{
if(current!=NULL)
{
destroy(current->leftChild);
destroy(current->rightChild);
delete current;
}
}
template<class Type> void BinaryTree<Type>::CreateBiTree(BinTreeNode<Type> *bn)
//输入并建立一颗二叉树Tree
{
Type item;
cin>>item;
if (item=='#') //判断输入是否结束 .RefValue
bn = NULL;
else{
bn = new BinTreeNode<char>();
bn->data = item;
cout<<"*"<<bn->data<<"*"<<endl;
CreateBiTree(bn->rightChild);
CreateBiTree(bn->leftChild);
//cin>>item; //输入
}
return ;
}
template<class Type> void BinaryTree<Type>::Traverse(BinTreeNode<Type> *current,ostream &out) const
//私有函数: 搜索并输出根为current的二叉树
{
if(current!=NULL) //current为空则返回,否则
{
out<<current->data<<' '; //输出current的数据值
Traverse(current->leftChild,out); //搜索并输出current的左子树
Traverse(current->rightChild,out);//搜索并输出current的右子树
}
}
template<class Type> istream& operator >> (istream& in, BinaryTree<Type> &Tree)
//重载操作:输入并建立一颗二叉树Tree. in是输入流对象
{
cout<<"Constructing binary tree: \n"<<"input end sign: "; //打印标题:构造二叉树
// cin>>item;
Tree.RefValue = '#';//item;
cout<<"Input data (end with "<<Tree.RefValue<<" ):"; //提示:输入数据
Tree.CreateBiTree(Tree.root);
return in;
}
template<class Type> ostream& operator << (ostream& out,BinaryTree<Type> &Tree)
//重载操作:输出一颗二叉树Tree. out是输出流对象
{
cout<<"Preorder traversal of binary tree: \n";
Tree.Traverse(Tree.root,out);
out<<endl;
return out;
}
int main()
{
BinaryTree<char> bt;
cin>>bt;
cout<<bt;
return 0;
}
abitz
2003-11-30
打赏
举报
回复
还是看书吧。
书上的源码就挺好的。
c语言字符数组无法赋值,C语言程序,数组元素无法赋值,请大家帮忙看看解决方案...
当前位置:我的异常网» C语言»C语言程序,数组元素无法赋值,请大家帮忙看看解决C语言程序,数组元素无法赋值,请大家帮忙看看解决方案www.myexceptions.net网友分享于:2013-03-20浏览:13次C语言程序,数组元素无法赋值,请大家帮忙看看请大家帮忙看看我的程序,明明写了赋值语句怎么会不赋值呢?我设断点看过了,spiral[count]没有被赋成matrix[i][...
最近空闲时间写的一套软件,用Web方式管理IIS设置,请大家帮忙测试
用Web方式管理IIS设置,是我用空闲时间写的一套软件,并是完全免费的。意在巩固自己所学习的IIS方面的知识,还有N多的BUG,请大家帮忙测试。 本软件为测试版软件,并以个人名义发布,由本软件产生的损失我不承担任何责任。不建议安装本测试版的软件至服务器。 推荐测试环境: Windows 2003 Server + Sql Server 2005 + .netframework 2.
编译uClinux时出错,请大家帮忙
编译uClinux时出错,请大家帮忙 make时显示: *** 2.4 kernels no longer build correctly with old versions of binutils. *** Please upgrade your binutils to 2.9.5. 我用的是从uclinux.org下的arm-elf-tools-20030314.sh uClinux
ASP模拟POST提交,然后XMLHTTP获取数据总是乱码,请大家帮忙,感谢!
目前在建的一个项目要求使用外部的一个网站达到切词的目的,由于外部网站的API接口要求必须是POST提交数据,因此只能模拟POST提交,然后再去读取提交后的数据,我用以下的代码,获取回来的中文总是乱码,英文和数字没有问题,请大家帮忙看看,非常感谢! On error resume next Function GetBody(ips) Set https = Server
看朋友日志发现的一个ios下block相关的内存管理问题,很奇怪,请大家帮忙一起来回答!
http://blog.csdn.net/fengsh998/article/details/38090205 这篇文章下面是我的回复,同样的代码只是把变量的定义从局部变量改为类的成员变量就发现了很大的差异,目前还没有找到明确的答案,请大家帮忙看一下!
C++ 语言
65,187
社区成员
250,526
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章