#π confused abt a traversing tree question
16 messages Β· Page 1 of 1 (latest)
@lilac apex
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.
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
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.
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.
@hollow egret wait i kinda asked chatgpt on different methods of navigating and it gave these;
Pre-order Traversal
In-order Traversal
Post-order Traversal
but i remember learning abt bfs and dfs
Those are versions of depth first search
acoording to wikipedia at least. https://en.wikipedia.org/wiki/Tree_traversal#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...
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.