Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How this recursive treewalker works?

Parent

How this recursive treewalker works?

+1
−4

Credit for User:Meriton for developing the following code (first published here).

function replaceIn(e) {
  if (e.nodeType == Node.TEXT_NODE) {
    e.nodeValue = e.nodeValue.replaceAll("a", "");
  } else {
    for (const child of e.childNodes) {
      replaceIn(child);
    }
  }
}

replaceIn(document.body);

How this recursive treewalker works?

As a side question which I grasp as important, can there be an even simpler and more direct version without the else?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

As explained [here](https://software.codidact.com/posts/286333), there are different types of nodes. ... (8 comments)
What is the goal that you want to achieve? Why do you want to avoid using an if-else? (6 comments)
Post
+5
−0

To understand the concept of recursive tree traversal, I would like to give you an analogy. Imagine a labyrinth of rooms connected like a tree structure. That is, there is an entry to the first room, and when entering a room you may find doors leading into rooms deeper down into the labyrinth, but it is guaranteed that there are no cycles.

Some rooms only have an entry, but don't lead to further rooms (leaves of the tree). Of these, some have walls that are painted blue and some of these have walls that are painted green.

Your task is to go through all rooms and paint the blue ones and only the blue ones yellow. With you, you take a rope that will always show you the way back to the entrance of the labyrinth. Or, in other words, when you are in one room, the rope will show you how to get back to the room from where you entered the current room. In addition to the rope you have some pen and lots of fresh sticky note sheets with you.

Your algorithm would start by entering the labyrinth, that is, go into the first room. From then on, you will do the following:

After entering a room:
    take a fresh sheet from your sticky notes and stick it to a wall
    if the room has blue walls:
        paint the blue walls yellow
    otherwise:
        for each door (from left to right), if any, leading to a new room:
             write to the sticky note which is the next door to enter
             enter that door and follow these instructions again.
    as you are now done with this room, dispose of the sticky note
    use the rope to return to the room from where you came

This algorithm is just a translation of your tree-traversal: A node in the tree that is a TEXT_NODE is known to be a leaf of the tree and it contains text to be replaced. This corresponds to the rooms with the blue walls: We know that there are no further rooms from such a room, but we know we have to do the painting here.

Any other node may or may not have child nodes (there are other leaf nodes than TEXT_NODEs, but their list of children is just empty). That is, if there are child nodes, for each child node move there and do just the same thing again. Now, look at the labyrinth analogy: The non-bluish rooms may be the green ones (from which no doors lead further), or just other rooms from which you can move into further rooms deeper down the labyrinth.

Finally, the sticky notes and the rope: These two represent the computer stack. Each sticky note is for data (variables) in the current room (the current depth of recursion): You get a fresh sticky note on entering the room, as you get a new set of local variables with each recursive call. The rope always brings you back to the room from which you entered the current room. Following the rope back corresponds to returning from the innermost recursive function call.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Sticky notes (3 comments)
Sticky notes
deleted user wrote almost 2 years ago

The rope can help a labyrinth researcher to know a way back but the sticky notes and pen help only to know that a room was already visited, is that correct?

In the case of paining the color can also indicate previous visit, but for any case but sticky notes help in any case (even if there was no painting to be done). Right?

Dirk Herrmann‭ wrote almost 2 years ago · edited almost 2 years ago

The stack that a computer program implicitly uses is built of so called stack frames. Whenever a function is called, a new stack frame is created "on top of the stack".

Each stack frame contains information that allows the program to return from the currently executed function to the calling function. This is represented by the rope, the information where we came from to allow us to return there.

But, there is more information in a stack frame: Data that needs to be stored for computations in the currently executed function. Sometimes that data is seen explicltly in the code in the form of function arguments and local variables. Sometimes that data is not explicitly visible in the source code. A for loop for example needs some kind of index or counter or similar to remember, how far the progress in the loop has been. The sticky note for each room represents the possibility of a stack frame to hold data.

In my analogy, the painting represents your goal to replace text.

deleted user wrote almost 2 years ago

Dirk Herrmann‭

I am confused

If we already painted a room and brought rope (and maybe also kept rope so to ease remembrance of how to come back to a given room) why than do we need sticky notes as well?