Python Tree Problem Please Help Reminder. Every node in a tree t is the root val
ID: 672848 • Letter: P
Question
Python Tree Problem Please Help
Reminder. Every node in a tree t is the root value of some subtree of t Definition. A subset s of nodes is tree-consistent if, for every node n included in S, S also includes all nodes in the subtree for which n is the root value. In other words, a tree-consistent subset of nodes contains all the nodes in a collection of subtrees For any tree, the set of all nodes and the empty set are both tree-consistent. Examples: x - tree(-1, [tree(1), tree(-2), tree (3, [tree(-4), tree(-5)])]) [tree (2 ) ] ), tree ( 1, [tree(-1, tree( 8, [tree(-7)]), tree(-3, [tree(-3)]), tree(-4, [tree(3), y - tree(-5), tree(7, Itree(-4), tree(-2)])])]) In x , if 3 is included then -4 and -5 must be included because 3 is the root value of a subtree containing -4 and -5. Therefore, the set of nodes 13, -4, -5} is tree-consistent, but 13 and 13, -4} are not. The tree-consistent subset with the highest sum is 1 In y , the tree-consistent subset with the highest sum is 12, 8, -7, 3, 7, -4, -2:Explanation / Answer
class Node:
def_init_(main,node)
main.node =node
main.node=None
main.right=None
def_str(main)
if main.root=none
main.root=Node(val)
else
current =main.root
while 1:
if val<current.node
if current.left
current =current.left
else
current.left=node(val);
break
while 1:
if val<current.info;
if curent.left;
current=current.left
tree
arr=2,8,-7,3,7,-4,-2
for i in arr ;
tree.create(i);
print("main order");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.