类似这样就可以了:
a
/ \
b c
/ \
d e
最好每个节点画在一个圆圈里面
a
/ \
b c
/ \
d e
最好每个节点画在一个圆圈里面
解决方案 »
- spring2.0整合hibernate3.1事务案例出错
- FMJ
- java操作共享文件夹
- 一个面试题:String s = new String("xyz");创建了几个String Object?
- 转一个JAVAEYE的文章:项目事故和安全语言,大家都来讨论讨论啊
- 如何使用相对路径定位资源文件
- 关于try{....return;}finally{....}的疑问
- 请求出一个字符的ASC值?急!
- java中的管道和线程问题,大虾来帮忙啊:)
- 请问如何知道在服务器,获取连接该服务器的机器名称?
- 用Imanage.put(Menu.font,font);方法设置默认字体为何不好用;
- 高手救命啊!都是概念
例如输入: 1*2+3
输出:
+
/ \
* 3
/ \
1 2
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);
}