Https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-trees/lessons/learn-trees-les/exercises/trees-iii

I’m really confused about step #3 here. It says, " Iterate through self.children and add to the new_children list any item that is not the same as the child_node argument."

What does it mean when it says “any item that is not the same as the child_node argument”?

There is nothing in this particular implementation of a tree that prevents having multiple identical child nodes. This simply ensures that all are removed.

(root)A->B->C->B->D->E->B
remove(B)
(root)A->C->D->E
1 Like

But aren’t all children in the self.chidren list by definition a child_node? How can you be apart of the children list and not be a child node?

(root) is A

children = [B,C,B,D,E,B]

We want to implement remove_child(B)

The method shown is to create a list new_children that contains every member of children except B. (This is what is meant by “any item that is not the same as the child_node argument.”)

… giving new_children == [C,D,E]
… and then assign that list to children, in other words, replace children with the new_list, via the statement
self.children = new_children

1 Like

So being a child_node means you have duplicates? Still really confused here. All I want to know is the difference between a child and a child_node and when or whether or not they overlap. Thanks

Child & child node are the same. There can be any number of duplicates, if you appended them in the first place. To the example node A, above, I appended three instances of child B, then had to remove them all with the command remove_child(B).

Then why does child != child_node appear in the code?

child is just an iteration variable; child_node is a specific node, the one that we are trying to get rid of, specified as an argument in the function call.

The iteration variable can have any name at all. This would work fine:

    for aardvark in self.children:
        if aardvark != child_node:
            new_children.append(aardvark)

They just chose “child” because it is common practice to use as an iteration variable the singular version of the collective noun naming the list: for animal in animals:, or for month in months:, etc.

Ok so the way that I understand it is that if a variable in the list, self.children, is not a child node, it will be added to the new children list…is that right?

I guess where I’m confused is that shouldn’t all the variables be child nodes? Otherwise, why would they be in the self.children list in the first place?

Or is it saying that it is only a child node if it is linked to the root? Once it has been removed, it is no longer a child node…is this right?

Thank you btw

Everything in the children list is a child node (a thing). That’s what children is: a list of child nodes, nothing more nor less.

When the remove_node() method is called, one of those nodes is called child_node, a variable name designating the specific child node to be targeted in the removal.

This method is no more complicated than an assignment to “create a function that, given a list of letters, returns a new list consisting of the original list less every instance of a specific letter…” Just do that, and you’ll have it.

That’s what I’m saying: if they are all child nodes, then using a statement like “If child != child_node:” is completely useless because it wouldn’t apply to a single one of them

def remove_letter(letter_list, letter_to_be_removed):
    new_list = []
    for letter in letter_list:
        if letter != letter_to_be_removed:
            new_list.append(letter)
    return new_list


ltrs =  ["B", "C", "B", "D", "E", "B"]

print(remove_letter(ltrs, "B"))` 

# Output:
['C', 'D', 'E']

Does it work?
Now, substitute:

letter_to_be_removed ==> child_node
letter ==> child
letter_list ==> children

Ok I think I get it, the child_node variable is referring to whichever child node is getting removed. The way the code was used to remove the specific node was confusing - by adding every node to a new list except the one being called for and then redefining the new list with the name of the old list.

Thanks alot

1 Like