Exemple #1
0
void WebTree::buildHeap(TreeNode *cur) {
	// Builds the heap
	
	heap->add(cur);

	if(cur->children.getSize()) {
		cur->children.goHead();
		buildHeap(cur->children.get());
		while(cur->children.next())
			buildHeap(cur->children.get());
	}
}
Exemple #2
0
 explicit BinaryHeap( const vector<Comparable> & items )
   : array( items.size( ) + 10 ), currentSize( items.size( ) )
 {
     for( int i = 0; i < items.size( ); i++ )
         array[ i + 1 ] = items[ i ];
     buildHeap( );
 }
Exemple #3
0
int main(){
	int i,n,*a;
	scanf("%d",&n);
	
	a=(int*)malloc(n*sizeof(int));
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	
	size=n;
	buildHeap(a);
	
	
	
	for(i=n-1;i>=1;i--){
		SWAP(a[0],a[i]);
		size--;
		max_heapify(a,0);
	}
	
	printf("Sorted: ");
	for(i=0;i<n;i++){
		printf("%d ",a[i]);
	}

}
int solution(int A[], int N) {
    // write your code in C99 (gcc 4.8.2)
    int* result=(int*)malloc(sizeof(int)*N);
    int answer=0;
    buildHeap(A,N);
    for(int i=0;i<N;i++)
    {
    result[i]=extractMax(A,N-i);    
    }
    for(int i=0;i<N;i++)
    {
     if(i==0){
        answer++;    
     }
     else{
        if(result[i]!=result[i-1])
        {
        answer++;    
        }
         
     }
        
    }
    return answer;
}
Exemple #5
0
// Heap Sort / O(NlogN) + O(N) / O(NlogN) / unstable 
void heapSort(int* arr, int size){
	int n = size, i;
	buildHeap(arr, size);
	for(i = 0; i < size - 1; ++i){
		swap(arr, arr + n - 1);
		heapify(arr, --n, 0);
	}
}
Exemple #6
0
void heapSort(int arr[], int n){
	int i;
	buildHeap(arr, n);
	for(i=n-1; i>=1; i--){
		swap(&arr[0], &arr[i]);
		n--;
		heapify(arr,0, n);
	}
}
Exemple #7
0
int main(void)
{
    int iarray[40]={0};
    for(int i=0;scanf("%d",&iarray[i]);i++)
        ;
    buildHeap(iarray);
    heapSort(iarray);
    return 0;
}
Exemple #8
0
 void heapSort(T data[], int n) {
   int heapSize = n;
   buildHeap(data, heapSize);
   for(int i = heapSize - 1; i > 0; i--){
     Swap(data[i], data[0]);
     heapSize = heapSize - 1;
     keepHeap(data, heapSize, 0);
   }
 }
Exemple #9
0
BinaryHeap<Comparable>::BinaryHeap(const vector<Comparable> & item)
:array(item.size() + 10), currentSize(item.size())
{
	for (int i = 0; i < item.size(); i++)
	{
		array[i + 1] = item[i];
	}
	buildHeap();
}
 int findKthLargest(vector<int>& nums, int k) {
     int n = nums.size();
     Operator opt(k < n/2);
     if (k >= n/2) {
         k = n+1-k;
     }
     buildHeap(nums, opt);
     for (int i=0; i<k-1; i++) popHeap(nums, n--, opt);
     return nums[0];
 }
Exemple #11
0
struct Heap* createAndBuildHeap(char data[], int freq[], int size)
{
    int i;
    struct Heap* Heap = BuildHeap(size);
    for (i = 0; i < size; ++i)
        Heap->array[i] = newNode(data[i], freq[i]);
    Heap->size = size;
    buildHeap(Heap);
    return Heap;
}
void heapSort(int* arr, int n) {
	// O(N lg N)
	Heap<T> h(arr, n);
	buildHeap(h);
	for (int i = h.size - 1; i >= 1; --i) {
		std::swap(h.arr[0], h.arr[i]);
		--h.size;
		maxHeapify(h, 0);
	}
}
Exemple #13
0
int main() {
    int a[] = {45, 3, 14, 7, 5, 90, 7, 12, 10, 4, 19};
    int size = sizeof(a) / sizeof(int);
    int i = 0;
    
    buildHeap(a, size);
    heapSort(a, size);
    for (i = 0; i < size; i++) {
        printf("%d ", a[i]);
    }
}
Exemple #14
0
void heapSort(int A[], int heapSize) {
  int i, tmp;
  
  buildHeap(A, heapSize);
  
  for(i = heapSize; i >= 2; i--) {
    tmp = A[1]; A[1] = A[heapSize]; A[heapSize] = tmp;
    heapSize = heapSize - 1;
    heapify(A, heapSize, 1);
  }
}
void heapSort(std::vector<int> & vect){
    int size = vect.size();
    buildHeap(vect);
    for(int i = vect.size() -  1; i > 0; i--){
        int temp = vect[0];
        vect[0] = vect[i];
        vect[i] = temp;
        size--;
        heapify(vect, 0, size);
    }
}
Exemple #16
0
void Huffman::huffTablePrint()
{
	buildHeap();
	huffEncode(root, "");
	cout << "\t-Binary Table-" << endl;

	for (map<char, string>::iterator it = huffTab.begin(); it != huffTab.end(); it++) {
		cout << it->first << " " << it->second << endl;    //prints out the table
	}
	cout << endl;
}
Exemple #17
0
/**
 * @brief Seradi pole 'array' pomoci haldy od nejvetsiho prvku po nejmensi
 * Pole 'array' je velikosti 'size'
 * Vraci serazene pole
 **/
