New code that can split an AVL tree into two AVL trees.

This is in preparation for a new feature where LinkedTreeMap will have multiple roots, each in its own hash bucket.
This commit is contained in:
Jesse Wilson 2012-09-15 06:13:33 +00:00
parent 01bd0d92e2
commit a0493b9732
2 changed files with 300 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
@ -424,6 +425,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
final K key;
V value;
int height;
int hash;
/** Create the header entry */
Node() {
@ -501,6 +503,204 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
}
}
/**
* Returns a new array containing the same nodes as {@code oldTable}, but with
* twice as many trees, each of (approximately) half the previous size.
*/
static <K, V> Node<K, V>[] doubleCapacity(Node<K, V>[] oldTable) {
// TODO: don't do anything if we're already at MAX_CAPACITY
int oldCapacity = oldTable.length;
@SuppressWarnings("unchecked") // Arrays and generics don't get along.
Node<K, V>[] newTable = new Node[oldCapacity * 2];
AvlIterator<K, V> iterator = new AvlIterator<K, V>();
AvlBuilder<K, V> leftBuilder = new AvlBuilder<K, V>();
AvlBuilder<K, V> rightBuilder = new AvlBuilder<K, V>();
// Split each tree into two trees.
for (int i = 0; i < oldCapacity; i++) {
Node<K, V> root = oldTable[i];
if (root == null) {
continue;
}
// Compute the sizes of the left and right trees.
iterator.reset(root);
int leftSize = 0;
int rightSize = 0;
while (iterator.hasNext()) {
Node<K, V> node = iterator.next();
if ((node.hash & oldCapacity) == 0) {
leftSize++;
} else {
rightSize++;
}
}
// Split the tree into two.
Node<K, V> leftRoot = null;
Node<K, V> rightRoot = null;
if (leftSize > 0 && rightSize > 0) {
leftBuilder.reset(leftSize);
rightBuilder.reset(rightSize);
iterator.reset(root);
while (iterator.hasNext()) {
Node<K, V> node = iterator.next();
if ((node.hash & oldCapacity) == 0) {
leftBuilder.add(node);
} else {
rightBuilder.add(node);
}
}
leftRoot = leftBuilder.root();
rightRoot = rightBuilder.root();
} else if (leftSize > 0) {
leftRoot = root;
} else {
rightRoot = root;
}
// Populate the enlarged array with these new roots.
newTable[i] = leftRoot;
newTable[i + oldCapacity] = rightRoot;
}
return newTable;
}
/**
* Walks an AVL tree in iteration order. Once a node has been returned, its
* left, right and parent links are <strong>no longer used</strong>. For this
* reason it is safe to transform these links as you walk a tree.
*/
static class AvlIterator<K, V> implements Iterator<Node<K, V>> {
private final ArrayDeque<Node<K, V>> stack = new ArrayDeque<Node<K, V>>();
void reset(Node<K, V> root) {
stack.clear();
for (Node<K, V> n = root; n != null; n = n.left) {
stack.add(n);
}
}
public boolean hasNext() {
return !stack.isEmpty();
}
public Node<K, V> next() {
Node<K, V> node = stack.removeLast();
for (Node<K, V> n = node.right; n != null; n = n.left) {
stack.add(n);
}
return node;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Builds AVL trees of a predetermined size by accepting nodes of increasing
* value. To use:
* <ol>
* <li>Call {@link #reset} to initialize the target size <i>size</i>.
* <li>Call {@link #add} <i>size</i> times with increasing values.
* <li>Call {@link #root} to get the root of the balanced tree.
* </ol>
*
* <p>The returned tree will satisfy the AVL constraint: for every node
* <i>N</i>, the height of <i>N.left</i> and <i>N.right</i> is different by at
* most 1. It accomplishes this by omitting deepest-level leaf nodes when
* building trees whose size isn't a power of 2 minus 1.
*
* <p>Unlike rebuilding a tree from scratch, this approach requires no value
* comparisons. Using this class to create a tree of size <i>S</i> is
* {@code O(S)}.
*/
static class AvlBuilder<K, V> {
private final ArrayDeque<Node<K, V>> stack = new ArrayDeque<Node<K, V>>();
private int leavesToSkip;
private int leavesSkipped;
private int size;
void reset(int targetSize) {
// compute the target tree size. This is a power of 2 minus one, like 15 or 31.
int treeCapacity = Integer.highestOneBit(targetSize) * 2 - 1;
leavesToSkip = treeCapacity - targetSize;
size = 0;
leavesSkipped = 0;
stack.clear();
}
void add(Node<K, V> node) {
node.left = node.parent = node.right = null;
node.height = 1;
// Skip a leaf if necessary.
if (leavesToSkip > 0 && (size & 1) == 0) {
size++;
leavesToSkip--;
leavesSkipped++;
}
stack.addLast(node);
size++;
// Skip a leaf if necessary.
if (leavesToSkip > 0 && (size & 1) == 0) {
size++;
leavesToSkip--;
leavesSkipped++;
}
/*
* Combine 3 nodes into subtrees whenever the size is one less than a
* multiple of 4. For example we combine the nodes A, B, C into a
* 3-element tree with B as the root.
*
* Combine two subtrees and a spare single value whenever the size is one
* less than a multiple of 8. For example at 8 we may combine subtrees
* (A B C) and (E F G) with D as the root to form ((A B C) D (E F G)).
*
* Just as we combine single nodes when size nears a multiple of 4, and
* 3-element trees when size nears a multiple of 8, we combine subtrees of
* size (N-1) whenever the total size is 2N-1 whenever N is a power of 2.
*/
int centerHeight = 2;
for (int scale = 4; (size & scale - 1) == scale - 1; scale *= 2) {
if (leavesSkipped == 0) {
Node<K, V> right = stack.removeLast();
Node<K, V> center = stack.removeLast();
Node<K, V> left = stack.removeLast();
center.left = left;
left.parent = center;
center.right = right;
right.parent = center;
center.height = centerHeight;
stack.addLast(center);
} else if (leavesSkipped == 1) {
Node<K, V> right = stack.removeLast();
Node<K, V> center = stack.removeLast();
center.right = right;
center.height = centerHeight;
center.height++;
stack.addLast(center);
leavesSkipped = 0;
} else if (leavesSkipped == 2) {
leavesSkipped = 0;
}
centerHeight++;
}
}
Node<K, V> root() {
if (stack.size() != 1) {
throw new IllegalStateException();
}
return stack.getLast();
}
}
private abstract class LinkedTreeMapIterator<T> implements Iterator<T> {
Node<K, V> next = header.next;
Node<K, V> lastReturned = null;

View File

@ -16,6 +16,8 @@
package com.google.gson.internal;
import com.google.gson.internal.LinkedTreeMap.AvlBuilder;
import com.google.gson.internal.LinkedTreeMap.Node;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@ -70,6 +72,104 @@ public final class LinkedTreeMapTest extends TestCase {
}
}
public void testAvlWalker() {
assertAvlWalker(node(node("a"), "b", node("c")),
"a", "b", "c");
assertAvlWalker(node(node(node("a"), "b", node("c")), "d", node(node("e"), "f", node("g"))),
"a", "b", "c", "d", "e", "f", "g");
assertAvlWalker(node(node(null, "a", node("b")), "c", node(node("d"), "e", null)),
"a", "b", "c", "d", "e");
assertAvlWalker(node(null, "a", node(null, "b", node(null, "c", node("d")))),
"a", "b", "c", "d");
assertAvlWalker(node(node(node(node("a"), "b", null), "c", null), "d", null),
"a", "b", "c", "d");
}
private void assertAvlWalker(Node<String, String> root, String... values) {
LinkedTreeMap.AvlIterator<String, String> iterator = new LinkedTreeMap.AvlIterator<String, String>();
iterator.reset(root);
for (String value : values) {
assertTrue(iterator.hasNext());
assertEquals(value, iterator.next().getKey());
}
assertFalse(iterator.hasNext());
}
public void testAvlBuilder() {
assertAvlBuilder(1, "a");
assertAvlBuilder(2, "(. a b)");
assertAvlBuilder(3, "(a b c)");
assertAvlBuilder(4, "(a b (. c d))");
assertAvlBuilder(5, "(a b (c d e))");
assertAvlBuilder(6, "((. a b) c (d e f))");
assertAvlBuilder(7, "((a b c) d (e f g))");
assertAvlBuilder(8, "((a b c) d (e f (. g h)))");
assertAvlBuilder(9, "((a b c) d (e f (g h i)))");
assertAvlBuilder(10, "((a b c) d ((. e f) g (h i j)))");
assertAvlBuilder(11, "((a b c) d ((e f g) h (i j k)))");
assertAvlBuilder(12, "((a b (. c d)) e ((f g h) i (j k l)))");
assertAvlBuilder(13, "((a b (c d e)) f ((g h i) j (k l m)))");
assertAvlBuilder(14, "(((. a b) c (d e f)) g ((h i j) k (l m n)))");
assertAvlBuilder(15, "(((a b c) d (e f g)) h ((i j k) l (m n o)))");
assertAvlBuilder(16, "(((a b c) d (e f g)) h ((i j k) l (m n (. o p))))");
assertAvlBuilder(30, "((((. a b) c (d e f)) g ((h i j) k (l m n))) o "
+ "(((p q r) s (t u v)) w ((x y z) A (B C D))))");
assertAvlBuilder(31, "((((a b c) d (e f g)) h ((i j k) l (m n o))) p "
+ "(((q r s) t (u v w)) x ((y z A) B (C D E))))");
}
private void assertAvlBuilder(int size, String expected) {
char[] values = "abcdefghijklmnopqrstuvwxyzABCDE".toCharArray();
AvlBuilder<String, String> avlBuilder = new AvlBuilder<String, String>();
avlBuilder.reset(size);
for (int i = 0; i < size; i++) {
avlBuilder.add(node(Character.toString(values[i])));
}
assertEquals(expected, toString(avlBuilder.root()));
}
public void testDoubleCapacity() {
@SuppressWarnings("unchecked") // Arrays and generics don't get along.
Node<String, String>[] oldTable = new Node[1];
oldTable[0] = node(node(node("a"), "b", node("c")), "d", node(node("e"), "f", node("g")));
Node<String, String>[] newTable = LinkedTreeMap.doubleCapacity(oldTable);
assertEquals("(b d f)", toString(newTable[0])); // Even hash codes!
assertEquals("(a c (. e g))", toString(newTable[1])); // Odd hash codes!
}
private static final Node<String, String> head = new Node<String, String>();
private Node<String, String> node(String value) {
Node<String, String> result = new Node<String, String>(null, value, head, head);
result.hash = value.hashCode();
return result;
}
private Node<String, String> node(Node<String, String> left, String value,
Node<String, String> right) {
Node<String, String> result = node(value);
if (left != null) {
result.left = left;
left.parent = result;
}
if (right != null) {
result.right = right;
right.parent = result;
}
return result;
}
private String toString(Node<?, ?> root) {
if (root == null) {
return ".";
} else if (root.left == null && root.right == null) {
return String.valueOf(root.key);
} else {
return String.format("(%s %s %s)", toString(root.left), root.key, toString(root.right));
}
}
private <T> void assertIterationOrder(Iterable<T> actual, T... expected) {
ArrayList<T> actualList = new ArrayList<T>();
for (T t : actual) {