Question

One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This

class BinaryTree: def-init-(self, data): self.data-data self.left None self.right None def get_left (self): return self.left

For example Test Result t-BinaryTree(12345) 12345, None, None] result convert-tree-to-list(t) print(result) 55, [24, [8

python pls and noticed the output added ""

One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in the list is the data value of the root, the second item in the list is the left subtree (this may be None if the left subtree is empty, or it may be a nested list) and the third item in the list is the right subtree (this may be None if the right subtree is empty or it may be a nested list). Define a function called convert_tree_to_list) that takes a binary tree as input and which returns the nested list format of the tree as output. You can assume the binary tree passed as input to the function is an instance of the following BinaryTree class, which is provided to you - you can use the functions available in this class:
class BinaryTree: def-init-(self, data): self.data-data self.left None self.right None def get_left (self): return self.left def get_right(self): return self.right def set_left (self, tree): self.left tree def set_right(self, tree): self.right-tree def set data (self, data): self.data data def get_data (self): return self.data
For example Test Result t-BinaryTree(12345) '12345', None, None] result convert-tree-to-list(t) print(result) 55', ['24", ['8',None, None], [ s1', ['25", None, None], None]], ['72", None, ['78,None, None]]] a BinaryTree (55) b BinaryTree (24) c = BinaryTree(8) d BinaryTree(51) e- BinaryTree (25) f BinaryTree (72) g - BinaryTree (78) f.set_right(g) d. set_left(e) b.set_left(c) b.set_right(d) a.set_left(b) a.set_right(f) result- convert tree to_list(a) print(result)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

use -11w as a reference in Convert Incl ton-- salf. item ileun 1 while głack l^ nodo sNone t hode iy hot None node s nade. La

Add a comment
Know the answer?
Add Answer to:
python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The nat...

    Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT