Topic 27: Two fork search tree and doubly linked list

Source: Internet
Author: User

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

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.