binthr~1.h
来自「经典c++程序的实现」· C头文件 代码 · 共 36 行
H
36 行
class thrBinNode { // Binary tree node class
public:
BELEM element; // The node's value
thrBinNode* left; // Pointer to left child
thrBinNode* right; // Pointer to right child
int ltag, rtag;
static thrBinNode* freelist;
// Two constructors -- with and without initial values
thrBinNode() { left = right = NULL; ltag = rtag = 0; }
thrBinNode(BELEM e, thrBinNode* l =NULL, thrBinNode* r =NULL)
{ element = e; left = l; right = r; }
~thrBinNode() { } // Destructor
thrBinNode* leftchild() const { return left; }
thrBinNode* rightchild() const { return right; }
BELEM value() const { return element; };
void setValue(BELEM val) { element = val; }
bool isLeaf() const // Return TRUE if is a leaf
{ return (left == NULL) && (right == NULL); }
void* operator new(size_t); // Overload new
void operator delete(void*); // Overload delete
};
// This creates space for the freelist variable
thrBinNode* thrBinNode::freelist = NULL;
void* thrBinNode::operator new(size_t) { // Overload new
if (freelist == NULL) return ::new thrBinNode; // Create new space
thrBinNode* temp = freelist; // Otherwise, get from freelist
freelist = freelist->left;
return temp; // Return the link node
}
void thrBinNode::operator delete(void* ptr) { // Overload delete
((thrBinNode*)ptr)->left = freelist; // Put on freelist
freelist = (thrBinNode*)ptr;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?