int* heapSort(int *array, int size) {
    MinHeap*heap = buildHeap(array, size);
    for(int i=heap->size-1;i>0;i--) {
        swap(heap, 0, i);
        heap->size-=1;
        for(int j=heap->size/2-1;j>=0;j--)
            heapify(heap, j);
    }
    for(int i=0;i<size;i++)
        array[i] = heap->array[i];
    free(heap);
    return array;
}
void heap_sort(PSTPoint* array, int begin, int end) {
  // First arrange the array into a heap (root element is always higher
  // than both child elements
  buildHeap(array,begin,end);
  while(end > begin) {
    // Since the highest is first, move it to the end
    PSTArray::swap(array,begin,end);
    // Now that the highest element is last, it is sorted so don't
    // touch it again
    end--;
    // Since the smallest element is now first, rebuild the heap
    downHeap(array,0,begin,end);
  }
}
Exemple #19
0
WebTree::WebTree(string i, string o) {
	// Constructor
	
	size = 0;
	head = NULL;
	heap = NULL;
	output.open(o.c_str());

	parseInput(i);
	head->updateKeywords();

	heap = new MinHeap<TreeNode>(size);
	buildHeap(head);
}
Exemple #20
0
int main() {
    if (testIndexes()) {
        testBuildHeap();
        testInsertHeap();
        testDecreaseKey();
        testExtractMin();
        testHeapSort();
    }
    int array[9] = {4,2,3,5,1,6,4,7,9};
    MinHeap* myHeap = buildHeap(array, 9);
    printf("%d", extractMin(myHeap));
    int* a = heapSort(array, 9);

    return 0;
}
int * heapSort(int *arr, int len)
{

	buildHeap(arr, len);

	for( int i = len - 1; i > 0 ; i--)
	{
		int tmp = arr[i];
		arr[i] = arr[0];
		arr[0] = tmp;
		
		heaplify(arr, i , 0);
	}
	
	return arr;
}
Exemple #22
0
int main(int argc, char *argv[]) {
	int i,j;
	int arr[]={23,43,26,10,21,13,31,16,12,8,29,11,19,17};
	int N=sizeof(arr)/sizeof(int);
	buildHeap(arr,N);
	for(i=0;i<N;i++){
		printf("%d ",arr[i]);
	}
	printf("\n");
	j=N;
	for(i=0;i<N;i++){
		printf("%d\n", deleteMin(arr, j));
		j--;
	}
	return 0;
}
Exemple #23
0
static void sort(unsigned int num, unsigned char *buf) {

	unsigned int i = 0, j = 0;
	if(0 == num)
		return ;

	i = num - 1;
	buildHeap(num, buf);

	while(i > 0) {

		buf[0] ^= buf[i] ^= buf[0] ^= buf[i];

		neatemHeap(0, i, buf);
		i --;
	}
}
Exemple #24
0
    int findKthLargest(vector<int>& nums, int k) {
        int size ;
        if((size=nums.size()) == 0) return 0;
        if(size == 1)return nums[0];
        buildHeap(nums,k);
        for(int i = k;i<size;++i)
        {
            if(nums[i]>nums[0])
            {
                swap(nums[i],nums[0]);
                modifyHeap(nums,0,k);
            }

        }

        return nums[0];
    }
