堆栈中最好的复杂交换元素

时间:2017-01-04 15:38:56

标签: c++ c++11 stack time-complexity

抱歉我的英文。我需要交换堆栈中的一些元素。 一些元素具有相同的优先级,因此当激活元素时。他必须站在优先考虑的元素中的第一位。

enter image description here

为此,我首先从堆栈中删除一个元素,然后再次插入。但事实证明O(n * 2)的复杂性。我理解正确吗?它可能会更好一点吗?

typedef std::shared_ptr<AdaptedWidget> window_ptr;
std::stack<window_ptr> m_windowsStack;

插入元素:

Insert with sorting by - int priority
void WindowManager::insertToStack(window_ptr window)
{
    if (!m_windowsStack.empty() && window->priority() <= m_windowsStack.top()->priority())
    {
        auto top = m_windowsStack.top();
        m_windowsStack.pop();
        insertToStack(window);
        m_windowsStack.push(top);
    }
    else
    {
        m_windowsStack.push(window);
    }
}

删除元素:

void WindowManager::deleteWindow(std::string title)
{
    if (!m_windowsStack.empty())
    {
        auto top = m_windowsStack.top();

        if(top->windowTitle().toStdString() != title)
        {
            m_windowsStack.pop();
            deleteWindow(title);
        }
        else
        {
            m_windowsStack.pop();
            return;
        }
        m_windowsStack.push(top);
    }
}

交换元素:

void WindowManager::swapWindowSamePriority(std::string title)
{
    auto window = findWindow(title);

    if(window)
    {
        deleteWindow(title);
        insertToStack(window);
    }
}

好还是坏?

1 个答案:

答案 0 :(得分:2)

我想,我明白了。实际上复杂度为O(n * 3),因为find()delete()insert()都是O(n)。

我建议您在swap()方法中创建新堆栈:

// example stack:
// 1->1->2->*2*->2->2->3->3->4->5
//           ^ 
//     element to move (swap)
void WindowManager::swapWindowSamePriority(std::string title)
{
    if (m_windowsStack.empty()) return;

    std::stack<window_ptr> tempStack;  // temporary stack
    while(title != m_windowsStack.top()->windowTitle().toStdString())
        // searching for needed element and moving element to tempStack
        tempStack.push(m_windowsStack.pop());

    auto window = m_windowsStack.pop();  // found window.  

    // m_windowsStack: 1->1->2
    // window: *2*
    // tempStack: 5->4->3->3->2->2

    // at this moment in m_windowsStack you have elements that were before window
    // and in tempStack - elements that were after it.

    while(tempStack.top()->priority() == window->priority()) 
        // pushing back elements from tempStack to m_windowsStack while thay have same priority as found window
        m_windowsStack.push(tempStack.pop())


    // m_windowsStack: 1->1->2->2->2
    // window: *2*
    // tempStack: 5->4->3->3

    // at this moment we have moved all elements with the same priority as found window to m_windowsStack.

    m_windowsStack.push(window) // pushing found window on top of elements with the same priority

    // m_windowsStack: 1->1->2->2->2->*2*  <-- that we needed
    // tempStack: 5->4->3->3

    while(!tempStack.empty()) 
        // moving remaining elements from tempStack to m_windowsStack
        m_windowsStack.push(tempStack.pop())

    // m_windowsStack: 1->1->2->2->2->*2*->3->3->4->5
    // tempStack: empty

}

这给你O(n * 2)最坏情况和O(n)avarage。