Question

instructions trom your teach In-order Traversal In this assignment, you are asked to write a function to return the list contCan 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 getLeftChild(self):
return self.__left
  
def getRightChild(self):
return self.__right
  
def __repr__(self):
return f'BinaryNode: {self.__value}'

def inorder(root):
# Your code here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 getLeftChild(self):

        return self.__left

    def getRightChild(self):

        return self.__right

    def __repr__(self):

        return f'BinaryNode: {self.__value}'

def inorder(root):

    arr = []

   

    inorderUtil(root, arr)

   

    return arr

   

def inorderUtil(root, arr):

    # if the current subtree is not empty

    if root != None:

   

        # recursively traverse the left subtree

        inorderUtil(root.getLeftChild(), arr)

       

        # add the current node into the tree

        arr.append(root.getValue())

           

        # recursively traverse the right subtree

        inorderUtil(root.getRightChild(), arr)

       

# create node

root = BinaryNode(15)

# create node

n1 = BinaryNode(5)

# set n1 as the left child to root

root.setLeftChild(n1)

n2 = BinaryNode(22)

# set n2 as the right child to root

root.setRightChild(n2)

n3 = BinaryNode(1)

n4 = BinaryNode(10)

n5 = BinaryNode(18)

n6 = BinaryNode(25)

n1.setLeftChild(n3)

n1.setRightChild(n4)

n2.setLeftChild(n5)

n2.setRightChild(n6)

print(inorder(root))

Sample Output

C: \Users\Rishab\Desktop>python BST.py [1, 5, 10, 15, 18, 22, 25]

Add a comment
Know the answer?
Add Answer to:
Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, valu...
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
  • Can you complete this program using same code please. DO not change any code. Thank you...

    Can you complete this program using same code please. DO not change any code. Thank you In this problem you are given the root of a BST and you want to return the summation of all the values in the BST. Remember that the summation of a BST is the summation of all the nodes in the left subtree + the summation of all the nodes in the right subtree + the value of the current node Expected behavior: root...

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

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

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

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

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

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

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Create a new Java Application that has the following methods: A method that generate a binary...

    Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...

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