给出Haskell中的以下树结构:
data Tree = Leaf Int | Node Int Tree Tree deriving Show
Run Code Online (Sandbox Code Playgroud)
如何让Haskell返回预订中的数据列表?
例如给一棵树:
Node 1 (Leaf 2) (Leaf 3)
Run Code Online (Sandbox Code Playgroud)
返回类似的东西:
preorder = [1,2,3]
Run Code Online (Sandbox Code Playgroud) 我正在尝试对霍夫曼树进行编码.我的树是对的.我只需要弄清楚如何修复我的递归函数来正确创建表.感谢您提供的任何帮助.
struct Code
{
char letter;
string code;
};
void createCode(BTree<Data>* root,string codeStr,vector<Code> &table)
{
if (root->getRightChild() == NULL && root->getLeftChild() == NULL)
{
Code code;
code.letter = root->getData().getLetter();
code.code = codeStr;
table.push_back(code);
}
else
{
createCode(root->getLeftChild(), codeStr.append("1"),table);
createCode(root->getRightChild(), codeStr.append("0"),table);
}
}
Run Code Online (Sandbox Code Playgroud) 我知道预订遍历的定义,并想了解为什么预订遍历策略有利于克隆树?我的意思是为什么它比其他遍历机制更喜欢顺序遍历和后序遍历?
如何在c#中使用队列进行深度优先搜索?
以下是我的数据结构:
public class Node
{
public string Name{get;set}
public IEnumerable<Node> Children{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个Node对象的集合,每个对象都带有子节点,它们还有子节点等等.
我想访问每个节点并将其转换为不同的形式.
像下面这样的东西:
public IEnumerable<IContent> BuildContentFrom(IEnumerable<Node> nodes)
{
var queue = new Queue<Node>(nodes);
while (queue.Any())
{
var next = queue.Dequeue();
yield return BuildContentFromSingle(next);
foreach (var child in next.Children)
{
queue.Enqueue(child);
}
}
}
public IContent BuildContentFromSingle(Node node)
{
var content = _contentFactory.Create(node);
return content;
}
Run Code Online (Sandbox Code Playgroud)
由于某些原因,上面并没有给我深度.你能帮忙吗?
我需要在二叉树中找到所有可能的子树:
allSubtrees :: BinaryT a -> [BinaryT a]
allSubtrees = undefined
Run Code Online (Sandbox Code Playgroud)
树是:
data BinaryT a =
Empty
| Node (BinaryT a) a (BinaryT a)
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
我是Haskell的新手,我知道Haskell中没有while/ forloop.Haskell就是递归.我的问题是,如何在没有无限递归的情况下获得树的所有可能的子树?
我一直致力于非常彻底地理解 DOM。目前我正在遍历 DOM 树,似乎发现了一些不一致的地方。
请参阅此小提琴的示例:http ://jsfiddle.net/AmhVk/4/
那么问题来了,为什么nodeList有像element[0]、element 1这样的可索引列表,而HTMLElement却没有?
有人可以非常彻底地向我解释这一点吗?谢谢...
<ul id="jow">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li class="active"><a href="">Item</a></li>
<li class="active"><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
<div id="ieps"></div>
Run Code Online (Sandbox Code Playgroud)
<ul id="jow">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li class="active"><a href="">Item</a></li>
<li class="active"><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
<div id="ieps"></div>
Run Code Online (Sandbox Code Playgroud)
另外,在小提琴中,如果我删除 1 个包含“active”类的 li。这仍然会返回一个 nodeList 而不是单个 HTMLElement:jsfiddle.net/AmhVk/5
使用什么类型的树遍历ast(特别是ast.NodeVisitor())?当我创建一个堆栈并将遍历的每个节点推入堆栈时,结果似乎是“广度优先”树遍历。这意味着顺序取决于树中的级别。
前任。树看起来像
Module
Assign
Name
Store
Call
Attribute
Str
Load
Run Code Online (Sandbox Code Playgroud)
堆栈看起来像
[Module,Assign,Name,Call,Store,Attribute,Str,Load]
Run Code Online (Sandbox Code Playgroud)
前任。代码
stack = []
class a(ast.NodeTransformer):
def visit_Num(self,node):
stack.append(node)
...
return node
... #this is all the other visit_*() functions
def visit_Str(self,node):
stack.append(node)
...
return node
if __name__ == "__main__":
with open('some_file.py','r') as pt:
tree = ast.parse(pt)
new_tree = a()
new_tree_edit = ast.fix_missing_locations(new_tree.visit(tree)) # I have tried with and without calling fix_missing_locations and got the same results.
print stack
Run Code Online (Sandbox Code Playgroud) 假设我们有这样一块板:

我们希望通过以下运动模式从左到右找到最有利可图的路径:

例如,在这个董事会中,最有利可图的途径是:

即{2,0} - > {2,1} - > {3,2} - > {3,3}
我写了以下代码:
import java.util.*;
public final class Board {
private final int[][] board;
private final int n;
public Board(int n) {
board = new int[n][n];
this.n = n;
generateBoard();
}
public static class Node {
public int x;
public int y;
public int value;
public Node(int x, int y, int value) {
this.x = x;
this.y = y;
this.value = value;
}
@Override
public String toString() {
return …Run Code Online (Sandbox Code Playgroud) 我有一棵树的遍历BFS和DFS遍历.如何从这些遍历中重建树?
例如:
BFS Traversal : 4 3 5 1 2 8 7 6
DFS Traversal : 4 3 1 7 2 6 5 8
Run Code Online (Sandbox Code Playgroud)
然后树会像吼叫:
4
/ \
3 5
/ \ \
2 1 8
| |
6 7
Run Code Online (Sandbox Code Playgroud) algorithm graph breadth-first-search tree-traversal depth-first-search
我试图理解使用 2 个堆栈实现后序遍历是多么直观。有人是如何想出它的,它只是一种观察或某种特定的思维方式,可以帮助人们想出这样的方法。如果是,那么请解释如何朝着正确的方向思考。
tree-traversal ×10
tree ×4
algorithm ×3
binary-tree ×3
haskell ×2
traversal ×2
.net-3.5 ×1
c# ×1
c++ ×1
dom ×1
graph ×1
graph-theory ×1
java ×1
javascript ×1
nodelist ×1
python ×1
stack ×1