11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2011 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * in compliance with the License. You may obtain a copy of the License at 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software distributed under the 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * express or implied. See the License for the specific language governing permissions and 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect; 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport javax.annotation.Nullable; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * A factory for copying nodes in binary search trees with different children. 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Typically, nodes will carry more information than the fields in the {@link BstNode} class, 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * often some kind of value or some aggregate data for the subtree. This factory is responsible for 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * copying this additional data between nodes. 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Louis Wasserman 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param <N> The type of the tree nodes constructed with this {@code BstNodeFactory}. 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertabstract class BstNodeFactory<N extends BstNode<?, N>> { 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a new {@code N} with the key and value data from {@code source}, with left child 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code left}, and right child {@code right}. If {@code left} or {@code right} is null, the 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * returned node will not have a child on the corresponding side. 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract N createNode(N source, @Nullable N left, @Nullable N right); 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a new {@code N} with the key and value data from {@code source} that is a leaf. 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public final N createLeaf(N source) { 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return createNode(source, null, null); 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 47