代码之家  ›  专栏  ›  技术社区  ›  flopex

如何删除二叉树的叶子?

  •  3
  • flopex  · 技术社区  · 14 年前

    我想把所有的叶子都去掉。我知道树叶没有孩子,这就是我迄今为止所拥有的。

     public void removeLeaves(BinaryTree n){  
    
        if (n.left == null && n.right == null){
    
            n = null;
    
        }
    
        if (n.left != null)
    
            removeLeaves(n.left);
    
        if (n.right != null)
    
            removeLeaves(n.right);
    
    }
    
    7 回复  |  直到 7 年前
        1
  •  5
  •   polygenelubricants    14 年前

    如果你像这样分解它会容易得多:

    public void removeLeaves(BinaryTree n){
      if (n.left != null) {
        if (n.left.isLeaf()) {
          n.removeLeftChild();
        } else {
          removeLeaves(n.left);
        }
      }
      // repeat for right child
      // ...
    }
    

    isLeaf , removeLeftChild removeRightChild 应该很容易实现。

        2
  •  5
  •   Heinzi    14 年前

    n = null; 不会帮你的,因为 n 只是函数的局部变量。相反,你需要设置 n.left = null; n.right = null; 在父母身上。

    我不会给您一个完整的解决方案,因为这闻起来很像家庭作业,但是您可以,例如,向函数中添加一个返回值,以指示所讨论的节点是否为叶,并在父节点中采取适当的操作(在调用 removeLeaves )

        3
  •  3
  •   Matthew Flaschen    14 年前

    而不是n=null,它应该是:

    if(n.parent != null)
      {
        if(n.parent.left == n)
        {
          n.parent.left = null;
        } 
        else if(n.parent.right == n)
        {
          n.parent.right == null);
        }
      }
    
        4
  •  1
  •   Petar Minchev    14 年前

    由于Java通过值传递引用 n = null; 根本不起作用。用这条线,n指向叶,而现在却指向无。所以实际上并不是从父对象中删除它,而是重新路由一个虚拟的本地引用。为了解决这个问题,按照马修的建议去做。

        5
  •  1
  •   Prateek Joshi    9 年前

    这里有一个简单的例子 爪哇 方法到 删除叶节点 从二叉树

    public BinaryTreeNode removeLeafNode(BinaryTreeNode root) {
        if (root == null)
            return null;
        else {
            if (root.getLeft() == null && root.getRight() == null) {     //if both left and right child are null
                root = null;                                             //delete it (by assigning null)
            } else {
                root.setLeft(removeLeafNode(root.getLeft()));            //set new left node 
                root.setRight(removeLeafNode(root.getRight()));          //set new right node   
            }
            return root;
        }
    
    }
    
        6
  •  0
  •   Prateek Joshi    9 年前
     /* @author abhineet*/
    
    public class DeleteLeafNodes {
    
    
        static class Node{
            int data;
            Node leftNode;
            Node rightNode;
            Node(int value){
                this.data = value;
                this.leftNode = null;
                this.rightNode = null;
            }
        }
    
    
    
        public static void main(String[] args) {
    
            Node root = new Node(1);
            Node lNode = new Node(2);
            lNode.leftNode = new Node(4);
            root.leftNode = lNode;
            Node rNode = new Node(3);
            rNode.rightNode = new Node(5);
            root.rightNode = rNode;
            printTree(root);
            deleteAllLeafNodes(root, null,0);
            System.out.println("After deleting leaf nodes::");
            printTree(root);
    
        }
    
        public static void deleteAllLeafNodes(Node root, Node parent, int direction){
            if(root != null && root.leftNode == null && root.rightNode == null){
                if(direction == 0){
                    parent.leftNode = null;
                }else{
                    parent.rightNode = null;
                }
    
            }
            if(root != null && (root.leftNode != null || root.rightNode != null)){
                deleteAllLeafNodes(root.leftNode, root, 0);
                deleteAllLeafNodes(root.rightNode, root, 1);
            }
    
        }
        public static void printTree(Node root){
            if(root != null){
                System.out.println(root.data);
                printTree(root.leftNode);
                printTree(root.rightNode);
            }
        }
    
    }
    
        7
  •  0
  •   Ishi    7 年前

    这应该管用-

    public boolean removeLeaves(Node n){  
        boolean isLeaf = false;
        if (n.left == null && n.right == null){
            return true;
            //n = null;
        }
    
        if (n!=null && n.left != null){
    
           isLeaf = removeLeaves(n.left);
           if(isLeaf) n.left=null; //remove left leaf
        }
    
        if (n!=null && n.right != null){
    
            isLeaf = removeLeaves(n.right);
            if(b) n.right=null; //remove right leaf
        }
        return false;
    
    }