Exemple #25
0
void testBuildHeap(CuTest* test)
{
    const int n = 100;
    Task* tasks = createTasks(n);
    DynamicArray* heap = dyNew(1);
    for (int i = 0; i < n; i++)
    {
        dyAdd(heap, &tasks[i]);
    }
    for (int i = 0; i < n; i++)
    {
        shuffle(heap);
        buildHeap(heap, taskCompare);
        CuAssertIntEquals(test, n, dySize(heap));
        assertHeapProperty(test, heap);
    }
    dyDelete(heap);
    free(tasks);
}
Exemple #26
0
void testBuildHeap() {
    printf("Test 2. buildHeap: ");
    int tmp[3] = {4, 3, 1};
    MinHeap *heap = buildHeap(tmp, 3);
    int res1[3] = {1,3,4};
    int res2[3] = {1,4,3};

    if ((cmpArray(heap->array, res1, 3) ||
        cmpArray(heap->array, res2, 3)) &&
        heap->size == 3) {
        printf("OK\n");
    } else {
        printf("NOK - chyba ve funkci buildHeap\n");
    }

    makeGraph(heap, "built.dot");
    printf("Vykreslenou haldu najdete v souboru build.dot\n");
    free(heap);
}
heap_sort(int arr[], int size)
{
	int max;
	int i, n = size;
	buildHeap(arr, size);
		
	printf("Heap size is %d\n", size);
	printf("Heap is : ");	
	display(arr,size);
	display_heap(arr,size);
	printf("\n");

	while(size>1)
	{
		max = del_root(arr,&size);
		arr[size+1]=max;
		
		printf("Root = %d is deleted and placed at arr[%d]\n", max, size+1);
		
		printf("Heap size is %d\n", size);
		printf("Heap is  : ");	
		
		for(i=1;i<=size;i++)
			printf("%d  ",arr[i]);
		printf("\n");
		
		display_heap(arr, size);
		printf("\n");
		
		printf("Array is : ");	
		for(i=1;i<=size;i++)
			printf("%d  ",arr[i]);
		printf("_____");
		
		for(i=size+1;i<=n;i++)
			printf("%d  ",arr[i]);
		
		printf("\n\n");
		
		STOP;
	}
}/*End of heap_sort*/
Exemple #28
0
int main(int argc, const char * argv[]) {
  
    // Number of Vertices
    int noOfVertices;
    // Toatl Number of Edges
    int totalNumberOfEdges = 0;
    
    //Read Standard Input
    cin >> noOfVertices;
    // Create a Graph with specified noOfVertices
    Graph G(noOfVertices);
    // Create a Weighted Connected Undirected Graph
    totalNumberOfEdges = G.createWeightedConnectedGraph();
    
    // Display Adjacency List
    //G.displayGraph();
 
    int* unSortedEdgeList;
    
    unSortedEdgeList = G.getUnSortedEdgeList(totalNumberOfEdges);
    
    
    // Heapify List
    buildHeap( unSortedEdgeList, 1, totalNumberOfEdges - 1);
    
    // Heapified list of data
    cout<<"Heapified List: ";
    displayData(unSortedEdgeList, 0, totalNumberOfEdges - 1 );
    
    // HeapSort List
    heapSort(unSortedEdgeList, 0 , totalNumberOfEdges - 1 );
    
    // End CPU time
    //endCPUTime = clock();
    
    // Sorted data
    cout<<"Sorted List   : ";
    displayData(unSortedEdgeList, 0, totalNumberOfEdges - 1 );


    return 0;
}
/* Max heap: sort the array in decreasing order.  */
void heapSort( int *v, int n )
{

    int k = n - 1;
    int tmp;


    buildHeap( v, n );
    /* After the build heap, we are sure of the following.  */
    assert( v[0] <= v[n - 1] );

    printArray( v, n );

    printf( "Sorting heap.\n" );

    for ( k = n - 1; k >= 1; k-- ) {
        /* Exchange the root of the binary tree (which is the smallest
         * element in the array) with the last leaf.  */
        tmp = v[0];
        v[0] = v[k];
        v[k] = tmp;

        printArray( v, n );

        /* With each iteration, sift is executed with a smaller problem which
         * decreases its size lineary (k--).  */
        sift( v, 0, k - 1 );

        /* With the first iteration now are sure that the last element of the 
         * array is the smallest one. After having done two or more iterations 
         * we can see that the current element is smaller or equal to the one 
         * immediately previous. This means that the array is ordered
         * decreasingly.
         */
        if ( k < n - 1 )
            assert( v[k + 1] <= v[k] );
    }

    return;

}
Exemple #30
0
float Prim(queue* Q, vertex* Graph)
{
	//Take seed to be the first vertex in heap array. Change its distance from the tree to be 0.
	decKey(Graph, 0, 0);
	
	// initialize the weight of the MST so far to 0.
	float weight=0;

	// while the heap isn't empty, delete the minimum element and update the
	// remaining vertices distances from the working tree S
	while (Q->last >= 0)
	{
		// printHeap(Q,Graph);
		// printf("%i", Q->last);
		vertex min = delMin(Q, Graph);
		// printf("\ndelMin happening. returns %f\n", sqrt(min.distFromS));
		// printHeap(Q,Graph);
		weight += sqrt(min.distFromS);
		// printf("\nweight:%f\n\n", weight);

		// grab the ptr to the adjacent verticies
		AdjListNode* adjVerts = min.adjacentVertices;

		// update verticies in adjVerts
		while(adjVerts!=NULL)
		{	
			// distance from min
			float e = adjVerts->edgeLength;
			// index of the neighbor
			int ind = adjVerts->self;
			if(e < Graph[ind].distFromS)
			{
				decKey(Graph,ind,e);
			}
			adjVerts = adjVerts->next;
			buildHeap(Q, Graph);
		}
	}
	return weight;
}