user4139906
user4139906

Reputation:

java-recursive binary search tree

I've written a boolean insert method that inserts values into a binary search tree which inserts the value if the value is not already there and returns true if so, and returns false if the value is already there so inserts nothing. I am trying to convert this iterative method into all recursion with no loops at all but I am having trouble with figuring out how. Any help is appreciated!

public boolean insert(int value) {
    Node travel= root, prev= null, newNode;
    boolean result= true;

    while (travel != null && travel.data != value) {
      prev= travel;
      if (value < travel.data)
        travel= travel.left;
      else travel= travel.right;
    }

    if (travel != null)
      result= false;
    else
      if (root == null)  
        root= new Node(value);
      else
        if (value < prev.data)
          prev.left= new Node(value);
        else prev.right= new Node(value);

    return result;
  }

Upvotes: 0

Views: 1910

Answers (2)

Nuwan
Nuwan

Reputation: 31

http://www.java2s.com/Code/Java/Collections-Data-Structure/BinaryTree.htm

public class BinarySearchTree {

    private Node root;

    public boolean insert(int value) {
        if (root == null) {
            root = new Node(value);
            return true;
        } else {
            return insert(root, value);
        }
    }

    private boolean insert(Node node, int value) {
        if (value < node.value) {
            if (node.left != null) {
                return insert(node.left, value);
            } else {
                node.left = new Node(value);
                return true;
            }

        } else if (value > node.value) {
            if (node.right != null) {
                return insert(node.right, value);
            } else {
                node.right = new Node(value);
                return true;
            }

        } else {
            return false;
        }
    }

    public void printInOrder(Node node) {
        if (node != null) {
            printInOrder(node.left);
            System.out.println("Traversed " + node.value);
            printInOrder(node.right);
        }
    }

    public static void main(String[] args) {
        BinarySearchTree t = new BinarySearchTree();
        System.out.println("insert 5: " + t.insert(5));
        System.out.println("insert 4: " + t.insert(4));
        System.out.println("insert 7: " + t.insert(7));
        System.out.println("insert 4: " + t.insert(4));
        t.printInOrder(t.root);
    }
}

class Node {

    Node left;
    Node right;
    int value;

    public Node(int value) {
        this.value = value;
    }
}

Upvotes: 2

chiwangc
chiwangc

Reputation: 3577

You may try the following:

public boolean insert(int value) {
    return insert(value, root);
}

public boolean insert(int value, Node explore) {

    if (explore != null) {
        if (value < explore.data) {
            if (explore.left != null) {
                return insert(value, explore.left);
            } else {
                explore.left = new Node(value);
                return true;
            }
        } else if (value > explore.data) {
            if (explore.right != null) {
                return insert(value, explore.right);
            } else {
                explore.right = new Node(value);
                return true;
            }
        } else {
            // In this case the value already exists
            return false;
        }
    } else {
        explore = new Node(value);
    }
    return true;                
}

Upvotes: 1

Related Questions