C++實現樹形選擇排序 (tree selection sort)

來源:互聯網
上載者:User

演算法邏輯: 根據節點的大小, 建立樹, 輸出樹的根節點, 並把此重設為最大值, 再重構樹.

因為樹中保留了一些比較的邏輯, 所以減少了比較次數.

也稱錦標賽排序, 時間複雜度為O(nlogn), 因為每個值(共n個)需要進行樹的深度(logn)次比較.

參考<資料結構>(嚴蔚敏版) 第278-279頁.

樹形選擇排序(tree selection sort)是堆排序的一個過渡, 並不是核心演算法.

但是完全按照書上演算法, 實現起來極其麻煩, 幾乎沒有任何人實現過.

需要記錄建樹的順序, 在重構時, 才能減少比較.

本著娛樂和分享的精神, 應人之邀, 簡單的實現了一下.

代碼:

/*  * TreeSelectionSort.cpp  *  *  Created on: 2014.6.11  *      Author: Spike  */       /*eclipse cdt,  gcc 4.8.1*/       #include <iostream>  #include <vector>  #include <stack>  #include <queue>  #include <utility>  #include <climits>         using namespace std;         /*樹的結構*/struct BinaryTreeNode{      bool from; //判斷來源, 左true, 右false      int m_nValue;      BinaryTreeNode* m_pLeft;      BinaryTreeNode* m_pRight;  };         /*構建葉子節點*/BinaryTreeNode* buildList (const std::vector<int>& L)  {      BinaryTreeNode* btnList = new BinaryTreeNode[L.size()];             for (std::size_t i=0; i<L.size(); ++i)      {          btnList[i].from = true;          btnList[i].m_nValue = L[i];          btnList[i].m_pLeft = NULL;          btnList[i].m_pRight = NULL;      }             return btnList;  }         /*不足偶數時, 需補充節點*//*本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/*/BinaryTreeNode* addMaxNode (BinaryTreeNode* list, int n)  {      /*最大節點*/    BinaryTreeNode* maxNode = new BinaryTreeNode(); //最大節點, 用於填充      maxNode->from = true;      maxNode->m_nValue = INT_MAX;      maxNode->m_pLeft = NULL;      maxNode->m_pRight = NULL;             /*複製數組*/    BinaryTreeNode* childNodes = new BinaryTreeNode[n+1]; //增加一個節點      for (int i=0; i<n; ++i) {          childNodes[i].from = list[i].from;          childNodes[i].m_nValue = list[i].m_nValue;          childNodes[i].m_pLeft = list[i].m_pLeft;          childNodes[i].m_pRight = list[i].m_pRight;      }      childNodes[n] = *maxNode;      delete[] list;      list = NULL;             return childNodes;  }         /*根據左右子樹大小, 建立樹*/BinaryTreeNode* buildTree (BinaryTreeNode* childNodes, int n)  {      if (n == 1) {          return childNodes;      }             if (n%2 == 1) {          childNodes = addMaxNode(childNodes, n);      }                    int num = n/2 + n%2;      BinaryTreeNode* btnList = new BinaryTreeNode[num];      for (int i=0; i<num; ++i) {          btnList[i].m_pLeft = &childNodes[2*i];          btnList[i].m_pRight = &childNodes[2*i+1];          bool less = btnList[i].m_pLeft->m_nValue <= btnList[i].m_pRight->m_nValue;          btnList[i].from = less;          btnList[i].m_nValue = less ?                  btnList[i].m_pLeft->m_nValue : btnList[i].m_pRight->m_nValue;      }             buildTree(btnList, num);         }         /*返回樹根, 重新計算數*/int rebuildTree (BinaryTreeNode* tree)  {      int result = tree[0].m_nValue;             std::stack<BinaryTreeNode*> nodes;      BinaryTreeNode* node = &tree[0];      nodes.push(node);             while (node->m_pLeft != NULL) {          node = node->from ? node->m_pLeft : node->m_pRight;          nodes.push(node);      }             node->m_nValue = INT_MAX;      nodes.pop();             while (!nodes.empty())      {          node = nodes.top();          nodes.pop();          bool less = node->m_pLeft->m_nValue <= node->m_pRight->m_nValue;          node->from = less;          node->m_nValue = less ?                  node->m_pLeft->m_nValue : node->m_pRight->m_nValue;      }             return result;  }         /*從上到下列印樹*/void printTree (BinaryTreeNode* tree) {             BinaryTreeNode* node = &tree[0];      std::queue<BinaryTreeNode*> temp1;      std::queue<BinaryTreeNode*> temp2;             temp1.push(node);             while (!temp1.empty())      {          node = temp1.front();          if (node->m_pLeft != NULL && node->m_pRight != NULL) {              temp2.push(node->m_pLeft);              temp2.push(node->m_pRight);          }                 temp1.pop();                 if (node->m_nValue == INT_MAX) {              std::cout << "MAX"  << " ";          } else {              std::cout << node->m_nValue  << " ";          }                 if (temp1.empty())          {              std::cout << std::endl;              temp1 = temp2;              std::queue<BinaryTreeNode*> empty;              std::swap(temp2, empty);          }      }  }         int main ()  {      std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};      BinaryTreeNode* tree = buildTree(buildList(L), L.size());             std::cout << "Begin : " << std::endl;      printTree(tree); std::cout << std::endl;             std::vector<int> result;      for (std::size_t i=0; i<L.size(); ++i)      {          int value = rebuildTree (tree);          std::cout << "Round[" << i+1 << "] : " << std::endl;          printTree(tree); std::cout << std::endl;          result.push_back(value);      }             std::cout << "result : ";      for (std::size_t i=0; i<L.size(); ++i) {          std::cout << result[i] << " ";      }      std::cout << std::endl;             return 0;  }

輸出:

Begin :   13   38 13   38 65 13 27   49 38 65 97 76 13 27 49          Round[1] :   27   38 27   38 65 76 27   49 38 65 97 76 MAX 27 49          Round[2] :   38   38 49   38 65 76 49   49 38 65 97 76 MAX MAX 49          Round[3] :   49   49 49   49 65 76 49   49 MAX 65 97 76 MAX MAX 49          Round[4] :   49   65 49   MAX 65 76 49   MAX MAX 65 97 76 MAX MAX 49          Round[5] :   65   65 76   MAX 65 76 MAX   MAX MAX 65 97 76 MAX MAX MAX          Round[6] :   76   97 76   MAX 97 76 MAX   MAX MAX MAX 97 76 MAX MAX MAX          Round[7] :   97   97 MAX   MAX 97 MAX MAX   MAX MAX MAX 97 MAX MAX MAX MAX          Round[8] :   MAX   MAX MAX   MAX MAX MAX MAX   MAX MAX MAX MAX MAX MAX MAX MAX          result : 13 27 38 49 49 65 76 97

作者:csdn部落格 Caroline-Wendy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.