Example #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_;
  }
 /*@ public normal_behavior
 @     requires repOk();
 @     ensures repOk() && contains(info);
 @*/
 public void add(Comparable info) {
   if (root == null) {
     root = new Node(info);
   } else {
     Node t = root;
     while (true) {
       if (t.info.compareTo(info) < 0) {
         if (t.right == null) {
           t.right = new Node(info);
           break;
         } else {
           t = t.right;
         }
       } else if (t.info.compareTo(info) > 0) {
         if (t.left == null) {
           t.left = new Node(info);
           break;
         } else {
           t = t.left;
         }
       } else { // no duplicates
         return;
       }
     }
   }
   size++;
 }
Example #3
0
  /**
   * Deletes the node specified by the parameter toDelete. parent specifies the parent of the node
   * to be deleted.
   */
  private void deleteNode(Node toDelete, Node parent) {
    if (toDelete.left != null && toDelete.right != null) {
      // Case 3: toDelete has two children.
      // Find a replacement for the item we're deleting -- as well as
      // the replacement's parent.
      // We use the smallest item in toDelete's right subtree as
      // the replacement.
      Node replaceParent = toDelete;
      Node replace = toDelete.right;
      while (replace.left != null) {
        replaceParent = replace;
        replace = replace.left;
      }

      // Replace toDelete's key and data with those of the
      // replacement item.
      toDelete.key = replace.key;
      toDelete.data = replace.data;

      // Recursively delete the replacement item's old node.
      // It has at most one child, so we don't have to
      // worry about infinite recursion.
      deleteNode(replace, replaceParent);
    } else {
      // Cases 1 and 2: toDelete has 0 or 1 child
      Node toDeleteChild;
      if (toDelete.left != null) toDeleteChild = toDelete.left;
      else toDeleteChild = toDelete.right; // null if it has no children

      if (toDelete == root) root = toDeleteChild;
      else if (toDelete.key < parent.key) parent.left = toDeleteChild;
      else parent.right = toDeleteChild;
    }
  }
 public boolean remove(Comparable info) {
   Node parent = null;
   Node current = root;
   while (current != null) {
     int cmp = info.compareTo(current.info);
     if (cmp < 0) {
       parent = current;
       current = current.left;
     } else if (cmp > 0) {
       parent = current;
       current = current.right;
     } else {
       break;
     }
   }
   if (current == null) return false;
   Node change = removeNode(current);
   if (parent == null) {
     root = change;
   } else if (parent.left == current) {
     parent.left = change;
   } else {
     parent.right = change;
   }
   return true;
 }
Example #5
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("");
    }
  }
Example #6
0
 static void connect(Node ch, Node p, Boolean isLeftChild) {
   if (ch != null) ch.parent = p;
   if (isLeftChild != null) {
     if (isLeftChild) p.left = ch;
     else p.right = ch;
   }
 }
Example #7
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;
  }
Example #8
0
 // ---------------- add( Node, Node ) ------------------
 private void add(Node cur, Node newNode) {
   int cmp = newNode.id.compareTo(cur.id);
   if (cmp <= 0) {
     if (cur.left == null) cur.left = newNode;
     else add(cur.left, newNode);
   } else if (cur.right == null) cur.right = newNode;
   else add(cur.right, newNode);
 }
Example #9
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;
  }
Example #10
0
 public static void cut(Node x, Node y) {
   makeRoot(x);
   expose(y);
   // check that exposed path consists of a single edge (y,x)
   if (y.right != x || x.left != null || x.right != null)
     throw new RuntimeException("error: no edge (x,y)");
   y.right.parent = null;
   y.right = null;
 }
Example #11
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;
   }
 }
Example #12
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;
  }
Example #13
0
 protected Node merge(Node left, Node right) {
   if (left == nullNode) return right;
   if (right == nullNode) return left;
   if (left.priority > right.priority) {
     left.right = left.right.merge(left.right, right);
     left.updateSize();
     return left;
   }
   right.left = right.left.merge(left, right.left);
   right.updateSize();
   return right;
 }
 public static void main(String[] args) {
   Node tree = new Node(1);
   tree.left = new Node(2);
   tree.left.left = new Node(4);
   tree.right = new Node(3);
   tree.right.left = new Node(5);
   tree.right.left.right = new Node(7);
   tree.right.left.right.left = new Node(9);
   tree.right.right = new Node(6);
   tree.right.right.right = new Node(8);
   tree.right.right.right.right = new Node(10);
   System.out.println("Total Leaves Count is :: " + getLeavesCount(tree));
 }
Example #15
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);
  }
  public Node buildTree(int[] A, int start, int end) {
    if (start == end) return new Node(start, end, A[start]);

    int middle = start + (end - start) / 2;

    Node left = buildTree(A, start, middle);
    Node right = buildTree(A, middle + 1, end);

    Node parent = new Node(start, end, Math.min(left.min, right.min));

    parent.left = left;
    parent.right = right;

    return parent;
  }
 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;
   }
 }
Example #18
0
  /**
   * Inserts the specified (key, data) pair in the tree so that the tree remains a binary search
   * tree.
   */
  public void insert(int key, Object data) {
    // Find the parent of the new node.
    Node parent = null;
    Node trav = root;
    while (trav != null) {
      parent = trav;
      if (key < trav.key) trav = trav.left;
      else trav = trav.right;
    }

    // Insert the new node.
    Node newNode = new Node(key, data);
    if (parent == null) // the tree was empty
    root = newNode;
    else if (key < parent.key) parent.left = newNode;
    else parent.right = newNode;
  }
Example #19
0
 @SuppressWarnings({"unchecked"})
 protected Node insert(Node node) {
   if (node.priority > priority) {
     Object[] result = split(node.key);
     node.left = (Node) result[0];
     node.right = (Node) result[1];
     node.updateSize();
     return node;
   }
   if (compare(node.key, this.key) < 0) {
     left = left.insert(node);
     updateSize();
     return this;
   }
   right = right.insert(node);
   updateSize();
   return this;
 }
 Node removeNode(Node current) {
   size--;
   Node left = current.left, right = current.right;
   if (left == null) return right;
   if (right == null) return left;
   if (left.right == null) {
     current.info = left.info;
     current.left = left.left;
     return current;
   }
   Node temp = left;
   while (temp.right.right != null) {
     temp = temp.right;
   }
   current.info = temp.right.info;
   temp.right = temp.right.left;
   return current;
 }
Example #21
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);
    }
  }
Example #22
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;
  }