Esempio n. 1
0
//add a leaf to the tree.  heap must stay complete at all times.
void addLeaf(heap *h,data *d)
{
	//We already have code from binary trees that we can use to
	//fill the heap completely, this should closely mirror that code

	//If the heap is empty
	if(h->root == NULL)
	{
		//Create a new leaf to put in the heap
		leaf* new_leaf = createLeaf(d);
		//Set the root to be the new leaf.
		//Note that left, right and parent are all already appropriately set for the root node
		h->root = new_leaf;
		
		//This is definitely a heap, as it contains only one node.
		dequePushFront(h->q, new_leaf);
	} 
	//If the heap is not empty
	else 
	{
		//Then our node will get linked to the first node in the deque (previously this was a queue)
		leaf* parent = dequeFront(h->q);

		//If the parent does not have a left child, then this new node becomes the left child
		if(parent->left == NULL)
		{
			//Create a new leaf, and link it in appropriately
			leaf* new_leaf = createLeaf(d);
			new_leaf->parent = parent;
			parent->left = new_leaf;
			//Enqueue the leaf
			dequePushBack(h->q, new_leaf);

			//Now we may have violated the max-heap property, we can fix this by using percolate up
			percolateUp(new_leaf);
		}
		//Otherwise the parent node should only have space in the right child
		else 
		{
			//Just to be sure that the left child was non-null
			assert(parent->left != NULL && parent->right == NULL);
			//Create a new leaf, and link it in appropriately
			leaf* new_leaf = createLeaf(d);
			new_leaf->parent = parent;
			parent->right = new_leaf;
			//Enqueue the leaf
			dequePushBack(h->q, new_leaf);

			//Now the front of the deque does not have any more space, so remove it
			dequePopFront(h->q);

			//Now we may have violated the max-heap property, we can fix this by using percolate up
			percolateUp(new_leaf);
		}
	}

	return;
}
Esempio n. 2
0
/**
 * Determines if there is a path from the source to the destination using an
 * iterative breadth-first search starting at the source.
 *
 * Remember to call clearVisited() before starting the search.
 *
 * @param graph
 * @param source
 * @param destination
 * @return 1 if there is a path, 0 otherwise.
 */
int bfsIterative(Graph* graph, Vertex* source, Vertex* destination)
{
    clearVisited(graph);
    int i;
    int found = 0;
    
    // queue
    Deque *collection = malloc(sizeof(Deque));
    Vertex *current;
    
    // initialize queue and add source
    collection = dequeNew();
    dequePushBack(collection, source);
    
    // search until destination is found or we run out of edges
    while (!(found == 1) && !dequeIsEmpty(collection))
    {
        // current vertex is the one first added to collection
        current = dequeFront(collection);
        dequePopFront(collection);
        current->isVisited = 1;
        
        // if the current node is the destination, we're done
        if (current == destination)
            found = 1;
        
        // otherwise add its neighbors to the queue
        else
        {
            for (i = 0; i < current->numNeighbors; ++i)
                if (!(current->neighbors[i]->isVisited))
                    dequePushBack(collection, current->neighbors[i]);
        }
    }
    
    // clean things up
    clearVisited(graph);
    dequeClear(collection);
    free(collection);
    
    // return if path was found
    return found;
}