# Doubly linked list (4)-sort binary tree converted to circular doubly linked list

Source: Internet
Author: User

construct a recursive function treetolist (Node root), a sorted two-fork tree, adjust the internal pointer to make it look from the outside, is a circular doubly linked list. Where the forward pointer is stored in the "small" area, and the back pointer is stored in the "large" area. The list needs to be adjusted to sort in ascending order and return the linked table header pointer.
The following article explains the process of this transformation in detail. http://cslibrary.stanford.edu/109/TreeListRecursion.htmlThe following is a translation of this article:

The great tree-list recursion problemcontent:
1. Ordered binary Tree
2. Circular doubly linked list
3. The challenge
4. Problem Statement
5. Lessons and Solution Code

Introduction:This problem uses two data structures-a sorted two-fork tree, and a circular doubly linked list. Both of these stores are sorted elements, but they look completely different.

1. Sort the binary tree (Ordered Binary Tree)In a sorted two-fork tree, each node contains a separate data element and a "small" and "large" pointer to the subtree (sometimes these two pointers are called the left and right pointers). The following is a sorted two-fork tree that contains elements 1 through 5.

Figure 1-sorted two-fork tree
where all nodes in the "small" subtree are less than or equal to the parent node. All nodes in the "large" subtree, greater than the parent node. So in the above example, the nodes in the "small" subtree will be less than or equal to 4, and all nodes in the "large" subtree will be greater than 4. This rule applies to every node in the tree. The null pointer is used to indicate the end of a branch. In fact, a null pointer represents a tree of 0 nodes. The node at the apex of the tree is called the root node.

2. Circular doubly linked list (Circular doubly Linked list)Here is a circular doubly linked list of 1 to 5

Figure 2-Circular doubly linked listThe circular doubly linked list has the following two additional features relative to the normal list:
a). " Bidirectional "represents each node with two pointers-a back pointer" next "and a forward pointer" previous ". b). " The Loop "represents a linked list without a start and end point. In fact, the last node's back pointer, pointing to the first node. The forward pointer to the first node, which points to the last node.
Typically, a null pointer represents a list of 0 elements. and the 1-length list looks a little odd. As shown in the following:

Figure 3-circular doubly linked list with a length of 1The node in the linked list of length 1 is both the first node and the last node (stub point), so forward pointers and back pointers point to itself. So even a 1-length linked list adheres to the above features.
Nature-nodes are constructed in different waysThis is the essence of great tree-list problem: The nodes in the two-fork tree have the same type structure as the nodes in the list-they all contain one element and two pointers. The only difference is that the two pointers in the binary tree are identified as "small" and "large", and the list is labeled "Previous" and "Next". After ignoring the identity, the two nodes are the same type.

3. ChallengesThe real difficulty is to convert this binary tree into a circular doubly linked list. The "small" pointer represents "previous", while the "large" pointer represents "next". After the conversion is complete, you need to adjust the list to sort in ascending order.

Figure 4-two fork tree after adding a back pointer (next)in the above figure, the black line represents the back pointer in the original binary tree, which is represented by arrows in the linked list. The forward pointer is not being demonstrated.

Figure 5-two fork tree with forward pointer and back pointer addedThe above diagram shows all the situations of this problem. The primitive binary tree uses black lines, and a back/forward pointer is added to the binary tree. Note the position of the head pointer, its forward pointer, and the back pointer, which form a linked list containing the element, 2. Although each node in the two graphs has different spatial allocations, this is only the difference in their data structure properties. The pointer structure is the key.

4. Summary of Issuesconstruct a recursive function treetolist (Node root), a sorted two-fork tree, adjust the internal pointer to make it look from the outside, is a circular doubly linked list. Where the forward pointer is stored in the "small" area, and the back pointer is stored in the "large" area. The list needs to be adjusted to sort in ascending order and return the linked table header pointer. The operation can be done at O (n) time-because it essentially needs to be done only once on each node. Usually figure 1 as input, after adjusting the pointer, generate Figure 2.

5. Specific implementation of some tipsa). Recursion is the key. Each subtree is recursive and the return result of each recursive is merged. B). Recursion will gradually adjust the small and large subtree to the linked list. Then combine all these linked lists into a complete list. Define a function append (Node A, node B) separately to receive input from two linked lists, and then return the merged list. This function, which is independent, also reduces the complexity of logic in recursive functions.

6. Code implementation
`#include <iostream>struct node{int data;struct node *small;struct node *large;};/ /Auxiliary function-merges two nodes. void Joinnode (node* A, Node * b) {/*a B*/a->large = B;b->small = A;} Auxiliary functions-Merge two loops doubly linked list node* appendlist (node *a, node *b) {node *alast, *blast;if (a = = null) return b;if (b = = null) return A;al AST = A->small;blast = B->small;joinnode (alast, b); Joinnode (BLast, a); return A;} recursive function//convert a sorted binary tree into a circular doubly linked list and return the linked list. node* treetolist (node *root) {node *alist, *blist;if (root = NULL) return null;alist = Treetolist (root->small); blist = t Reetolist (Root->large);//construct a linked list of length length-1 Root->small = Root;root->large = Root;alist = Appendlist (AList, root); alist = Appendlist (alist, blist); return alist;} node* createnode (int data) {node * node = new Node;node->data = Data;node->small = Null;node->large = Null;return node;}  void Insertnode (node **rootref, int data) {Node *root = *rootref;if (root = = NULL) *rootref = CreateNode (data); Else{if (data <= root->data) Insertnode (& (Root->small), data) Elseinsertnode (& (Root->large), data);}} void Printlist (const node *head) {Const node *current = Head;while (current! = NULL) {std::cout << current->data &L t;< ""; current = current->large;if (current = = head)//has completed a list traversal break;} Std::cout << Std::endl;} int main () {Node *root = NULL, *head = Null;//4-->2-->1-->3-->5insertnode (&root, 4); Insertnode (& root, 2); Insertnode (&root, 1); Insertnode (&root, 3); Insertnode (&root, 5); head = treetolist (root); Printlist (head); return 0;}`
Output:
1 2 3) 4 5

Doubly linked list (4)-sort binary tree converted to circular doubly linked list

Related Keywords:

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.