Guest User

Untitled

a guest
Apr 28th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.42 KB | None | 0 0
  1. #ifndef TEST_H
  2. #define TEST_H
  3. #include <unordered_map>
  4. #include <iostream>
  5. #include <vector>
  6. #include "dsexceptions.h"
  7. using namespace std;
  8.  
  9. // Binomial queue class
  10. //
  11. // CONSTRUCTION: with no parameters
  12. //
  13. // ******************PUBLIC OPERATIONS*********************
  14. // void insert( x )       --> Insert x
  15. // deleteMin( )           --> Return and remove smallest item
  16. // Comparable findMin( )  --> Return smallest item
  17. // bool isEmpty( )        --> Return true if empty; else false
  18. // void makeEmpty( )      --> Remove all items
  19. // void merge( rhs )      --> Absorb rhs into this heap
  20. // ******************ERRORS********************************
  21. // Throws UnderflowException as warranted
  22.  
  23. static int numOfElement = 0;
  24. template <typename Comparable>
  25. class BinomialQueue
  26. {
  27.   public:
  28.     BinomialQueue( ) : theTrees( DEFAULT_TREES )
  29.     {
  30.         for( auto & root : theTrees )
  31.             root = nullptr;
  32.         currentSize = 0;
  33.     }
  34.  
  35.     BinomialQueue( const Comparable & item ) : theTrees( 1 ), currentSize{ 1 }
  36.       { theTrees[ 0 ] = new BinomialNode{ item, nullptr, nullptr }; }
  37.  
  38.     BinomialQueue( const BinomialQueue & rhs )
  39.       : theTrees( rhs.theTrees.size( ) ),currentSize{ rhs.currentSize }
  40.     {
  41.         for( int i = 0; i < rhs.theTrees.size( ); ++i )
  42.             theTrees[ i ] = clone( rhs.theTrees[ i ] );
  43.     }
  44.  
  45.     BinomialQueue( BinomialQueue && rhs )
  46.       : theTrees{ std::move( rhs.theTrees ) }, currentSize{ rhs.currentSize }
  47.     {
  48.     }
  49.  
  50.     ~BinomialQueue( )
  51.       { makeEmpty( ); }
  52.  
  53.    
  54.     /**
  55.      * Deep copy.
  56.      */
  57.     BinomialQueue & operator=( const BinomialQueue & rhs )
  58.     {
  59.         BinomialQueue copy = rhs;
  60.         std::swap( *this, copy );
  61.         return *this;
  62.     }
  63.        
  64.     /**
  65.      * Move.
  66.      */
  67.     BinomialQueue & operator=( BinomialQueue && rhs )
  68.     {
  69.         std::swap( currentSize, rhs.currentSize );
  70.         std::swap( theTrees, rhs.theTrees );
  71.        
  72.         return *this;
  73.     }
  74.    
  75.     /**
  76.      * Return true if empty; false otherwise.
  77.      */
  78.     bool isEmpty( ) const
  79.       { return currentSize == 0; }
  80.  
  81.     /**
  82.      * Returns minimum item.
  83.      * Throws UnderflowException if empty.
  84.      */
  85.     const Comparable & findMin( ) const
  86.     {
  87.         if( isEmpty( ) )
  88.             throw UnderflowException{ };
  89.  
  90.         return theTrees[ findMinIndex( ) ]->element;
  91.     }
  92.    
  93.     /**
  94.      * Insert item x into the priority queue; allows duplicates.
  95.      */
  96.     void insert( const Comparable & x )
  97.       {
  98.         BinomialQueue oneItem{ x }; merge( oneItem );
  99.         // Insert into map
  100.         pair<Comparable, BinomialNode*> two (x, oneItem.theTrees[0]);
  101.         a.insert(two);
  102.         numOfElement++;
  103.       }
  104.  
  105.     /**
  106.      * Insert item x into the priority queue; allows duplicates.
  107.      */
  108.     void insert( Comparable && x )
  109.       {
  110.         BinomialQueue oneItem{ std::move( x ) }; merge( oneItem );
  111.         // Insert into map
  112.         pair<Comparable, BinomialNode*> two (x, oneItem.theTrees[0]);
  113.         a.insert(two);
  114.         numOfElement++;
  115.        }
  116.    
  117.     /**
  118.      * Remove the smallest item from the priority queue.
  119.      * Throws UnderflowException if empty.
  120.      */
  121.     void deleteMin( )
  122.     {
  123.         Comparable x;
  124.         deleteMin( x );
  125.     }
  126.  
  127.     /**
  128.      * Remove the minimum item and place it in minItem.
  129.      * Throws UnderflowException if empty.
  130.      */
  131.     void deleteMin( Comparable & minItem )
  132.     {
  133.         if( isEmpty( ) )
  134.             throw UnderflowException{ };
  135.  
  136.         int minIndex = findMinIndex( );
  137.         minItem = theTrees[ minIndex ]->element;
  138.  
  139.         BinomialNode *oldRoot = theTrees[ minIndex ];
  140.         BinomialNode *deletedTree = oldRoot->leftChild;
  141.         delete oldRoot;
  142.  
  143.         // Construct H''
  144.         BinomialQueue deletedQueue;
  145.         deletedQueue.theTrees.resize( minIndex + 1 );
  146.         deletedQueue.currentSize = ( 1 << minIndex ) - 1;
  147.         for( int j = minIndex - 1; j >= 0; --j )
  148.         {
  149.             deletedQueue.theTrees[ j ] = deletedTree;
  150.             deletedTree = deletedTree->nextSibling;
  151.             deletedQueue.theTrees[ j ]->nextSibling = nullptr;
  152.         }
  153.  
  154.         // Construct H'
  155.         theTrees[ minIndex ] = nullptr;
  156.         currentSize -= deletedQueue.currentSize + 1;
  157.  
  158.         merge( deletedQueue );
  159.     }
  160.  
  161.     /**
  162.      * Make the priority queue logically empty.
  163.      */
  164.     void makeEmpty( )
  165.     {
  166.         currentSize = 0;
  167.         for( auto & root : theTrees )
  168.             makeEmpty( root );
  169.     }
  170.  
  171.     /**
  172.      * Merge rhs into the priority queue.
  173.      * rhs becomes empty. rhs must be different from this.
  174.      * Exercise 6.35 needed to make this operation more efficient.
  175.      */
  176.     void merge( BinomialQueue & rhs )
  177.     {
  178.         if( this == &rhs )    // Avoid aliasing problems
  179.             return;
  180.  
  181.         currentSize += rhs.currentSize;
  182.  
  183.         if( currentSize > capacity( ) )
  184.         {
  185.             int oldNumTrees = theTrees.size( );
  186.             int newNumTrees = max( theTrees.size( ), rhs.theTrees.size( ) ) + 1;
  187.             theTrees.resize( newNumTrees );
  188.             for( int i = oldNumTrees; i < newNumTrees; ++i )
  189.                 theTrees[ i ] = nullptr;
  190.         }
  191.  
  192.         BinomialNode *carry = nullptr;
  193.         for( int i = 0, j = 1; j <= currentSize; ++i, j *= 2 )
  194.         {
  195.             BinomialNode *t1 = theTrees[ i ];
  196.             BinomialNode *t2 = i < rhs.theTrees.size( ) ? rhs.theTrees[ i ] : nullptr;
  197.  
  198.             int whichCase = t1 == nullptr ? 0 : 1;
  199.             whichCase += t2 == nullptr ? 0 : 2;
  200.             whichCase += carry == nullptr ? 0 : 4;
  201.  
  202.             switch( whichCase )
  203.             {
  204.               case 0: /* No trees */
  205.               case 1: /* Only this */
  206.                 break;
  207.               case 2: /* Only rhs */
  208.                 theTrees[ i ] = t2;
  209.                 rhs.theTrees[ i ] = nullptr;
  210.                 break;
  211.               case 4: /* Only carry */
  212.                 theTrees[ i ] = carry;
  213.                 carry = nullptr;
  214.                 break;
  215.               case 3: /* this and rhs */
  216.                 carry = combineTrees( t1, t2 );
  217.                 theTrees[ i ] = rhs.theTrees[ i ] = nullptr;
  218.                 break;
  219.               case 5: /* this and carry */
  220.                 carry = combineTrees( t1, carry );
  221.                 theTrees[ i ] = nullptr;
  222.                 break;
  223.               case 6: /* rhs and carry */
  224.                 carry = combineTrees( t2, carry );
  225.                 rhs.theTrees[ i ] = nullptr;
  226.                 break;
  227.               case 7: /* All three */
  228.                 theTrees[ i ] = carry;
  229.                 carry = combineTrees( t1, t2 );
  230.                 rhs.theTrees[ i ] = nullptr;
  231.                 break;
  232.             }
  233.         }
  234.  
  235.         for( auto & root : rhs.theTrees )
  236.             root = nullptr;
  237.         rhs.currentSize = 0;
  238.     }    
  239.  
  240.     bool contains(Comparable x){
  241.  
  242.       auto it = a.find(x);
  243.       if(it == a.end()){
  244.         return false;
  245.       }
  246.       cout <<"Key: " << a.find(x)->first << " Mapped to: " << a.find(x)->second << endl;
  247.       return true;
  248.     }
  249.  
  250.   private:
  251.  
  252.     struct BinomialNode
  253.     {
  254.         Comparable    element;
  255.         BinomialNode *leftChild;
  256.         BinomialNode *nextSibling;
  257.  
  258.         BinomialNode( const Comparable & e, BinomialNode *lt, BinomialNode *rt )
  259.           : element{ e }, leftChild{ lt }, nextSibling{ rt } { }
  260.        
  261.         BinomialNode( Comparable && e, BinomialNode *lt, BinomialNode *rt )
  262.           : element{ std::move( e ) }, leftChild{ lt }, nextSibling{ rt } { }
  263.     };
  264.  
  265.     const static int DEFAULT_TREES = 1;
  266.  
  267.     vector<BinomialNode *> theTrees;  // An array of tree roots
  268.     int currentSize;                  // Number of items in the priority queue
  269.    
  270.     unordered_map<Comparable, BinomialNode*>a;
  271.     /**
  272.      * Find index of tree containing the smallest item in the priority queue.
  273.      * The priority queue must not be empty.
  274.      * Return the index of tree containing the smallest item.
  275.      */
  276.     int findMinIndex( ) const
  277.     {
  278.         int i;
  279.         int minIndex;
  280.  
  281.         for( i = 0; theTrees[ i ] == nullptr; ++i )
  282.             ;
  283.  
  284.         for( minIndex = i; i < theTrees.size( ); ++i )
  285.             if( theTrees[ i ] != nullptr &&
  286.                 theTrees[ i ]->element < theTrees[ minIndex ]->element )
  287.                 minIndex = i;
  288.  
  289.         return minIndex;
  290.     }
  291.  
  292.     /**
  293.      * Return the capacity.
  294.      */
  295.     int capacity( ) const
  296.       { return ( 1 << theTrees.size( ) ) - 1; }
  297.  
  298.     /**
  299.      * Return the result of merging equal-sized t1 and t2.
  300.      */
  301.     BinomialNode * combineTrees( BinomialNode *t1, BinomialNode *t2 )
  302.     {
  303.         if( t2->element < t1->element )
  304.             return combineTrees( t2, t1 );
  305.         t2->nextSibling = t1->leftChild;
  306.         t1->leftChild = t2;
  307.         return t1;
  308.     }
  309.  
  310.     /**
  311.      * Make a binomial tree logically empty, and free memory.
  312.      */
  313.     void makeEmpty( BinomialNode * & t )
  314.     {
  315.         if( t != nullptr )
  316.         {
  317.             makeEmpty( t->leftChild );
  318.             makeEmpty( t->nextSibling );
  319.             delete t;
  320.             t = nullptr;
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * Internal method to clone subtree.
  326.      */
  327.     BinomialNode * clone( BinomialNode * t ) const
  328.     {
  329.         if( t == nullptr )
  330.             return nullptr;
  331.         else
  332.             return new BinomialNode{ t->element, clone( t->leftChild ), clone( t->nextSibling ) };
  333.     }
  334. };
  335.  
  336. #endif
Add Comment
Please, Sign In to add comment