Question

A binary search tree(BST) relies on the property that keys that are less than the parent...

A binary search tree(BST) relies on the property that keys that are less than the parent are found in the left subtree, and keys that are greater than the parent are found in the right subtree.

Implement a BST with the following basic components

1. Create a BST for a list of data (= 10, 5, 8, 2, 4, 12, 11, 4, 9, 15)[ use insert(value) function\

2. Print the values in inorder, preorder, and post order

Please code in Python 3

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

Code for the above question in python :

    class Node:
        def __init__(self,key):
            self.left = None
            self.right = None
            self.val = key
   
    # A utility function to insert a new node with the given key
    def insert(root,node):
        if root is None:
            root = node
        else:
            if root.val < node.val:
                if root.right is None:
                    root.right = node
                else:
                    insert(root.right, node)
            else:
                if root.left is None:
                    root.left = node
                else:
                    insert(root.left, node)
   
    # A function to do inorder tree traversal
    def printInorder(root):
   
        if root:
   
            # First recur on left child
            printInorder(root.left)
   
            # then print the data of node
            print(root.val,end=' ')
   
            # now recur on right child
            printInorder(root.right),
   
   
   
    # A function to do postorder tree traversal
    def printPostorder(root):
   
        if root:
   
            # First recur on left child
            printPostorder(root.left)
   
            # the recur on right child
            printPostorder(root.right)
   
            # now print the data of node
            print(root.val,end=' '),
   
   
    # A function to do preorder tree traversal
    def printPreorder(root):
   
        if root:
   
            # First print the data of node
            print(root.val,end=' ') ,
   
            # Then recur on left child
            printPreorder(root.left)
   
            # Finally recur on right child
            printPreorder(root.right)
   
    # data = 10, 5, 8, 2, 4, 12, 11, 4, 9, 15
   
    # Driver code
    root = Node(10)
    insert(root,Node(5))
    insert(root,Node(8))
    insert(root,Node(2))
    insert(root,Node(4))
    insert(root,Node(12))
    insert(root,Node(11))
    insert(root,Node(4))
    insert(root,Node(9))
    insert(root,Node(15))
   
   
    print("Preorder traversal of binary tree is")
    printPreorder(root)
   
    print("\nInorder traversal of binary tree is")
    printInorder(root)
   
    print("\nPostorder traversal of binary tree is")
    printPostorder(root)

Screenshot of tested code:

Please upvote if you find the answer useful.

Add a comment
Know the answer?
Add Answer to:
A binary search tree(BST) relies on the property that keys that are less than the parent...
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 python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

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

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

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

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • Can someone help with these two problems? The following binary tree contains the characters 'A' through...

    Can someone help with these two problems? The following binary tree contains the characters 'A' through 'G' in its nodes. List the nodes in the order that they are visited by: A preorder traversal. An inorder traversal. A postorder traversal. The binary tree in Problem 2 is a Binary Search Tree since for every node, all elements stored in the left subtree of the node are smaller than the element in the node and all elements stored in the right...

  • QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first...

    QUESTION 8   In the _____ traversal, the root is processed first, before its subtrees. breadth first preorder postorder inorder 0.10000 points    QUESTION 9 What kind of traversal does the following algorithm (in pseudo-code) describe? Algorithm traversal (root) if (root is not null)      traversal (leftSubTree)      process (root)      traversal (rightSubTree) end if end traversal breadth first preorder inorder postorder 0.10000 points    QUESTION 10 What kind of traversal does the following algorithm (in pseudo-code)  describe? Algorithm traversal (root) if...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

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