Skip to content

Add terms for tree traversal, preorder, postorder #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The latest released version is [`2.0.0`][release].
* [Parent](#parent)
* [Literal](#literal)
* [Glossary](#glossary)
* [Tree traversal](#tree-traversal)
* [Utilities](#utilities)
* [List of Utilities](#list-of-utilities)
* [References](#references)
Expand Down Expand Up @@ -304,6 +305,102 @@ Files are provided by the host environment and not defined by unist.

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

###### Preorder

In **preorder** (**NLR**) is [depth-first][traversal-depth] [tree
traversal][traversal] that performs the following steps for each node _N_:

1. **N**: visit _N_ itself
2. **L**: traverse [_head_][term-head] (then its _next sibling_, recursively
moving forward until reaching _tail_)
3. **R**: traverse [_tail_][term-tail]

###### Postorder

In **postorder** (**LRN**) is [depth-first][traversal-depth] [tree
traversal][traversal] that performs the following steps for each node _N_:

1. **L**: traverse [_head_][term-head] (then its _next sibling_, recursively
moving forward until reaching _tail_)
2. **R**: traverse [_tail_][term-tail]
3. **N**: visit _N_ itself

## Tree traversal

**Tree traversal** is a common task when working with a [_tree_][term-tree] to
search it.
Tree traversal is typically either _breadth-first_ or _depth-first_.

In the following examples, we’ll work with this tree:

```ascii
+---+
| A |
+-+-+
|
+-----+-----+
| |
+-+-+ +-+-+
| B | | F |
+-+-+ +-+-+
| |
+-----+--+--+ |
| | | |
+-+-+ +-+-+ +-+-+ +-+-+
| C | | D | | E | | G |
+---+ +---+ +---+ +---+
```

###### Breadth-first traversal

**Breadth-first traversal** is visiting a node and all its
[_siblings_][term-sibling] to broaden the search at that level, before
traversing [_children_][term-child].

For the syntax tree defined in the diagram, a breadth-first traversal first
searches the root of the tree (**A**), then its children (**B** and **F**), then
their children (**C**, **D**, **E**, and **G**).

###### Depth-first traversal

Alternatively, and more commonly, **depth-first traversal** is used.
The search is first deepened, by traversing [_children_][term-child], before
traversing [_siblings_][term-sibling].

For the syntax tree defined in the diagram, a depth-first traversal first
searches the root of the tree (**A**), then one of its children (**B** or
**F**), then their children (**C**, **D**, and **E**, or **G**).

For a given node _N_ with [_children_][term-child], a **depth-first traversal**
performs three steps, simplified to only binary trees (every node has
[_head_][term-head] and [_tail_][term-tail], but no other children):

* **N**: visit _N_ itself
* **L**: traverse [_head_][term-head]
* **R**: traverse [_tail_][term-tail]

These steps can be done in any order, but for non-binary trees, **L** and **R**
occur together.
If **L** is done before **R**, the traversal is called _left-to-right_
traversal, otherwise it is called _right-to-left_ traversal.
In the case of non-binary trees, the other children between _head_ and _tail_
are processed in that order as well, so for _left-to-right_ traversal, first
_head_ is traversed (**L**), then its _next sibling_ is traversed, etcetera,
until finally _tail_ (**R**) is traversed.

Because **L** and **R** occur together for non-binary trees, we can produce four
types of orders: NLR, NRL, LRN, RLN.

NLR and LRN (the two _left-to-right_ traversal options) are most commonly used
and respectively named [_preorder_][term-preorder] and
[_postorder_][term-postorder].

For the syntax tree defined in the diagram, _preorder_ and _postorder_ traversal
thus first search the root of the tree (**A**), then its head (**B**), then its
children from left-to-right (**C**, **D**, and then **E**).
After all [_descendants_][term-descendant] of **B** are traversed, its next
sibling (**F**) is traversed and then finally its only child (**G**).

## Utilities

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

[term-tree]: #tree

[term-preorder]: #preorder

[term-postorder]: #postorder

[term-child]: #child

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

[term-descendant]: #descendant

[term-head]: #head

[term-tail]: #tail

[term-generated]: #generated

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

[term-file]: #file

[traversal]: #tree-traversal

[traversal-depth]: #depth-first-traversal

[list-of-utilities]: #list-of-utilities

[webidl]: https://heycam.github.io/webidl/
Expand Down