Binary Search Tree Find minimum not clear

The logic I tried:

 def min_tree_value(self):
        
       while self.left:
            self.left = self.left.left
        
        return self.data 

Actual Python program Logic:

def min_tree_value(self):
        if self.left is None:
            return self.data
        
        return self.left.min_tree_value()

The actual Python program logic is in recursion form. I tried the same logic in While loop()

I'm not sure whether my logic is correct. Do help me to figure out the incorrect logic and point where I'm Wrong.

Upvotes: 0

Views: 180

Answers (2)

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5309

First of all, the question asks about finding the minimum element in a binary tree.

The algorithm you used, will find the minimum element in the Binary Search Tree (as the leftmost element is the minimum).

For finding minimum element in a simple Binary Tree, use the following algorithm:

# Returns the min value in a binary tree 

def find_min_in_BT(root): 
    if root is None: 
        return float('inf') 
    res = root.data 
    lres = find_min_in_BT(root.leftChild) 
    rres = find_min_in_BT(root.rightChild) 
    if lres < res: 
        res = lres 
    if rres < res: 
        res = rres 
    return res 

Additions to the answer after OP changed the question:

The logic for the algorithm you tried is correct, with a small correction in the implementation: self = self.data. Both of them find the leftmost element.

I have also tested both the functions which return the same output:

class Node:

    def __init__(self, data):

        self.left = None
        self.right = None
        self.data = data


    def insert(self, data):

        if self.data:
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
            elif data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
        else:
            self.data = data

    def findval(self, lkpval):
        if lkpval < self.data:
            if self.left is None:
                return str(lkpval)+" Not Found"
            return self.left.findval(lkpval)
        elif lkpval > self.data:
            if self.right is None:
                return str(lkpval)+" Not Found"
            return self.right.findval(lkpval)
        else:
            print(str(self.data) + ' is found')

    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

    def min_tree_value_original(self):
        if self.left is None:
            return self.data
        return self.left.min_tree_value_original()

    def min_tree_value_custom(self):
        
        while self.left:
            self = self.left
        return self.data 
    
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)
root.insert(3)
root.insert(1)
root.insert(0)
root.insert(-1)
root.insert(-2)

print(root.min_tree_value_original())
print(root.min_tree_value_custom())

Output:

-2
-2

Here -2 is the smallest and the leftmost element in the BST.

Upvotes: 0

user2390182
user2390182

Reputation: 73460

Your logic is almost there, but not quite:

def min_tree_value(self):
    node = self
    while node.left:
        # don't change the structure by rebinding node.left,
        # but iterate the tree by moving along nodes!
        node = node.left  
    return node.data 

Note that in the original code, you never reassign self before returning its value, so you always returned the root value.

Upvotes: 1

Related Questions