# Include <iostream>
# Include <time. h>
# Define max_num 10
Using namespace STD;
Struct bstreenode
{
Int m_nvalue;
Bstreenode * m_pleft;
Bstreenode * m_pright;
};
Int COUNT = 0;
Void addtree (bstreenode ** T, int num)
{
If (* t = NULL)
{
* T = (bstreenode *) malloc (sizeof (bstreenode ));
(* T)-> m_nvalue = num;
(* T)-> m_pleft = NULL;
(* T)-> m_pright = NULL;
}
Else if (* t)-> m_nvalue> num)
{
Addtree (& (* t)-> m_pleft), num );
}
Else
{
Addtree (& (* t)-> m_pright), num );
}
}
Void midtraversal (bstreenode * t)
{
If (T! = NULL)
{
Midtraversal (t-> m_pleft );
Count ++;
If (count % 5 = 0)
{
Cout <t-> m_nvalue <Endl;
}
Else
{
Cout <t-> m_nvalue <"\ t ";
}
Midtraversal (t-> m_pright );
}
}
Void convertnode (bstreenode * pnode, bstreenode ** plastnodeinlist)
{
If (pnode = NULL)
{
Return;
}
Bstreenode * pcurrent = pnode;
If (pcurrent-> m_pleft! = NULL)
{
Convertnode (pcurrent-> m_pleft, plastnodeinlist );
}
// Put the current node into the double-linked list
// Plastnodeinlist <-- pcurrent
// Plastnodeinlist --> pcurrent
Pcurrent-> m_pleft = * plastnodeinlist;
If (* plastnodeinlist! = NULL)
{
(* Plastnodeinlist)-> m_pright = pcurrent;
}
* Plastnodeinlist = pcurrent;
If (pcurrent-> m_pright! = NULL)
{
Convertnode (pcurrent-> m_pright, plastnodeinlist );
}
}
Void printlist (bstreenode * l, int flag)
{
Bstreenode * P = L;
While (P! = NULL)
{
Count ++;
If (count % 5 = 0)
{
Cout <p-> m_nvalue <Endl;
}
Else
{
Cout <p-> m_nvalue <"\ t ";
}
If (1 = Flag)
{
P = p-> m_pright;
}
Else
{
P = p-> m_pleft;
}
}
}
Bstreenode * convertsolution (bstreenode * pheadoftree)
{
Bstreenode * plastnodeinlist = NULL;
Convertnode (pheadoftree, & plastnodeinlist );
Cout <"Last node is:" <plastnodeinlist-> m_nvalue <Endl;
Plastnodeinlist-> m_pright = NULL;
Cout <"revert list is: \ n ";
Printlist (plastnodeinlist, 2 );
Bstreenode * pheadoflist = plastnodeinlist;
While (pheadoflist & pheadoflist-> m_pleft)
Pheadoflist = pheadoflist-> m_pleft;
Cout <"normal list is: \ n ";
Printlist (pheadoflist, 1 );
Return pheadoflist;
}
Int main ()
{
Bstreenode * t = NULL;
Int num, I;
Srand (unsigned INT) Time (null ));
For (I = 0; I <max_num; I ++)
{
Num = rand () % 100 + 1;
Addtree (& T, num );
}
Cout <"mid reversal tree is: \ n ";
Midtraversal (t );
Cout <"\ ndouble list is: \ n ";
Printlist (convertsolution (t), 1 );
Return 0;
}