原理:对于数组描述的完全二叉树,序号为i的节点的左右儿子分别为2*i和2*i+1,如果它们不超过数组的元素个数heapSize
利用Graphviz 可视化时,在.dot文件中,输入x->y即可画出x到y的一条有向边
tree.dot文件的示例如下:
digraph tree{
node [style=filled,color=red]; //设置节点属性
edge [color = "black", decorate = false]; //设置边属性
liu80->xu99;
liu80->pan94;
xu99->ma84;
xu99->zhu95;
pan94->yang90;
pan94->jiang81;
ma84->huang89;
ma84->zhou94;
}
C++代码如下:
void draw() // 利用 Graphviz 可视化输出堆
{
ofstream file;
string path = "H:/Graphviz/bin/tree.dot"; // "H:/Graphviz/bin/tree.dot"
file.open(path);
cout << endl << "tree.dot is successfully opened\n\n";
if (file.fail()) {
printf("fail to open tree.dot!\n");
return;
}
else
{
file &