Git Product home page Git Product logo

Comments (5)

ekurz20 avatar ekurz20 commented on June 9, 2024 1

Yea, I answered my own question. I changed the class find function to:

def find(self, value):
        if self.root:
            return BST._find(value, self.root)
        else:
            return False

And got the desired results.

from cmc-csci046.

mikeizbicki avatar mikeizbicki commented on June 9, 2024

Great questions!

  1. You should always use super inside the __init__ function for a subclass, so you're second version is correct.

  2. You are correct that the function should return False instead of None. It returns None because any function that ends without a return statement implicitly returns None. You can fix this by adding an else statement after your last if within _find that returns False.

    I thought I had a not in the comments about this, but I just looked and it looks like it got deleted while I was editing my solution code. Sorry about that.

PS. I made some minor edits to your comment for formatting, but overall it was a great example of a well formatted comment.

from cmc-csci046.

ekurz20 avatar ekurz20 commented on June 9, 2024

Thanks for the reply!

Regarding the 2nd question, I added an else statement (see below), and it still returns None with the same Input and Output code as above.

def _find(value, node):
        if value > node.value and node.right: #and node right exists
            return BST._find(value,node.right)
        elif value < node.value and node.left:
            return BST._find(value,node.left) #I removed the self. in front of _find
        if value == node.value:
            return True
        else:
            return False

This is what my class find function is:

def find(self, value):
        if self.root:
            if BST._find(value, self.root):
                return True
        else:
            return False

Do I need to change the return True to something else in the nested if statement?

Thanks, Ethan

from cmc-csci046.

jbernardez22 avatar jbernardez22 commented on June 9, 2024

I also had a couple questions on homework 10 the BST.py file if anyone can help!

  1. I'm getting a NameError: name 'root' is not defined when I run what I currently have and am not sure how to adjust it.
class BST(BinaryTree):
    '''
    FIXME:
    BST is currently not a subclass of BinaryTree.
    You should make the necessary changes in the class declaration line above 
    and in the constructor below.
    '''

    def __init__(self, xs=None):
        '''
        FIXME:
        If xs is a list (i.e. xs is not None),
        then each element of xs needs to be inserted into the BST.
        '''
        super().__init__(root)
  1. I am having trouble testing my code because it is not even running and I am getting the error below. Is this an issue with how Iā€™m testing this or an issue somewhere in my code and how can I approach this?

Screen Shot 2020-04-06 at 9 58 23 PM

The error is referencing the line:

from Trees.BinaryTree import BinaryTree, Node

from cmc-csci046.

mikeizbicki avatar mikeizbicki commented on June 9, 2024

@jbernardez22

  1. The root variable in super().__init__(root) should just be self. That is, the full command should be super().__init__().

  2. You should run the python3 command from the ~/trees folder instead of ~/trees/Trees. Then, you can import the BST.py file with the command import Trees.BST.

from cmc-csci046.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.