Hi, how are you? I'm so stressed out and no one is helping me and I'm asking because I don't
No one is helping me at school, and the professor is not answering and not interested. The quality of teaching is so bad and I am struggling alone because I didn't learn it properly. Please give me an answer me.
I think it's going to be long. I'm sorry.
The problem is subtracting the tree from the binary tree class to the level order list.
class RefBinaryTree:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def insert_left(self,data):
t = RefBinaryTree(data)
if self.left is None:
self.left = t
else:
t.left = self.left
self.left = t
def insert_right(self,data):
t = RefBinaryTree(data)
if self.right is None:
self.right = t
else:
t.right = self.right
self.right = t
def set_value(self,val):
self.data = val
def get_value(self):
return self.data
def get_right_subtree(self):
return self.right
def get_left_subtree(self):
return self.left
def test_tree(tree):
tree.insert_left(31)
tree.insert_right(5)
tree.get_left_subtree().insert_left(27)
tree.get_right_subtree().insert_right(1)
tree.get_right_subtree().get_right_subtree().insert_left(7)
return tree
def tree_to_list(tree):
treeList = []
if not tree:
return [None]
level_process = [tree.get_value()]
print(level_process)
item_left = item.left.get_left_subtree()
print(item_left)
while len(level_process) > 0:
num_level = []
next_level = []
for n in level_process:
num_level.append(n)
if n.left.get_value() != None:
next_level.append(n.left.get_value())
if n.left.get_value() == None:
next_level.append(None)
if n.right.get_value() != None:
next_level.append(n.right.get_value())
if n.right.get_value() == None:
next_level.append(None)
treeList.append(num_level)
level_process = next_level
return treeList
tree = RefBinaryTree(2)
a_tree = test_tree(tree)
tree_list = tree_to_list(a_tree)
print(tree_list)
As a test tree, I made the tree that I showed you in the assignment. And then what I have to do is to organize it from tree to list, but I tried to write code (as a level order) and put it in, but there is an error.
I don't think there's any recognition in the downstream.I don't know why not.
for n in level_process:
It's definitely the node of the tree that's stored in the level process, but there's something like left behind the intro Attribute error appears. I don't know how to make it possible. Or I'll use something like a re-creation Is there a simple way to do it? I want to know...
binary-tree python3
The insert of the binary tree is a strange shape. For binary trees, it is right to provide one insert rather than implementing insert_left and insert_right separately. If the value to be inserted is less than the value I have, I should add it to the left node, and if it is large or equal, I should implement it to add it to the right node on the right. And if you already have a node on the left or right, you just pass the value and let it handle it.
class RefBinaryTree:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def insert(self,data):
if data < self.data:
if self.left:
self.left.insert(data)
else:
self.left = RefBinaryTree(data)
else:
if self.right:
self.right.insert(data)
else:
self.right = RefBinaryTree(data)
def print_tree(self):
if self.left:
self.left.print_tree()
print(self.data)
if self.right:
self.right.print_tree()
root_node = RefBinaryTree(2)
root_node.insert(31)
root_node.insert(5)
root_node.insert(27)
root_node.insert(1)
root_node.insert(7)
root_node.print_tree()
According to the code you uploaded, the root has a value of 2, and 31, 5, 27, 1, 7 are sequentially put in the tree. And I made a print_tree method that prints it out in order. Refer to the print_tree method and implement the function of making a list. Press Run under the code to run immediately.
I used a refreshing method. It's also good to know that you're using the repetition that you posted in the question.
© 2024 OneMinuteCode. All rights reserved.