Complete the code provided below, so that it : Accepts strings from the user till the user enters "End!", and creates a BST from them with respect to the order of their entry if the user enters "End!" as the first input, then it should: Display "BST needs at least one node, enter a valid input please!" Accepts strings from the user till the user enters "End!" Next, it should display "which node would you pick?", and accept a string (i.e. the key attribute for this Node) from user as an input. Then, display the distance between the given node from the root of the BST. The distance between root and a node can be calculated by the number of edges between them If the string (ie node) is not found then: Your code should display "Not Found!" Then, your code should display: ' Would you like to choose another node? Y/N' If the user enters 'N' end the program, otherwise let the user choose another node and display the distance for the new node. The program should continue till the user chooses 'N' as a response to the above question.     class Node:     def __init__(self, key):         self.key = key         self.left = None         self.right = None          class BinarySearchTree:     def __init__(self):         self.root = None     def search(self, desired_key):         current_node = self.root         while current_node is not None:             # Return the node if the key matches.             if current_node.key == desired_key:                 return current_node             elif desired_key < current_node.key:                 current_node = current_node.left                          else:                 current_node = current_node.right            # The key was not found in the tree.         return None     def insert(self, node):         # Check if the tree is empty         if self.root is None:             self.root = node         else:             current_node = self.root             while current_node is not None:                  if node.key < current_node.key:                     if current_node.left is None:                         current_node.left = node                         current_node = None                     else:                         current_node = current_node.left                 else:                     if current_node.right is None:                         current_node.right = node                         current_node = None                     else:                         current_node = current_node.right                                   def Distance(self,node):                  # ** Your code goes here **                                                                           if __name__ == '__main__':       # ** Your code goes here **

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Complete the code provided below, so that it :

  • Accepts strings from the user till the user enters "End!", and creates a BST from them with respect to the order of their entry

    • if the user enters "End!" as the first input, then it should:
      • Display "BST needs at least one node, enter a valid input please!"
      • Accepts strings from the user till the user enters "End!"
  • Next, it should display "which node would you pick?", and accept a string (i.e. the key attribute for this Node) from user as an input.

  • Then, display the distance between the given node from the root of the BST.

    • The distance between root and a node can be calculated by the number of edges between them
    • If the string (ie node) is not found then:
      • Your code should display "Not Found!"
  • Then, your code should display: ' Would you like to choose another node? Y/N'

    • If the user enters 'N' end the program, otherwise let the user choose another node and display the distance for the new node.
  • The program should continue till the user chooses 'N' as a response to the above question.

 

 


class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None
        
class BinarySearchTree:
    def __init__(self):
        self.root = None

    def search(self, desired_key):
        current_node = self.root
        while current_node is not None:
            # Return the node if the key matches.
            if current_node.key == desired_key:
                return current_node
            elif desired_key < current_node.key:
                current_node = current_node.left
            
            else:
                current_node = current_node.right
  
        # The key was not found in the tree.
        return None
    def insert(self, node):

        # Check if the tree is empty
        if self.root is None:
            self.root = node
        else:
            current_node = self.root
            while current_node is not None: 
                if node.key < current_node.key:
                    if current_node.left is None:
                        current_node.left = node
                        current_node = None
                    else:
                        current_node = current_node.left
                else:
                    if current_node.right is None:
                        current_node.right = node
                        current_node = None
                    else:
                        current_node = current_node.right
                        
    
    def Distance(self,node):
        
        # ** Your code goes here **
                

                                   
                    
if __name__ == '__main__':  

    # ** Your code goes here **

