two fork search tree and bidirectional linked list code (c + +)
This address: Http://blog.csdn.net/caroline_wendy
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->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 + +)