As discussed in the last slog, mutation may occur in linked lists but it can also occur in trees. Let us consider a class for a binary tree node and a class for a binary search tree, as shown below.
class BTNode:
' ' 'Binary Tree node.
' ' '
def __init__(self, data, left=None, right=None):
' ' ' (BTNode, object, BTNode, BTNode) -> NoneType
Create BTNode (self) with data and children left and right.
' ' '
self.data, self.left, self.right = data, left, right
class BST:
' ' ' Binary search tree.
root: BTNode -- root of binary search tree
Assumptions: -- all data in root.left is less than root.data
-- all data in root.right is more than root.data
-- None indicates an empty tree
' ' '
def __init__(self, root=None):
' ' ' (BST, BTNode or None) -> NoneType
Create BST with BTNode root.
' ' '
self.root = root
In order to mutate this binary search tree, certain methods must be used.
In class, we went over "insert" and "delete", as shown below.
def insert(self, data):
' ' ' (BST, object) -> NoneType
Insert data into this BST, if it is not already in it.
' ' '
return_node = node
if not node:
return_node = BTNode(data)
elif data < node.data:
node.left = insert(node.left, data)
elif data > node.data:
node.right = insert(node.right, data)
else:
pass
return return_node
I found this an important example to work through because most mutations follow this pattern with "if node None", other cases, and "else". Working through this example and then practicing another mutation code are very helpful to understand the pattern in code for mutation.
Overall, binary search tree mutation code does require a bit of practice, especially once the codes become more difficult.
Until next time, mutate trees!
Feel free to leave a comment.
class BTNode:
' ' 'Binary Tree node.
' ' '
def __init__(self, data, left=None, right=None):
' ' ' (BTNode, object, BTNode, BTNode) -> NoneType
Create BTNode (self) with data and children left and right.
' ' '
self.data, self.left, self.right = data, left, right
class BST:
' ' ' Binary search tree.
root: BTNode -- root of binary search tree
Assumptions: -- all data in root.left is less than root.data
-- all data in root.right is more than root.data
-- None indicates an empty tree
' ' '
def __init__(self, root=None):
' ' ' (BST, BTNode or None) -> NoneType
Create BST with BTNode root.
' ' '
self.root = root
In order to mutate this binary search tree, certain methods must be used.
In class, we went over "insert" and "delete", as shown below.
def insert(self, data):
' ' ' (BST, object) -> NoneType
Insert data into this BST, if it is not already in it.
' ' '
return_node = node
if not node:
return_node = BTNode(data)
elif data < node.data:
node.left = insert(node.left, data)
elif data > node.data:
node.right = insert(node.right, data)
else:
pass
return return_node
I found this an important example to work through because most mutations follow this pattern with "if node None", other cases, and "else". Working through this example and then practicing another mutation code are very helpful to understand the pattern in code for mutation.
Overall, binary search tree mutation code does require a bit of practice, especially once the codes become more difficult.
Until next time, mutate trees!
Feel free to leave a comment.
No comments:
Post a Comment