java-recursive二进制搜索树

时间:2014-10-30 04:49:46

标签: java recursion binary-search-tree

我编写了一个布尔值插入方法,它将值插入到二进制搜索树中,如果值不存在则插入值,如果是,则返回true,如果值已经存在则返回false,因此不插入任何内容。我试图将这个迭代方法转换为所有递归,没有任何循环,但我无法弄清楚如何。任何帮助表示赞赏!

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您可以尝试以下方法:

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