简体   繁体   中英

Need help finishing binary tree python code

You will be adding to the classes Node and Tree that we developed in our lectures. There are several short methods that you will have to write.

Write a method is_similar() that takes as input two binary trees and returns true if the nodes have the same key values and are arranged in the same order and false otherwise.

def is_similar (self, pNode):

Write a method print_level() that takes as input the level and prints out all the nodes at that level. If that level does not exist for that binary search tree it prints nothing. Use the convention that the root is at level 1.

def print_level (self, level):

Write a method get_height() that returns the height of a binary tree. Recall that the height of a tree is the longest path length from the root to a leaf.

def get_height (self):

Write a method num_nodes() that returns the number of nodes in the left subtree from the root and the number of nodes in the right subtree from the root and the root itself. This function will be useful to determine if the tree is balanced.

def num_nodes (self):

Input: The input will read from a file. The file will be formatted as follows:

Line 1: Several integers separated by spaces to be inserted into Tree 1

Line 2: Several integers separated by spaces to be inserted into Tree 2 You will read both lines of data. Create two Trees and insert the integers in the order given. Then you will use these two trees to test the methods that you have written.

Output: The output will be formatted as follows:

The Trees are similar: (True or False)

Levels of Tree 1:

print each level on its own line

Levels of Tree 2:

print each level on its own line

Height of Tree 1: Nodes in Tree 1: Height of Tree 2: Nodes in Tree 2: For example, given the following input file:

14 17 1 14 17 1 This would be the output: The Trees are similare: True

Levels of Tree 1: 14 1 17

Levels of Tree 2: 14 1 17

Height of Tree 1: 1 Nodes in Tree 1: 3 Height of Tree 2: 1 Nodes in Tree 2: 3

You will be writing helper methods for the Tree class that we developed. The following is the outline of the code that you will be submitting. You may include other functions that we developed for completeness. You may add helper functions as needed.

Below is the code that I need help finishing. Not entirely sure how to start the helper functions or main so any help would be appreciated.

import os

class Node (object):
    def __init__ (self, data):
        self.data = data
        self.lchild = None
        self.rchild = None

class Tree (object):
    def __init__ (self):
        self.root = None

  # insert data into the tree
    def insert (self, data):
        new_node = Node (data)

        if (self.root == None):
            self.root = new_node
            return
        else:
            current = self.root
            parent = self.root

            while (current != None):
                parent = current
                if (data < current.data):
                    current = current.lchild
                else:
                    current = current.rchild

            # found location now insert node
            if (data < parent.data):
                parent.lchild = new_node
            else:
                parent.rchild = new_node

    # Returns true if two binary trees are similar
    def is_similar (self, pNode):
        pass

    # Prints out all nodes at the given level
    def print_level (self, level):
        pass

    # Returns the height of the tree
    def get_height (self): 
        pass

    # Returns the number of nodes in tree which is 
    # equivalent to 1 + number of nodes in the left 
    # subtree + number of nodes in the right subtree
    def num_nodes (self):
        pass

def main():
    # write code here

main()

As a hint, think of how you would need to traverse the binary tree in the implementation of each helper method.

For num_nodes , I am not sure what "and the number of nodes in the right subtree from the root and the root itself." means. Should we return the number of nodes in the right subtree + 1?

@classmethod
def count_below(node):
    count=0
    if (node == None):
        return 0 # if one of the root's childs was None

    if (node.lchild == None and node.rchild == None): # leaf
        return 1 # base case

    if (node.lchild != None):
        count+=count_below(node.lchild)
    if (node.rchild != None):
        count+=count_below(node.rchild)

    return count


def num_nodes(self):
    if (self.root == None):
        return 0
    return count_below(self.root.lchild), count_below(self.root.rchild) + 1

@classmethod
def depth_below(node): 
    if node is None: 
        return 0 # base case  


    # Compute the depth of each subtree 
    ldepth = depth_below(node.lchild) # recurse left
    rdepth = depth_below(node.rchild) # recurse right

    # once all the recursive calls performed on this node's childs resolve
    # return the depth of the subtree of this node with the greater depth
    if (ldepth > rdepth): 
        return ldepth+1
    else: 
        return rdepth+1

def get_height(self):
    return depth_below(self.root) # depth from root

@classmethod
def explore_childs(node, current_level, target_level):
    if (node.lchild==None and node.rchild==None):
        return # base case

    if (current_level == target_level):
        if (node.lchild!=None):
            print(node.lchild.data)
        if (node.rchild!=None):
            print(node.rchild.data)
        return # base case

    if (node.lchild!=None):
        explore_childs(node.lchild, current_level+1, target_level) # recurse left
    if (node.rchild!=None):
        explore_childs(node.rchild, current_level+1, target_level) # recurse right

def print_level(self, level):
    if (level > self.get_height()):
        pass # throw error

    explore_childs(root, 0, level)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM