⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbinarysearchtreewithge.h

📁 这是数据结构、算法与应用-C++语言描述的代码
💻 H
字号:
// extension of binary search tree with duplicates
// includes findGE

#ifndef dBinarySearchTreeWithGE_
#define dBinarySearchTreeWithGE_



#include "dBinarySearchTree.h"

using namespace std;

template<class K, class E>
class dBinarySearchTreeWithGE : public dBinarySearchTree<K,E>
{
   public:
      pair<const K, E>* findGE(const K& theKey) const;
};

template<class K, class E>
pair<const K, E>* dBinarySearchTreeWithGE<K,E>::findGE(const K& theKey) const
{// Return pointer to pair with smallest key >= theKey.
 // Return NULL if no element has key >= theKey.
   binaryTreeNode<pair<const K, E> > *currentNode = root;
   pair<const K, E> *bestElement = NULL; // element with smallest key
                                         // >= theKey found so far
   // search the tree
   while (currentNode != NULL)
      // is currentNode->element a candidate?
      if (currentNode->element.first >= theKey)
      {// yes, currentNode->element is
       // a better candidate than bestElement
         bestElement = &currentNode->element;
         // smaller keys in left subtree only
         currentNode = currentNode->leftChild;
      }
      else
         // no, currentNode->element.first is too small
         // try right subtree
         currentNode = currentNode->rightChild;

   return bestElement;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -