python - How do you get a minimum value from a binary search tree -
i have created binary search class struggling create minimum function find smallest value in binary tree.
class binarysearchtree: def __init__(self, data): self.data = data self.left = none self.right = none def insert(self, new_data): if new_data == self.data: return elif new_data < self.data: if self.left == none: self.left = binarysearchtree(new_data) else: self.left.insert(new_data) else: if self.right == none: self.right = binarysearchtree(new_data) else: self.right.insert(new_data) def create_string(self,spaces): info = ' ' * spaces + str(self.data) if self.left != none: info += '\n(l)' + self.left.create_string(spaces + 4) if not self.right == none: info += '\n(r)' + self.right.create_string(spaces + 4) return info def __str__(self): representation = self.create_string(0) return representation def get_left(self): return self.left def get_right(self): return self.right def get_data(self): return self.data def minimum(tree): if tree.left: return minimum(tree.get_left) else: return tree.get_left
so wrote minimum function reason returns nonetype error. have idea how get_left on binary tree until there no more nodes
non-recursive solution
def minimum(tree): while tree.left not none: tree = tree.left return tree.data
Comments
Post a Comment