Convert a binary search tree to a sorted two-way linked list-do not create a new node

Source: Internet
Author: User

The code is successfully compiled and run for the first time... Happy ~

Problem description:

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. For example:

10
/\
6 14
/\/\
4 8 12 16
Convert to a two-way linked list
4 = 6 = 8 = 10 = 12 = 14 = 16

Algorithm:

If there is no limit on "No new node can be created", you only need to perform a central traversal to construct a new node for the data value of each node.

Due to restrictions, we can only use existing nodes to adjust their pointers and convert the search tree to a two-way linked list. After the algorithm is completed, the original search tree does not exist.

Similar to the middle-order traversal, we use recursion to convert the left and right subtree of node T into a linked list and link them to the left and right sides of node T.

It is worth noting that the return values of the chain table of left and right subtree of T are different. T-> left should return the End Node of the chain table (largest ), t-> right should return the head node (minimum) of the linked list. Therefore, we need to set a singular flag to identify whether the processed node is a left child or a right child.

Code implementation:

# Include <iostream> using namespace STD; // The node Structure struct bstreenode {int data; bstreenode * left; bstreenode * right ;}; // The left and right subtree of the parent node returns one pointer and one tail. The flag is used to distinguish bstreenode * transform (bstreenode * t, int flag) {If (t-> left) // turn left subtree {T-> left = transform (t-> left, 0); t-> left-> right = T;} If (t-> right) // turn right subtree {T-> right = transform (t-> right, 1); t-> right-> left = T;} If (flag = 0) // This is the left subtree of the parent node and returns the rightmost node {While (t-> right) t = T-> right; return t;} If (flag = 1) // This is the right subtree of the parent node and returns the leftmost node {While (t-> left) t = T-> left; return t ;}} void main () {bstreenode * N4 = new bstreenode; bstreenode * N6 = new bstreenode; bstreenode * n8 = new bstreenode; bstreenode * N10 = new bstreenode; bstreenode * n12 = new bstreenode; bstreenode * N14 = new bstreenode; bstreenode * n16 = new bstreenode; // construct a binary tree N4-> DATA = 4; N4-> left = N4-> right = NULL; n8-> DATA = 8; n8-> left = n8-> right = NULL; n12-> DATA = 12; n12-> left = n12-> right = NULL; n16-> DATA = 16; n16-> left = n16-> right = NULL; N6-> DATA = 6; N6-> left = N4; N6-> right = n8; n14-> DATA = 14; N14-> left = n12; N14-> right = n16; N10-> DATA = 10; N10-> left = N6; n10-> right = N14; bstreenode * head = transform (N10, 1); // return the leftmost node while (Head-> right) // right output check {cout 


Output:

4 6 8 10 12 14 16

16 14 12 10 8 6 4


Convert a binary search tree to a sorted two-way linked list-do not create a new node

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.