当画一个节点的时候样传入的是坐标X,Y和父节点的就可以了
MiddleHead(TreeNode node, int x1, int y1, int x2, int y2, double alpha)
node:表示当前的节点,
x1, y1 当前节点的父节点的坐标(父节点不存在的时候可以设为负数,然后在后面判断)
x2, y2 当前节点的坐标
alpha表示父节点和当前节点连线的倾角(不能使用相同的角度,否则会出现2个节点重合的现象)
下面是我在用的画图的方法,可以参考一下。
public void MiddleHead(AVLTreeNode node, int x1, int y1, int x2, int y2, double alpha)//中根遍历并输出图形
{
if (node == null)
return;
int x3 = x2 - (int)(myDraw.LineLength * Math.Sin(alpha));
int y3 = y2 + (int)(myDraw.LineLength * Math.Cos(alpha));
int x4 = x2 + (int)(myDraw.LineLength * Math.Sin(alpha));
int y4 = y2 + (int)(myDraw.LineLength * Math.Cos(alpha));
double nextAngle = alpha * 3 / 5.0;
MiddleHead(node.LeftChild, x2, y2, x3, y3, nextAngle);
// Console.Write(node.Data);
if (y1 > 0)
{
int temx1 = 0;
int temy1 = 0;
int temx2 = 0;
int temy2 = 0;
double sita3 = Math.Asin((1.0) * (y2 - y1) / Math.Sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)));//角度
double sita4 = Math.Acos((1.0) * (x2 - x1) / Math.Sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)));//角度
temx1 = x1 + (int)(myDraw.NodeRedius * Math.Cos(sita4));
temy1 = y1 + (int)(myDraw.NodeRedius * Math.Sin(sita3));
temx2 = x2 - (int)(myDraw.NodeRedius * Math.Cos(sita4));
temy2 = y2 - (int)(myDraw.NodeRedius * Math.Sin(sita3));
DrawLinkLine(temx1, temy1, temx2, temy2, myDraw.FrontColor, g);//画连接线
}
DrawNode(x2, y2, node.Data, myDraw.FrontColor);//画节点
MiddleHead(node.RightChild, x2, y2, x4, y4, nextAngle);
}