Question

Python question. How do i make this code work? im trying to print the value of...

Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal

class Node:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class BinarySearchTree:
def insertNode(self, value):
self.root = self._insert(self.root, value)
  
def levelOrderTraversal(self, root):
if root is None:
return []
result, current = [], [root]
while current:
next_level, vals = [], []
for node in current:
vals.append(node.val)
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
current = next_level
result.append(vals)
return result

t=BinarySearchTree()

for i in range(12):
x = (4*i+6)%21

t.levelOrder(x)

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

Python code:

class Node:

def __init__(self, x):

self.val = x

self.left = None

self.right = None


def levelOrderTraversal(root,height):

if root is None:

return

if height == 0:

print(root.val);

elif height > 0 :

levelOrderTraversal(root.left , height-1);

levelOrderTraversal(root.right , height-1);


def lengthH(node):

##Checking condition of root node being null.Then length will be zero

if node is None:

return 0;

## Either left subtree or right subtree exists

else:

##Calculate left subtree length

leftLength = lengthH(node.left)

##Calculate Right subtree length

rightLength = lengthH(node.right)

##We should consider the highest length

##Using if else block for that operation.

if leftLength>rightLength:

return leftLength+1;

else:

return rightLength+1;


root = Node(56)

root.right = Node(30)

root.left = Node(20)

##Saving right and left substree nodes

rootr = root.right

rootl = root.left

##Assigning new child Node using Saved node references

rootr.left = Node(100)

rootr.right = Node(50)

rootl.left = Node(150)

rootl.right = Node(200)

##Calculate length of tree using recursive method

lengthOfTree = lengthH(root)

for i in range(lengthOfTree):

levelOrderTraversal(root , i);




Output

56
20
30
150
200
100
50

Screenshot

II, anu must your programs and apps UTILITIE TUI ICC. main.py E saved 1 class Node: def __init__(self, x): self.val = x self.

20 150 200 100 50 def length (node): ##Checking condition of root node being null. Then length will be zero if node is None:main.py E saved 56 38 150 root = Node (56) root.right = Node (30) root.left = Node (20) 200 100 50 ##Saving right and left su

Please refer attached screenshot for proper identation.

Add a comment
Know the answer?
Add Answer to:
Python question. How do i make this code work? im trying to print the value of...
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
  • Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST...

    Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST Node: class RBBST: def init_(self, val, color): self.val val self.left None self.right None self.color color def init_(self) : self.root None def init_rbbst(self, val, color): self.root def insert(self, val): if (self.root is None): self.init_rbbst (val, RED) else: RBBST_Node(val, color) RED True BLACK False self. insertNode (self. root, val) def insertNode(self, current, val): (ii) def rotate_left (self, current) : (ii) def flip_colors (self, current) :

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

  • (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL)...

    (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...

  • Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserial...

    Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test should pass: node = Node('root', Node('left', Node('left.left')), Node('right')) assert deserialize(serialize(node)).left.left.val == 'left.left'

  • PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in...

    PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in an arbitrary node of a BST, and return the value of the predecessor of the given node. Code : class Node: def __init__(self, value): self.value = value self.left = None self.right = None    def predecessor(node): # TODO

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

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

  • Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the...

    Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the binary tree passed to it is a valid binary search tree. I've provided a simple TreeNode class, and the beginnings of isBinarySearchTree(root). class TreeNode: '''Node for a simple binary tree structure''' def __init__(self, value, left, right): self.value = value self.left = left self.right = right    def isBinarySearchTree(tree):

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

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

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