#ifndef __CONTIGUOUS_BIN_NODE_H___ #define __CONTIGUOUS_BIN_NODE_H___ #include ////////////////////////////////////////////// // These classes are defined in one file pair // because they're very tightly bound ////////////////////////////////////////////// template class CBinNodeL; template class CBinNodeR; template class ContiguousBinNode { public: ContiguousBinNode(); ContiguousBinNode( T& irData ); ~ContiguousBinNode(); ContiguousBinNode* LChild(); ContiguousBinNode* RChild(); int LChildOffset(); int RChildOffset(); //void LinkRChild( int iRChild ); int GetSubTreeSize(); //SubTreeSize is the number of nodes in the subtree not including the node called void SetSubTreeSize( int iSubTreeSize ); bool IsRoot(); ContiguousBinNode* Parent(); void LinkParent( int iParentOffset ); //Previously Asymetric ContiguousBinNode* LSibling(); ContiguousBinNode* RSibling(); int RSiblingOffset(); enum { //msNoChildren = -1, msNoChildren = 0, msNoParent = 0 }; T mData; protected: int mSubTreeSize; int mParentOffset; }; /* /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template class CBinNodeL : public ContiguousBinNode { public: //Constuction //Navigation Accessors virtual CBinNodeR* RSibling(); virtual int RSiblingOffset(); }; /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template class CBinNodeR : public ContiguousBinNode { public: //Constuction //Navigation Accessors virtual CBinNodeL* LSibling(); }; */ /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// #ifndef NULL #define NULL 0 #endif /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode::ContiguousBinNode() { } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode::ContiguousBinNode( T& irData ) : mData(irData) { } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode::~ContiguousBinNode() { } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode* ContiguousBinNode::LChild() { rAssert( mSubTreeSize != msNoChildren ); return (this+1); /* if( mSubTreeSize != msNoChildren ) return (this+1); else return NULL; */ } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode* ContiguousBinNode::RChild() { rAssert( mSubTreeSize != msNoChildren ); return (this+1)->RSibling(); /* if( mSubTreeSize != msNoChildren ) return (this+1)->RSibling(); else return NULL; */ } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template int ContiguousBinNode::LChildOffset() { rAssert( mSubTreeSize != msNoChildren ); return 1; /* if( mSubTreeSize != msNoChildren ) return (1); else return 0; */ } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template int ContiguousBinNode::RChildOffset() { rAssert( mSubTreeSize != msNoChildren ); return LChild()->RSiblingOffset() + 1; /* if( mSubTreeSize != msNoChildren ) // return ((CBinNodeL*)(this+1))->RSiblingOffset() + 1; return LChild()->RSiblingOffset() + 1; else return 0; */ } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template bool ContiguousBinNode::IsRoot() { if( mParentOffset == msNoParent ) return true; else return false; } /////////////////////////////////////////////////////////////// //SubTreeSize is the number of nodes in the subtree not including the node called /////////////////////////////////////////////////////////////// template int ContiguousBinNode::GetSubTreeSize() { return mSubTreeSize; } /////////////////////////////////////////////////////////////// //SubTreeSize is the number of nodes in the subtree not including the node called /////////////////////////////////////////////////////////////// template void ContiguousBinNode::SetSubTreeSize( int iSubTreeSize ) { mSubTreeSize = iSubTreeSize; } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template void ContiguousBinNode::LinkParent( int iParentOffset ) { mParentOffset = iParentOffset; } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode* ContiguousBinNode::Parent() { return (this+mParentOffset); } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode* ContiguousBinNode::LSibling() { return (this+mParentOffset)->LChild(); } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template ContiguousBinNode* ContiguousBinNode::RSibling() { // // Even unbalanced binary trees should have siblings // Though there are exceptions, I'm not dealing with them right now // return (this+mSubTreeSize+1); } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template int ContiguousBinNode::RSiblingOffset() { // // Even unbalanced binary trees should have siblings // Though there are exceptions, I'm not dealing with them right now // return mSubTreeSize+1; } /* /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template CBinNodeR* ContiguousBinNode::RSibling() { rAssert(false); return NULL; } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template int ContiguousBinNode::RSiblingOffset() { rAssert(false); return 0; } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template CBinNodeL* ContiguousBinNode::LSibling() { rAssert(false); return NULL; } ////////-------------CBinNodeR-------------//////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template CBinNodeL* CBinNodeR::LSibling() { return (this+mParentOffset)->LChild(); } ////////-------------CBinNodeL-------------//////////////////// /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template CBinNodeR* CBinNodeL::RSibling() { // // Even unbalanced binary trees should have siblings // Though there are exceptions, I'm not dealing with them right now // return (CBinNodeR*)(this+mSubTreeSize); } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// template int CBinNodeL::RSiblingOffset() { // // Even unbalanced binary trees should have siblings // Though there are exceptions, I'm not dealing with them right now // return mSubTreeSize; } */ #endif