Ejemplo n.º 1
0
  /**
   * 唯一的构建方法
   *
   * @param _key 值set,必须字典序
   * @param _length 对应每个key的长度,留空动态获取
   * @param _value 每个key对应的值,留空使用key的下标作为值
   * @param _keySize key的长度,应该设为_key.size
   * @return 是否出错
   */
  public int build(List<String> _key, int _length[], int _value[], int _keySize) {
    if (_keySize > _key.size() || _key == null) return 0;

    // progress_func_ = progress_func;
    key = _key;
    length = _length;
    keySize = _keySize;
    value = _value;
    progress = 0;

    resize(65536 * 32); // 32个双字节

    base[0] = 1;
    nextCheckPos = 0;

    Node root_node = new Node();
    root_node.left = 0;
    root_node.right = keySize;
    root_node.depth = 0;

    List<Node> siblings = new ArrayList<Node>();
    fetch(root_node, siblings);
    insert(siblings);

    // size += (1 << 8 * 2) + 1; // ???
    // if (size >= allocSize) resize (size);

    used = null;
    key = null;
    length = null;

    return error_;
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner in = new Scanner(System.in);

    int N = in.nextInt();

    Node[] nodes = new Node[N];

    for (int i = 0; i < N; i++) {
      nodes[i] = new Node(i + 1);
    }

    for (int i = 0; i < N; i++) {
      int a = in.nextInt();
      int b = in.nextInt();

      Node n = nodes[i];

      if (a != -1) n.left = nodes[a - 1];
      if (b != -1) n.right = nodes[b - 1];
    }

    int T = in.nextInt();

    for (int i = 0; i < T; i++) {
      int K = in.nextInt();

      swap(nodes[0], K, 1);

      Inorder(nodes[0]);
      System.out.println("");
    }
  }
Ejemplo n.º 3
0
  private Node makeSiblings(Node node, Node sibling) {
    int parentLevel = MAX_BITS - Long.numberOfLeadingZeros(node.bits ^ sibling.bits);

    Node parent = createNode(node.bits, parentLevel, 0);

    // the branch is given by the bit at the level one below parent
    long branch = sibling.bits & parent.getBranchMask();
    if (branch == 0) {
      parent.left = sibling;
      parent.right = node;
    } else {
      parent.left = node;
      parent.right = sibling;
    }

    return parent;
  }
Ejemplo n.º 4
0
  private Node merge(Node node, Node other) {
    if (node == null) {
      return copyRecursive(other);
    } else if (other == null) {
      return node;
    } else if (!inSameSubtree(node.bits, other.bits, Math.max(node.level, other.level))) {
      return makeSiblings(node, copyRecursive(other));
    } else if (node.level > other.level) {
      long branch = other.bits & node.getBranchMask();

      if (branch == 0) {
        node.left = merge(node.left, other);
      } else {
        node.right = merge(node.right, other);
      }
      return node;
    } else if (node.level < other.level) {
      Node result = createNode(other.bits, other.level, other.weightedCount);

      long branch = node.bits & other.getBranchMask();
      if (branch == 0) {
        result.left = merge(node, other.left);
        result.right = copyRecursive(other.right);
      } else {
        result.left = copyRecursive(other.left);
        result.right = merge(node, other.right);
      }

      return result;
    }

    // else, they must be at the same level and on the same path, so just bump the counts
    double oldWeight = node.weightedCount;

    weightedCount += other.weightedCount;
    node.weightedCount = node.weightedCount + other.weightedCount;
    node.left = merge(node.left, other.left);
    node.right = merge(node.right, other.right);

    if (oldWeight < ZERO_WEIGHT_THRESHOLD && node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
      nonZeroNodeCount++;
    }

    return node;
  }
Ejemplo n.º 5
0
 private void setChild(Node parent, long branch, Node child) {
   if (parent == null) {
     root = child;
   } else if (branch == 0) {
     parent.left = child;
   } else {
     parent.right = child;
   }
 }
Ejemplo n.º 6
0
  private Node copyRecursive(Node node) {
    Node result = null;

    if (node != null) {
      result = createNode(node.bits, node.level, node.weightedCount);
      result.left = copyRecursive(node.left);
      result.right = copyRecursive(node.right);
    }

    return result;
  }
