Real world pre/post-order tree traversal examples

后端 未结 2 1872
我寻月下人不归
我寻月下人不归 2021-01-31 21:59

I understand pre-order, in-order, and post-order tree traversal algorithms just fine. (Reference). I understand a few uses: in-order for traversing binary search trees in order

2条回答
  •  臣服心动
    2021-01-31 22:14

    Topological sorting is a post-order traversal of trees (or directed acyclic graphs).

    The idea is that the nodes of the graph represent tasks and an edge from A to B indicates that A has to be performed before B. A topological sort will arrange these tasks in a sequence such that all the dependencies of a task appear earlier than the task itself. Any build system like UNIX make has to implement this algorithm.

    The example that Dario mentioned — destroying all nodes of a tree with manual memory management — is an instance of this problem. After all, the task of destroying a node depends on the destruction of its children.

提交回复
热议问题