Question
COMPLETE IN PYTHON PLEASE! In the Tree class, finish the method called get_nodes. This method should take in a piece of data that the method
COMPLETE IN PYTHON PLEASE!
In the Tree class, finish the method called get_nodes. This method should take in a piece of data that the method is searching for, and it should return a list of all of the nodes in the tree that contain that piece of data. The list returned should be in hierarchical order, meaning the list should be ordered from the highest ranking node to lowest ranking node. You can assume that there will be at most one node with the given data on any particular level of the tree.
"""Tree class and tree node class."""
class Node():
"""Node in a tree."""
def __init__(self, data, children=None):
children = children or []
assert isinstance(children, list), \
"children must be a list!"
self.data = data
self.children = children
def __repr__(self):
"""Reader-friendly representation."""
return f"
class Tree():
"""Tree."""
def __init__(self, root):
self.root = root
def __repr__(self):
"""Reader-friendly representation."""
return f"
def get_nodes(self, data):
""" Return a list of nodes with the given data"""
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started