Title: Enter a binary search tree and convert the two-fork search tree into a sorted doubly linked list. Requires that no new nodes can be created, only the direction of the nodes in the tree can be adjusted. As shown in the following:
The nodes of the binary tree are defined as follows:
1 struct Binarytreenode 2 {3 int M_nvalue; 4 binarytreenode* M_pleft; 5 binarytreenode*6 };
1binarytreenode* CONVERT (binarytreenode*prootoftree)2 {3 //Plastnodelist points to the last node of the linked list that has been converted4bianrytreenode* plastnodeinlist =NULL;5Convertnode (Prootoftree, &plastnodeinlist);6 //plastnodeinlist point to the tail node of the doubly linked list, you need to find the head node .7bianrytreenode* pheadoflist =plastnodeinlist;8 while(Pheadoflist! = NULL && Pheadoflist->m_pleft! =NULL)9Pheadoflist = pheadoflist->M_pleft;Ten returnpheadoflist; One } A - voidConvertnode (binarytreenode* Pnode, binarytreenode**plastnodelist) - { the if(Pnode = =NULL) - return; -binarytreenode* pcurrent =Pnode; - if(Pcurrent->m_pleft! =NULL) +Convertnode (pcurrent->M_pleft, plastnodelist); -Pcurrent->m_pleft = *plastnodeinlist; + if(*plastnodeinlist! =NULL) A(*plastnodeinlist)->m_pright =pcurrent; at*plastnodeinlist =pcurrent; - if(pcurrent->m_pright!=NULL) -Convertnode (pcurrent->m_pright, plastnodelist); -}
Topic 27: Two fork search tree and doubly linked list