Question

in python11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node cl

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

Program Code to Copy

class Node:

   

    def __init__(self):

        self.left = None

        self.right = None

        self.data = None

    def __init__(self, data):

        self.left = None

        self.right = None

        self.data = data

# Insert Node

    def insert(self, data):

        if self.data:

            if data < self.data:

                if self.left is None:

                    self.left = Node(data)

                else:

                    self.left.insert(data)

            elif data > self.data:

                if self.right is None:

                    self.right = Node(data)

                else:

                    self.right.insert(data)

        else:

            self.data = data

    def stringMethod(self, root) :

        res = ""

        if root.data is None:

            res = "Empty Tree"

            return res

        else :

            res1 = "\nPreorder: " + str(self.preorderTraversal(root)) + "\n"

            res2 = "Inorder: " + str(self.inorderTraversal(root)) + "\n"

            res3 = "Postorder: " + str(self.postorderTraversal(root)) + "\n"

            res = res1 + res2 + res3

            return res

    def find(self, node, x):

        if (node == None):

            return False

        if (node.data == x):

            return True

        """ then recur on left subtree """

        res1 = self.find(node.left, x)

        """ now recur on right subtree """

        res2 = self.find(node.right, x)

        return res1 or res2

#***************Helper Methods****************************

    # Print the Tree

    def PrintTree(self):

        if self.left:

            self.left.PrintTree()

        print( self.data),

        if self.right:

            self.right.PrintTree()

    # Preorder traversal

    # Root -> Left -> Right

    def preorderTraversal(self, root):

            res = ""

            if root is not None:

                res = res + str(root.data) + " "

                res = res + self.preorderTraversal(root.left)

                res = res + self.preorderTraversal(root.right)

            else:

                res = res + 'N' + " "

            return res

   # Inorder traversal

   # Left -> Root -> Right

    def inorderTraversal(self, root):

        res = ""

        if root:

            res = res + self.inorderTraversal(root.left)

            res = res + str(root.data) + " "

            res = res + self.inorderTraversal(root.right)

        else :

            res = res + 'N' + " "

        return res

    # Postorder traversal

    # Root -> Left -> Right

    def postorderTraversal(self, root):

        res = ""

        if root:

            res = res + self.postorderTraversal(root.left)

            res = res + self.postorderTraversal(root.right)

            res = res + str(root.data) + " "

        else:

            res = res + 'N' + " "

        return res

print("Test Bed for Binary Search Tree Class")

test = input("Enter test Number (1-6):\n")

root = Node(None)

data = [6, 8, 4, 3, 7, 5, 9]

print("Make a Balanced Tree.")

print("Inserting: ", end = " ")

print(data)

for i in range (0, len(data)):

    root.insert(data[i])

print(root.stringMethod(root))

print(root.find(root,5))

Program Screenshots

main.py 1~ class Node: 2 def _init_(self): 3 4 self.left = None self.right = None self.data = None 7 def _init_(self, data):

def stringMethod(self, root) : 31 32 res if root.data is None: res = Empty Tree return res 33 34 35 else: 36 +str(self.preo**** He Lper Methods****** ****** 60 61 # Print the Tree 62 def Print Tree(self): if self.left: self.left.PrintTree () print(main.py 85 # Left -> Root -> Right def inorderTraversal(self, root): 86 87 res o if root: 88 res +self.inorderTraversal(root.

Program Output

Empty Tree with "find" method returning False.

120 121 print(Test Bed for Binary Search Tree Class) 122 test 123 root input(Enter test Number (1-6) :\n) Node(None) 124

Tree with data

120 121 print(Test Bed for Binary Search Tree Class) 122 test = input(Enter test Number (1-6) : \n) 123 Node(None) 124 ro

Add a comment
Know the answer?
Add Answer to:
in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...
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
  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • Binary Search tree Implementation of a BST class that include the following operations: - Insertion, Search,...

    Binary Search tree Implementation of a BST class that include the following operations: - Insertion, Search, Deletion - Traversals: Inorder, Preorder, Postorder using c++

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

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

  • Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10,...

    Binary Search Trees (a) 5 pointsl Insert 5, 12, 7, 1, 6, 3, 13, 2, 10, 11 into an empty binary search tree in the given order. Show the resulting BST after every insertion. (b) 5 points) What are the preorder, inorder, and postorder traversals of the BST you have after (a)? (c) 5 points Delete 2, 7, 5, 6, 11 from the BST you have after (a) in the given order Show the resulting BST after every deletion.

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

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