Change the insert and _insert methods accordingly to comply with the new format. - If a new word is inserted that has a matching sorted order of its letters as an existing Node, do not insert a new Node. Instead, modify the existing Node and append the inserted word into the list.   Starter Code class Node:     def __init__(self, value):         self.value = value         self.left = None         self.right = None              def __str__(self):         return ("Node({})".format(self.value))      __repr__ = __str__ class BinarySearchTree:     '''         >>> x=BinarySearchTree()         >>> x.insert('mom')           >>> x.insert('omm')          >>> x.insert('mmo')          >>> x.root                   Node({'mmo': ['mom', 'omm', 'mmo']})         >>> x.insert('sat')         >>> x.insert('kind')         >>> x.insert('ats')          >>> x.root.left         Node({'ast': ['sat', 'ats']})         >>> x.root.right is None         True         >>> x.root.left.right         Node({'dikn': ['kind']})     '''     def __init__(self):         self.root = None     # Modify the insert and _insert methods to allow the operations given in the PDF     def insert(self, value):         if self.root is None:             self.root=Node(value)         else:             self._insert(self.root, value)     def _insert(self, node, value):         if(value

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Change the insert and _insert methods accordingly to comply with the new format.

- If a new word is inserted that has a matching sorted order of its letters as an existing Node, do not insert a new Node. Instead, modify the existing Node and append the inserted word into the list.

 

Starter Code

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        
    def __str__(self):
        return ("Node({})".format(self.value)) 

    __repr__ = __str__


class BinarySearchTree:
    '''
        >>> x=BinarySearchTree()
        >>> x.insert('mom')  
        >>> x.insert('omm') 
        >>> x.insert('mmo') 
        >>> x.root          
        Node({'mmo': ['mom', 'omm', 'mmo']})
        >>> x.insert('sat')
        >>> x.insert('kind')
        >>> x.insert('ats') 
        >>> x.root.left
        Node({'ast': ['sat', 'ats']})
        >>> x.root.right is None
        True
        >>> x.root.left.right
        Node({'dikn': ['kind']})
    '''

    def __init__(self):
        self.root = None


    # Modify the insert and _insert methods to allow the operations given in the PDF
    def insert(self, value):
        if self.root is None:
            self.root=Node(value)
        else:
            self._insert(self.root, value)


    def _insert(self, node, value):
        if(value<node.value):
            if(node.left==None):
                node.left = Node(value)
            else:
                self._insert(node.left, value)
        else:   
            if(node.right==None):
                node.right = Node(value)
            else:
                self._insert(node.right, value)

 

Modify the insert and _insert method

Once all the changes are complete, you should be able to run the following lines of code and get
the following output:
>>> x=BinarySearchTree()
>> x.insert('mom')
>>> x.insert('omm')
>>> x.insert( 'mmo')
>>> x.root
Node ({ 'mmo': ['mom', 'omm', 'mmo']})
Modified functions
insert(self, value)
Inserts a word into the binary search tree. Words that have the same letter are saved together in a
list, in a dictionary. The key of the dictionary is the sorted order of all the letters in the word.
You will have to modify the _insert helper method as well.
Input
value Word to insert into the binary search tree
str
Transcribed Image Text:Once all the changes are complete, you should be able to run the following lines of code and get the following output: >>> x=BinarySearchTree() >> x.insert('mom') >>> x.insert('omm') >>> x.insert( 'mmo') >>> x.root Node ({ 'mmo': ['mom', 'omm', 'mmo']}) Modified functions insert(self, value) Inserts a word into the binary search tree. Words that have the same letter are saved together in a list, in a dictionary. The key of the dictionary is the sorted order of all the letters in the word. You will have to modify the _insert helper method as well. Input value Word to insert into the binary search tree str
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education