Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.

Question

In algorithms/python, for Binary Search Trees, given the below class definitions for Node and Tree class, complete the **find_min function** to return the node which has **the minimum value in the BST:**

```
class Node:
def __init__(self, data):
self.data = data
self.right_child = None
self.left_child = None
class Tree:
def __init__(self):
self.root = None
# returns pointer to node with min value
def find_min(self):
#### IMPLEMENT THIS FUNCTION BODY #####
```

Answer

```
def find_min(self):
current = self.root
while current and current.left_child:
current = current.left_child
return current
```

^^^ the algorithm for above function is basically based on fact that in BST, by design, the smallest value is the left most node (just like the largest is right most node)^^^ note that the "while current" part of "while current and current.left_child) is there to take care of case that self.root is None

Question

In algorithms/python, for Binary Search Trees, given the below class definitions for Node and Tree class, complete the **find_min function** to return the node which has **the minimum value in the BST:**

```
class Node:
def __init__(self, data):
self.data = data
self.right_child = None
self.left_child = None
class Tree:
def __init__(self):
self.root = None
# returns pointer to node with min value
def find_min(self):
#### IMPLEMENT THIS FUNCTION BODY #####
```

Answer

?

Question

In algorithms/python, for Binary Search Trees, given the below class definitions for Node and Tree class, complete the **find_min function** to return the node which has **the minimum value in the BST:**

```
class Node:
def __init__(self, data):
self.data = data
self.right_child = None
self.left_child = None
class Tree:
def __init__(self):
self.root = None
# returns pointer to node with min value
def find_min(self):
#### IMPLEMENT THIS FUNCTION BODY #####
```

Answer

```
def find_min(self):
current = self.root
while current and current.left_child:
current = current.left_child
return current
```

^^^ the algorithm for above function is basically based on fact that in BST, by design, the smallest value is the left most node (just like the largest is right most node)^^^ note that the "while current" part of "while current and current.left_child) is there to take care of case that self.root is None

If you want to change selection, open document below and click on "Move attachment"

status | not learned | measured difficulty | 37% [default] | last interval [days] | |||
---|---|---|---|---|---|---|---|

repetition number in this series | 0 | memorised on | scheduled repetition | ||||

scheduled repetition interval | last repetition or drill |

Do you want to join discussion? Click here to log in or create user.