コード例 #1
0
  public static BTNode Insert(BTNode root, int value) {

    BTNode NewNode = new BTNode(value);
    // System.out.print(value);

    if (root == null) {
      root = NewNode;
      return root;
    }

    Queue<BTNode> q = new LinkedList<BTNode>();
    q.offer(root);
    BTNode current = null;
    System.out.println("root: " + root.getData() + " ");

    while (!q.isEmpty()) {
      current = q.poll();
      System.out.print(current.getData() + " ");

      if (current.getLeft() == null) {
        current.setLeft(NewNode);
        return root;
      } else {
        q.offer(current.getLeft());
      }

      if (current.getRight() == null) {
        current.setRight(NewNode);
        return root;
      } else {
        q.offer(current.getRight());
      }
    }
    return root;
  }