編程演算法 - 二叉搜尋樹 與 雙向鏈表 代碼(C++)

來源:互聯網
上載者:User

標籤:儲存   eclips   while   break   .net   out   end   head   clip   

二叉搜尋樹 與 雙向鏈表 代碼(C++)


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


題目:輸入一顆二叉搜尋樹, 將該二叉搜尋樹轉換成一個排序的雙向鏈表.

要求不能建立不論什麼新的結點, 僅僅能調整數中結點的指標的指向.


方法: 使用中序遍曆每個結點, 並進行串連, 即左子樹指前, 右子樹指後, 並儲存前一個節點.


本程式包括演算法原理, 測試程式, 及 輸出.


/* * main.cpp * *  Created on: 2014.6.12 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include <iostream>#include <stack>#include <queue>using namespace std;struct BinaryTreeNode {int value;BinaryTreeNode* left;BinaryTreeNode* right;};void printTree (BinaryTreeNode* tree){BinaryTreeNode* node = tree;std::queue<BinaryTreeNode*> temp1;std::queue<BinaryTreeNode*> temp2;temp1.push(node);while (!temp1.empty()){node = temp1.front();if (node->left != NULL) {temp2.push(node->left);}if (node->right != NULL) {temp2.push(node->right);}temp1.pop();std::cout << node->value  << " ";if (temp1.empty()){std::cout << std::endl;temp1 = temp2;std::queue<BinaryTreeNode*> empty;std::swap(temp2, empty);}}}BinaryTreeNode* buildTree (const std::vector<int>& L){if (L.empty())return nullptr;std::queue<BinaryTreeNode*> parentQueue;std::queue<BinaryTreeNode*> childQueue;BinaryTreeNode* root = new BinaryTreeNode();root->value = L[0];parentQueue.push(root);std::size_t times = 1;while (times < L.size()){BinaryTreeNode* parent = parentQueue.front();parentQueue.pop();BinaryTreeNode* lchild = new BinaryTreeNode();lchild->value = L[times];lchild->left = nullptr;lchild->right = nullptr;++times;parent->left = lchild;childQueue.push(lchild);if (times == L.size()) break;BinaryTreeNode* rchild = new BinaryTreeNode();rchild->value = L[times];rchild->left = nullptr;rchild->right = nullptr;++times;parent->right = rchild;childQueue.push(rchild);if (parentQueue.empty()) {parentQueue = childQueue;std::queue<BinaryTreeNode*> empty;std::swap(childQueue, empty);}}return root;}void printList (BinaryTreeNode* pHeadOfList){while (pHeadOfList != NULL && pHeadOfList->right != NULL){std::cout << pHeadOfList->value << " ";pHeadOfList = pHeadOfList->right;}std::cout << pHeadOfList->value << " ";std::cout << std::endl;return;}void ConvertNode(BinaryTreeNode* root, BinaryTreeNode*& pLast){if (root == NULL)return;BinaryTreeNode* current = root;if (current->left != NULL)ConvertNode(current->left, pLast);current->left = pLast;if (pLast != NULL)pLast->right = current;pLast = current;if (current->right != NULL)ConvertNode(current->right, pLast);}BinaryTreeNode* Convert(BinaryTreeNode* root){BinaryTreeNode* pLast = NULL;ConvertNode(root, pLast);BinaryTreeNode* head = pLast;while (head != NULL && head->left != NULL)head = head->left;return head;}int main (void){std::vector<int> L = {10, 6, 14, 4, 8, 12, 16};BinaryTreeNode* tree = buildTree(L);std::cout << "----Tree:----\n";printTree(tree);BinaryTreeNode* list = Convert(tree);std::cout << "----List:----\n";printList(list);return 0;}



輸出:

----Tree:----10 6 14 4 8 12 16 ----List:----4 6 8 10 12 14 16 







編程演算法 - 二叉搜尋樹 與 雙向鏈表 代碼(C++)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.