flopex
flopex

Reputation: 454

How do I remove the leaves of a binary tree?

I'm trying to remove all of the leaves. I know that leaves have no children, this is what I have so far.

 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);

}

Upvotes: 3

Views: 16757

Answers (8)

Aman Thakur
Aman Thakur

Reputation: 59

Easy method with recusrion .

 public static Node removeLeaves(Node root){
          if (root == null) { 
            return null; 
        } 
        if (root.left == null && root.right == null) { 
            return null; 
        } 

        root.left = removeLeaves(root.left); 
        root.right = removeLeaves(root.right); 

        return root;
  }

Upvotes: 1

Ishi
Ishi

Reputation: 1

This should work-

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;

}

Upvotes: 0

abhineet
abhineet

Reputation: 225

 /* @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);
        }
    }

}

Upvotes: 0

Prateek Joshi
Prateek Joshi

Reputation: 4067

Here's a simple java method to delete leaf nodes from binary tree

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;
    }

}

Upvotes: 1

polygenelubricants
polygenelubricants

Reputation: 383686

It's much easier if you break this down like this:

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 and removeRightChild should be trivial to implement.

Upvotes: 5

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

Instead of n = null, it should be:

if(n.parent != null)
  {
    if(n.parent.left == n)
    {
      n.parent.left = null;
    } 
    else if(n.parent.right == n)
    {
      n.parent.right == null);
    }
  }

Upvotes: 3

Petar Minchev
Petar Minchev

Reputation: 47363

Since Java passes references by values n = null; simply does not work. With this line n was pointing to the leaf and now points to nothing. So you aren't actually removing it from the parent, you are just rerouting a dummy local reference. For the solution do what Matthew suggested.

Upvotes: 1

Heinzi
Heinzi

Reputation: 172200

n = null; won't help you, since n is just a local variable of your function. Instead, you'd need to set n.left = null; or n.right = null; on the parent.

I won't give you a complete solution, since this smells a lot like homework, but you could, for example, add a return value to your function to indicate whether the node in question is a leaf or not and take appropriate actions in the parent (after the call to removeLeaves).

Upvotes: 7

Related Questions