avl_GetBestFit.cpp.h revision 210e87cc03f92d54681b81a81cc1fdbd48a9d2c8
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync/* $Id$ */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync/** @file
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * kAVLGetBestFit - Get Best Fit routine for AVL trees.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * Intended specially on heaps. The tree should allow duplicate keys.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync *
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync/*
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * Copyright (C) 1999-2002 knut st. osmundsen (bird-src-spam@anduin.net)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync *
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * available from http://www.virtualbox.org. This file is free software;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * you can redistribute it and/or modify it under the terms of the GNU
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * General Public License (GPL) as published by the Free Software
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync *
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * The contents of this file may alternatively be used under the terms
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * of the Common Development and Distribution License Version 1.0
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * VirtualBox OSE distribution, in which case the provisions of the
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * CDDL are applicable instead of those of the GPL.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync *
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * You may elect to license modified versions of this file under the
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * terms and conditions of either the GPL or the CDDL or both.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync#ifndef _kAVLGetBestFit_h_
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync#define _kAVLGetBestFit_h_
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync/**
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * Finds the best fitting node in the tree for the given Key value.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * @returns Pointer to the best fitting node found.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * @param ppTree Pointer to Pointer to the tree root node.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * @param Key The Key of which is to be found a best fitting match for..
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * @param fAbove TRUE: Returned node is have the closest key to Key from above.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * FALSE: Returned node is have the closest key to Key from below.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * @sketch The best fitting node is always located in the searchpath above you.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * >= (above): The node where you last turned left.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync * <= (below): the node where you last turned right.
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsyncKAVL_DECL(PKAVLNODECORE) KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync{
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync register PKAVLNODECORE pNode = KAVL_GET_POINTER_NULL(ppTree);
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (pNode)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync PKAVLNODECORE pNodeLast = NULL;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (fAbove)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync { /* pNode->Key >= Key */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync while (KAVL_NE(pNode->Key, Key))
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (KAVL_G(pNode->Key, Key))
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (pNode->pLeft != KAVL_NULL)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNodeLast = pNode;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNode = KAVL_GET_POINTER(&pNode->pLeft);
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync return pNode;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (pNode->pRight != KAVL_NULL)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNode = KAVL_GET_POINTER(&pNode->pRight);
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync return pNodeLast;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync { /* pNode->Key <= Key */
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync while (KAVL_NE(pNode->Key, Key))
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (KAVL_G(pNode->Key, Key))
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (pNode->pLeft != KAVL_NULL)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNode = KAVL_GET_POINTER(&pNode->pLeft);
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync return pNodeLast;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync if (pNode->pRight != KAVL_NULL)
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync {
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNodeLast = pNode;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync pNode = KAVL_GET_POINTER(&pNode->pRight);
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync else
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync return pNode;
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync }
040abec2534dadc53ebc8fa378ef03f4feecb7dbvboxsync
/* perfect match or nothing. */
return pNode;
}
#endif