Convert a binary search tree into a sorted two-way linked list

Source: Internet
Author: User

# 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;
}

Contact Us

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.