VS2010程序:windows已在xxx.exe中触发一个断点,其原因可能是堆被损坏,这说明xx.exe中或它所加载的任何DLL中有bug。
时间:2011-12-28
来源:互联网
#pragma once
#include<stdlib.h>
template<class T>
class MinHeap
{
public:
MinHeap(int MinHeapSize = 10);
~MinHeap() {delete [] heap;}
int Size() const {return CurrentSize;}
T Min()
{
if (CurrentSize == 0)
throw OutOfBounds();
return heap[1];
}
MinHeap<T>& Insert( T& x);
MinHeap<T>& DeleteMin(T& x);
void Initialize(T a[], int size, int ArraySize);
void Deactivate() {heap = 0;}
void Output() const;
MinHeap<T>& GetHeap();
private:
int CurrentSize, MaxSize;
T *heap;
};
template<class T>
MinHeap<T>&MinHeap<T>::GetHeap()
{
return heap;
}
template<class T>
MinHeap<T>::MinHeap(int MinHeapSize)
{
MaxSize = MinHeapSize;
heap = new T[MaxSize+1];
CurrentSize = 0;
}
template<class T>
MinHeap<T>& MinHeap<T>::Insert( T& x)
{
if (CurrentSize == MaxSize)
{
cout<<"溢出!"<<endl;
system("pause");
}
//为x寻找应插入的位置
//i从新的叶节点开始,并沿着树上升
int i = ++CurrentSize;
while (i != 1 && x < heap[i/2])
{
heap[i] = heap[i/2]; // 将元素下移
i /= 2; // 移向父节点
}
heap[i] = x;
return *this;
}
template<class T>
MinHeap<T>& MinHeap<T>::DeleteMin(T& x)
{
if (CurrentSize == 0)
{
cout<<"删除异常!"<<endl;
system("pause");
}
x = heap[1];
T y = heap[CurrentSize--]; //最后一个元素
// 从根开始, 为y寻找合适的位置
int i = 1, // 堆的当前节点
ci = 2; // i的子节点
while (ci <= CurrentSize)
{
// 使heap[ci] 是i较小的子节点
if (ci < CurrentSize
&& heap[ci] > heap[ci+1])
ci++;
// 能把y放入heap[i]吗?
if (y <= heap[ci])
break; // 能
// 不能
heap[i] = heap[ci]; // 子节点上移
i = ci; // 下移一层
ci *= 2;
}
heap[i] = y;
return *this;
}
template<class T>
void MinHeap<T>::Initialize(T a[], int size, int ArraySize)
{ //size为数组a的元素的个数,ArraySize为数组从a[1]算起的元素个数
delete [] heap;
heap=new T[size+1];
for(int i=1;i<=size;i++)
heap[i]=a[i];
CurrentSize = size;
MaxSize = ArraySize;
// 产生一个最小堆
for (int i = CurrentSize/2; i >= 1; i--)
{
T y = heap[i]; // 子树的根
// 寻找放置y的位置
int c = 2*i; // c 的父节点是y的目标位置
while (c <= CurrentSize)
{
// 使heap[c]是较小的子节点
if (c < CurrentSize &&
heap[c] > heap[c+1]) c++;
// 能把y放入heap[c/2]吗?
if (y <= heap[c]) break; // 能
// 不能
heap[c/2] = heap[c]; // 子节点上移
c *= 2; // 下移一层
}
heap[c/2] = y;
}
}
template<class T>
void MinHeap<T>::Output() const
{
cout << "The " << CurrentSize
<< " elements are"<< endl;
for (int i = 1; i <= CurrentSize; i++)
cout << heap[i] << " ";
cout << endl;
}
#pragma once
#include"MinHeap.h"
class BinaryTreeNode
{friend class BinaryTree;
public:
BinaryTreeNode( char &e,BinaryTreeNode *l,BinaryTreeNode *r);
~BinaryTreeNode(){}
private:
char data;
BinaryTreeNode*Lchild;
BinaryTreeNode*Rchild;
};
BinaryTreeNode::BinaryTreeNode( char& e,BinaryTreeNode*l,BinaryTreeNode*r)
{
data=e;
Lchild=l;
Rchild=r;
}
class BinaryTree
{
public:
BinaryTree(void){root=0;}
~BinaryTree(void){}
void MakeTree( char&element,BinaryTree &l,BinaryTree &r);
friend BinaryTree HuffmanTree(char [],int ch_Length,int []);//参数一次表示字符数组及其长度,频度数组
private:
BinaryTreeNode *root;
};
void BinaryTree::MakeTree( char&element,BinaryTree&l,BinaryTree&r)
{
root=new BinaryTreeNode(element,l.root,r.root);
l.root=r.root=0;
}
#pragma once
#include"BinaryTree.h"
#include<iostream>
using namespace std;
class Huffman
{
public:
Huffman(void){weight=0;}
~Huffman(void){}
friend BinaryTree HuffmanTree(char [],int ch_Length,int []);
friend bool operator<(Huffman&x1,Huffman&x2);
friend bool operator>(Huffman&x1,Huffman&x2);
friend bool operator<=(Huffman&x1,Huffman&x2);
friend ostream& operator<<(ostream&out,Huffman&x1);
private:
int weight;
BinaryTree tree;
};
ostream& operator<<(ostream&out,Huffman&x1)
{
out<<x1.weight<<endl;
return out;
}
bool operator<(Huffman&x1,Huffman&x2)
{
return x1.weight<x2.weight;
}
bool operator>(Huffman&x1,Huffman&x2)
{
return x1.weight>x2.weight;
}
bool operator<=(Huffman&x1,Huffman&x2)
{
return x1.weight<=x2.weight;
}
BinaryTree HuffmanTree(char c[],int ch_Length,int f[])
{
char a=' ';
Huffman x,y;
Huffman *w=new Huffman[ch_Length];
memset((void*)w,0,ch_Length*sizeof(Huffman));
BinaryTree z,zero;
for(int i=1;i<=ch_Length;i++)
{
z.MakeTree(a,zero,zero);
w[i].weight=f[i-1];
w[i].tree=z;
}
MinHeap<Huffman>H(1);
H.Initialize(w,ch_Length,ch_Length);
H.Output();
for(int j=1;j<ch_Length;j++)
{
H.DeleteMin(x);
H.DeleteMin(y);
z.MakeTree(a,x.tree,y.tree);
x.weight+=y.weight;
x.tree=z;
H.Insert(x);
}
H.DeleteMin(x);
H.Deactivate();
delete []w;
return x.tree;
}
// 压缩数据.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include"Huffman.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int *b=new int[4];
for(int i=0;i<4;i++)
{
b[i]=10-i;
}
char a[5]={'a','b','c','d'};
BinaryTree temp;
HuffmanTree(a,4,b);
return 0;
}
//求高手帮我把代码改好啊
#include<stdlib.h>
template<class T>
class MinHeap
{
public:
MinHeap(int MinHeapSize = 10);
~MinHeap() {delete [] heap;}
int Size() const {return CurrentSize;}
T Min()
{
if (CurrentSize == 0)
throw OutOfBounds();
return heap[1];
}
MinHeap<T>& Insert( T& x);
MinHeap<T>& DeleteMin(T& x);
void Initialize(T a[], int size, int ArraySize);
void Deactivate() {heap = 0;}
void Output() const;
MinHeap<T>& GetHeap();
private:
int CurrentSize, MaxSize;
T *heap;
};
template<class T>
MinHeap<T>&MinHeap<T>::GetHeap()
{
return heap;
}
template<class T>
MinHeap<T>::MinHeap(int MinHeapSize)
{
MaxSize = MinHeapSize;
heap = new T[MaxSize+1];
CurrentSize = 0;
}
template<class T>
MinHeap<T>& MinHeap<T>::Insert( T& x)
{
if (CurrentSize == MaxSize)
{
cout<<"溢出!"<<endl;
system("pause");
}
//为x寻找应插入的位置
//i从新的叶节点开始,并沿着树上升
int i = ++CurrentSize;
while (i != 1 && x < heap[i/2])
{
heap[i] = heap[i/2]; // 将元素下移
i /= 2; // 移向父节点
}
heap[i] = x;
return *this;
}
template<class T>
MinHeap<T>& MinHeap<T>::DeleteMin(T& x)
{
if (CurrentSize == 0)
{
cout<<"删除异常!"<<endl;
system("pause");
}
x = heap[1];
T y = heap[CurrentSize--]; //最后一个元素
// 从根开始, 为y寻找合适的位置
int i = 1, // 堆的当前节点
ci = 2; // i的子节点
while (ci <= CurrentSize)
{
// 使heap[ci] 是i较小的子节点
if (ci < CurrentSize
&& heap[ci] > heap[ci+1])
ci++;
// 能把y放入heap[i]吗?
if (y <= heap[ci])
break; // 能
// 不能
heap[i] = heap[ci]; // 子节点上移
i = ci; // 下移一层
ci *= 2;
}
heap[i] = y;
return *this;
}
template<class T>
void MinHeap<T>::Initialize(T a[], int size, int ArraySize)
{ //size为数组a的元素的个数,ArraySize为数组从a[1]算起的元素个数
delete [] heap;
heap=new T[size+1];
for(int i=1;i<=size;i++)
heap[i]=a[i];
CurrentSize = size;
MaxSize = ArraySize;
// 产生一个最小堆
for (int i = CurrentSize/2; i >= 1; i--)
{
T y = heap[i]; // 子树的根
// 寻找放置y的位置
int c = 2*i; // c 的父节点是y的目标位置
while (c <= CurrentSize)
{
// 使heap[c]是较小的子节点
if (c < CurrentSize &&
heap[c] > heap[c+1]) c++;
// 能把y放入heap[c/2]吗?
if (y <= heap[c]) break; // 能
// 不能
heap[c/2] = heap[c]; // 子节点上移
c *= 2; // 下移一层
}
heap[c/2] = y;
}
}
template<class T>
void MinHeap<T>::Output() const
{
cout << "The " << CurrentSize
<< " elements are"<< endl;
for (int i = 1; i <= CurrentSize; i++)
cout << heap[i] << " ";
cout << endl;
}
#pragma once
#include"MinHeap.h"
class BinaryTreeNode
{friend class BinaryTree;
public:
BinaryTreeNode( char &e,BinaryTreeNode *l,BinaryTreeNode *r);
~BinaryTreeNode(){}
private:
char data;
BinaryTreeNode*Lchild;
BinaryTreeNode*Rchild;
};
BinaryTreeNode::BinaryTreeNode( char& e,BinaryTreeNode*l,BinaryTreeNode*r)
{
data=e;
Lchild=l;
Rchild=r;
}
class BinaryTree
{
public:
BinaryTree(void){root=0;}
~BinaryTree(void){}
void MakeTree( char&element,BinaryTree &l,BinaryTree &r);
friend BinaryTree HuffmanTree(char [],int ch_Length,int []);//参数一次表示字符数组及其长度,频度数组
private:
BinaryTreeNode *root;
};
void BinaryTree::MakeTree( char&element,BinaryTree&l,BinaryTree&r)
{
root=new BinaryTreeNode(element,l.root,r.root);
l.root=r.root=0;
}
#pragma once
#include"BinaryTree.h"
#include<iostream>
using namespace std;
class Huffman
{
public:
Huffman(void){weight=0;}
~Huffman(void){}
friend BinaryTree HuffmanTree(char [],int ch_Length,int []);
friend bool operator<(Huffman&x1,Huffman&x2);
friend bool operator>(Huffman&x1,Huffman&x2);
friend bool operator<=(Huffman&x1,Huffman&x2);
friend ostream& operator<<(ostream&out,Huffman&x1);
private:
int weight;
BinaryTree tree;
};
ostream& operator<<(ostream&out,Huffman&x1)
{
out<<x1.weight<<endl;
return out;
}
bool operator<(Huffman&x1,Huffman&x2)
{
return x1.weight<x2.weight;
}
bool operator>(Huffman&x1,Huffman&x2)
{
return x1.weight>x2.weight;
}
bool operator<=(Huffman&x1,Huffman&x2)
{
return x1.weight<=x2.weight;
}
BinaryTree HuffmanTree(char c[],int ch_Length,int f[])
{
char a=' ';
Huffman x,y;
Huffman *w=new Huffman[ch_Length];
memset((void*)w,0,ch_Length*sizeof(Huffman));
BinaryTree z,zero;
for(int i=1;i<=ch_Length;i++)
{
z.MakeTree(a,zero,zero);
w[i].weight=f[i-1];
w[i].tree=z;
}
MinHeap<Huffman>H(1);
H.Initialize(w,ch_Length,ch_Length);
H.Output();
for(int j=1;j<ch_Length;j++)
{
H.DeleteMin(x);
H.DeleteMin(y);
z.MakeTree(a,x.tree,y.tree);
x.weight+=y.weight;
x.tree=z;
H.Insert(x);
}
H.DeleteMin(x);
H.Deactivate();
delete []w;
return x.tree;
}
// 压缩数据.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include"Huffman.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int *b=new int[4];
for(int i=0;i<4;i++)
{
b[i]=10-i;
}
char a[5]={'a','b','c','d'};
BinaryTree temp;
HuffmanTree(a,4,b);
return 0;
}
//求高手帮我把代码改好啊
作者: VCLanguage 发布时间: 2011-12-28
异常时break程序,看调用堆栈,找自己写的函数代码,结合上下文分析
作者: Demon__Hunter 发布时间: 2011-12-28
memset((void*)w,0,ch_Length*sizeof(Huffman));
可能是这的问题吧,类的对象不要用sizeof
对类成员变量的初始化放到构造函数里
可能是这的问题吧,类的对象不要用sizeof
对类成员变量的初始化放到构造函数里
作者: cuidx 发布时间: 2011-12-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28