社区
C++ 语言
帖子详情
請大家幫忙。
orjava
2003-11-30 10:17:27
我剛學完C++﹐想拿一些源碼作參考﹐望各位大哥能提供一些﹐
部分也可﹐先謝謝各位了﹗﹗
...全文
69
2
打赏
收藏
請大家幫忙。
我剛學完C++﹐想拿一些源碼作參考﹐望各位大哥能提供一些﹐ 部分也可﹐先謝謝各位了﹗﹗
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
还是看书吧。
书上的源码就挺好的。
四、T100采购管理之请购与请购变更管理
本文详细介绍了制造业中的请购作业目的,强调了请购在控制成本和需求准确性方面的重要性。请购流程包括作业流程和操作演示,如生产原料的请购,并涉及请购变更管理和留置管理,确保采购过程的灵活性和效率。
请猜谜 * 请猜谜 = 请边赏灯边猜
在A村的元宵节灯会上有一个有趣的数字谜题:请猜谜*请猜谜=请边赏灯边猜。通过编程找出唯一符合谜题条件的三位数897。
邮件中的:请看附件;请知悉,英语怎么说。要比较正式的用语
博客介绍了邮件中正式用语的英文表达。‘请看附件’可表述为‘Please refer to the attachment.’或‘Please refer to the attached.’;‘请知悉’可表述为‘Please be notified.’或‘Please be kindly notified.’。
[Syteline]请购单转采购单
博客介绍了请购单到采购单的操作流程,包括新建请购单头、请购单行,将请购单转换为采购单并生成单据号码,最后进行采购单打印。
请领导批阅文件怎么说_请领导吃饭,不要对外说,职场员工为何如此保密?
公司员工小王晋升为领导秘书后,因曾透露请领导吃饭,引发职场风言风语。职场中,未经证实的小道消息流传广,是员工解压方式。请领导吃饭等事若被他人知晓,无论职位有无晋升,都会给职场发展带来烦恼,所以应保守秘密。
C++ 语言
65,210
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章