Data structure--tree--two-fork lookup tree Convert to sorted circular doubly linked list

Source: Internet
Author: User

Title Description

Enter a binary lookup tree to convert the two-fork lookup tree into a sorted circular doubly linked list.
Requires that no new nodes be created, only pointers are adjusted, and new storage space is not opened O (1)

Problem analysis

First, the solution to our problem is to simplify the problem first. (premise you should understand binary search tree and doubly linked list)

If there is no requirement, then we will naturally think of traversing the whole tree and then sorting it out and then rebuilding a list of what you want to be.

So what we need to consider is how to traverse?

Then slowly complex problems, can not be newly established, then it is necessary to change the original basis, how to change the past?

At this time, I would like to teach you about the tree problem, there is a trick particularly good is to draw out, a lot of problems actually drawn out is easy, just the head to think about unless your memory and geometric ability is very strong ...

Drawing analysis

Orange Line originally exists, other colors are to be added after the order, the need to follow the fork removal;

This painting is more ugly, forgive me, but the meaning is this meaning, you draw also came out. This side because the request is a circular chain list, so also need to connect 1 and 7, I do not explain on the diagram, afraid of the picture more chaotic.

Then there may be people wondering, look at the figure we know how to change the pointer, but actually how to change it? Not every picture is as regular as it is.

Don't worry, the problem is still big, we still need to change the problem again smaller.

First we think that if only three elements of the 123 arrangement, then it is good to do.

If you add another 4, it will be difficult.

As a tree, you need to think about it in three ways, left, middle, and right.

When the fourth element appears, if the fourth element is in the right and 31 groups, it has become a linked list, then simply consider this list as the original 3, you can change back to simple problems.

In the same vein, looking at the larger image above, if the left subtree of the 6 element becomes a linked list, the right subtree becomes a list of the final needs, and we just need to connect them to three.

So how do you turn the left into a linked list?

Similarly, as long as the left subtree of 2 elements into a linked list, the right sub-tree becomes a linked list, not connected to the line?

。。。。

And then gradually we find that the algorithm or function we ultimately need is:

1, the simplest, up to 3 elements, the tree into a linked list

2. Merge linked list

3, recursive traversal (we must first deal with the innermost, processing well outside can be merged, along with our thinking down, not exactly recursion it?) )

Code description
/** * Two fork lookup tree converted to sorted circular doubly linked list * Create the following form of the tree * 6* 2 7* 1-3 5***/#include<cstdio>#include<cstdlib>#include<iostream>using namespaceStd;typedefstructnode{intData//data fields    structnode* Pleft;//left dial hand leaf    structnode* Pright;//Right sub-lobe}node, *pnode;//Node is equivalent to struct node,pnode equivalent to struct node*//Insert a node in the tree in order to create a tree (iteration)voidInsert_tree (pnode root, Pnode newNode) {pnode Nownode=Root; Pnode NextNode=Root; //find a location before stopping     while(NextNode! =NULL) {        //if the element to be joined is greater than the current node, it should be placed to the right        if(Newnode->data > nextnode->data) {Nownode=NextNode; NextNode= nextnode->Pright; }        Else{Nownode=NextNode; NextNode= nextnode->Pleft; }    }    if(Newnode->data > nownode->data) {Nownode->pright =NewNode; }    Else{Nownode->pleft =NewNode; }} //Create TreePnode Create_tree () {intdataarray[7] = {6,7,2,1,4,3,5}; Pnode Root= (Pnode)malloc(sizeof(NODE)); Root->data = dataarray[0]; Root->pleft =NULL; Root->pright =NULL;  for(intI=1; i<=6; i++) {Pnode NewNode= (Pnode)malloc(sizeof(NODE)); NewNode->data =Dataarray[i]; NewNode->pleft =NULL; NewNode->pright =NULL;    Insert_tree (Root,newnode); }    returnRoot;}//connect two linked listsPnode connect_list (pnode First, pnode second) {if(First = =NULL)returnsecond; if(Second = =NULL)returnFirst ; //take the last element of the left and right side, respectively, to form loops and linksPnode first_last = first->Pleft; Pnode Second_last= second->Pleft; First_last->pright =second; Second->pleft =First_last; Second_last->pright =First ; First->pleft =Second_last; returnFirst ;}//tree-"Linked list"Pnode tree_to_list (pnode root) {if(Root = =NULL)returnNULL; //the left becomes a linked list, the right becomes a list, and finally it's connected.Pnode first = Tree_to_list (root->pleft); Pnode Second= Tree_to_list (root->pright); Root->pleft =Root; Root->pright =Root; First=Connect_list (first, root); First=Connect_list (first, second); returnFirst ;} //Print all elements of the resulting linked listvoidprint_list (Pnode phead) {intA = phead->data;  while(Phead! =NULL) {cout<<pHead->data<<" "; Phead= phead->Pright; if(A = = Phead->data) Break; } cout<<Endl; return;}intMain () {Pnode root=NULL; Root=Create_tree ();    Print_list (Tree_to_list (root)); return 0;}

Summarize

The topic of the tree, to pay attention to a few points

1. How to Traverse

2. Turn big problems into small problems

3. Try to solve the problem with recursion

Data structure--tree--two-fork lookup tree Convert to sorted circular doubly linked list

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.