Question
TestCodeForAssigment.py
def main():
    # testing recursive find, pre- and post-order, and __len__

    """ creating the following BST:
                           25
                  15                29
            12         20      27        50
                    17 """
    t1 = BST()
    t1.insert_rec(25)
    t1.insert_rec(15)
    t1.insert_rec(12)
    t1.insert_rec(20)
    t1.insert_rec(17)
    t1.insert_rec(29)
    t1.insert_rec(27)
    t1.insert_rec(50)

    print("Let's see if we can find 27 using the non-recursive find: ", t1.find(27))
    print("Let's see if we can find 27 using the recursive find: ", t1.findRecursive(27))

    print("The length of the BST tree is ",t1.__len__())

    print("The preorder traversal of the tree:", list(t1.preOrder()))

    print("The postorder traversal of the tree:", list(t1.postOrder()))

main()

Test your code, you can use for these:TestCodeForAssigment.py
2. Consider the binary search tree from the left side of Figure 7.8 (before the 6 is deleted). List the order that the nodes would be visited for each traversal order (preorder, in-order, and postorder). 3. Write an invariant for the BST class
0 0
Add a comment Improve this question Transcribed image text
Answer #1

binarySearchTree.py

class bst:
def __init__(self,data):
self.left=None
self.right=None
self.data=data
#insert data int BST   
def insert(root,data):
newnode=bst(data)
if root is None:
root=newnode
else:
if root.data < newnode.data:
if root.right is None:
root.right=newnode
else:
root.right.insert(data)
else:
if root.left is None:
root.left=newnode
else:
root.left.insert(data)
  
def inorder(root):
if root:
if root.left is not None:
root.left.inorder()
print(root.data,end=" ")
if root.right is not None:
root.right.inorder()
  
def preOrder(root):
if root:
print(root.data,end=" ")
if root.left is not None:
root.left.inorder()
if root.right is not None:
root.right.inorder()
  
def postOrder(root):
if root:
if root.left is not None:
root.left.inorder()
if root.right is not None:
root.right.inorder()
print(root.data,end=" ")

#find size of BST Recursively   
def size(root):
treeSize=0
if root is not None:
treeSize+=1
  
if root.left is not None:
treeSize+=root.left.size()
if root.right is not None:
treeSize+=root.right.size()
return treeSize
  
#Recursive search in BST
def findRecursive(root,data):
if root:
if(root.data==data):
return True
if root.left is not None and root.data>data:
return root.left.findRecursive(data)
if root.right is not None and root.data < data:
return root.right.findRecursive(data)
return False
  
#Search without Recursion
def find(root,data):
while root is not None:
if(root.data==data):
return True
if root.data>data:
root=root.left
if root.data < data:
root=root.right
return False   

if __name__=="__main__":
root=bst(25)
root.insert(15)
root.insert(20)
root.insert(29)
root.insert(12)
root.insert(17)
root.insert(50)
root.insert(27)
print("The inorder traversal of the tree : ",end=" ")
root.inorder()

print("\nLet's see if we can find 27 using the non-recursive find: ",root.find(27))
print("\nLet's see if we can find 27 using the recursive find: ", root.findRecursive(27))

print("\nThe length of the BST tree is ",root.size())

print("\nThe preorder traversal of the tree:",end=" ")
root.preOrder()

print("\nThe postorder traversal of the tree:",end=" ")
root.postOrder()

# Complex

  

OUTPUT

Python 3.6.5 Shell File Edit Shell Debug Options Window Help Python 3.6.5 (v3.6.5:559c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32 Type copyright, credits or license)for more information. RESTART: C:/Users/PRADY/AppData/Local/Programs/Python/Python36-32/binarySearchTree.py The inorder traversal of the tree12 15 17 20 25 27 29 50 Lets see if we can find 27 using the non-recursive find: True Lets see if we can find 27 using the recursive find: True The length of the BST tree is 8 The preorder traversal of the tree: 25 12 15 17 20 27 29 50 The postorder traversal of the tree: 12 15 17 20 27 29 50 25 Ln: 14 Col: 4 Type here to search 01-11-2018 15

Add a comment
Know the answer?
Add Answer to:
TestCodeForAssigment.py def main(): # testing recursive find, pre- and post-order, and __len__ """ creating the following...
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
  • 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...

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

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

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

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

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

  • hi, I am suppose to implement that method in java for bst but the result doesn't...

    hi, I am suppose to implement that method in java for bst but the result doesn't work as excpected * _Part 8: Implement this method._ * * toString() is a method defined by Java's Object class * that can be used to provide a String representation for your * class. It is called anytime you concatenate an Object to * a String. * * Build a string representation of the BST that describes both * the values stored inside and...

  • Creating a simple graph in C++; need solution ASAP. EDIT: Pls comment letting me know what...

    Creating a simple graph in C++; need solution ASAP. EDIT: Pls comment letting me know what other information you need rather than just "..." Thank you. Here is the assignment: In this final practice problem, you’ll: read a set of data representing a directed, unweighted graph build an in-memory graph structure using the data display the graph using depth-first traversal display the graph using breadth-first traversal Input data - The data consists of records like this: 16 3 15 4...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from...

    Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that...

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