我正在尝试实现二进制堆。我已成功实施display()
和insert()
个功能。
问题:内部deleteHeap()
功能
方法 1.将堆的根复制到变量
2.现在将堆的最后一个元素复制到root中。减少堆节点数n
3.现在检查是否违反了堆的属性。
4.从顶部开始。标记根节点作为父节点。检查是否有任何子节点大于父节点。
5.用父母交换最大的孩子,并将这个孩子标记为新的父母。
6.如果parent_index< = n / 2返回;
7.Else转到第5步。
注意 该程序在运行时表现异常。 我建议你运行一次。!
**Code**
#include<stdio.h>
#include<stdlib.h>
int a[100],n=0;
void display()
{
int i=1;
printf("\nThe heap contains - ");
while(i<=n)
printf("%d ",a[i++]);
}
void fixheap1()
{
int j=n,temp;//Child's index
int i=n/2;//Parent's index
while(a[j]>a[i] && i!=0)
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i=i/2;
j=j/2;
}
}
void insert(int key)
{
a[++n]=key;
fixheap1();
}
int findGreatest(int j,int k)
{
if(a[j]>a[k])
return j;
else
return k;
}
void fixheap2()
{
int i=1,j,k,temp,max;
while(i<=n/2)
{
j=2i;
k=2i+1;
if(k>n && j<=n)
max=j;
else
if(j>n)
break;
else
max=findGreatest(j,k);
temp=a[max];
a[max]=a[i];
a[i]=temp;
i=max;
}
}
void deleteHeap()
{
int key=a[1];
a[1]=a[n];
n--;
fixheap2();
printf("\nElement successfully deleted %d",key);
}
int main()
{
int i,choice=0,key;
while(choice!=5)
{
system("clear");
printf("\n\t\tHeaps\n1.Insert into heap\n2.Display heap\n3.Heap sort\n4.Delete from heap\n5.Exit\n\nChoice - ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the number of elements to be inserted - ");
scanf("%d",&i);
printf("\nEnter the elements - ");
while(i--)
{
scanf("%d",&key);
insert(key);
}
printf("\nElements successfully inserted");
break;
case 2: display();
break;
case 3:
break;
case 4: deleteHeap();
break;
}
getchar();
getchar();
}
printf("\n\nThank You\n\n");
}
编辑1:有人指出错误 我已经更新了我的功能
void fixheap2()
{
int i=1,j,k,temp,max;
while(i<=n/2)
{
j=2i;
k=2i+1;
if(k>n && j<=n) //k does not exists
max=j;
else
if(j>n) //j also doesn't exist
break;
else
max=findGreatest(j,k); //find the index of greater
if(a[max]>a[i]) //if the child is greater than root
{
temp=a[max];
a[max]=a[i];
a[i]=temp;
i=max;
}
else
break;
}
}
示例输入和输出:
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 1
Enter the number of elements to be inserted - 6
Enter the elements - 6 5 4 3 2 1
Elements successfully inserted
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 2
The heap contains - 6 5 4 3 2 1
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 4
Element successfully deleted 6
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 2
The heap contains - 1 5 4 3 2
Heaps
1.Insert into heap
2.Display heap
3.Heap sort
4.Delete from heap
5.Exit
Choice - 4
Element successfully deleted 1
答案 0 :(得分:2)
您的fixheap2
没有考虑两个孩子都有效但钥匙小于父母的情况。在这种情况下,堆属性已经恢复,你应该停止与孩子交换(或者你将它重新无效)。
答案 1 :(得分:0)
@Sneftel上面已经指出了第一个错误
好的!我注意到程序中有一个小错误,编译器忽略了这个错误,很容易被注意到。
在编辑1部分
j=2i
k=2i+1
显然,2i
未评估为2*i
,这很明显,因为我们正在使用c编码,但有趣的是,它被评估为零。
但这是一个愚蠢的错误。
所以,如果我有一个程序
int main()
{
int i=21,j,k;
j=2i+1;
k=2i;
printf("\n%d\n%d\n",j,k);
}
输出
1
0