I’m trying to write a function to remove a node from a binary tree. I haven’t coded the function yet, and I am trying to think about the different conditions I should consider for removing a node. I am guessing that the possible conditions are:
-
The node has no children
-
The node has one child
-
The node has 2 children
In each of these cases what would be the algorithm to perform a delete function?
This is something you would find in any standard textbook about algorithms, but let’s suppose you are interested in the unbalanced case (balanced trees usually performs some rebalancing operations called “rotations” after a removal) and you use the “obvious” datastructure (a
tree_nodestructure that holds the value and two pointers to othertree_node):NULL;O(tree_height) = O(n), but it is not a problem (at least in theory) because this would be neverthless the complexity of finding a node.