Question

IMPORTANT: For this exercise, you will be defining a function which USES the BinaryTree ADT. A Binary Tree implementation is

class BinaryTree:
    def __init__(self, data, left=None, right=None):
        self.__data = data
        self.__left = left
        self.__right = right

    def insert_left(self, new_data):
        if self.__left == None:
            self.__left = BinaryTree(new_data)
        else:
            t = BinaryTree(new_data, left=self.__left)
            self.__left = t

    def insert_right(self, new_data):
        if self.__right == None:
            self.__right = BinaryTree(new_data)
        else:
            t = BinaryTree(new_data, right=self.__right)
            self.__right = t

    def get_left(self):
        return self.__left

    def get_right(self):
        return self.__right

    def set_data(self, data):
        self.__data = data

    def get_data(self):
        return self.__data

    def set_left(self, left):
        self.__left = left

    def set_right(self, right):
        self.__right = right
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right def insert_left(self, new_data): if self.__left == None: self.__left = BinaryTree(new_data) else: t = BinaryTree(new_data, left=self.__left) self.__left = t def insert_right(self, new_data): if self.__right == None: self.__right = BinaryTree(new_data) else: t = BinaryTree(new_data, right=self.__right) self.__right = t def get_left(self): return self.__left def get_right(self): return self.__right def set_data(self, data): self.__data = data def get_data(self): return self.__data def set_left(self, left): self.__left = left def set_right(self, right): self.__right = right def getHeight(root): if (root.get_left() == None and root.get_right() == None): return 0 left = 0 if (root.get_left() != None): left = getHeight(root.get_left()) right = 0 if (root.get_right() != None): right = getHeight(root.get_right()) return (max(left, right) + 1) sum=[] def calculateLevelSum(node, level): global sum if (node == None): return sum[level] += node.get_data() calculateLevelSum(node.get_left(), level + 1) calculateLevelSum(node.get_right(), level + 1) def get_sum_levels(t): global sum sum=[0]*(getHeight(t)+1) calculateLevelSum(t,0) return sum t=BinaryTree(7) t.insert_left(5) t.insert_right(2) tr=t.get_right() tr.insert_left(3) tr.insert_right(3) print(get_sum_levels(t)) 

class BinaryTree: def init (self, data, left=None, right=None): self data = data self. left = left self right right def inser

def getHeight (root): if (root.get_left() return 0 == None and root.get_right() == None): left = 0 if (root.get_left() != Non

below is the output

[7.7. 7.6] >|

I had attached code and the output as well as screenshot of code for your better understanding

note that I had used the same Binary tree that was given in the example above so that you can easily verify the working of the program.

Add a comment
Know the answer?
Add Answer to:
class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right...
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 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...

    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...

  • C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

    C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...

  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, valu...

    Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, value): self.__value = value self.__left = None self.__right = None self.__parent = None self.__height = 1    def getValue(self): return self.__value    def setHeight(self, height): self.__height = height    def getHeight(self): return self.__height    def setParent(self, node): self.__parent = node    def getParent(self): return self.__parent    def setLeftChild(self, child): self.__left = child child.setParent(self)    def setRightChild(self, child): self.__right = child child.setParent(self)    def createLeftChild(self, value): self.__left = BinaryNode(value)    def createRightChild(self, value): self.__right = BinaryNode(value)    def...

  • 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...

  • java Description You are to create a class named BinaryTree. This will simulate a balanced and...

    java Description You are to create a class named BinaryTree. This will simulate a balanced and ordered binary tree. You also need to create a TreeNode class, which defines the data contained in each node of the tree. In this case, it is an integer. Remember that the node also needs a way to track back to its parent, as well as its left and right children. For the purpose of this assignment, each node should also contain an indication...

  • Write an implementation of a generic java BinaryTree class that fits any primitive data type. your...

    Write an implementation of a generic java BinaryTree class that fits any primitive data type. your class at minimum should have the following. Node class with a minimum of three fields and 3 constructor. BinaryTree class should include the following at least 3 Fields, at least 2 constructors, add, remove, find, getSize method (which returns the amount nods in the tree), getHeight (should return the height), countLeftNode (Should return the mount of left node found in the tree), countRightNode (Should...

  • Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search...

    Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search tree data structure completed in class, so that you have an opportunity to practice both using the recursive pattern covered in class and navigating the binary tree structure. The methods you'll implement are: count_less_than: takes an argument x, and returns the number of elements in the tree with values less than x successor: takes an argument x, and returns the smallest value from the...

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