[Question 100] Question 1

Source: Internet
Author: User
 

1.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
};

# Include <stdio. h>
Struct bstreenode // struct
{
Int m_nvalue; // node Value
Bstreenode * m_pleft; // left child
Bstreenode * m_pright; // right child
};
Typedef bstreenode doublelist; // use typedef to declare a custom type
 
Doublelist * phead;
Doublelist * plistindex;
Void converttodoublelist (bstreenode * pcurrent); // convert it to a two-way linked list
// Create a binary search tree
Void addbstreenode (bstreenode * & pcurrent, int value)
{
If (null = pcurrent) // use the end Insertion Method
{
Bstreenode * pbstree = new bstreenode (); // Declaration
Pbstree-> m_pleft = NULL;
Pbstree-> m_pright = NULL;
Pbstree-> m_nvalue = value;
Pcurrent = pbstree; // make the created node equal to the last NULL pointer
}
Else
{
If (pcurrent-> m_nvalue)> value) // place a small value to the left
{
Addbstreenode (pcurrent-> m_pleft, value); // recursive call
}
Else if (pcurrent-> m_nvalue) <value) // place the big one to the right
{
Addbstreenode (pcurrent-> m_pright, value );
}
Else
{
Printf ("adding nodes again ");
// Cout <"adding nodes again" <Endl;
}
}
}

Void ergodicbstree (bstreenode * pcurrent) // recursive traversal in the middle order
{
If (null = pcurrent)
{
Return;
}
Ergodicbstree (pcurrent-> m_pleft );
Converttodoublelist (pcurrent); // The node is connected to the end of the linked list
Ergodicbstree (pcurrent-> m_pright );
}

Void converttodoublelist (bstreenode * pcurrent) // convert a binary tree to a two-way linked list
{
Pcurrent-> m_pleft = plistindex;
If (null! = Plistindex) // elements contained in a two-way linked list
{
Plistindex-> m_pright = pcurrent;
}
Else
{
Phead = pcurrent;
}
Plistindex = pcurrent; // this step is the key to ensure that after each step, the end of the double-stranded table is inserted.
Printf ("% d \ n", pcurrent-> m_nvalue); // output the current node value inserted
}
Int main ()
{
Bstreenode * proot = NULL;
Plistindex = NULL;
Phead = NULL;
Addbstreenode (proot, 10 );
Addbstreenode (proot, 4 );
Addbstreenode (proot, 6 );
Addbstreenode (proot, 8 );
Addbstreenode (proot, 12 );
Addbstreenode (proot, 14 );
Addbstreenode (proot, 15 );
Addbstreenode (proot, 16 );
Ergodicbstree (proot); // traverse the tree in the central order, create a linked list, and output each node in the traversal order
Return 0;
}

 

//////////////////////////////////////// ///////
4
6
8
10
12
14
15
16
Press any key to continue
//////////////////////////////////////// //////

--------------------------------------------------------------
This is also the last question. Let's take a look at the concise code and understand the efficiency and beauty of C:
Void change (node * P, node * & last) // supports sequential traversal.
{
If (! P)
Return;
Change (p-> left, last );
If (last)
Last-> right = P;
P-> left = last;
 
Last = P;
Change (p-> right, last );
}

Void main ()
{
Node * root = create ();
Node * tail = NULL;
Change (root, tail );
While (tail)
{
Cout <tail-> data <"";
Tail = tail-> left;
}
Cout <Endl;
}

-------------------------- The following is the second Writing Method ------------------------
# Include <iostream>
Using namespace STD;

Class node {
Public:
Int data;
Node * left;
Node * right;
 
Node (int d = 0, node * LR = 0, node * RR = 0): Data (D), left (LR), right (RR ){}
};

Node * Create ()
{
Node * root;
Node * P4 = new node (4 );
Node * P8 = new node (8 );
Node * P6 = new node (6, P4, P8 );
 
Node * p12 = new node (12 );
Node * p16 = new node (16 );
Node * p14 = new node (14, p12, p16 );
 
Node * P10 = new node (10, P6, p14 );
Root = P10;
 
Return root;
}

Node * Change (node * P, bool asright)
{
If (! P)
Return NULL;
Node * pleft = change (P-> left, false );
If (pleft)
Pleft-> right = P;
P-> left = pleft;
 
Node * pright = change (P-> right, true );
If (pright)
Pright-> left = P;
P-> right = pright;
 
Node * r = P;
If (asright)
{
While (R-> left)
R = r-> left;
} Else {
While (R-> right)
R = r-> right;
}
Return R;
}

Void main (){
Node * root = create ();
Node * tail = change (root, false );
While (tail)
{
Cout <tail-> data <"";
Tail = tail-> left;
}
Cout <Endl;
 
Root = create ();
Node * head = change (root, true );
While (head)
{
Cout Head = head-> right;
}
Cout <Endl;
}

 

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.