zh

# 按树图遍历

• #forEachByBreadthFirst(call, scope) - 广度优先遍历
• #forEachByBreadthFirstReverse(call, scope) - 广度优先反向遍历
• #forEachByDepthFirst(call, scope, postOrder) - 深度优先遍历，分先序和后序遍历两种
• #forEachByDepthFirstReverse(call, scope, postOrder) - 深度优先反向遍历，分先序和后序遍历两种

### 示例

``````var model = new Q.GraphModel();
var a = new Q.Node('A');
model.add(a);
var a1 = new Q.Node('A1');
model.add(a1);
var a2 = new Q.Node('A2');
model.add(a2);
var a3 = new Q.Node('A3');
model.add(a3);
var a21 = new Q.Node('A21');
model.add(a21);
var a22 = new Q.Node('A22');
model.add(a22);
a1.parent = a;
a2.parent = a;
a21.parent = a2;
a22.parent = a2;

Q.log('forEachByBreadthFirst');
model.forEachByBreadthFirst(function(node){
Q.log(node.name);
}, null, true);

Q.log('forEachByDepthFirst by post-order');
model.forEachByDepthFirst(function(node){
Q.log(node.name);
}, null, true);

Q.log('forEachByDepthFirst by pre-order');
model.forEachByDepthFirst(function(node){
Q.log(node.name);
});
``````

forEachByBreadthFirst

``````A

A3

A1

A2

A21

A22
``````

forEachByDepthFirst by post-order

``````A1

A21

A22

A2

A

A3
``````

forEachByDepthFirst by pre-order

``````A

A1

A2

A21

A22

A3
``````

ScreenShot 2014-06-05 at 下午4.32.33.png