BOOL CBinTree::RemoveNode(BinTreeNode* pSearchNode) { BinTreeNode* pNode = Find(pSearchNode); if (!pNode) return FALSE; int iCount = mCount; BinTreeNode* pParent = pNode->GetParent(); // Ok, so it has a parent, then we'll simply just disconnect it. if (pParent) { if (pParent->GetLeftChild() == pNode) { pParent->SetLeftChild(NULL); } else { ASSERT(pParent->GetRightChild() == pNode); pParent->SetRightChild(NULL); } } else { // No parent? Then we're deleting the root node. ASSERT(pNode == mRoot); mRoot = NULL; } // Disconnected, now we reconnect its children (if any) // just by adding them as we add any other node. Their // respective children will come along, since Insert doesnt // tamper with the inserted node's children. if (pNode->GetLeftChild()) Insert(pNode->GetLeftChild()); if (pNode->GetRightChild()) Insert(pNode->GetRightChild()); mCount = iCount-1; // Give the subclass a chance to do stuff to the removed node. OnRemoveNode(pNode); return TRUE; }
bool BinTreeNode::operator==(BinTreeNode x) { if(data != x.GetData()) return false; else if(leftChild != x.GetLeftChild()) return false; else if(rightChild != x.GetRightChild()) return false; return true; }