コード例 #1
0
ファイル: IntBST.java プロジェクト: Web5design/CSF_Homework
  public void insertValue(int value) {
    if (rootNode == null) {
      rootNode = new IntNode(value);
    } else {
      IntNode currentNode = rootNode;

      while (currentNode.getValue() != value) {
        if (value > currentNode.getValue()) { // Right child path
          if (currentNode.getRightChild() == null) {
            currentNode.setRightChild(new IntNode(value));
          }
          currentNode = currentNode.getRightChild();
        } else { // Left child path
          if (currentNode.getLeftChild() == null) {
            currentNode.setLeftChild(new IntNode(value));
          }
          currentNode = currentNode.getLeftChild();
        }
      }
    }
  }
コード例 #2
0
ファイル: IntBST.java プロジェクト: Web5design/CSF_Homework
  public boolean search(int value) {
    IntNode currentNode = rootNode;

    while (currentNode != null && (currentNode.getValue() != value)) {
      if (value > currentNode.getValue()) { // Continue down the right child path
        currentNode = currentNode.getRightChild();
      } else { // Continue down the left child path
        currentNode = currentNode.getLeftChild();
      }
    }
    // If the current node exists and its value is equal to the input, return true, else return
    // false
    return currentNode != null && (currentNode.getValue() == value);
  }