堆二叉树打印方法
在我的作业中,我知道我需要使用堆删除函数,该函数返回要打印的变量。但作业中的要求相当模糊,我很好奇是否有人能给我更好的解释。我对如何使用第二个或第三个参数感到非常困惑,我知道需要进行一些排序,并且我需要两个 for 循环,但之后我迷失了。作业内容如下:
template <class T, class P> void print_list (vector<T>&,
const int, const int, P);
该函数从堆结构中检索项目并将其打印在标准输出上。为了从堆结构中检索单个项目,它调用remove函数。该函数的第一个参数是堆结构的向量,第二个参数是 stdout 上打印项目的分配大小,第三个参数是单行上打印项目的最大数量,最后一个参数是谓词。
这是我的代码:
#include "340.h"
#ifndef H_PROG7
#define H_PROG7
// data files
#define D1 "prog7.d1"
#define D2 "prog7.d2"
#define D3 "prog7.d3"
#define INT_SZ 4 // width of integer
#define FLT_SZ 7 // width of floating-pt number
#define STR_SZ 12 // width of string
#define INT_LN 15 // no of integers on single line
#define FLT_LN 9 // no of floating-pt nums on single line
#define STR_LN 5 // no of strings on single line
// function prototypes
template<class T,class P> void insert(vector<T>&, const T&, P);
template<class T,class P> T remove(vector<T>&, P);
template<class T,class P> void upheap(vector<T>&, int, P);
template<class T,class P> void downheap(vector<T>&, int, P);
template<class T,class P>
void get_list(vector<T>&, const char*, P);
template<class T,class P>
void print_list(vector<T>&, const int, const int, P);
template<class T, class P>
void get_list(vector<T>& v, const char* file, P func) {
ifstream inFile("file");
T data;
while(inFile >> data) {
inFile >> data;
insert(v, data, func);
}
}
template<class T, class P>
void insert(vector<T>& v, const T& data, P func) {
v.push_back( data );
upheap( v, v.size()-1, func );
}
template<class T,class P>
void upheap(vector<T>& v, int start, P func) {
while( start <= v.size()/2 ) {
unsigned int parent = start / 2;
if( parent - 1 <= v.size() && v[parent - 1] > v[parent] )
parent = parent - 1;
if( v[start] <= v[parent] )
break;
swap( v[start], v[parent] );
start = parent;
}
}
template<class T,class P>
void downheap(vector<T>& v, int start, P func) {
while(start <= v.size()/2 ) {
unsigned int child = 2 * start;
if( child + 1 <= v.size() && v[child + 1] > v[child])
child = child + 1;
if( v[start] >= v[child] )
break;
swap( v[start], v[child] );
start = child;
}
}
template<class T,class P>
T remove(vector<T>& v, P func) {
swap( v[0], v.back() );
T& item = v.back();
v.pop_back();
downheap( v, 1, func );
return item;
}
template<class T,class P>
void print_list(vector<T>& v, const int size, const int line, P func) {
for(int i = 1; i < v.size(); i++) {
cout << remove(v, func) << " ";
}
}
#endif
In my assignment I know I am required to use my heap remove function which returns the variable to be printed. But the requirement in teh assignment is quite vague and I was curious if someone could give me a better explanation. I am quite confused as to how to use the second or third arguments I know some sorting is required and I need two for loops but after that I am lost. here is what the assignment says:
template <class T, class P> void print_list (vector<T>&,
const int, const int, P);
This function retrieves items from a heap structure and prints them out on stdout. To retrieve a single item from the heap structure, it calls the remove function. The first argument to this function is a vector for the heap structure, the second argument is the allocated size of a printed item on stdout, the third argument is the maximum number of printed items on a single line, and the last argument is a predicate.
Here is my code:
#include "340.h"
#ifndef H_PROG7
#define H_PROG7
// data files
#define D1 "prog7.d1"
#define D2 "prog7.d2"
#define D3 "prog7.d3"
#define INT_SZ 4 // width of integer
#define FLT_SZ 7 // width of floating-pt number
#define STR_SZ 12 // width of string
#define INT_LN 15 // no of integers on single line
#define FLT_LN 9 // no of floating-pt nums on single line
#define STR_LN 5 // no of strings on single line
// function prototypes
template<class T,class P> void insert(vector<T>&, const T&, P);
template<class T,class P> T remove(vector<T>&, P);
template<class T,class P> void upheap(vector<T>&, int, P);
template<class T,class P> void downheap(vector<T>&, int, P);
template<class T,class P>
void get_list(vector<T>&, const char*, P);
template<class T,class P>
void print_list(vector<T>&, const int, const int, P);
template<class T, class P>
void get_list(vector<T>& v, const char* file, P func) {
ifstream inFile("file");
T data;
while(inFile >> data) {
inFile >> data;
insert(v, data, func);
}
}
template<class T, class P>
void insert(vector<T>& v, const T& data, P func) {
v.push_back( data );
upheap( v, v.size()-1, func );
}
template<class T,class P>
void upheap(vector<T>& v, int start, P func) {
while( start <= v.size()/2 ) {
unsigned int parent = start / 2;
if( parent - 1 <= v.size() && v[parent - 1] > v[parent] )
parent = parent - 1;
if( v[start] <= v[parent] )
break;
swap( v[start], v[parent] );
start = parent;
}
}
template<class T,class P>
void downheap(vector<T>& v, int start, P func) {
while(start <= v.size()/2 ) {
unsigned int child = 2 * start;
if( child + 1 <= v.size() && v[child + 1] > v[child])
child = child + 1;
if( v[start] >= v[child] )
break;
swap( v[start], v[child] );
start = child;
}
}
template<class T,class P>
T remove(vector<T>& v, P func) {
swap( v[0], v.back() );
T& item = v.back();
v.pop_back();
downheap( v, 1, func );
return item;
}
template<class T,class P>
void print_list(vector<T>& v, const int size, const int line, P func) {
for(int i = 1; i < v.size(); i++) {
cout << remove(v, func) << " ";
}
}
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,第二个参数似乎是列表中的一个元素在打印出来时允许占用的空格数。第三个参数是允许在一行上打印的元素数量。在 print_list 函数中,您需要跟踪已打印的元素数量,以确定是否需要根据此参数转到下一行。使用这两者,您应该能够很好地调整输出。您将需要研究如何进行输出格式化。
例如。列表 = [1, 9, 14, 2000, 244, 777, 3, 98102, 88, 53, 14],大小 = 6,行 = 3
输出:
你的最后一个参数是一个谓词,你似乎没有在任何函数中实际使用它(当你根本不使用参数时,这表明你做错了什么)。谓词函数参数应该用来做一些对于传入的类型 T 来说是唯一的事情。例如,在 print_list 中,它应该用来打印 T 类型的变量。如果您按照当前的方式打印一个元素,不保证该类型能够正确打印。当然,它适用于 int 和 double 等基本类型,但想象一下具有多个不同成员的类型。考虑一下您将如何在其他函数中使用此谓词 - 您可以仅将对象与 '<' 进行比较吗或“>”?
From what I can gather it looks like the 2nd parameter is the number of spaces one element in the list is allowed to take up when it's printed out. The 3rd parameter is the number of elements that are allowed to be printed on one line. In your print_list function, you will need to keep track of how many elements you've printed to determine if you need to go to the next line or not based on this parameter. Using both of these, you should be able to align your output nicely. You will need to look into how to do output formatting.
Eg. list = [1, 9, 14, 2000, 244, 777, 3, 98102, 88, 53, 14], size = 6, line = 3
Output:
Your last parameter is a predicate, which you don't seem to actually be using in any of your functions (a sign you're doing something wrong when you're not using a parameter at all). The predicate function parameter should be used to do something that is unique to the type T being passed in. For example, in print_list, it should be used to print a variable of type T. If you print an element the way you are currently doing, there is no guarantee that the type will get printed properly. Of course, it would work for basic types like int and double, but imagine a type with several different members. Think about how you would need to use this predicate in your other functions too - can you just compare objects with '<' or '>'?