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

Source: Internet
Author: User

Convert a binary search tree into a sorted two-way linked list
Question:
Enter a binary search tree and convert it into a sorted two-way linked list.
You must not create any new node. You only need to adjust the pointer point.
10
/\
6 14
/\/\
4 8 12 16
Convert to a two-way linked list
4 = 6 = 8 = 10 = 12 = 14 = 16.
 
First, we define the data structure of the Binary Search Tree node as follows:
Struct bstreenode
{
Int m_nvalue; // value of Node
Bstreenode * m_pleft; // left child of Node
Bstreenode * m_pright; // right child of Node
};

 


/**
*
*/
Package com. lhp;

Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. Io. serializable;

/**
1. Convert the binary search tree into a sorted two-way linked list
Question:
Enter a binary search tree and convert it into a sorted two-way linked list.
You must not create any new node. You only need to adjust the pointer point.
10
/\
6 14
/\/\
4 8 12 16
Convert to a two-way linked list
4 = 6 = 8 = 10 = 12 = 14 = 16.
 
First, we define the data structure of the Binary Search Tree node as follows:
Struct bstreenode
{
Int m_nvalue; // value of Node
Bstreenode * m_pleft; // left child of Node
Bstreenode * m_pright; // right child of Node
};
*/

/**
* Binary Search Tree
*/
Class bstree implements cloneable, serializable {
/**
* V0.8
*/
Private Static final long serialversionuid =-7240326774488306261l;

Private bsnode m_root; // Root Node
 
Private bsnode templistnode; // temporary variable used when doublelist is created
Private bsnode templisthead; // temporary variable used when doublelist is created
 
Public void setm_root (bsnode mroot ){
M_root = mroot;
}

Public bsnode getm_root (){
Return m_root;
}
 
/**
* Deep copy
*/
Public object clone (){
Try {
// The Node copy Algorithm without thinking about the tree is used in this simple method, and complex type copy is not afraid. However, all related classes must be serializable.
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Objectoutputstream OOS = new objectoutputstream (baos );
Oos. writeobject (this );
Oos. Close ();
Bytearrayinputstream BAIS = new bytearrayinputstream (baos. tobytearray ());
Objectinputstream OIS = new objectinputstream (BAIS );
Object Ob = Ois. readobject ();
Ois. Close ();
Return (bstree) ob;
} Catch (exception e ){
System. Out. println ("clonenotsupportedexception:" + E );
E. printstacktrace ();
}
Return NULL;
}
 
/**
* Add a subnode
* @ Param Binary Search Tree node
*/
Public synchronized void addnode (bsnode node ){
If (null = This. m_root ){
This. m_root = node;
Return;
}

Bsnode tempnode = This. m_root;

While (true ){
If (node. getm_nvalue ()> tempnode. getm_nvalue () {// greater than the parent node
If (null = tempnode. getm_pright ()){
Tempnode. setm_pright (node );
Return;
} Else {
Tempnode = tempnode. getm_pright ();
Continue;
}
} Else if (node. getm_nvalue () <tempnode. getm_nvalue () {// smaller than the parent node
If (null = tempnode. getm_pleft ()){
Tempnode. setm_pleft (node );
Return;
} Else {
Tempnode = tempnode. getm_pleft ();
Continue;
}
} Else {// equal to the parent node
Return;
}
}
}
 
/**
* Generate a two-way linked list
* @ Return bidirectional linked list
* @ Throws clonenotsupportedexception
*/
Public synchronized bsdoublelist changetodoublelist (){
Bstree temptree = (bstree) This. Clone...
// In fact, it is changed to changetreetodoublelist (this. getm_root (); to match the meaning of the question. However, I like to use deep copy without damaging the original tree.
If (null! = Temptree ){
Changetreetodoublelist (temptree. getm_root ());
}
Bsdoublelist Dlist = new bsdoublelist ();
Dlist. sethead (templisthead );
Return Dlist;
}
 
Private void changetreetodoublelist (bsnode node ){
If (null = node ){
Return;
}
If (null! = Node. getm_pleft ()){
Changetreetodoublelist (node. getm_pleft ());
}
// ------------- Convert ---------------
Node. setm_pleft (templistnode );
If (null = templistnode ){
Templisthead = node;
} Else {
Templistnode. setm_pright (node );
}
//---------------------------------
Templistnode = node;
If (null! = Node. getm_pright ()){
Changetreetodoublelist (node. getm_pright ());
}
}
 
 
/**
* Print in-order traversal
*/
Public synchronized void print (){
If (null = This. m_root ){
System. Out. Print ("hashcode:" + this. hashcode () + "; empty tree ;");
Return;
}
System. Out. Print ("hashcode:" + this. hashcode () + "; tree :");
Print (this. m_root );
System. Out. println ();
}
 
Private void print (bsnode node ){
If (null! = Node ){
Print (node. getm_pleft ());
System. Out. Print (node. getm_nvalue () + "");
Print (node. getm_pright ());
}
}
}

/**
* Binary Search Tree (two-way linked list) node
*/
Class bsnode implements serializable {
/**
* V1.0
*/
Private Static final long serialversionuid = 6136767364555910395l;
 
Private int m_nvalue; // Value
Private bsnode m_pleft; // left (precursor) node
Private bsnode m_pright; // right (next) node
 
Public bsnode (INT value ){
This. m_nvalue = value;
}
 
Public int getm_nvalue (){
Return m_nvalue;
}
Public void setm_nvalue (INT mnvalue ){
M_nvalue = mnvalue;
}
Public bsnode getm_pleft (){
Return m_pleft;
}
Public void setm_pleft (bsnode mpleft ){
M_pleft = mpleft;
}
Public bsnode getm_pright (){
Return m_pright;
}
Public void setm_pright (bsnode mpright ){
M_pright = mpright;
}
}

/**
* Two-way linked list
*/
Class bsdoublelist {
Private bsnode head; // The leftmost header node.

Public bsnode gethead (){
Return head;
}

Public void sethead (bsnode head ){
This. Head = head;
}
 
Public synchronized void print (){
If (null! = This. Head ){
System. Out. Print ("hashcode:" + this. hashcode () + "; two-way linked list (forward ):");
While (true ){
If (null! = This. Head. getm_pright ()){
System. Out. Print (this. Head. getm_pright (). getm_nvalue () + "");
This. Head = This. Head. getm_pright ();
} Else {
Break;
}
}
System. Out. println ();
System. Out. Print ("hashcode:" + this. hashcode () + "; two-way linked list (reverse ):");
While (true ){
If (null! = This. Head ){
System. Out. Print (this. Head. getm_nvalue () + "");
This. Head = This. Head. getm_pleft ();
} Else {
Break;
}
}
} Else {
System. Out. println ("hashcode:" + this. hashcode () + "; empty linked list ;");
}
System. Out. println ();
}
 
}

Public class one {
Public static void main (string [] ARGs ){
Bstree tree = new bstree ();
Tree. addnode (New bsnode (10 ));
Tree. addnode (New bsnode (6 ));
Tree. addnode (New bsnode (14 ));
Tree. addnode (New bsnode (4 ));
Tree. addnode (New bsnode (8 ));
Tree. addnode (New bsnode (12 ));
Tree. addnode (New bsnode (16 ));
Tree. Print ();
Bsdoublelist Dlist = tree. changetodoublelist ();
Dlist. Print ();
System. Out. println ("original tree :");
Tree. Print ();
}
}

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.