Java Implementation: convert a binary search tree into a sorted two-way linked list (tree)
Question
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.
10
/
6 14
//
4 8 12 16
Convert to a two-way linked list 4 = 6 = 8 = 10 = 12 = 14 = 16.
First, we define the data structure of the Binary Search Tree node as follows:
Struct BSTreeNode { Int m_nValue; // value of node |
BSTreeNode * m_pLeft; // left child of node |
BSTreeNode * m_pRight; // right child of node }; |
Solutions
Anyone familiar with the Binary Tree display in ascending order knows that for the current node, the left subnode is displayed first, then the subnode is displayed, and finally the subnode is displayed. This recursion completes the display of the central order of the binary tree. Similarly, this algorithm is also completed using the above method, but you need to understand the following three points: 1. the minimum subnode of the current node, that is, the Header element of the bidirectional linked list consisting of the current node, is the left subleaf node of the current element. 2. the next element of the two-way linked list constructed by the current node is the minimum node of the right child node of the current node, as shown in 1. 3. the last element of the two-way linked list constructed by the current node is the node of the last operation.
The Code is as follows:
Public class BSTreeNode {private Integer value; private BSTreeNode leftNode; private BSTreeNode rightNode;/*** insert a node. * @ Param value */public void add (int value) {if (this. value = null) {this. value = value;} else if (this. value> value) {if (leftNode = null) {leftNode = new BSTreeNode (); leftNode. value = value;} else {leftNode. add (value) ;}} else if (this. value <value) {if (rightNode = null) {rightNode = new BSTreeNode (); rightNode. value = value;} else {rightNode. add (value) ;}}/ *** traverse all nodes in the central order */public List
List () {List
Result = new ArrayList
(); If (this. value = null) {return result;} if (leftNode! = Null) {result. addAll (leftNode. list ();} result. add (this); if (rightNode! = Null) {result. addAll (rightNode. list ();} return result;} public BSTreeNode convertshortlist () {Map
Map = new HashMap
(); DoConvertLinkedList (map); return map. get (headNode);} private void doConvertLinkedList (Map
Map) {if (leftNode! = Null) {leftNode. doConvertLinkedList (map);} handleCurrentNode (map); if (rightNode! = Null) {rightNode. doConvertLinkedList (map) ;}} private void handleCurrentNode (Map
Map) {BSTreeNode lastNode = map. get (lastNode); this. leftNode = lastNode; if (lastNode = null) {map. put (headNode, this);} else {lastNode. rightNode = this;} map. put (lastNode, this);} public Integer getValue () {return this. value;} public BSTreeNode getLeftNode () {return this. leftNode;} public BSTreeNode getRightNode () {return this. rightNode ;}}
The jUnit test is as follows:
public class BSTreeNodeTest {@Testpublic void testAdd(){BSTreeNode root = new BSTreeNode();root.add(10);assertEquals(root.getValue(), Integer.valueOf(10));root.add(20);assertEquals(root.getRightNode().getValue(), Integer.valueOf(20));root.add(30);assertEquals(root.getRightNode().getRightNode().getValue(), Integer.valueOf(30));root.add(25);assertEquals(root.getRightNode().getRightNode().getLeftNode().getValue(), Integer.valueOf(25));}@Testpublic void testList() {BSTreeNode root = new BSTreeNode();List
valueList = new ArrayList
();valueList.add(10);valueList.add(20);valueList.add(30);valueList.add(25);for (Integer value : valueList){root.add(value.intValue());}List
nodeList = root.list();Collections.sort(valueList);for (int i = 0; i < nodeList.size(); i++){BSTreeNode node = nodeList.get(i);assertEquals(node.getValue(), valueList.get(i));}}@Testpublic void testConvertLinkedList(){BSTreeNode root = new BSTreeNode();int count = 100;Random random = new Random();List
valueList = new ArrayList
();for (int i = 0; i < count; i++){int value = random.nextInt(1000);if (!valueList.contains(value)){valueList.add(value);}}for (Integer value : valueList){root.add(value.intValue());}Collections.sort(valueList);BSTreeNode currentNode = root.convertLinkedList();for (int i = 0; i < valueList.size(); i++){assertEquals(currentNode.getValue(), valueList.get(i));currentNode = currentNode.getRightNode();}}}