#πŸ”’ confused abt a traversing tree question

16 messages Β· Page 1 of 1 (latest)

lilac apex
#

can someone answer the bottom question im confused what it mean 3 different methods of navigating a tree i thought there is only 1 way

twilit girderBOT
#

@lilac apex

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rough yoke
lilac apex
# rough yoke What is the one way you're thinking of?

idk just like traversing a linked list? going left to the root if not then u go right etc i get for a number tree thats balanced u can get if the number is bigger or not and go left or right accoridng to that but what methods is there for that type of tree just random letters

rough yoke
#

I think one of the three ways they're inteding is "Depth First Search", and then the second is the other search you would have learned. I can't think off the top of my head what the third would be though.

hollow egret
#

I think both implementations would be very similar.

#

Pretty sure this is right. ```py
from collections import deque

def breadth_first(node):
queue = deque([node])
while queue:
node = queue.popleft()
queue.extend(node.children)
yield node

def depth_first(node):
queue = deque([node])
while queue:
node = queue.pop()
queue.extend(node.children)
yield node

#

recursion is bad for trees.

lilac apex
#

but i remember learning abt bfs and dfs

hollow egret
#

Those are versions of depth first search

#

In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are...

lilac apex
#

yeah is that would the question was looking for

#

well id assume so

twilit girderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.