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 point pointer of the node in the tree can be adjusted.

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


In a binary tree, each node has two pointers that point to child nodes. In a doubly linked list, each node also has two pointers, pointing to the previous node and the latter node, respectively. Since the two nodes are similar in structure, and the binary search tree is also a sort of data structure, it is theoretically possible to implement 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 convert.

Since the chain list after conversion is ordered, we suspect that the sequential traversal of each node in the tree is due to the fact that the mid-order traversal algorithm is characterized by traversing each node of the binary tree in the order in which it was first arrived. When traversing to the root node, we take the tree as three parts: a node with a value of 10, a root node of 6 Zuozi, and a right subtree with a root node of 14. Based on the definition of a 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), and it links the smallest node of the right subtree (that is, a node with a value of 12).


In 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 its Saozi right subtree, as 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 can be created, only the point pointer of 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);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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