Trees/Computer Science/Depth-First Search: How does in-order traversal work?

I don't fully understand the in-order traversal. At least not in the code.

You start at 5 and recurse to 1, always to the left. Then I print 1. After that, the 3 would be printed, meaning you climb back up.

I don't understand how the climb works. After the 1, you'd want to jump to the right, which isn't possible because the 1 has no children. The same thing happens after I've spent the 1, 3, and the 4: How do I jump back up to the 5?

Well, I understand the principle in pseudocode: Always left, root, right. But not in the real code.

I have this code snippet from ChatGPT:

 def in_order_traversal(root): if root is not None: # Zuerst den linken Teilbaum durchlaufen in_order_traversal(root.left) # Dann den Wurzelknoten besuchen print(root.value) # Schließlich den rechten Teilbaum durchlaufen in_order_traversal(root.right)
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
11 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
11 months ago

Since your sample tree is small, you can also go through the algorithm well step-by-step.

I’ll show the drain for the left subtree. We start with the root node (n(5)):

n(5) is not None > wahr
in_order_traversal(n(3))
  n(3) is not None > wahr
  in_order_traversal(n(1))
    n(1) is not None > wahr
    in_order_traversal(None)
      None is not None > unwahr
    Ausgabe von 1
    in_order_traversal(None)
      None is not None > unwahr
  Ausgabe von 3
  in_order_traversal(n(4))
    n(4) is not None > wahr
    in_order_traversal(None)
      None is not None > unwahr
    Ausgabe von 4
    in_order_traversal(None)
      None is not None > unwahr
  Ausgabe von 5
  in_order_traversal(n(8))
    usw. ...

The in_order_traversal– Function is recursive. That means she keeps calling herself. Whenever the program flow changes to a new functional context, I have set the indentation stage further to the right in the above course.

Since a subnode is transferred during each call, the tree structure can be folded down. The examination of whether the transferred node exists is the termination condition of the recursion. In this case, the function does not call itself again, but is terminated. Thus, the program flow can jump again to the caller (or here climb up).

regex9
11 months ago
Reply to  Alibali86756

I have already explained what the indentations stand for in my above text.

In the best case, you will create a sketch/step-by-step schedule yourself. Basically, you only need to consider a rule:

When a function is called, the program flow jumps in its body, its instructions work (until the end of the functional body or one return– instruction has been reached) and then jumps back to the caller (or behind the call location). From there it goes as usual.

regex9
11 months ago

When crossing to pre-order logic, it doesn't work great than differently in-order. The only difference is that at each call the pre-order function the node value is output first before it goes further into the subnodes.

Algorithm (Python):

 def pre_order_traversal(root): if root is not None: print(root.value) pre_order_traversal(root.left) pre_order_traversal(root.right)

Take this tree as an example:

 5 3 8

working steps, starting with the call of pre_order_traversal(n(5)) :

 n(5) is not None > wahr Ausgabe von 5 Aufruf von pre_order_traversal(n(3)) n(3) is not None > wahr Ausgabe von 3 Aufruf von pre_order_traversal(None) None is not None > unwahr Aufruf von pre_order_traversal(None) None is not None > unwahr Aufruf von pre_order_traversal(n(8)) n(8) is not None > wahr Ausgabe von 8 Aufruf von pre_order_traversal(None) None is not None > unwahr Aufruf von pre_order_traversal(None) None is not None > unwahr

As already mentioned above: An indentation step to the right stands for a context change into the called function. When the function has been completely processed, it goes back to the caller (an indentation to the left). In the end, you're back at the first caller.

Destranix
11 months ago

You need to place the previous nodes in a data structure. A stack usually in depth search, a cue in width search. How exactly depends on how exactly you carry.

Destranix
11 months ago
Reply to  Alibali86756

The progammstack is used for recursion, yes.

Destranix
11 months ago

During recursion, jump marks are placed on the stack by the recursion call.

In what order this happens you decide by the order of your calls.

For in-order, this would look like:

handle(left);
//Handle current
handle(right);

For mail order according to:

handle(left);
handle(right);
//Handle current
MrAmazing2
11 months ago

Look at how Recursion works, best visualized. Then you should understand how the “high climbing” works.