日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)

發(fā)布時(shí)間:2023/12/20 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

樹形選擇排序 (tree selection sort) 具體解釋 及 代碼(C++)


本文地址:?http://blog.csdn.net/caroline_wendy


算法邏輯: 依據(jù)節(jié)點(diǎn)的大小, 建立樹, 輸出樹的根節(jié)點(diǎn), 并把此重置為最大值, 再重構(gòu)樹.

由于樹中保留了一些比較的邏輯, 所以降低了比較次數(shù).

也稱錦標(biāo)賽排序, 時(shí)間復(fù)雜度為O(nlogn), 由于每一個(gè)值(共n個(gè))須要進(jìn)行樹的深度(logn)次比較.

參考<數(shù)據(jù)結(jié)構(gòu)>(嚴(yán)蔚敏版) 第278-279頁.


樹形選擇排序(tree selection sort)是堆排序的一個(gè)過渡, 并非核心算法.?

可是全然依照書上算法, 實(shí)現(xiàn)起來極其麻煩, 差點(diǎn)兒沒有不論什么人實(shí)現(xiàn)過.

須要記錄建樹的順序, 在重構(gòu)時(shí), 才干降低比較.


本著娛樂和分享的精神, 應(yīng)人之邀, 簡單的實(shí)現(xiàn)了一下.


代碼:

/** 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;/*樹的結(jié)構(gòu)*/ struct BinaryTreeNode{bool from; //推斷來源, 左true, 右falseint m_nValue;BinaryTreeNode* m_pLeft;BinaryTreeNode* m_pRight; };/*構(gòu)建葉子節(jié)點(diǎn)*/ 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; }/*不足偶數(shù)時(shí), 需補(bǔ)充節(jié)點(diǎn)*/ BinaryTreeNode* addMaxNode (BinaryTreeNode* list, int n) {/*最大節(jié)點(diǎn)*/BinaryTreeNode* maxNode = new BinaryTreeNode(); //最大節(jié)點(diǎn), 用于填充maxNode->from = true;maxNode->m_nValue = INT_MAX;maxNode->m_pLeft = NULL;maxNode->m_pRight = NULL;/*復(fù)制數(shù)組*/BinaryTreeNode* childNodes = new BinaryTreeNode[n+1]; //添加一個(gè)節(jié)點(diǎn)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; }/*依據(jù)左右子樹大小, 創(chuàng)建樹*/ 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); } /*返回樹根, 又一次計(jì)算數(shù)*/ 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



總結(jié)

以上是生活随笔為你收集整理的数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。