Java implementation: Turn the two-dollar lookup tree into a sorted doubly linked list (tree)

Source: Internet
Author: User
Tags addall

Topic

Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list. Requires that no new nodes be created, only the pointer is adjusted.

10

/    \
6 14

/   \     /   \

4 8 12 16

convert to doubly linked list 4=6=8=10=12=14=16 .

First we define the data structure of the two-tuple lookup 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

};


Thinking of solving problems         Familiar with the binary tree in the sequence display of the students know, for the current node, the first display left dial hand nodes, and then show themselves, finally showed a child node, the use of such recursion to complete the two-fork tree in the sequence display.Similarly, this algorithm is done in the same way, but it is necessary to understand the following three points:1. The child node of the current node, that is, the head element of the doubly linked list consisting of the current node, is the left sub-leaf node of the current element.2. The next element of the doubly linked list constructed by the current node is the smallest node of the right child node of the current node, see 1.3. The previous element of the doubly 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 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.va Lue < value) {if (Rightnode = = null) {Rightnode = new Bstreenode (); rightnode.value = value;} else {rightnode.add (value); }}}/** * middle order traversal all nodes */public list<bstreenode> List () {list<bstreenode> result = new Arraylist<bstreenode > (); 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 convertlinkedlist () {map<string, bstreenode> Map = new hashmap<string, bstreenode> (); Doconvertlinkedlist (map); return Map.get ("Headnode");} private void Doconvertlinkedlist (map<string, Bstreenode> map) {if (Leftnode! = null) {leftnode.doconvertlinkedlist (map);} Handlecurrentnode (map); if (rightnode! = null) {rightnode.doconvertlinkedlist (map);}} private void Handlecurrentnode (map<string, bstreenode> 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;}}

JUnit tests are as follows
public class Bstreenodetest {@Testpublic void Testadd () {Bstreenode root = new Bstreenode (); Root.add (n); Assertequals ( Root.getvalue (), integer.valueof ()), Root.add, Assertequals (Root.getrightnode (). GetValue (), integer.valueof ( ); Root.add (+); Assertequals (Root.getrightnode (). Getrightnode (). GetValue (), integer.valueof (+)); Root.add (25) ; Assertequals (Root.getrightnode (). Getrightnode (). Getleftnode (). GetValue (), integer.valueof (25));} @Testpublic void Testlist () {Bstreenode root = new Bstreenode (); list<integer> valueList = new arraylist<integer> (); Valuelist.add (Valuelist.add); Valuelist.add ( (Valuelist.add); for (Integer value:valuelist) {Root.add (Value.intvalue ());} list<bstreenode> nodeList = Root.list (); Collections.sort (valueList); for (int i = 0; i < nodelist.size (); i++) {Bstreenode node = nodelist.get (i); Assertequals (n Ode.getvalue (), Valuelist.get (i));}} @Testpublic void Testconvertlinkedlist () {Bstreenode root = new Bstreenode (); int count = 100; Random RANdom = new Random (); list<integer> valueList = new arraylist<integer> (); for (int i = 0; i < count; i++) {int value = Random.nexti NT (+); 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 ();}}}





Java implementation: Turn the two-dollar lookup tree into a sorted doubly linked list (tree)

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.