Beispiel #1
0
int
Heap::showHeap(Heap* h)
{
	if(h==NULL)
	{
		return 0;
	}
	cout << h->p.node1<<"-" << h->p.node2 << " " << h->p.dist << endl;
	if(h->left!=NULL)
	{
		showHeap(h->left);
	}	
	if(h->right!=NULL)
	{
		showHeap(h->right);
	}
	return 0;
}
Beispiel #2
0
void main()
{
	clrscr();
	int n, i, p, k;
	scanf("%d",&n);
	for(i=1;i<=n;H[i]=i,i++)
		scanf("%d",&A[i]);
	hn = n;
	heapify();
	showHeap(n);
	scanf("%d",&p);
	for(i=1;i<=p;i++)
	{
		scanf("%d",&A[++n]);
		insert();
	}
	showHeap(n);
	heapSort();
	showHeap(n);
}
Beispiel #3
0
int
Heap::showHeap()
{
	showHeap(root);
	return 0;
}
Beispiel #4
0
Heap*
Heap::insertToHeapNoHeapify(string& n1, string& n2, double d)
{
	if((strcmp(n1.c_str(),"Pdlim4")==0) && (strcmp(n2.c_str(),"Trex1")==0))
	{
		cout <<"Stop here! "<< endl;
		showHeap();
	}
	
	Heap* n=new Heap;
	n->p.node1.append(n1.c_str());
	n->p.node2.append(n2.c_str());
	n->p.dist=d;
	if(root==NULL)
	{
		root=n;
		return n;
	}
	int depth=0;
	//Heap* potParent=findNewPosition(root,n,depth);
	Heap* potParent=findNewPosition_iterative(n);
	if(potParent==NULL)
	{
		root->parent=n;
		n->left=root;
		root=n;
		return n;
	}
	if(potParent->left==NULL)
	{
		potParent->left=n;
		n->parent=potParent;
	}
	else if(potParent->right==NULL)
	{
		potParent->right=n;
		n->parent=potParent;
	}
	else
	{
		Heap* leftchild=potParent->left;
		Heap* rightchild=potParent->right;
		if(leftchild->p.dist>=n->p.dist)
		{
			n->left=leftchild;
			n->parent=potParent;
			potParent->left=n;
			leftchild->parent=n;
		}
		else if(rightchild->p.dist>=n->p.dist)
		{
			n->right=rightchild;
			n->parent=potParent;
			potParent->right=n;
			rightchild->parent=n;
		}
		else
		{
			cerr <<"Something strange happened! Did not find a node with empty children" << endl;
			exit(-1);
		}
	}
	if(root->parent!=NULL)
	{
		cout <<"Some shit happened at " << root->p.node1 << " " << root->p.node2 << " root's parent is garbage"  << endl;
	}
	return n;	
}