Tree traversal. Inorder, preorder, postorder

I understand the idea of ​​traversing a tree, as well as implementations, but here's the question. Why do they all need?

Now I only know that bypassing pre-orders is used to parse mathematical expressions. I also read from Wikipedia, than:

  • Order crawling is especially common for using order traversal in the binary search tree, since it will return values ​​from the base set in order, according to the comparator that set up the binary search tree (hence the name). Bypass order
  • Bypassing a tree in a preorder when inserting values ​​into a new tree is the usual way to create a complete copy of a binary search tree. You can also use pre-order workarounds to get a prefix expression (Polish notation) from expression trees: move the expression tree first. (what I already said)

But these examples are rather vague. Can anyone describe this in more detail. Especially with examples.

+4
source share
2 answers

Consider the problem of performing some operation with files in a directory tree. When the operation is to delete files, you need to clean each directory before deleting the directory itself, so you need to go through post-processing. In contrast, when copying a hierarchy, you first need to copy the directories, so you need a preliminary crawl.

Honestly, I don't see what is vague about the BST workaround. If you want to display the contents of the BST on the screen in the user interface, you want the keys to be displayed sorted, right? (If you haven’t done this, then using a BST is likely to be a bad idea, as a hash table is often fast.)

+5
source

I can come up with many examples. For example, performance.

Imagine a tree in real life. It has a stem and the stem has 3 branches. Each of these inner branches has 3 outer branches. Thus, it has 9 outer branches.

One of the three inner branches is dead, and then its 3 outer branches also disappeared.

Now you want to cut all dead branches. The tree has 13 branches (1 stem 3 internal and 9 external). You must look at them all individually to determine if you want to cut them or not. No

Now imagine that there is a robot that wants to cut all dead branches. In his cuts of software, he looks at the stalk. He is dead? No. Then he looks at the first inner branch, is she dead? Yes! Then he will cut off this branch and at the same time its outer branches will be cut.

Instead of making 13 options, you need to do only 10. (Stock, 2 healthy internal branches, 6 of their external branches and a diseased internal branch)

+3
source

Source: https://habr.com/ru/post/1437072/


All Articles