コード例 #1
0
  public void addNode(Node n) {
    if (root == null) {
      root = n;
      System.out.println(n.data + " added as the root to the tree");
    } else {

      Node current = root;
      Node parent;
      while (true) {
        parent = current;

        if (n.data < current.data) {
          current = current.lchild;
          if (current == null) {
            parent.lchild = n;
            return;
          }
        } else {
          current = current.rchild;
          if (current == null) {
            parent.rchild = n;
            return;
          }
        }
      }
    }
  }
コード例 #2
0
  private static Node deserialize(Queue<Node> q, Node node) {
    if (node == null) return null;

    node.lchild = deserialize(q, q.remove()); // left storage
    node.rchild = deserialize(q, q.remove()); // right storage

    return node;
  }