Skip to content

Commit 4b3889b

Browse files
authored
Add terms for tree traversal, preorder, postorder
Closes GH-22. Closes GH-23. Reviewed-by: Christian Murphy <[email protected]>
1 parent 49032b9 commit 4b3889b

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

readme.md

+109
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The latest released version is [`2.0.0`][release].
2323
* [Parent](#parent)
2424
* [Literal](#literal)
2525
* [Glossary](#glossary)
26+
* [Tree traversal](#tree-traversal)
2627
* [Utilities](#utilities)
2728
* [List of Utilities](#list-of-utilities)
2829
* [References](#references)
@@ -304,6 +305,102 @@ Files are provided by the host environment and not defined by unist.
304305

305306
For example, see projects such as [**vfile**][vfile].
306307

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+
307404
## Utilities
308405

309406
**Utilities** are functions that work with nodes.
@@ -491,6 +588,10 @@ This work is licensed under a
491588

492589
[term-tree]: #tree
493590

591+
[term-preorder]: #preorder
592+
593+
[term-postorder]: #postorder
594+
494595
[term-child]: #child
495596

496597
[term-parent]: #parent-1
@@ -501,6 +602,10 @@ This work is licensed under a
501602

502603
[term-descendant]: #descendant
503604

605+
[term-head]: #head
606+
607+
[term-tail]: #tail
608+
504609
[term-generated]: #generated
505610

506611
[term-type]: #type
@@ -509,6 +614,10 @@ This work is licensed under a
509614

510615
[term-file]: #file
511616

617+
[traversal]: #tree-traversal
618+
619+
[traversal-depth]: #depth-first-traversal
620+
512621
[list-of-utilities]: #list-of-utilities
513622

514623
[webidl]: https://heycam.github.io/webidl/

0 commit comments

Comments
 (0)