21.13 LAB: Distance in BST
Complete the code provided below, so that it :
Accepts strings from the user till the user enters "End!", and creates a BST from them with respect to the order of their entry
o if the user enters "End!" as the first input, then it should:
- Display "BST needs at least one node, enter a valid input please!"
· Accepts strings from the user till the user enters "End!"
• Next, it should display "which node would you pick?", and accept a string (i.e. the key attribute for this Node) from user as an input.
• Then, display the distance between the given node from the root of the BST.
o The distance between root and a node can be calculated by the number of edges between them
o If the string (ie node) is not found then:
- Your code should display "Not Found!"
• Then, your code should display: 'Would you like to choose another node? Y/N'
o If the user enters 'N' end the program, otherwise let the user choose another node and display the distance for the new node.
• The program should continue till the user chooses 'N' as a response to the above question.
For example, for the following input:
apple
banana
melon
End!
orange
Y
melon
N
the output should be:
Which node would you pick?
Not Found!
Would you like to choose another node? Y/N
Which node would you pick?
Would you like to choose another node? Y/N
377344.2021528.qx3zgy7
Transcribed Image Text:21.13 LAB: Distance in BST Complete the code provided below, so that it : Accepts strings from the user till the user enters "End!", and creates a BST from them with respect to the order of their entry o if the user enters "End!" as the first input, then it should: - Display "BST needs at least one node, enter a valid input please!" · Accepts strings from the user till the user enters "End!" • Next, it should display "which node would you pick?", and accept a string (i.e. the key attribute for this Node) from user as an input. • Then, display the distance between the given node from the root of the BST. o The distance between root and a node can be calculated by the number of edges between them o If the string (ie node) is not found then: - Your code should display "Not Found!" • Then, your code should display: 'Would you like to choose another node? Y/N' o If the user enters 'N' end the program, otherwise let the user choose another node and display the distance for the new node. • The program should continue till the user chooses 'N' as a response to the above question. For example, for the following input: apple banana melon End! orange Y melon N the output should be: Which node would you pick? Not Found! Would you like to choose another node? Y/N Which node would you pick? Would you like to choose another node? Y/N 377344.2021528.qx3zgy7
main.py
Load default template...
1
2 class Node:
def _init_(self, key):
self.key = key
self.left = None
3
4
self.right = None
7
8 class BinarySearchTree:
def _init_(self):
9
10
self.root = None
11
12
def search (self, desired_key):
current_node = self.root
while current_node is not None:
# Return the node if the key matches.
if current_node.key == desired_key:
return current_node
elif desired_key < current_node.key:
current_node = current_node.left
13
14
15
16
17
18
19
20
21
else:
22
current_node = current_node.right
23
24
# The key was not found in the tree.
25
return None
26
def insert(self, node):
27
# Check if the tree is empty
if self.root is None:
28
29
30
self.root = node
31
else:
current_node = self.root
while current_node is not None:
if node.key < current_node.key:
if current_node.left is None:
32
33
34
35
36
current_node.left = node
37
current_node =
None
38
else:
39
current_node = current_node.left
40
else:
if current_node.right is None:
current_node.right = node
current_node = None
else:
41
42
43
44
45
current_node = current_node.right
46
47
48
def Distance(self,node):
49
50
# ** Your code goes here **
51
52
53
54
55 if
name ==
_main_':
56
57
# ** Your code goes here **
Transcribed Image Text:main.py Load default template... 1 2 class Node: def _init_(self, key): self.key = key self.left = None 3 4 self.right = None 7 8 class BinarySearchTree: def _init_(self): 9 10 self.root = None 11 12 def search (self, desired_key): current_node = self.root while current_node is not None: # Return the node if the key matches. if current_node.key == desired_key: return current_node elif desired_key < current_node.key: current_node = current_node.left 13 14 15 16 17 18 19 20 21 else: 22 current_node = current_node.right 23 24 # The key was not found in the tree. 25 return None 26 def insert(self, node): 27 # Check if the tree is empty if self.root is None: 28 29 30 self.root = node 31 else: current_node = self.root while current_node is not None: if node.key < current_node.key: if current_node.left is None: 32 33 34 35 36 current_node.left = node 37 current_node = None 38 else: 39 current_node = current_node.left 40 else: if current_node.right is None: current_node.right = node current_node = None else: 41 42 43 44 45 current_node = current_node.right 46 47 48 def Distance(self,node): 49 50 # ** Your code goes here ** 51 52 53 54 55 if name == _main_': 56 57 # ** Your code goes here **
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY