
PYTHON
MY CODE IS NOT WORKING PROPERLY
(IM USING PYCHARM AS MY IDE)
1. Generate a random binary tree with 12 elements. You can leave the perfect option as
False.
2. Print the tree.
3. Test the tree properties. The documentation uses the assert statement.
4. Create a binary search tree manually (not using the bst function, but instead building
it by hand. Put at least 8 elements in it.
5. Print this tree.
6. Print the values in the tree in several orders (inorder, postorder, preorder, etc.)
CODE BELOW
from binarytree import build, bst, Node
# Generate a random binary tree with 12 elements
random_tree = build(height=3, min_value=1, max_value=100)
# Print the tree
print("Random binary tree:")
print(random_tree)
# Test the tree properties
assert random_tree.is_binary_tree
assert not random_tree.is_perfect
# Create a binary search tree manually
bst_tree = Node(50)
bst_tree.left = Node(30)
bst_tree.right = Node(70)
bst_tree.left.left = Node(20)
bst_tree.left.right = Node(40)
bst_tree.right.left = Node(60)
bst_tree.right.right = Node(80)
bst_tree.right.right.right = Node(90)
# Print the BST
print("Binary search tree:")
print(bst_tree)
# Print the values in the tree in several orders
print("Inorder traversal:")
print(bst_tree.inorder)
print("Postorder traversal:")
print(bst_tree.postorder)
print("Preorder traversal:")
print(bst_tree.preorder)

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 4 images

- Please, help. I'm working with notebook python. class Tree: """A rooted binary tree""" def __init__(self): self.root = None self.left = None self.right = None def is_empty(tree: Tree) -> bool: """Return True if and only if tree is empty.""" return tree.root == tree.left == tree.right == None def join(item: object, left: Tree, right: Tree) -> Tree: """Return a tree with the given root and subtrees.""" tree = Tree() tree.root = item tree.left = left tree.right = right return tree The following code builds a binary tree, MORSE, which implements the upper-case Morse code, as per the image below. More information on Morse code can be found on Wikipedia. You will need to run this code block to use in your solutions. In [33]: EMPTY = Tree()arrow_forwardplease, can you help me, I'm working with notebooks python. class Tree: """A rooted binary tree""" def __init__(self): self.root = None self.left = None self.right = None def is_empty(tree: Tree) -> bool: """Return True if and only if tree is empty.""" return tree.root == tree.left == tree.right == None def join(item: object, left: Tree, right: Tree) -> Tree: """Return a tree with the given root and subtrees.""" tree = Tree() tree.root = item tree.left = left tree.right = right return tree The following code builds a binary tree, MORSE, which implements the upper-case Morse code, as per the image below. More information on Morse code can be found on Wikipedia. You will need to run this code block to use in your solutions. EMPTY = Tree()H = join('H',EMPTY,EMPTY)V = join('V',EMPTY,EMPTY)F = join('F',EMPTY,EMPTY)L = join('L',EMPTY,EMPTY)P = join('P',EMPTY,EMPTY)J = join('J',EMPTY,EMPTY)B = join('B',EMPTY,EMPTY)X =…arrow_forwardExplain if and how a non-binary decision tree can be converted into a binary one. Identify a type of decision tree that can be converted into a binary tree. Provide a conversion algorithm and its computational complexity analysis. Implement the algorithm in Python and provide the full code, input file, instructions on how to run it, and screenshots documenting the transformation of a non-binary tree into a binary one. Note: You are free to choose how to implement the tree (e.g., an array, linked list, etc.), but you must include an explanation of your choice.arrow_forward
- C++ Sketch a left rotation on an AVL. build the following AVL tree: insert 5insert 2insert 7insert 9insert 11 follow the insert algorithm draw out the original tree prior to any rotations-which node breaks the avl condition-label N, R, A, B, C, and the parent of N.-draw out the final tree after any rotationarrow_forwardAVL Tree Implementation program Important note: You must separate your program into three files: header file (*.h), implementation file (*.cpp) and main file (*.cpp). Write a program to build an AVL tree by accepting the integers input from users. For each input, balance the tree and display it on the screen; you then calculate the indorder traversals as well. There should be a menu to drive the program. It should be similar as follows: AVL Tree Implementation A: Insert an integer to tree and show the balanced tree at each insertion. B: Display the balanced tree and show inorder traversal. C: Exit To be sure, your program is correctly working, use the following data to test AVL tree: 15, 18, 10, 7, 57, 6, 13, 12, 9, 65, 19, 16, 23 You should perform more test with different data sets.arrow_forwardcan you give me the java code to this problemarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





