Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list, requiring that no new nodes can be created, only the point pointer of the node in the tree can be adjusted.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/82/0A/wKiom1dIRfrxQCSkAAAJnPK-y1I749.png "title=" Binary search tree. png "alt=" Wkiom1dirfrxqcskaaajnpk-y1i749.png "/>
As shown above in the two-fork search tree, the bidirectional linked list converted to sort is 5-><-6-><-7-><-8-><-9-><-10-><-11.
Because the request is converted into a doubly linked list, and there are two pointers for each node of the binary tree, so you can directly adjust the pointer direction, and for the search binary tree, the value of the left subtree of each node is smaller than the value of the root node, and the value of each right subtree is greater than the value of the current node, and it is required to be converted into a sorted doubly linked list. For each node of the access order should be first left before the right, that is, the idea of using the middle sequence traversal, so as to ensure that the order;
For pointers, you can refer to the pointer to the left node for each node as the Prev pointer to the previous node in the doubly linked list, and the pointer to the right node for each node as the next pointer to the next node in the doubly linked list, so that for the leftmost node of the search binary tree, it is actually the smallest value in the tree. That is, the head node of the doubly linked list, and the most right node of the tree is the tail node of the linked list, which is the maximum value;
It can be found that for the root node, its prev should point to the right node of Zuozi, and next should point to the leftmost node of the right subtree, so for the whole tree, it can be divided into the left and the left sub-tree to be recursive;
The
is programmed as follows:
#include <iostream> #include <assert.h>using namespace std;struct binarytreenode//two fork tree node structure { int _val; binarytreenode *_ Lnode; binarytreenode *_rnode; binarytreenode (Int val) Constructor :_val (Val) ,_lnode (NULL) ,_rnode (NULL) {} };//creates a two-fork search tree, where a two-fork search tree binarytreenode* _create (const int *arr , size_t& index, size_t size) { if (index < size) && (arr[index] != ' # ')) { binarytreenode* root = new binarytreenode (Arr[index]); &nbsP; root->_lnode = _create (arr, ++index, size); root->_rnode = _create (arr, ++index, size); return root; } else return null;} Binarytreenode* createbinarytree (const int *arr, size_t size) { assert (arr && size); size_t index = 0; return _create (arr, index, size);} Void destorybinarytree (Binarytreenode* listnode) { binarytreenode) after the destruction of the transformed doubly linked list * tmp = listnode; while (listnode != null) { tmp = listnode; listnode = listnode->_lnode; delete tmp; }}//Pre-sequence traversal test binary tree Void prevorder (binarytreenode *root) { if (root != null) { cout<<root->_val<< " "; Prevorder (Root->_lnode); prevorder (Root->_Rnode); }}//two fork search tree converted to doubly linked list void bsttolist (binarytreenode* root, binarytreenode** Lastnode) { if (root != null) { bsttolist (Root->_lnode, lastnode); root->_lnode = *lastnode;//If the left node is empty, the previous pointer to the current node points to the last node in the current list if (*lastnode ! = null) (*lastnode)->_rnode = root;//sets the next pointer of the last node of the current list to the current node *lastnode = root;//updates the last node of the list to the current node bsttolist (root->_rnode, Lastnode);//continue to traverse the right subtree until it is empty }}void printlist (Binarytreenode* listnode) { assert (ListNode); binarytreenode* tmp = listnode; cout<< "Listhead:" <<tmp->_val<<endl; cout << "Forward Print List:"; while (Tmp->_rnode != null)//The right pointer of each node points to the next node { cout<<tmp->_val<< "; " tmp = tmp->_Rnode; } cout<<tmp->_val<< "->null" <<endl; cout<< "Listtail:" <<tmp->_val <<endl; cout<< "Reverse Printing chain list:"; while (tmp->_lnode ! = null)//The left pointer of each node points to the previous node { cout< <tmp->_val<< " tmp = tmp->_lnode"; ; } cout<<tmp->_val<< "->NULL" <<ENDL;} Int main () { int arr[] = {8,6,5, ' # ', ' # ', 7, ' # ', ' # ', 10, 9, ' # ', ' # ', 11, ' # ', ' # '}; binarytreenode* root = createbinarytree (arr, sizeof (arr)/ sizeof (Arr[0])); cout<< "prevorder: "; prevorder (Root) ; cout<<endl; binarytreenode* lastnode = null; bsttolist (Root, &lastnode); binarytreenode* listnode = root; while (listnode->_lnode != null)//Get the head node of the list listnode = listnode->_lnode; printlist ( ListNode); destorybinarytree (listnode); return 0;}
To run the program:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/42/wKioL1dPuhDiRkoVAAAgm4YgYHo622.png "title=" Bsttolistret.png "alt=" Wkiol1dpuhdirkovaaagm4ygyho622.png "/>
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1785449
Binary search tree and doubly linked list--27