3
\$\begingroup\$

Please review my BST Code

1. Class node

class Node:

2. Constructor

def __init__(self, data):
    self.left = None
    self.right = None
    self.data = data 

3. Insert node

def insert(self, data):
    if self.data:
        if data < self.data:
            if not self.left:
                self.left = Node(data)
            else:
                self.left.insert(data)
        elif data > self.data:
            if not self.right :
                self.right = Node(data)
            else:
                self.right.insert(data)
    else:
        self.data = data

4. Node in delete any value

def getMinValue(self,node):
    current = node
    while(current.left is not None):
        current = current.left
    return current
def delValue(self,data):       
    if data < self.data:
        self.left = self.left.delValue(data)
    elif data > self.data:
        self.right = self.right.delValue(data)
    else:
        if self.left is None:
            temp = self.right
            self = None
            return temp
        elif self.right is None:
            temp = self.left
            self = None
            return temp
        temp = self.getMinValue(self.right)
        self.data = temp.data
        self.right = self.right.delValue(temp.data)
    return self

5. Node in search any value

def getSearchValue(self,data):
    if data == self.data:
        return print(self.data,"True")
    if data < self.data:
        if self.left:
            self.left.getSearchValue(data)   
    if data > self.data:
        if self.right:
            self.right.getSearchValue(data)

6. Print tree

def print_tree(self):
    if self.left:
        self.left.print_tree()
    print(self.data)
    if self.right:
        self.right.print_tree()
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review! You are welcome to also include a little bit of example code in your question and also telling the reviewers about your intention. Did you merely write it to learn Python? etc. \$\endgroup\$
    – AlexV
    Commented Sep 25, 2019 at 11:45
  • 1
    \$\begingroup\$ Welcome to Code Review. Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Heslacher
    Commented Sep 25, 2019 at 14:23
  • \$\begingroup\$ Why did you add linked-list as a tag? \$\endgroup\$
    – AlexV
    Commented Sep 26, 2019 at 9:13

2 Answers 2

3
\$\begingroup\$

A few quick points that came to my mind when looking at your code:

Documentation

Your code has no documentation whatsoever. Python has so called documentation strings, which are basically """triple quoted text blocks""" immediately following def whatever(...). Example:

def print_tree(self):
    """Print the content of the tree

    This method performs an in-order traversal of the tree
    """
    # ... your code here

Since your question title indicates that you're working with Python 3, also consider using type hints to document your code.

Naming

There is the infamous Style Guide for Python Code (aka PEP 8) which recommends to use lower_case_with_underscores for variable names and (member) functions. You do this for print_tree, but use camelCase for the other member functions.

Searching the tree

Your getSearchValue function is a little bit awkward in that it always returns None. Although your code promises to "get" the value, you instead print it to the console (together with the string "True") and return the return value of print which is None (aka no return value in that case). Your function also only returns something (other than the implicit None) if the value was found. In my opinion something like

def has_value(self, data):
    """Return True or False indicating whether the value is in the BST"""
    if data == self.data:
        return True
    if data < self.data:
        if self.left is not None:
            return self.left.has_value(data)   
    if data > self.data:
        if self.right is not None:
            return self.right.has_value(data)
    return False

would be a more appropriate approach. As you can see, this function returns an appropriate bool value to signal the result.

Another minor tweak: this implementation uses if ... is not None: to explicitly check for None as signaling value. Since None is a singleton in Python, you should always use is (not) to check for equality.

Unnecessary parentheses

The parentheses around the condition in while(current.left is not None): are not needed. while works the same way as if in that regard. They are sometimes used for longer conditions that span multiple lines, since Python does implicit line joining in that case.


I'm also not fully convinced about your delValue function, but unfortunately I'm a little bit short on time at the moment.

\$\endgroup\$
2
  • \$\begingroup\$ Thank a lot Alex to spent your valuable time to get the review of my code. Let me correct as per your suggestion and get back to you. Today I learned many coding fundamentals and styles from your suggestions. Thanks a lot once again. \$\endgroup\$ Commented Sep 25, 2019 at 12:32
  • \$\begingroup\$ Please remember that it's against the site-policy to change the code in question once you have an answer. If you have significant improvements over the current version, asking a new question is the way to go. \$\endgroup\$
    – AlexV
    Commented Sep 25, 2019 at 12:45
1
\$\begingroup\$
**Code after the suggestion**

1. Class node

class Node:

2. Constructor

'''this is the constructor'''
def __init__(self, data):
    self.left = None
    self.right = None
    self.data = data

3. Insert node

def insert(self, data):
''' this function work is insert the data in bst'''
    if self.data:
        '''insert left side value '''
        if data < self.data:
            if not self.left:
                self.left = Node(data)
            else:
                self.left.insert(data)
            '''insert right side value'''
        elif data > self.data :
            if not self.right :
                self.right = Node(data)
            else:
                self.right.insert(data)
    else:
        self.data = data

4. Node in delete any value

def getMinValue(self,node):
    '''this fuction work is get minimum value'''
    current = node
    while current.left is not None:
        current = current.left
    return current
def delValue(self,data):       
    '''this fuction work delete value'''
    if data < self.data:
        self.left = self.left.delValue(data)
    elif data > self.data:
        self.right = self.right.delValue(data)
    else:
        if self.left is None:
            temp = self.right
            self = None
            return temp
        elif self.right is None:
            temp = self.left
            self = None
            return temp
        temp = self.getMinValue(self.right)
        self.data = temp.data
        self.right = self.right.delValue(temp.data)
    return self

5. Node in search any value

def getSearchValue(self,data):
    '''this function work finde a value in bst or return True or False'''
    if data == self.data:
        return print(True)
    if data < self.data:
        if self.left:
            self.left.getSearchValue(data)
    if data > self.data:
        if self.right:
            self.right.getSearchValue(data)
        return print(False)

6. Print tree

def printTree(self):
    '''this fuction work print a tree'''
    if self.left:
        self.left.printTree()
    print(self.data)
    if self.right:
        self.right.printTree()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.