/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ #ifndef tuscany_tree_hpp #define tuscany_tree_hpp /** * Functions to work with trees. */ #include "stream.hpp" #include "string.hpp" #include "function.hpp" #include "list.hpp" #include "monad.hpp" #include "value.hpp" namespace tuscany { /** * Make a tree from a leaf and two branches. */ template const list mktree(const T& e, const list& left, const list& right) { return mklist(e, left, right); } /** * Find a leaf with the given key in a tree. */ template const list assoctree(const T& k, const list& tree) { if (isNil(tree)) return tree; if (k == car(car(tree))) return car(tree); if (k < car(car(tree))) return assoctree(k, cadr(tree)); return assoctree(k, caddr(tree)); } /** * Construct a new tree from a leaf and a tree. */ template const list constree(const T& e, const list& tree) { if (isNil(tree)) return mktree(e, list(), list()); if (e == car(tree)) return tree; if (e < car(tree)) return mktree(car(tree), constree(e, cadr(tree)), caddr(tree)); return mktree(car(tree), cadr(tree), constree(e, caddr(tree))); } /** * Make a tree from an unordered list of leaves. */ template const list mktree(const list& l) { if (isNil(l)) return l; return constree(car(l), mktree(cdr(l))); } /** * Convert a tree to an ordered list of leaves. */ template const list flatten(const list& tree) { if (isNil(tree)) return tree; return append(flatten(cadr(tree)), cons(car(tree), flatten(caddr(tree)))); } /** * Sort a list. */ template const list sort(const list& l) { return flatten(mktree(l)); } /** * Make a balanced tree from an ordered list of leaves. */ template const list btreeHelper(const list& elements, const size_t n) { if (n == 0) return cons(list(), elements); const size_t leftSize = (n - 1) / 2; { const list leftResult = btreeHelper(elements, leftSize); { const list leftTree = car(leftResult); const list nonLeftElements = cdr(leftResult); const size_t rightSize = n - (leftSize + 1); { const T thisEntry = car(nonLeftElements); const list rightResult = btreeHelper(cdr(nonLeftElements), rightSize); { const list rightTree = car(rightResult); const list remainingElements = cdr(rightResult); { return cons(mktree(thisEntry, leftTree, rightTree), remainingElements); } } } } } } template const list mkbtree(const list& elements) { return car(btreeHelper(elements, length(elements))); } } #endif /* tuscany_tree_hpp */