binary search tree impelementation and java

前端 未结 6 669
离开以前
离开以前 2021-01-03 09:10

I am trying to implement BST algorithm using Cormen\'s pseudo code yet having issue.

Here is my Code for Node:

public class Node {
    Node left;
            


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 09:22

    ...what is up with your delete code? It doesn't make a lot of sense. I would consider rewriting it in a more logical way. Without the meaningless single-letter variable names. And add comments!

    One possible algorithm is:

    Get the parent of the node to delete
    Get the right-most node of the left subtree, or the leftmost node of the right subtree
    Remove the node to delete and replace it with the node you found
    Rebalance the tree
    

    ...or, if you want to hack up this stuff so it's right, I'd start looking at the

    if (x != null){
        Node tmp = getParent(t,x);
        tmp = getParent(t,y);
    }
    

    part, because that's clearly wrong.

提交回复
热议问题