# Programming algorithms-Two fork search tree and bidirectional linked list code (c + +)

Source: Internet
Author: User

two fork search tree and bidirectional linked list code (c + +)

Title: Enter a binary search tree to convert the two-fork search tree into a sorted doubly linked list.

Requires that no new nodes be created, only pointers to the nodes in the number can be adjusted.

Methods: Use the middle sequence to traverse each node and make a connection, that is, Zuozi refers to the front, right subtree refers to, and save the previous node.

This procedure includes algorithm principle, test procedure, and output.

`/* * main.cpp * * Created on:2014.6.12 * author:spike *//*eclipse CDT, gcc 4.8.1*/#include <iostream> #includ E <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-&GT;value = l[0];p arentqueue.push (root), std::size_t times = 1;while (Times < L.size ()) {binarytreenode* parent = Parentqu Eue.front ();p arentqueue.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 << "";p headoflist = 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";p rinttree (tree); binarytreenode* list = Convert (tree), std::cout << "----list:----\ n";p rintlist (list); return 0;}`

Output:

Programming algorithms-Two fork search tree and bidirectional linked list code (c + +)

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## 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.