//画二叉树
void paint(CDC *pDC,int parent,int x,int y,int c,int d)//c表示该节点的层次,d表示该节点与其父的左右关系
{
double pi=3.1415926;//用于计算分支度数,需改进,有交叉树
int ix,iy,ix0,iy0;
float hd1,hd2;
if(c%2==0&&d!=4){hd1=pi/6;hd2=pi/3;}
else{hd1=pi/3;hd2=pi/6;}
ix0=50*cos(hd1);
iy0=50*sin(hd1);
if(htnode[parent].leftChild!=-1)
{
paint(pDC,htnode[parent].leftChild,x-ix0,y+iy0,c+1,-1);
}
pDC->Ellipse(x-10,y-10,x+8,y+8);
char string[5];
itoa(htnode[parent].weight,string,10);
pDC->TextOut(x-4,y-8,str);
if(d!=0)
{
ix=x-8*cos(hd2)*d;
iy=y-8*sin(hd2);
pDC->MoveTo(ix,iy);
ix=x-36*cos(hd2)*d;
iy=y-36*sin(hd2);
pDC->LineTo(ix,iy);
}
if(htnode[parent].rightChild!=-1)
paint(pDC,htnode[parent].rightChild,x+ix0,y+iy0,c+1,1);
}
void paint(CDC *pDC)
{
paint(pDC,2*num-2,175,10,0,0);
}
void paint(CDC *pDC,int parent,int x,int y,int c,int d)//c表示该节点的层次,d表示该节点与其父的左右关系
{
double pi=3.1415926;//用于计算分支度数,需改进,有交叉树
int ix,iy,ix0,iy0;
float hd1,hd2;
if(c%2==0&&d!=4){hd1=pi/6;hd2=pi/3;}
else{hd1=pi/3;hd2=pi/6;}
ix0=50*cos(hd1);
iy0=50*sin(hd1);
if(htnode[parent].leftChild!=-1)
{
paint(pDC,htnode[parent].leftChild,x-ix0,y+iy0,c+1,-1);
}
pDC->Ellipse(x-10,y-10,x+8,y+8); //画圆圈
char string[5];
itoa(htnode[parent].weight,string,10); //将数字转换为字符
pDC->TextOut(x-4,y-8,str); // 写字
if(d!=0)
{
ix=x-8*cos(hd2)*d; //计算节点的位置
iy=y-8*sin(hd2);
pDC->MoveTo(ix,iy); //移动当前画笔位置
ix=x-36*cos(hd2)*d; // 计算下一个节点坐标
iy=y-36*sin(hd2);
pDC->LineTo(ix,iy); // 画线连接
}
if(htnode[parent].rightChild!=-1)
paint(pDC,htnode[parent].rightChild,x+ix0,y+iy0,c+1,1); //递归调用下一层的绘图
}
void paint(CDC *pDC)
{
paint(pDC,2*num-2,175,10,0,0);
}