Java, Binary tree remove method

后端 未结 5 1921
[愿得一人]
[愿得一人] 2020-12-21 13:18

I am trying to write a remove(node cRoot, Object o) function for a sorted binary tree.

Here is what I have so far:

private boolean remov         


        
5条回答
  •  不知归路
    2020-12-21 13:40

    The basic pseudo-code for erasing a node from a sorted tree is pretty simple:

    1. erase the node value
    2. find child node with maximum value
    3. make it the root node
    4. if it had children - goto 2 recursively

    Basically what you are doing is bubbling nodes up the tree, each time the maximum of the children node in each node, so that in the end you stay with a sorted tree, and only one node missing at the end of the full path you went.

    Also - see wikipedia on the subject, they have some sample code in C as well.

提交回复
热议问题