H5C3J 学习笔记 part5 Tree2Active
针对这道题目,二叉树中,如果某子节点A(根结点永远可见)到根结点之间,所有值均小于A的值,那么该节点为可见节点。要求根据给定的值,计算其可见节点数量。
为了能得到一个直观的对这道题目的理解,我在之前的Demo基础上,做了一些改进,现在,只要Hold住任何一个子节点,相信你就能明白所谓“可见节点”是什么意思了。
[![](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-28%252520%2525E4%2525B8%25258B%2525E5%25258D%2525889.05.26.png)](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-28%252520%2525E4%2525B8%25258B%2525E5%25258D%2525889.05.26.png)
[Demo](http://hector.ziki.me/tree2active/Tree2Active.html)
这道题的计算办法如下,(不才愚钝,写得有些麻烦,希望得到高人指点):
function countVisibleNodeNumber(tree,max,result)
{
if(tree == null) return 0;
if(tree.x > max)
{
max = tree.x;
result++;
}
countVisibleNodeNumber(tree.l,max);
countVisibleNodeNumber(tree.r,max);
return result;
}
`</pre>
等比排放途经节点坐标的计算办法纪录如下:
<pre>` function findPositionOnLine(p1,p2,d12,d13)
{
var px = p1.x + (p2.x - p1.x)*d13/d12;
var py = p1.y + (p2.y - p1.y)*d13/d12;
return new Point(px,py);
}
其中,p1为根节点坐标,p2为末尾节点坐标,d12为根节点到末尾节点的距离,d13为根节点到目标点的距离。
另外,有个令人难过的方面,就是ivank是基于WebGL的,在Safari里运行例子还好,会给你一个这样的提示:
[![](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-25%252520%2525E4%2525B8%25258B%2525E5%25258D%2525889.03.02.png)](https://archive.writeitdown.site/%2525E5%2525B1%25258F%2525E5%2525B9%252595%2525E5%2525BF%2525AB%2525E7%252585%2525A7%2525202013-09-25%252520%2525E4%2525B8%25258B%2525E5%25258D%2525889.03.02.png)
Chrome下和FireFox都可以正常运行。局限性又来了。
其实之前想要实现类似物理链条效果的,结果解了大半夜二元二次方程,唉……-_-#
最后这句话会鞭策我很长一段时间的。
After careful consideration we unfortunately cannot offer you a position as a XXXXXX Engineer at this time.