How to recursively remove nodes in a binary tree?

Asked

Viewed 146 times

0

I cannot understand and encode the working of the node removal method in a binary tree.

Note: Add and Print (RED) methods have already been implemented.

package trees;

public class binaryTree {

    Node root;

    public void add(char value){
        addNode(root, value);
    }

    public void addNode(Node node, char value){

        if (root == null){
            root = new Node(value);
        }else{
            if (value < node.getValue()){
                if (node.getLeft() != null){
                    addNode(node.getLeft(), value);
                }else {
                    node.setLeft(new Node(value));
                }
            }else{
                if (value > node.getValue()){
                    if (node.getRight() != null){
                        addNode(node.getRight(), value);
                    }else{
                        node.setRight(new Node(value));
                    }
                }

            }

        }

    }

    public void printTreeRED(){
        print(root);
    }

    private void print(Node node){
        if (node != null){
            System.out.println(node.getValue() + " ");
            print(node.getLeft());
            print(node.getRight());
        }
    }
  • What have you already implemented ? I only see methods to add and show.

  • I am first trying to do the methods, then implement. And I locked in the removal method.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.