Sequential Traversal
Void convertnode (bstreenode * pnode, bstreenode * & plastnodeinlist)
{
If (pnode = NULL)
Return;
Bstreenode * pcurrent = pnode;
// Convert the left sub-tree
If (pcurrent-> m_pleft! = NULL)
Convertnode (pcurrent-> m_pleft, plastnodeinlist );
// Put the current node into the double-linked list
Pcurrent-> m_pleft = plastnodeinlist;
If (plastnodeinlist! = NULL)
Plastnodeinlist-> m_pright = pcurrent;
Plastnodeinlist = pcurrent;
// Convert the right sub-tree
If (pcurrent-> m_pright! = NULL)
Convertnode (pcurrent-> m_pright, plastnodeinlist );
}
//////////////////////////////////////// ///////////////////////////////
// Covert a binary search tree into a sorted double-linked list
// Input: pheadoftree-the head of tree
// Output: The head of sorted double-linked list
//////////////////////////////////////// ///////////////////////////////
Bstreenode * convert_solution1 (bstreenode * pheadoftree)
{
Bstreenode * plastnodeinlist = NULL;
Convertnode (pheadoftree, plastnodeinlist );
// Get the head of the double-linked list
Bstreenode * pheadoflist = plastnodeinlist;
While (pheadoflist & pheadoflist-> m_pleft)
Pheadoflist = pheadoflist-> m_pleft;
Return pheadoflist;
}
Convert a binary search tree into a sorted two-way linked list