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.
The essence of this problem is to investigate the use of recursion and the traversal of trees. The result of the ordinal traversal of the two-tuple lookup number is the order of the ordered target nodes.
Simply link the nodes to the end of the list in the order of the middle order traversal.
Code:
template<typename t> struct TreeNode {T data; treenode* Plchild; treenode* Prchild; }; Requires two output parameters to be initialized to null template<typename t> void convertbstree2list (the root node of the treenode<t>* ptreeroot/* tree */, treenode<t>*& plisthead/* The head pointer of the doubly linked list */, treenode<t>*& plistlast/* the tail pointer of the doubly linked list */) {if (Ptreeroot = = NULL) {return;} The middle sequence traverses the left subtree convertbstree2list (Ptreeroot->plchild, Plisthead, plistlast); Handle the current node, link the node to the tail of the doubly linked list//modify the left pointer of the current node, pointing to the tail of the doubly linked list ptreeroot->plchild = Plistlast; if (plistlast)//not the first node {plistlast->prchild = Ptreeroot;} else//First Node {plisthead = Ptreeroot;} plistlast = Ptreer Oot The middle sequence traverses right subtree convertbstree2list (Ptreeroot->prchild, Plisthead, plistlast); }