Question

In: Computer Science

Python! Modify the following code to include countries name which should be displaying onto the console...

Python!

Modify the following code to include countries name which should be displaying onto the console from countries with the lowest population to the highest Morroco :38,964, china:1000000000, usa:400,000,000, England:55,000,000.   

class Node:
    def __init__(self,value):
        self.value=value
        self.right=None
        self.left=None
class BinarySearchTree:
    def __init__(self):
        self.root=None
    #adding the element to the bst
    def add(self,value):
        node=Node(value)
        temp=self.root
        flag=1
        while(temp):
            flag=0
            if(node.value>temp.value):
                if(temp.right):
                    temp=temp.right
                else:
                    temp.right=node
                    break
            else:
                if(temp.left):
                    temp=temp.left
                else:
                    temp.left=node
                    break
        if(flag):
            self.root=node
    #pre order traversing
    def preOrder(self,root):
        if(root):
            print(root.value)
            self.preOrder(root.left)
            self.preOrder(root.right)
    #in order traversing
    def inOrder(self,root):
        if(root):
            self.inOrder(root.left)
            print(root.value)
            self.inOrder(root.right)
    #post order traversing
    def postOrder(self,root):
        if(root):
            self.postOrder(root.left)
            self.postOrder(root.right)
            print(root.value)

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

class Node:
    def __init__(self,value):
        self.value=value
        self.right=None
        self.left=None

class BinarySearchTree:
    def __init__(self):
        self.root=None
    #adding the element to the bst
    def add(self,value):
        node=Node(value)
        temp=self.root
        flag=1
        while(temp):
            flag=0
            #the objects need to implement __gt__() method in order to use
            #> operator to compare two values. if this is any built in data
            #types like int, str, float etc, the method is already implemented
            #but if it is a custom class, you should implement __gt__() method
            if(node.value>temp.value):
                if(temp.right):
                    temp=temp.right
                else:
                    temp.right=node
                    break
            else:
                if(temp.left):
                    temp=temp.left
                else:
                    temp.left=node
                    break
        if(flag):
            self.root=node
    
    #methods to perform the appropriate traversal by calling the appropriate recursive
    #methods
    def printPreOrder(self):
        self.preOrder(self.root)

    def printInOrder(self):
        self.inOrder(self.root)

    def printPostOrder(self):
        self.postOrder(self.root)

    #pre order traversing
    def preOrder(self,root):
        if(root):
            print(root.value)
            self.preOrder(root.left)
            self.preOrder(root.right)
    #in order traversing
    def inOrder(self,root):
        if(root):
            self.inOrder(root.left)
            print(root.value)
            self.inOrder(root.right)
    #post order traversing
    def postOrder(self,root):
        if(root):
            self.postOrder(root.left)
            self.postOrder(root.right)
            print(root.value)


''' a class to represent a Country'''
class Country:
    #constructor taking country name and population
    def __init__(self, name, population):
        #assigning values to instance variables
        self.name=name
        self.population=population
    #magic method to implement > operator operation between two Country objects
    #this is required for the above binary search tree to work with Country objects
    #if your BinarySearchTree use < operator in future, then you might want to
    #implement __lt__ operator function also (<)
    def __gt__(self, other):
        #returns True if this country's population is greater than other's
        return self.population>other.population
    #magic method to return a string containing country details. this method gets
    #invoked when a Country object is printed
    def __str__(self):
        return '{}:{}'.format(self.name,self.population)


def main():
    #creating a BinarySearchTree
    tree=BinarySearchTree()
    #adding some Country objects to it
    tree.add(Country('Morroco',38964))
    tree.add(Country('China',1000000000))
    tree.add(Country('USA',400000000))
    tree.add(Country('England',55000000))
    #printing the tree in 'in order' so that countries will be printed according
    #to increasing order of population
    tree.printInOrder()
main()

Related Solutions

PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
PYTHON COMPUTER CODE PROGRAMMING - DISPLAYING A PLOT OF TEMPERATURE VERSUS DEPTH WHEN GIVEN A LATITUDE....
PYTHON COMPUTER CODE PROGRAMMING - DISPLAYING A PLOT OF TEMPERATURE VERSUS DEPTH WHEN GIVEN A LATITUDE. PYTHON ASSIGNMENT. NOT SURE HOW TO PLOT THIS DATA WITH THE GIVEN INFO BELOW: Just as the atmosphere may be divided into layers characterized by how the temperature changes as altitude increases, the oceans may be divided into zones characterized by how the temperature changes as depth increases. We shall divide the oceans into three zones: the surface zone comprises the water at depths...
Write Python code that asks the user for a password from the console and continues asking...
Write Python code that asks the user for a password from the console and continues asking for a password until the user enters a password that has greater than 10 characters. (language python)
Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
Modify the object provided in the code below to include a variety of overloaded operators in...
Modify the object provided in the code below to include a variety of overloaded operators in C++. Your object should have the following member variables: 1. float *arr a. A dynamic float array that constitutes the data in your myArray object. 2. int size a. The size of the array that the myArray object holds Modify the provided code to include a variety of overloaded operators Operators to overload: 1. bool operator!=(myArray& obj2) a. Tests to see if the calling...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
python code You are to write a program which produces a triangle as seen below. •Include...
python code You are to write a program which produces a triangle as seen below. •Include a function named goingUpwhich accepts a value and prints lines of stars beginning at 1 star and ending at the number of stars which was sent to it. For example, if the value sent to it is 4, the function would print this:********** •Include a function named goingDown which accepts a value and prints lines of stars beginning at 1 LESS THAN the value...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Python: You will modify the given code to create a guessing game that takes user input...
Python: You will modify the given code to create a guessing game that takes user input and allows you to make multiple guesses. There should also be a loop Use the following attached Sample Code as a guide. There are mistakes in the code!!! #Guessing Game import random number=random.randint(1, another number) print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT