# Sword Point (Java Edition): Two binary search tree and doubly linked list

Source: Internet
Author: User

Title: Enter a binary search tree to convert the two-fork search tree into a sorted doubly linked list. Requires that a new node cannot be created. Only the pointer to the node in the tree can be adjusted.

For example, in a two-fork search tree. The sort doubly linked list after the output conversion is:

In a binary tree, each node has two pointers to the child nodes. In a doubly linked list. Each node also has two pointers. They point to the previous node and the latter node, respectively. Because the two nodes are structurally similar, the binary search tree is also a sort of data structure at the same time. It is therefore theoretically possible to achieve a two-fork search tree and a sort of bidirectional linked list conversion.

In the search binary tree, the value of the left Dial hand node is always less than the value of the parent node, and the value of the right child node is always greater than the parent node's value. So when we convert a doubly linked list that is called a sort, the pointer to the left Dial hand node is adjusted to the pointer to the previous node in the list, and the pointer to the right child node is adjusted to the two tables as pointers to the next node.

Next we consider how to transform.

Since the chain list after conversion is ordered, we suspect that the sequential traversal of each node in the tree. This is due to the fact that the mid-sequence traversal algorithm is characterized by traversing each node of the binary tree in the order in which it was arrived. When traversing to the root node. We take the tree as three parts: a node with a value of 10. The root node is 6 Zuozi, and the root node is the right subtree of 14.

Based on the definition of the sorted list. A node with a value of 10 is linked to the largest node of its left subtree (that is, a node with a value of 8). At the same time it also links the smallest node of the right subtree (that is, a node with a value of 12).

According to the order of the sequence traversal, when we traverse the transition to the root node (a node with a value of 10), its left subtree has been converted into a sorted list, and the last node in the list is the largest node of the current value. We link the node root with a value of 8, at which point the last node in the list is 10. Then we go through the transformation of the right subtree, and link the root node with the smallest node in the right subtree.

As to how to convert it to the Saozi right subtree. Because the traversal and conversion process is the same, we naturally think of recursion.

Java Code Implementation:

`/** * Enter a binary search tree to convert the two-fork search tree into a sorted doubly linked list.* requires that no new nodes be created, no matter what. Only the pointer to the node in the tree can be adjusted.*/package Swordforoffer;import Utils. binarytreenode;/** * @author Jinshuangqi * * August 6, 2015 */public class E27convertbinarysearchtree {public Binarytreenode Co Nvert (Binarytreenode root) {binarytreenode node = Null;convert (Root,node); while (node! = NULL && Node.leftnode! = NULL) {node = Node.leftnode;} return node;} public void convert (Binarytreenode root,binarytreenode lastnode) {if (root = null) return; Binarytreenode current = Root;if (Current.leftnode! = null) convert (current.leftnode,lastnode); Current.leftnode = Lastnode;if (Lastnode! = null) Lastnode.rightnode = Current;if (Current.rightnode! = null) CONVERT (Current.rightnode, lastnode);}}`

Sword Point (Java Edition): Two binary search tree and 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.