Title: Enter a binary search tree and convert the two-fork search tree into a sorted two-way list.
Requires that no new nodes can be created, only the pointer to the node in the number of points can be adjusted.
Method: Use the middle order to traverse each node, and make a connection, that is, before the Zuozi refers to the right subtree after the point, and save the previous node.
This procedure contains the algorithm principle, the test procedure, and the output.
Code:
* * * main.cpp * * Created on:2014.6.12 * author:spike//*eclipse CDT, gcc 4.8.1*/#incl
Ude <iostream> #include <stack> #include <queue> using namespace std;
struct Binarytreenode {int m_nvalue;
binarytreenode* M_pleft;
binarytreenode* M_pright;
};
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->m_pleft!= NULL) {Temp2.push (node->m_pleft);
} if (Node->m_pright!= NULL) {Temp2.push (node->m_pright);
} temp1.pop ();
Std::cout << node->m_nvalue << ""; 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->m_nvalue = 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->m_nvalue = L[times];
Lchild->m_pleft = nullptr;
Lchild->m_pright = nullptr;
++times;
Parent->m_pleft = Lchild;
Childqueue.push (Lchild); if (Times = = L.size ()) break;
binarytreenode* rchild = new Binarytreenode ();
Rchild->m_nvalue = L[times];
Rchild->m_pleft = nullptr;
Rchild->m_pright = nullptr;
++times;
Parent->m_pright = 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->m_
Pright!= NULL) {std::cout << pheadoflist->m_nvalue << "";
Pheadoflist = pheadoflist->m_pright;
} std::cout << pheadoflist->m_nvalue << "";
Std::cout << Std::endl;
Return } void Convertnode (binarytreenode* pNode, binarytreenode** plastnodeinlist) {if (Pnode = NULL) return;
binarytreenode* pcurrent = Pnode;
if (pcurrent->m_pleft!= NULL) convertnode (Pcurrent->m_pleft, plastnodeinlist);
Pcurrent->m_pleft = *plastnodeinlist;
if (*plastnodeinlist!= NULL) (*plastnodeinlist)->m_pright = pcurrent;
*plastnodeinlist = pcurrent;
if (pcurrent->m_pright!= NULL) convertnode (Pcurrent->m_pright, plastnodeinlist);
} binarytreenode* Convert (binarytreenode* prootoftree) {binarytreenode* plastnodeinlist = NULL;
Convertnode (Prootoftree, &plastnodeinlist);
binarytreenode* pheadoflist = plastnodeinlist;
while (pheadoflist!= null && pheadoflist->m_pleft!= null) pheadoflist = pheadoflist->m_pleft;
return pheadoflist; 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; }
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Output:
----Tree:----
6
4 8
----List:----
4 6 8 10 12 14 16
Author: csdn Blog Caroline-wendy