@@ -23,6 +23,7 @@ The latest released version is [`2.0.0`][release].
23
23
* [ Parent] ( #parent )
24
24
* [ Literal] ( #literal )
25
25
* [ Glossary] ( #glossary )
26
+ * [ Tree traversal] ( #tree-traversal )
26
27
* [ Utilities] ( #utilities )
27
28
* [ List of Utilities] ( #list-of-utilities )
28
29
* [ References] ( #references )
@@ -304,6 +305,102 @@ Files are provided by the host environment and not defined by unist.
304
305
305
306
For example, see projects such as [ ** vfile** ] [ vfile ] .
306
307
308
+ ###### Preorder
309
+
310
+ In ** preorder** (** NLR** ) is [ depth-first] [ traversal-depth ] [ tree
311
+ traversal] [ traversal ] that performs the following steps for each node _ N_ :
312
+
313
+ 1 . ** N** : visit _ N_ itself
314
+ 2 . ** L** : traverse [ _ head_ ] [ term-head ] (then its _ next sibling_ , recursively
315
+ moving forward until reaching _ tail_ )
316
+ 3 . ** R** : traverse [ _ tail_ ] [ term-tail ]
317
+
318
+ ###### Postorder
319
+
320
+ In ** postorder** (** LRN** ) is [ depth-first] [ traversal-depth ] [ tree
321
+ traversal] [ traversal ] that performs the following steps for each node _ N_ :
322
+
323
+ 1 . ** L** : traverse [ _ head_ ] [ term-head ] (then its _ next sibling_ , recursively
324
+ moving forward until reaching _ tail_ )
325
+ 2 . ** R** : traverse [ _ tail_ ] [ term-tail ]
326
+ 3 . ** N** : visit _ N_ itself
327
+
328
+ ## Tree traversal
329
+
330
+ ** Tree traversal** is a common task when working with a [ _ tree_ ] [ term-tree ] to
331
+ search it.
332
+ Tree traversal is typically either _ breadth-first_ or _ depth-first_ .
333
+
334
+ In the following examples, we’ll work with this tree:
335
+
336
+ ``` ascii
337
+ +---+
338
+ | A |
339
+ +-+-+
340
+ |
341
+ +-----+-----+
342
+ | |
343
+ +-+-+ +-+-+
344
+ | B | | F |
345
+ +-+-+ +-+-+
346
+ | |
347
+ +-----+--+--+ |
348
+ | | | |
349
+ +-+-+ +-+-+ +-+-+ +-+-+
350
+ | C | | D | | E | | G |
351
+ +---+ +---+ +---+ +---+
352
+ ```
353
+
354
+ ###### Breadth-first traversal
355
+
356
+ ** Breadth-first traversal** is visiting a node and all its
357
+ [ _ siblings_ ] [ term-sibling ] to broaden the search at that level, before
358
+ traversing [ _ children_ ] [ term-child ] .
359
+
360
+ For the syntax tree defined in the diagram, a breadth-first traversal first
361
+ searches the root of the tree (** A** ), then its children (** B** and ** F** ), then
362
+ their children (** C** , ** D** , ** E** , and ** G** ).
363
+
364
+ ###### Depth-first traversal
365
+
366
+ Alternatively, and more commonly, ** depth-first traversal** is used.
367
+ The search is first deepened, by traversing [ _ children_ ] [ term-child ] , before
368
+ traversing [ _ siblings_ ] [ term-sibling ] .
369
+
370
+ For the syntax tree defined in the diagram, a depth-first traversal first
371
+ searches the root of the tree (** A** ), then one of its children (** B** or
372
+ ** F** ), then their children (** C** , ** D** , and ** E** , or ** G** ).
373
+
374
+ For a given node _ N_ with [ _ children_ ] [ term-child ] , a ** depth-first traversal**
375
+ performs three steps, simplified to only binary trees (every node has
376
+ [ _ head_ ] [ term-head ] and [ _ tail_ ] [ term-tail ] , but no other children):
377
+
378
+ * ** N** : visit _ N_ itself
379
+ * ** L** : traverse [ _ head_ ] [ term-head ]
380
+ * ** R** : traverse [ _ tail_ ] [ term-tail ]
381
+
382
+ These steps can be done in any order, but for non-binary trees, ** L** and ** R**
383
+ occur together.
384
+ If ** L** is done before ** R** , the traversal is called _ left-to-right_
385
+ traversal, otherwise it is called _ right-to-left_ traversal.
386
+ In the case of non-binary trees, the other children between _ head_ and _ tail_
387
+ are processed in that order as well, so for _ left-to-right_ traversal, first
388
+ _ head_ is traversed (** L** ), then its _ next sibling_ is traversed, etcetera,
389
+ until finally _ tail_ (** R** ) is traversed.
390
+
391
+ Because ** L** and ** R** occur together for non-binary trees, we can produce four
392
+ types of orders: NLR, NRL, LRN, RLN.
393
+
394
+ NLR and LRN (the two _ left-to-right_ traversal options) are most commonly used
395
+ and respectively named [ _ preorder_ ] [ term-preorder ] and
396
+ [ _ postorder_ ] [ term-postorder ] .
397
+
398
+ For the syntax tree defined in the diagram, _ preorder_ and _ postorder_ traversal
399
+ thus first search the root of the tree (** A** ), then its head (** B** ), then its
400
+ children from left-to-right (** C** , ** D** , and then ** E** ).
401
+ After all [ _ descendants_ ] [ term-descendant ] of ** B** are traversed, its next
402
+ sibling (** F** ) is traversed and then finally its only child (** G** ).
403
+
307
404
## Utilities
308
405
309
406
** Utilities** are functions that work with nodes.
@@ -491,6 +588,10 @@ This work is licensed under a
491
588
492
589
[ term-tree ] : #tree
493
590
591
+ [ term-preorder ] : #preorder
592
+
593
+ [ term-postorder ] : #postorder
594
+
494
595
[ term-child ] : #child
495
596
496
597
[ term-parent ] : #parent-1
@@ -501,6 +602,10 @@ This work is licensed under a
501
602
502
603
[ term-descendant ] : #descendant
503
604
605
+ [ term-head ] : #head
606
+
607
+ [ term-tail ] : #tail
608
+
504
609
[ term-generated ] : #generated
505
610
506
611
[ term-type ] : #type
@@ -509,6 +614,10 @@ This work is licensed under a
509
614
510
615
[ term-file ] : #file
511
616
617
+ [ traversal ] : #tree-traversal
618
+
619
+ [ traversal-depth ] : #depth-first-traversal
620
+
512
621
[ list-of-utilities ] : #list-of-utilities
513
622
514
623
[ webidl ] : https://heycam.github.io/webidl/
0 commit comments