Anúncio
PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf
PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf
PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf
PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf
Anúncio
PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf
Próximos SlideShares
Python programming workshopPython programming workshop
Carregando em ... 3
1 de 5
Anúncio

Mais conteúdo relacionado

Mais de alicesilverblr(20)

Anúncio

PLEASE ANSWER IN PYTHONIn this assignment you will be adding to th.pdf

  1. PLEASE ANSWER IN PYTHON In this assignment you will be adding to the classes Node and Tree that we developed in Binary Search Tree Lecture and testing them. There are several short methods that you will have to write. Write a method range() that returns the range of values stored in a binary search tree of integers. The range of values equals the maximum value in the binary search tree minus the minimum value. If there is one value in the tree the range is 0. If the tree is empty the range is undefined. def range (self): Write a method get level() that takes as input the level and returns a list of all the nodes at that level from left to right. If that level does not exist for that binary search tree return an empty list . Use the convention that the root is at level 0. def get level (self, level): Write a method left side view() when given the root of a binary tree, imagine yourself standing on the left side of it, return the values of the nodes you can see ordered from top to bottom. def left side view (self): Write a method sum leaf nodes() that returns the sum of the value of all leaves. Recall that a leaf node does not have any children. def sum leaf node (self): In this assignment you will be writing helper methods for the Tree class that we developed and test them. The following is the outline of the code that you will be submitting. You may include the other functions that we developed for completeness. Input: 50 30 70 10 40 60 80 7 25 38 47 58 65 77 96 50 30 70 10 40 60 80 7 25 38 47 58 65 77 96 58 77 65 30 38 50 7 25 47 96 80 10 60 70 40 Starter Code
  2. import sys class Node (object): # constructor def __init__(self, data): self.data = data self.lChild = None self.rChild = None def print_node(self, level=0): if self.lChild != None: self.lChild.print_node(level + 1) print(' ' * 3 * level + '->', self.data) if self.rChild != None: self.rChild.print_node(level + 1) def get_height(self): if self.lChild != None and self.rChild != None: return 1 + max(self.lChild.get_height(), self.rChild.get_height()) elif self.lChild != None: return 1 + self.lChild.get_height() elif self.rChild != None: return 1 + self.rChild.get_height() else: return 1 class Tree(object): # constructor def __init__(self): self.root = None def print(self, level): self.root.print_node(level) def get_height(self): return self.root.get_height() # Inserts data into Binary Search Tree and creates a valid BST def insert(self, data): new_node = Node(data) if self.root == None: self.root = new_node return
  3. else: parent = self.root curr = self.root # finds location to insert new node while curr != None: parent = curr if data < curr.data: curr = curr.lChild else: curr = curr.rChild # inserts new node based on comparision to parent node if data < parent.data: parent.lChild = new_node else: parent.rChild = new_node return # Returns the range of values stored in a binary search tree of integers. # The range of values equals the maximum value in the binary search tree minus the minimum value. # If there is one value in the tree the range is 0. If the tree is empty the range is undefined. def range(self): # Returns a list of nodes at a given level from left to right def get_level(self, level): # Returns the list of the node that you see from left side # The order of the output should be from top to down def left_side_view(self): # returns the sum of the value of all leaves. # a leaf node does not have any children. def sum_leaf_nodes(self): def make_tree(data): tree = Tree() for d in data: tree.insert(d) return tree # Develop your own main function or test cases to be able to develop.
  4. # Our tests on the Gradescop will import your classes and call the methods. def main(): # Create three trees - two are the same and the third is different line = sys.stdin.readline() line = line.strip() line = line.split() tree1_input = list(map(int, line)) # converts elements into ints t1 = make_tree(tree1_input) t1.print(t1.get_height()) print("Tree range is: ", t1.range()) print("Tree left side view is: ", t1.left_side_view()) print("Sum of leaf nodes is: ", t1.sum_leaf_nodes()) print("##########################") # Another Tree for test. line = sys.stdin.readline() line = line.strip() line = line.split() tree2_input = list(map(int, line)) # converts elements into ints t2 = make_tree(tree2_input) t2.print(t2.get_height()) print("Tree range is: ", t2.range()) print("Tree left side view is: ", t2.left_side_view()) print("Sum of leaf nodes is: ", t2.sum_leaf_nodes()) print("##########################") # Another Tree line = sys.stdin.readline() line = line.strip() line = line.split() tree3_input = list(map(int, line)) # converts elements into ints t3 = make_tree(tree3_input) t3.print(t3.get_height()) print("Tree range is: ", t3.range()) print("Tree left side view is: ", t3.left_side_view()) print("Sum of leaf nodes is: ", t3.sum_leaf_nodes()) print("##########################") if __name__ == "__main__":
  5. main()
Anúncio