Ejemplo n.º 7
0
  static void swap(Node root, int K, int C) {

    if (root == null) return;

    if ((C % K) == 0) {
      Node tmp = root.right;
      root.right = root.left;
      root.left = tmp;
    }

    C = C + 1;
    swap(root.left, K, C);
    swap(root.right, K, C);
  }
Ejemplo n.º 8
0
 public static Node insert(Node root, int data) {
   if (root == null) {
     return new Node(data);
   } else {
     Node cur;
     if (data <= root.data) {
       cur = insert(root.left, data);
       root.left = cur;
     } else {
       cur = insert(root.right, data);
       root.right = cur;
     }
     return root;
   }
 }
Ejemplo n.º 9
0
  public static QuantileDigest deserialize(DataInput input) {
    try {
      double maxError = input.readDouble();
      double alpha = input.readDouble();

      QuantileDigest result = new QuantileDigest(maxError, alpha);

      result.landmarkInSeconds = input.readLong();
      result.min = input.readLong();
      result.max = input.readLong();
      result.totalNodeCount = input.readInt();

      Deque<Node> stack = new ArrayDeque<>();
      for (int i = 0; i < result.totalNodeCount; i++) {
        int flags = input.readByte();

        Node node = deserializeNode(input);

        if ((flags & Flags.HAS_RIGHT) != 0) {
          node.right = stack.pop();
        }

        if ((flags & Flags.HAS_LEFT) != 0) {
          node.left = stack.pop();
        }

        stack.push(node);
        result.weightedCount += node.weightedCount;
        if (node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
          result.nonZeroNodeCount++;
        }
      }

      if (!stack.isEmpty()) {
        Preconditions.checkArgument(
            stack.size() == 1, "Tree is corrupted. Expected a single root node");
        result.root = stack.pop();
      }

      return result;
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Ejemplo n.º 10
0
  /**
   * Build a tree from a frequency table. Ensures that escape and eof are represented in the tree.
   *
   * @param table a field of TABLESIZE ints indicating the frequency od each byte
   * @return the root node of the newly created tree
   */
  @SuppressWarnings("unchecked")
  public Node build(int[] table) {
    root = null;
    if ((table == null) || (table.length != TABLESIZE)) {
      return null;
    }
    freq = table;
    if (freq[Huff.escape] == 0) freq[Huff.escape] = 1;
    if (freq[Huff.eof] == 0) freq[Huff.eof] = 1;

    ArrayList nodes = new ArrayList(freq.length);
    for (int i = 0; i < freq.length; i++) {
      if (freq[i] == 0) continue;
      nodes.add(new Node((byte) i, freq[i]));
    }
    Collections.sort(nodes);
    while (nodes.size() > 1) {
      Node a = (Node) nodes.remove(0);
      Node b = (Node) nodes.remove(0);
      int al = a.ch.length;
      byte[] bn = new byte[al + b.ch.length];
      for (int i = 0; i < al; i++) {
        bn[i] = a.ch[i];
      }
      for (int i = 0; i < b.ch.length; i++) {
        bn[i + al] = b.ch[i];
      }
      Arrays.sort(bn);
      Node n = new Node(bn, a.lfreq + b.lfreq);
      n.left = a;
      n.right = b;
      nodes.add(n);
      Collections.sort(nodes);
    }
    root = (Node) nodes.get(0);
    return root;
  }
Ejemplo n.º 11
0
  /**
   * 获取直接相连的子节点
   *
   * @param parent 父节点
   * @param siblings (子)兄弟节点
   * @return 兄弟节点个数
   */
  private int fetch(Node parent, List<Node> siblings) {
    if (error_ < 0) return 0;

    int prev = 0;

    for (int i = parent.left; i < parent.right; i++) {
      if ((length != null ? length[i] : key.get(i).length()) < parent.depth) continue;

      String tmp = key.get(i);

      int cur = 0;
      if ((length != null ? length[i] : tmp.length()) != parent.depth)
        cur = (int) tmp.charAt(parent.depth) + 1;

      if (prev > cur) {
        error_ = -3;
        return 0;
      }

      if (cur != prev || siblings.size() == 0) {
        Node tmp_node = new Node();
        tmp_node.depth = parent.depth + 1;
        tmp_node.code = cur;
        tmp_node.left = i;
        if (siblings.size() != 0) siblings.get(siblings.size() - 1).right = i;

        siblings.add(tmp_node);
      }

      prev = cur;
    }

    if (siblings.size() != 0) siblings.get(siblings.size() - 1).right = parent.right;

    return siblings.size();
  }