Tree (General Tree) - Data Structures & Algorithms Tutorials In Python #9
Why Stack Overflow recommends it
Stack Overflow users link it as a worked example of a Python tree class with parent and children pointers, either as the source of code they are asking about or as a diagram and reference in answers.
Recommended by 1 people across 1 different months, 2020 to 2024.
What it is
A Python tutorial from codebasics (part 9 of a data structures and algorithms series) that explains the general tree data structure and implements it step by step. It builds a tree of electronic item categories and ends with an exercise.
What people said
Recommends RookRaven · 2024-06 · Stack Overflow · on “How can I implement a tree in Python?”
There are two parts to this question. How to implement a generic tree in Python Is there an inbuilt DS for it? The second question seems to be answered multiple times, so I'll take the first one. Implement a Generic Tree Data structure [N-ary Tree] in Python [LINK] image credit: [this video] I've tried to implement an N-ary Tree structure and some basic operations like, Insertion Traversal/ Print Search from collections import deque from typing import List, Optional class GenericTree: def __init__(self, data: str): self.data = data self.children = [] self.parent = None def add_child(self, child: 'GenericTree') -> None: """ Add a child to the current node """ child.parent = self self.children.appe
Cites Ian O'Reilly · 2020-09 · Stack Overflow · on “Nonbinary Tree Traversal Method”
I am trying to create a python nonbinary tree class with methods that will allow me to get specific nodes, and apply other methods to those found nodes. I have started with code from this very nice example: [this video] I have added a method that accepts a string that corresponds to the node ("/" separated, skipping the root) I am looking for, recursively searches the tree, and in theory, returns the node using "self" so I can apply another method on it. However, when I return(self) it gives me a nonetype back instead of the node. Advice on how to fix this, or a suggestion of another way to go if this is a bad way to structure things would be greatly appreciated! Thanks in Advance. Note: this is
Cites Zuhair Abid · 2021-02 · Stack Overflow · on “What does assigning self to a variable in Python Class mean?”
While I was learning Tree data structure in python, I came across this code: class TreeNode: def __init__(self, data): self.data = data self.children = [] self.parent = None def get_level(self): level = 0 p = self.parent while p: level += 1 p = p.parent return level def print_tree(self): spaces = ' ' * self.get_level() * 3 prefix = spaces + "|__" if self.parent else "" print(prefix + self.data) if self.children: for child in self.children: child.print_tree() def add_child(self, child): child.parent = self self.children.append(child) def build_product_tree(): root = TreeNode("Electronics") laptop = TreeNode("Laptop") laptop.add_child(TreeNode("Mac")) laptop.add_child(TreeNode("Surface")) lapt