📄 subject_23921.htm
字号:
<p>
序号:23921 发表者:Zerg 发表日期:2002-12-07 19:26:42
<br>主题:帮我写个二叉树的实例吧.(有定义)谢谢!!!
<br>内容:帮我写个二叉树的实例吧.(有定义)谢谢!!!<BR>#include<iostream><BR><BR>using std::cout;<BR>using std::endl;<BR>using std::cin;<BR><BR>////////////////////////////////////////////////////////<BR>class BinNode { // Binary tree node class<BR>public:<BR> int element; // The node's value<BR> BinNode* left; // Pointer to left child<BR> BinNode* right; // Pointer to right child<BR> static BinNode* freelist;<BR> // Two constructors -- with and without initial values<BR> BinNode() { left = right = NULL; } //Overload BinNode<BR> BinNode(int e, BinNode* l =NULL, BinNode* r =NULL)<BR> { element = e; left = l; right = r; } //initialize left,right,element<BR> ~BinNode() { } // Destructor<BR> BinNode* leftchild() const //return leftpoint<BR> { return left; }<BR> BinNode* rightchild() const //return rightpoint<BR> { return right; } <BR> int value() const //return element<BR> { return element; };<BR> void setValue(int val) <BR> { element = val; } //set the element<BR> bool isLeaf() const // Return TRUE if is a leaf<BR> { return (left == NULL) && (right == NULL); } <BR> void* operator new(size_t); // Overload new <BR> void operator delete(void*); // Overload delete<BR>};<BR>//////////////////////////////////////////////////////////////////////////////////<BR>// This creates space for the freelist variable<BR>BinNode* BinNode::freelist = NULL; //initialize freelist<BR><BR>void* BinNode::operator new(size_t) {<BR> if(freelist == NULL) return(::new BinNode);<BR> BinNode* temp = freelist; freelist = freelist->left;<BR> return temp;<BR>}<BR>//////////////////////////////<BR>void BinNode::operator delete(void* ptr) {<BR> ((BinNode*)ptr)->left = freelist;<BR> freelist = (BinNode*)ptr;<BR>}<BR>////////////////////<BR>//<BR>//!!!!!!!!!!!!!!!!!!!!!!!!!!以上是教材上的内容!!!!!!!!!!!!!!!!!!!!<BR>//<BR>////////////////////////////////////////////////////////////////////////////////<BR>//以下是自己写的,问题多多!!!!!!!!!<BR>////////////////////////////////////////////////////////////////////////<BR>BinNode Creatree(BinNode *p);<BR>int main()<BR>{<BR><BR><BR><BR><BR><BR><BR> <BR> return 0;<BR>}<BR>///////////////////////////////////////////<BR>BinNode Creatree(BinNode *p)<BR>{<BR> int temp;<BR> int num=10;<BR><BR> while(num--)<BR> { <BR> cin>>temp;<BR> if( temp==0 )<BR> { p = NULL; }<BR> else<BR> {<BR> p = new BinNode( temp );<BR> p->element = temp; <BR><BR> Creatree(p->left);<BR> Creatree(p->right);<BR> }<BR> }<BR> <BR> return *p;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -