Beginner's series of C + + Classic questions (I.)

Source: Internet
Author: User

This is the first article I've written for the rookie series.

Title: 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.
For example, the two-dollar lookup tree
    10
/    \
6 14
/\/\
4 8 12 16
convert to doubly linked list
4=6=8=10=12=14=16.
Analysis: The question is Microsoft's face. Many tree-related topics are solved by recursive thinking, which is no exception. Here we use two different recursive ideas to analyze.
idea one: When we arrive at a node ready to adjust to the node as the root node of the subtree, first adjust its left subtree to convert the left sub-tree into a well-ordered left sub-list, and then adjust its right sub-tree conversion right sub-list. Most recently linked left dial hand the right-most node of the linked list (the largest node of the left subtree), the leftmost node of the current node and right sub-list (the smallest node of the right subtree). All nodes are adjusted recursively from the root node of the tree.
idea two: we can traverse the whole tree in sequence. The tree is traversed in this way, and the smaller nodes are accessed first. If we visit a node each, assuming that the previously visited node has been adjusted to a sort doubly linked list, we then link the pointer that adjusts the current node to the end of the list. When all nodes have been visited, the whole tree is converted into a sort doubly linked list.
Reference code:
First we define the data structure of the two-tuple lookup tree node as follows:

struct Bstreenode//A node in the binary search tree{    int          m_nvalue;//value of node    bstreenode  *m_pleft;  //left child of node    Bstreenode  *m_pright;//Right Child of Node};

Idea of a corresponding code:
Covert a sub binary-search-tree into a  Sorted double-linked list//input:pnode-the Head of the sub tree//Asright-whether Pnode is the right child of  Its parent//output:if asright are true, return the least node in the sub-tree//else return the greatest node in The sub-tree///////////////////////////////////////////////////////////////////////bstreenode* ConvertNode (    bstreenode* Pnode, bool asright) {if (!pnode) return NULL;    Bstreenode *pleft = NULL;    Bstreenode *pright = NULL;    Convert the left sub-tree if (pnode->m_pleft) Pleft = Convertnode (Pnode->m_pleft, false);         Connect the greatest node in the left sub-tree to the current node if (pleft) {pleft->m_pright = Pnode;    Pnode->m_pleft = Pleft;    }//Convert the right sub-tree if (pnode->m_pright) pright = Convertnode (Pnode->m_pright, true); Connect theLeast node in the right sub-tree to the current node if (pright) {pnode->m_pright = Pright;    Pright->m_pleft = Pnode;    } bstreenode *ptemp = Pnode; If The current node was the right child of its parent,//return the least node in the whose root is the Curren    T node if (asright) {while (ptemp->m_pleft) ptemp = ptemp->m_pleft;  }//If The current node was the left child of its parent and//return the greatest node in the tree whose root is the    Current node else {while (ptemp->m_pright) ptemp = ptemp->m_pright; } return ptemp;} Covert a binary search tree into a sorted double-linked list//input:the Head of tree//output:the head of sorted double-linked list////////////////////////////// bstreenode* Convert (bstreenode* pheadoftree) {//As we want to return thE Head of the sorted double-linked list,//We set the second parameter to be true return Convertnode (Pheadoftree, t Rue);}
Idea two corresponds to the code:

Covert a sub binary-search-tree into a Sorted double-linked list//input:pnode-the Head of the sub tree//plastnodeinlist-the tail of the D ouble-linked list///////////////////////////////////////////////////////////////////////void ConvertNode (    bstreenode* Pnode, bstreenode*& plastnodeinlist) {if (Pnode = = NULL) return;    Bstreenode *pcurrent = Pnode; Convert the left sub-tree if (pcurrent->m_pleft! = NULL) Convertnode (Pcurrent->m_pleft, plastnodeinlist    );     Put the current node into the double-linked list pcurrent->m_pleft = plastnodeinlist;    if (plastnodeinlist! = NULL) plastnodeinlist->m_pright = pcurrent;    Plastnodeinlist = pcurrent; Convert the right sub-tree if (pcurrent->m_pright! = NULL) Convertnode (Pcurrent->m_pright, Plastnodeinl IST);} /////////////////////////////////////////////////////////////////////Covert a binary search tree into a sorted double-linked list//input:pheadoftree-the Head of tree//output:the he Ad of sorted double-linked list///////////////////////////////////////////////////////////////////////bstreenode*    Convert_solution1 (bstreenode* pheadoftree) {Bstreenode *plastnodeinlist = NULL;    Convertnode (Pheadoftree, plastnodeinlist);    Get the head of the double-linked list bstreenode *pheadoflist = plastnodeinlist;    while (pheadoflist && pheadoflist->m_pleft) pheadoflist = pheadoflist->m_pleft; return pheadoflist;}

This article has been included in the "sword refers to offer--name Enterprise interviewer explaining typical programming problem" a book, there are changes, welcome attention. Blogger Huang is entitled to the copyright of this blog post. Network Reprint Please specify the source http://zhedahht.blog.163.com/. to organize your publication, please contact the author. Have any suggestions to solve the problem, please let me know in the comments, or add my Weibo http://WEIBO.COM/ZHEDAHHT or HTTP://T.163.COM/ZHEDAHHT to discuss with me. Thank you. I also wrote the English version of this blog, interested readers please go to http://codercareer.blogspot.com/2011/09/interview-question-no-1-binary-search.html view. Thanks for sharing!







Related Article

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.