def remove(self, key):
if self._root is None:<EOL><INDENT>raise KeyError(str(key))<EOL><DEDENT>head = Node()  <EOL>node = head<EOL>node.right = self._root<EOL>parent = None<EOL>grand_parent = None<EOL>found = None  <EOL>direction = <NUM_LIT:1><EOL>while node[direction] is not None:<EOL><INDENT>last = direction<EOL>grand_parent = parent<EOL>parent = node<EOL>node = node[direction]<EOL>direction = <NUM_LIT:1> if (self._cmp(self._cmp_data, node.key, key) < <NUM_LIT:0>) else <NUM_LIT:0><EOL>if self._cmp(self._cmp_data, key, node.key) == <NUM_LIT:0>:<EOL><INDENT>found = node<EOL><DEDENT>if not RBTree.is_red(node) and not RBTree.is_red(node[direction]):<EOL><INDENT>if RBTree.is_red(node[<NUM_LIT:1> - direction]):<EOL><INDENT>parent[last] = RBTree.jsw_single(node, direction)<EOL>parent = parent[last]<EOL><DEDENT>elif not RBTree.is_red(node[<NUM_LIT:1> - direction]):<EOL><INDENT>sibling = parent[<NUM_LIT:1> - last]<EOL>if sibling is not None:<EOL><INDENT>if (not RBTree.is_red(sibling[<NUM_LIT:1> - last])) and (not RBTree.is_red(sibling[last])):<EOL><INDENT>parent.red = False<EOL>sibling.red = True<EOL>node.red = True<EOL><DEDENT>else:<EOL><INDENT>direction2 = <NUM_LIT:1> if grand_parent.right is parent else <NUM_LIT:0><EOL>if RBTree.is_red(sibling[last]):<EOL><INDENT>grand_parent[direction2] = RBTree.jsw_double(parent, last)<EOL><DEDENT>elif RBTree.is_red(sibling[<NUM_LIT:1>-last]):<EOL><INDENT>grand_parent[direction2] = RBTree.jsw_single(parent, last)<EOL><DEDENT>grand_parent[direction2].red = True<EOL>node.red = True<EOL>grand_parent[direction2].left.red = False<EOL>grand_parent[direction2].right.red = False<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if found is not None:<EOL><INDENT>found.key = node.key<EOL>found.value = node.value<EOL>parent[int(parent.right is node)] = node[int(node.left is None)]<EOL>node.free()<EOL>self._count -= <NUM_LIT:1><EOL><DEDENT>self._root = head.right<EOL>if self._root is not None:<EOL><INDENT>self._root.red = False<EOL><DEDENT>if not found:<EOL><INDENT>raise KeyError(str(key))<EOL><DEDENT>
T.remove(key) <==> del T[key], remove item <key> from tree.