如何在二叉搜索树中查找值大于指定值的节点

发布于 2024-09-01 14:03:09 字数 126 浏览 4 评论 0原文

我有一棵红黑树,基本操作是插入、删除、遍历中序、后序和前序等。

我希望创建一个方法,可以返回树中大于指定值的节点。与小于也相同。

谁能指出我一些伪代码/算法(它们可能意味着相同的事情)

干杯

I have a red black tree and the basic operations insert, delete, traversing inorder, postorder and preorder etc.

I wish to create a method that can return the nodes in the tree that are greater than a specified value. Same with less than too.

Can anyone point me to some pseudocode / algorithm (they probably mean the same thing)

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-09-08 14:03:09

下面是我创建的代码,它可以很好地测试以升序显示大于或等于指定值的节点的节点。有人可以检查我的代码并提出改进建议,如果可能的话建议我如何为我的 LessThan 方法执行此操作。目前我的 LessThan 方法按降序返回,我只是不知道如何让它升序。干杯。

 // --------------------------------------------------------------------------------
// GetNodesGreaterThan
// --------------------------------------------------------------------------------
/** \fn void  RedBlackTree<T>::GetNodesGreaterThan(T &data)
 *  \brief This method checks to see if tree is empty otherwise calls the 
 *          recursive the GreaterThan method.
 *  \param templated
 *  \return void
 */
template <class T>
void  RedBlackTree<T>::GetNodesGreaterThan(T &data)
{
    GreaterThan(m_root, data);
}

// --------------------------------------------------------------------------------
// GreaterThan
// --------------------------------------------------------------------------------
/** \fn void RedBlackTree<T>::GreaterThan(NodeType<T> *p, const T &data) const
 *  \brief This method finds the nodes in the tree that are greater than  or equal to 
 *          a specified value and puts nodes into a list.
 *  \param templated pointer
 *  \param constant template
 *  \return void
 */
template <class T>
void RedBlackTree<T>::GreaterThan(NodeType<T> *p, const T &data) const
{
    if ( p != NULL)                                         // we have hit a leaf node
    {
        if ((p->m_data == data) || (data < p->m_data)){     // record the node whos value is
            GreaterThan(p->m_lLink, data);                  // move down left link
            //cout << p->m_data << " ";                     // Display data (debug purpose)
        }
        GreaterThan(p->m_rLink, data);                      // move down right link 
    }
}

// --------------------------------------------------------------------------------
// GetNodesLessThan
// --------------------------------------------------------------------------------
/** \fn void  RedBlackTree<T>::GetNodesLessThan(T &data)
 *  \brief This method checks to see if tree is empty otherwise calls the 
 *          recursive the LessThan method.
 *  \param template
 *  \return void
 */
template <class T>
void  RedBlackTree<T>::GetNodesLessThan(T &data)
{
    ClearList();                                            // clears the node list
    LessThan(m_root, data);
}

// --------------------------------------------------------------------------------
// LessThan
// --------------------------------------------------------------------------------
/** \fn void RedBlackTree<T>::LessThan(NodeType<T> *p, const T &data) const
 *  \brief This method finds the nodes in the tree that are less than  or equal to 
 *          a specified value and puts nodes into a list.
*  \param templated pointer
 *  \param constant template
 */
template <class T>
void RedBlackTree<T>::LessThan(NodeType<T> *p, const T &data) const
{
    if ( p != NULL)
    {
        if ((p->m_data == data) || (data > p->m_data)){     // record the node whos value is
            LessThan(p->m_rLink, data);                     // move down left link
            //cout << p->m_data << " ";                     // Display data (debug purpose)
            //m_list[m_countOfElements] = p->m_data;            // add to list
            //m_countOfElements++;                              // increment # of elements
        }
        LessThan(p->m_lLink, data);                         // move down left link
    }
}

Below is the code I have created which tests fine for displaying my nodes in ascending order for nodes greater than or equal to a specified value. Can someone please review my code and recommend improvements and if possible recommend how I may do this for my LessThan method. Currently my LessThan method returns in descending order and I just can not see how to get it ascending. Cheers.

 // --------------------------------------------------------------------------------
// GetNodesGreaterThan
// --------------------------------------------------------------------------------
/** \fn void  RedBlackTree<T>::GetNodesGreaterThan(T &data)
 *  \brief This method checks to see if tree is empty otherwise calls the 
 *          recursive the GreaterThan method.
 *  \param templated
 *  \return void
 */
template <class T>
void  RedBlackTree<T>::GetNodesGreaterThan(T &data)
{
    GreaterThan(m_root, data);
}

// --------------------------------------------------------------------------------
// GreaterThan
// --------------------------------------------------------------------------------
/** \fn void RedBlackTree<T>::GreaterThan(NodeType<T> *p, const T &data) const
 *  \brief This method finds the nodes in the tree that are greater than  or equal to 
 *          a specified value and puts nodes into a list.
 *  \param templated pointer
 *  \param constant template
 *  \return void
 */
template <class T>
void RedBlackTree<T>::GreaterThan(NodeType<T> *p, const T &data) const
{
    if ( p != NULL)                                         // we have hit a leaf node
    {
        if ((p->m_data == data) || (data < p->m_data)){     // record the node whos value is
            GreaterThan(p->m_lLink, data);                  // move down left link
            //cout << p->m_data << " ";                     // Display data (debug purpose)
        }
        GreaterThan(p->m_rLink, data);                      // move down right link 
    }
}

// --------------------------------------------------------------------------------
// GetNodesLessThan
// --------------------------------------------------------------------------------
/** \fn void  RedBlackTree<T>::GetNodesLessThan(T &data)
 *  \brief This method checks to see if tree is empty otherwise calls the 
 *          recursive the LessThan method.
 *  \param template
 *  \return void
 */
template <class T>
void  RedBlackTree<T>::GetNodesLessThan(T &data)
{
    ClearList();                                            // clears the node list
    LessThan(m_root, data);
}

// --------------------------------------------------------------------------------
// LessThan
// --------------------------------------------------------------------------------
/** \fn void RedBlackTree<T>::LessThan(NodeType<T> *p, const T &data) const
 *  \brief This method finds the nodes in the tree that are less than  or equal to 
 *          a specified value and puts nodes into a list.
*  \param templated pointer
 *  \param constant template
 */
template <class T>
void RedBlackTree<T>::LessThan(NodeType<T> *p, const T &data) const
{
    if ( p != NULL)
    {
        if ((p->m_data == data) || (data > p->m_data)){     // record the node whos value is
            LessThan(p->m_rLink, data);                     // move down left link
            //cout << p->m_data << " ";                     // Display data (debug purpose)
            //m_list[m_countOfElements] = p->m_data;            // add to list
            //m_countOfElements++;                              // increment # of elements
        }
        LessThan(p->m_lLink, data);                         // move down left link
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文