有序树遍历显然有应用; 按顺序获取内容.
预序遍历似乎对创建树的副本非常有用.
二叉树的后序遍历是否常见?
假设我有一个简单的二叉树节点类,如下所示:
public class BinaryTreeNode {
public String identifier = "";
public BinaryTreeNode parent = null;
public BinaryTreeNode left = null;
public BinaryTreeNode right = null;
public BinaryTreeNode(BinaryTreeNode parent, String identifier)
{
this.parent = parent; //passing null makes this the root node
this.identifier = identifier;
}
public boolean IsRoot() {
return parent == null;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何添加一个能够以递归方式遍历任何大小树的方法,从左到右访问每个现有节点,而无需重新访问已遍历的节点?
这会有用吗?:
public void traverseFrom(BinaryTreeNode rootNode)
{
/* insert code dealing with this node here */
if(rootNode.left != null)
rootNode.left.traverseFrom(rootNode.left);
if(rootNode.right != null) …Run Code Online (Sandbox Code Playgroud) 我需要介于.closest()和之间的功能.parents().我将一些CSS应用于某个元素的所有父母,直到某个父母.现在我正在while循环,但似乎有更好的方法来做到这一点.
var goUp = $(".distant-child");
while(!goUp.hasClass("ancestor-to-stop-at")){
goUp.css("height","100%");
goUp = goUp.parent();
}
Run Code Online (Sandbox Code Playgroud)
我宁愿做其中一件事:
$(".distant-child").closest(".ancestor-to-stop-at").css("height","100%"); //won't work because .closest() only returns the top ancestor
$(".distant-child").parents(".ancestor-to-stop-at").css("height","100%"); //won't work because .parents() doesn't take this parameter and won't stop at the specified element.
Run Code Online (Sandbox Code Playgroud)
如何在没有while循环的情况下实现这一目标?
似乎直觉上很明显,以下法律应该成立:
traverse f . fmap g = traverse (f . g)
Run Code Online (Sandbox Code Playgroud)
似乎直接适用的唯一Traversable法律是
fmap g = runIdentity . traverse (Identity . g)
Run Code Online (Sandbox Code Playgroud)
这改变了问题
traverse f . runIdentity . traverse (Identity . g)
Run Code Online (Sandbox Code Playgroud)
似乎具有适当形式的唯一法律适用于此是自然法则.然而,这是关于应用变换,我没有看到任何这些.
除非我遗漏了什么,否则唯一剩下的就是参数化证明,我还没有得到关于如何编写这些内容的线索.
简而言之:如何在列表中找到最小值?(感谢kaarel的建议)
很长的故事:
我在amzi prolog中创建了一个加权图并给出了2个节点,我能够检索路径列表.但是,我需要在此路径中找到最小值,但无法遍历列表来执行此操作.我可以请您就如何确定清单中的最小值寻求建议吗?
我的代码目前看起来像这样:
arc(1,2). arc(2,3). arc(3,4). arc(3,5). arc(3,6). arc(2,5). arc(5,6). arc(2,6). path(X,Z,A) :- (arc(X,Y),path(Y,Z,A1),A is A1+1;arc(X,Z), A is 1).
因此,"键入findall(Z,路径(2,6,Z),L)." 在听众中允许我获得一个列表[3,2,2,1].我需要从这里检索最小值并将其乘以一个数量.有人可以建议如何检索最小值?谢谢!
我在PostgreSQL(不是树)中实现了节点图
表格的结构采用这种格式
id | node1 | node2
--------------------
1 | 1 | 2
2 | 1 | 3
3 | 4 | 1
4 | 5 | 1
5 | 1 | 6
Run Code Online (Sandbox Code Playgroud)
这显示了节点1与其连接的节点之间的关系.
...是我需要一个函数或方法来在sql中查找特定的节点路径.
我想调用SELECT getGraphPath(startnode,targetnode)之类的函数,这将以任何形式显示路径(行或字符串)
例如SELECT getGraphPath(1,18)给出:
[1]-->[3]-->[17]-->[18]
[1]-->[4]-->[10]-->[18]
Run Code Online (Sandbox Code Playgroud)
甚至是行:
Result |
--------
1
3
17
18
Run Code Online (Sandbox Code Playgroud)
我还想知道如何使用广度优先搜索和深度优先搜索来遍历图形.
过去几周我一直在学习迭代器.我仍然不明白迭代链接列表和遍历链接列表之间的主要区别.我知道遍历意味着要经历(访问每个元素)链接列表,并且在迭代时你基本上做同样的事情,但是有什么不同,为什么你不能遍历所有东西(标准库数据结构)?
我正在编写一个简单的程序,如果一个数组被排序为false,则返回true,并且我在eclipse中不断得到一个异常,我只是想不通原因.我想知道是否有人可以看看我的代码并解释为什么我得到一个超出界限的数组异常.感谢您的高级帮助.
public static boolean isSorted(int[] a)
{
int i;
for(i = 0; i < a.length; i ++);{
if (a[i] < a[i+1]) {
return true;
} else {
return false;
}
}
}
public static void main(String[] args)
{
int ar[] = {3,5,6,7};
System.out.println(isSorted(ar));
}
Run Code Online (Sandbox Code Playgroud) 我可以使用recursivevisitor类遍历clang AST的特定子树,但我想要做的是按节点遍历clang AST节点.
如果有人能帮助我,我真的很感激.
提前致谢.
我正试图在我的光线跟踪器中遍历3D KD树.树是正确的,但我的遍历算法似乎有问题,因为与使用强力方法相比,我遇到了一些错误(一些小的表面区域似乎被忽略).
注意:所讨论的光线都不与任何轴平行.
这是我的遍历算法:
IntersectionData* intersectKDTree(const Ray &ray, KDTreeNode* node, double tMin, double tMax) const{
if (node->GetObjectCount()==0) return 0;
IntersectionData* current = 0;
bool intersected = false;
if (node->m_isLeaf){
...test all primitives in the leaf...
}
else{
int axis = node->m_splitAxis;
double splitPos = node->m_splitPos;
double tSplit = (splitPos-ray.point[axis])/ray.direction[axis];
KDTreeNode* nearNode = ray.point[axis]<splitPos?node->m_leftnode:node->m_rightnode;
KDTreeNode* farNode = ray.point[axis]<splitPos?node->m_rightnode:node->m_leftnode;
if (tSplit > tMax)
return intersectKDTree(ray, nearNode , tMin, tMax);//case A
else if (tSplit < tMin){
if(tSplit>0)
return intersectKDTree(ray, farNode, tMin, tMax);//case B …Run Code Online (Sandbox Code Playgroud) traversal ×10
binary-tree ×2
c++ ×2
java ×2
arrays ×1
clang ×1
graph ×1
haskell ×1
iteration ×1
jquery ×1
list ×1
minimum ×1
path ×1
postgresql ×1
prolog ×1
raytracing ×1
sorting ×1
sql ×1
terminology ×1
tree ×1