Implement the class template template< typename elemtype inl ine void Binary Tree<elemtype> insert(const elemtype& elem) if ( m root) m root- new BTNode<elemtype>(elem); //if succeed in mem allocation, elem will be passed to constructor else m root->insert value(elem)
Implement the class template template < typename elemtype > inline void BinaryTree<elemtype>::insert(const elemtype& elem) { if (! m_root) m_root = new BTNode<elemtype>(elem); //if succeed in mem allocation, elem will be passed to constructor else m_root->insert_value(elem); };
Implement the class template(cont template< typename elemtype inl ine void Binary Tree<elemtype> remove( const elemtype& elem) if (! m root) if(m root->m Val==elem) remove root //will be handled speciall else m root->remove value(elem, m root)
Implement the class template (cont.) template < typename elemtype > inline void BinaryTree<elemtype>::remove(const elemtype& elem) { if (! m_root) if (m_root->m_Val == elem) remove_root(); //will be handled specially else m_root->remove_value(elem, m_root); };
Implement the class template(cont template typename elemtype class Binary Tree publIc void clear i if (m root)i clear(m root); m root=0;)) private void clear(BTNode <elemtype>%); template typename elemtype void Binary Tree<elemtype> clear(B TNode<elemtype>* pt) if (pt) lear(pt->lchild) clear(pt->rchild) delete pt; 3
Implement the class template (cont.) template < typename elemtype > class BinaryTree { public: void clear() { if (m_root) { clear(m_root); m_root = 0; } } private: void clear(BTNode<elemtype>*); }; template < typename elemtype > void BinaryTree<elemtype>::clear(BTNode<elemtype>* pt) { if (pt) { clear(pt->lchild); clear(pt->rchild); delete pt; } };
Implement the class template(cont template typename valtype void B TNode<valtype> insert value(const valtype& val) if (val Val m iCnt++ return; if(val <m Val) if(! lchild) Child=new BTNode(val) child->insert value(val) else if(! rchild) rchild=new BTNode( val) rchild->insert value(val) };
Implement the class template (cont.) template < typename valtype > void BTNode<valtype>::insert_value(const valtype& val) { if (val == m_Val) { m_iCnt++; return; } if (val < m_Val) if (! lchild) lchild = new BTNode(val); else lchild->insert_value(val); else if (! rchild) rchild = new BTNode(val); else rchild->insert_value(val); };
Implement the class template(cont template typename valtype if (rchild) void BTNode<valtype> remove value (const valtype& val, BTNode& parent parent =rchild if (lchild) (val<m Val) if( parent->lchild if(! Child) parent->lchild= Child return else else BTNode<valtype Ichild->remove value(val, Child) Child leaf(lchild, parent->Child) if (val>m Val) if ( rchild) parent=lchild return delete this rchild->remove value(val, rchild); j else∥/ val equals m val
Implement the class template (cont.) template < typename valtype > void BTNode<valtype>::remove_value (const valtype& val, BTNode*& parent) { if (val < m_Val) if (! lchild) return; else lchild->remove_value(val, lchild) else if (val > m_Val) if (! rchild) return; else rchild->remove_value(val, rchild); else // val equals m_Val { if (rchild) { parent = rchild; if (lchild) if (! parent->lchild) parent->lchild = lchild; else BTNode<valtype>:: lchild_leaf(lchild, parent->lchild); } else parent = lchild; delete this; } }