/* * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package nsk.share; import java.util.*; /** * This denotation provides naming and indexing for nodes * of a binary, or ternary, or n-ary tree. * *

Here, n would be the length of a symbols * string used as an alphabeth for nodes naming. For a * binary tree, n=2, and an aplhabeth could be * the "LR" string. This implies the following * naming for tree nodes: *

 *              (empty)
 *             /       \
 *            L         R
 *          /   \     /   \
 *         LL   LR   RL   RR
 *        /  \ /  \ /  \ /  \
 * 
* *

Anyway, the tree root node is named with the empty * string "" and is indexed with 2-zeroes array * {0,0}. * *

Index for a tree node is 2-elements int[] * array. The 1st element is the node's level in a tree. * The 2nd element is the item's number among all nodes of * that level; provided that node items are enumerated from * 0 to nlevel-1. * Given a level, lexicographic order is assumed for the * nodes of the same level. * *

For example: given the above sample tree, the node * "L" has the index {1,0}, while the * node "RL" has the index {2,2}. * *

In general case, ordering of characters used for nodes * naming is implied by the given alphabeth. This may differ * from the ``natural'' ordering. For example, if alphabeth * is "ZYX...CBA", then ordering for nodes would be * opposite to ``natural''. */ public class TreeNodesDenotation extends Denotation { /** * Symbols to denote tree nodes. * * @see #TreeNodeDenotation(String) */ private String alphabeth; /** * Standard denotation for a binary tree; alphabeth * is "LR". * * @see #TreeNodesDenotation(String) */ public TreeNodesDenotation() { this("LR"); } /** * Denotation for nodes of a tree. * *

Each tree node is marked with a string of symbols * from the given alphabeth. A string length * equals to the node's level. The root node is always * denoted with the empty string. * *

For example, an alphabeth for a binary * tree could be "LR", or "01", or * any 2-symbols string. However, "lL" or * "rR" would be illegal because of collision * between upper- and lower- case letters. * *

In general case, it is illegal for alphabeth * to contain two or several copies of the same symbol. * This constructor deems lower- and upper-case variants * of the same letter are the same symbol. * * @throws IllegalArgumentException If the alphabeth * looks illegal. */ public TreeNodesDenotation(String alphabeth) { if (alphabeth.length() == 0) throw new IllegalArgumentException("empty alphabeth"); // Check for lower- to upper- case collision: this.alphabeth = alphabeth.toUpperCase(); int length = this.alphabeth.length(); Set pool = new HashSet(); // still empty for (int i=0; iname is legal, and return the * numeric index for the tree node denoted by the given * name. * * @throws IllegalArgumentException If the name * is illegal. */ public int[] indexFor(String name) { int level = name.length(); int factor = alphabeth.length(); long item = 0; for (int i=0; i Integer.MAX_VALUE) throw new IllegalArgumentException("too long name: " + name); }; int[] index = new int [] { level, (int)item }; return index; } /** * Check if the index[] is legal, and return * a symbolic name for the tree node denoted by the * given index[]. * * @throws IllegalArgumentException If the index[] * is illegal. */ public String nameFor(int[] index) { if (index.length != 2) throw new IllegalArgumentException( "index dimention: " + index.length); StringBuffer name = new StringBuffer(); // still empty int level = index[0]; int item = index[1]; if (level < 0 || item < 0) throw new IllegalArgumentException( "negative index: " + level + ", " + item); int factor = alphabeth.length(); for (int i=0; i