Advertisement
Guest User

Untitled

a guest
May 28th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1.  
  2.  
  3.  
  4. //////////////////////////////////////////////////////////////////////////
  5. //  Node Implementations (for BinaryTree)
  6.  
  7. template <typename T>
  8. sort::Node<T>::Node( )
  9. {
  10.     Left = nullptr;
  11.     Right = nullptr;
  12.     Parent = nullptr;
  13. }
  14.  
  15. template <typename T>
  16. sort::Node<T>::~Node( )
  17. {
  18.     if (Left)
  19.         delete Left;
  20.     if (Right)
  21.         delete Right;
  22. }
  23.  
  24.  
  25. //////////////////////////////////////////////////////////////////////////
  26. //  BinaryTree Implementations
  27.  
  28. template <typename T>
  29. sort::BinaryTree<T>::BinaryTree( )
  30. {
  31.     m_Root = nullptr;
  32. }
  33.  
  34. template <typename T>
  35. sort::BinaryTree<T>::~BinaryTree( )
  36. {
  37.     Clear( );
  38.  
  39.     for( Node<T> * node : m_NodeReserve )
  40.     {
  41.         delete node;
  42.     }
  43. }
  44.  
  45. template <typename T>
  46. void sort::BinaryTree<T>::Initialize( const std::vector<T> & data )
  47. {
  48.     Clear( );
  49.  
  50.     m_InitReserveSize = data.size( );
  51.  
  52.     while( m_NodeReserve.size( ) < data.size( ) )
  53.     {
  54.         AddNodeToReserve( new Node<T>( ) );
  55.     }
  56. }
  57.  
  58. template <typename T>
  59. void sort::BinaryTree<T>::Run( const std::vector<T> & data )
  60. {
  61.     for( uint i = 0; i < data.size( ); i++ )
  62.     {
  63.         AddValueToNodeSlot( nullptr, &m_Root, data[i]);
  64.     }
  65. }
  66.  
  67. template <typename T>
  68. std::string sort::BinaryTree<T>::Name( ) const
  69. {
  70.     return "Binary Tree Sort";
  71. }
  72.  
  73. template <typename T>
  74. void sort::BinaryTree<T>::FillSortedData( std::vector<T> & target ) const
  75. {
  76.     if( !m_Root )
  77.         return;
  78.  
  79.     target.reserve( m_InitReserveSize );
  80.  
  81.     ProcessNodeToSortedData( *m_Root, target );
  82. }
  83.  
  84. template <typename T>
  85. void sort::BinaryTree<T>::ProcessNodeToSortedData(
  86.     const Node<T> & targetNode,
  87.     std::vector<T> & targetContainer
  88.     ) const
  89. {
  90.     if( targetNode.Left )
  91.         ProcessNodeToSortedData( *targetNode.Left, targetContainer );
  92.  
  93.     targetContainer.push_back( targetNode.Value );
  94.  
  95.     if (targetNode.Right )
  96.         ProcessNodeToSortedData( *targetNode.Right, targetContainer );
  97. }
  98.  
  99. template <typename T>
  100. void sort::BinaryTree<T>::AddValueToNodeSlot( Node<T> * parent, Node<T> ** slot, const T & value )
  101. {
  102.     if ( *slot == nullptr )
  103.     {
  104.         Node<T> * newNode = GetNewNode( );
  105.         newNode->Parent = parent;
  106.         newNode->Value = value;
  107.  
  108.         *slot = newNode;
  109.     }
  110.     else
  111.     {
  112.         if( value < (*slot)->Value )
  113.             AddValueToNodeSlot( *slot, &(*slot)->Left, value );
  114.         else
  115.             AddValueToNodeSlot( *slot, &(*slot)->Right, value );
  116.     }
  117. }
  118.  
  119. template <typename T>
  120. void sort::BinaryTree<T>::AddNodeToReserve( sort::Node<T> * node )
  121. {
  122.     node->Left = nullptr;
  123.     node->Right = nullptr;
  124.     node->Parent = nullptr;
  125.     node->Value = T(); // Note: Could cause performance problems on a complex type T
  126.     m_NodeReserve.push_back( node );
  127. }
  128.  
  129. template <typename T>
  130. sort::Node<T> * sort::BinaryTree<T>::GetNewNode( )
  131. {
  132.     if( m_NodeReserve.size() == 0 )
  133.     {
  134.         std::cout << "Performance warning: New node manually allocated in sort::BinaryTree.\n";
  135.         return new Node<T>( );
  136.     }
  137.  
  138.     return GetReservedNode( );
  139. }
  140.  
  141. template <typename T>
  142. sort::Node<T> * sort::BinaryTree<T>::GetReservedNode( )
  143. {
  144.     Node<T> * result;
  145.     result = m_NodeReserve.back( );
  146.     m_NodeReserve.pop_back( );
  147.     return result;
  148. }
  149.  
  150. template <typename T>
  151. void sort::BinaryTree<T>::Clear( )
  152. {
  153.     if( m_Root )
  154.         delete m_Root;
  155.  
  156.     m_Root = nullptr;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement