4 ms·
Filtering any data structure means building up a new filtered copy, not removing from the current one, no? >>> x = [1, 2, 3, 4] >>> y = list(filter(lam
by tom_mellior 7y ago
Filtering any data structure means building up a new filtered copy, not removing from the current one, no?
>>> x = [1, 2, 3, 4]
>>> y = list(filter(lambda n: n < 3, x))
>>> x, y
([1, 2, 3, 4], [1, 2])
Same with trees: No removal is needed for filtering, you could implement it something like:
def filter_tree(f, tree):
filtered_tree = new_empty_tree() # it's useful if this is *not* None!
for element in tree:
if f(element):
filtered_tree.add(element)
return filtered_tree
Regardless, removal of individual, even internal, nodes from trees is of course possible without removing entire subtrees. The details depend very much on the actual kind of tree, but you can start at https://en.wikipedia.org/wiki/Binary_tree#Deletion https://en.wikipedia.org/wiki/Binary_tree#Deletion