In: Computer Science
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)
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()