面试100题:1.把二元查找树转变成排序的双向链表

来源:互联网 发布:监控远程控制软件 编辑:程序博客网 时间:2024/06/05 06:27

题目

输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。

        10

      /      \

    6        14

   /  \      /   \

 4   8   12  16

转换成双向链表4<=>6<=>8<=>10<=>12<=>14<=>16。

分析

要明白二元查找树和双向链表的各自特点

  1. 二元查找树有左右子树,左节点永远小于根节点,右子树永远大于(等于)根节点。
  2. 二元查找树的中序遍历,会将树按照从小到大有序排列。所以该题目应该考虑从中序遍历着手,在中序遍历的同时修改二元查找树的左右子树链接指向,从而构造一个排序的双向链表。
  3. 二元查找树的左右子节点可以作为双向链表的左链接和右链接的候选,只不过需要合理的调整它们使之指向正确的左右链接结点。

解一

利用中序遍历,递归求解

  1. /*Title: 1.把二元查找树转变成排序的双向链表:解一 
  2. Author:  gocode 
  3. Date:    2012-09-27*/  
  4.   
  5. /*       10 
  6.       /      \ 
  7.      6        14 
  8.    /  \      /   \ 
  9.   4    8    12   16    */  
  10.   
  11. #include <stdio.h>  
  12. #include <iostream>  
  13. using namespace std; //如果不加std,则cout需要写成std::cout  
  14.   
  15. // 定义双向链表的节点  
  16. typedef struct BSTreeNode  
  17. {  
  18.     int m_nValue; // 结点值  
  19.     BSTreeNode *m_pLeft; // 左节点  
  20.     BSTreeNode *m_pRight; // 右节点  
  21. } DoubleList;  
  22. // 定义全局变量  
  23. DoubleList *pHead;  
  24. DoubleList *pListIndex; // 辅助节点指针,指示双向链表的当前节点  
  25.   
  26. //void  convertToDoubleList(BSTreeNode * pCurrent); // 如果该函数放在ergodicBSTree函数之后,则此处必须声明convertToDoubleList  
  27.   
  28. // 二叉树转换成双向链表  
  29. void  convertToDoubleList(BSTreeNode * pCurrent)  
  30. {  
  31.     // 当前pCurrent结点是后加入的节点,它永远把pListIndex作为左节点,  
  32.     // 因为pListIndex是作为双向链表的当前节点指示器,所以pListIndex在pCurrent的左边,相应的pListIndex的右节点就是pCurrent  
  33.     pCurrent->m_pLeft = pListIndex;  
  34.     if (NULL != pListIndex)  
  35.         pListIndex->m_pRight = pCurrent;  
  36.     else  
  37.         pHead = pCurrent; // 建立双向链表的头节点  
  38.    
  39.     pListIndex = pCurrent; // 移动pListIndex指示到最新加入的节点  
  40.     cout<<pCurrent->m_nValue<<" ";  
  41. }  
  42.   
  43. // 创建二元查找树  
  44. void addBSTreeNode(BSTreeNode *& pCurrent, int value)  
  45. {  
  46.     // 当前节点为空,则创建新节点并赋值  
  47.     if (NULL == pCurrent)  
  48.     {  
  49.         BSTreeNode * pBSTree = new BSTreeNode();  
  50.         pBSTree->m_pLeft = NULL;  
  51.         pBSTree->m_pRight = NULL;  
  52.         pBSTree->m_nValue = value;  
  53.         pCurrent = pBSTree;  
  54.     }  
  55.     // 当前节点不为空,则与value比较大小,决定放在左还是右  
  56.     else  
  57.     {  
  58.         if ((pCurrent->m_nValue) > value)  
  59.             addBSTreeNode(pCurrent->m_pLeft, value);  
  60.         else if ((pCurrent->m_nValue) < value)  
  61.             addBSTreeNode(pCurrent->m_pRight, value);  
  62.         else  
  63.         {  
  64.             // 假定不允许有重复值节点存在  
  65.             // cout<<"重复加入节点"<<endl;  
  66.         }  
  67.     }  
  68. }  
  69.   
  70. // 遍历二元查找树中序将按照从小到大排序,符合要求  
  71. void ergodicBSTree(BSTreeNode * pCurrent)  
  72. {  
  73.     if (NULL == pCurrent)  
  74.         return;  
  75.     // 左子树不为空,则遍历左子树  
  76.     if (NULL != pCurrent->m_pLeft)  
  77.         ergodicBSTree(pCurrent->m_pLeft);  
  78.   
  79.     // 节点接到链表尾部  
  80.     convertToDoubleList(pCurrent);  
  81.   
  82.     // 右子树不为空,则遍历右子树  
  83.     if (NULL != pCurrent->m_pRight)  
  84.         ergodicBSTree(pCurrent->m_pRight);  
  85. }  
  86.   
  87. // 打印双向链表  
  88. void displayDoubleList(DoubleList * pHead)  
  89. {  
  90.     pListIndex = pHead;  
  91.     while(NULL != pListIndex)  
  92.     {  
  93.         cout<<pListIndex->m_nValue<<" ";  
  94.         pListIndex= pListIndex->m_pRight;  
  95.     }  
  96.     cout<<endl;  
  97. }  
  98.   
  99. int main()  
  100. {  
  101.     BSTreeNode * pRoot = NULL;  
  102.   
  103.     addBSTreeNode(pRoot, 10);  
  104.     addBSTreeNode(pRoot, 4);  
  105.     addBSTreeNode(pRoot, 6);  
  106.     addBSTreeNode(pRoot, 8);  
  107.     addBSTreeNode(pRoot, 12);  
  108.     addBSTreeNode(pRoot, 14);  
  109.     addBSTreeNode(pRoot, 15);  
  110.     addBSTreeNode(pRoot, 16);  
  111.   
  112.     cout<<"List the converting result: "<<endl;  
  113.     ergodicBSTree(pRoot);  
  114.     cout<<endl<<"List the converting DoublList: "<<endl;  
  115.     displayDoubleList(pHead);  
  116.     cout<<"End!"<<endl;  
  117.     getchar();  
  118.     return 0;  
  119. }  

结果


解二

递归求解

  1. /*Title: 1.把二元查找树转变成排序的双向链表:解二 
  2. Author:  gocode 
  3. Date:    2012-09-27*/  
  4.   
  5. /*       10 
  6.       /      \ 
  7.      6        14 
  8.    /  \      /   \ 
  9.   4    8    12   16  */  
  10.   
  11. #include <iostream>  
  12. using namespace std;  
  13.   
  14. // Node类  
  15. class Node{  
  16. public:  
  17.     int data;  
  18.     Node *left;  
  19.     Node *right;  
  20.   
  21.     Node(int d = 0, Node *lr = NULL, Node *rr = NULL):data(d), left(lr), right(rr){}  
  22. };  
  23.   
  24. // 创建二叉查找树  
  25. // 先创建左右节点,然后创建父节点  
  26. Node *create()  
  27. {  
  28.     Node *root;  
  29.   
  30.     /*Node *p7 = new Node(7); 
  31.     Node *p9 = new Node(9); 
  32.     Node *p8 = new Node(8, p7, p9);*/  
  33.   
  34.     Node *p4 = new Node(4);  
  35.     Node *p8 = new Node(8);  
  36.     Node *p6 = new Node(6, p4, p8);  
  37.   
  38.     Node *p12 = new Node(12);  
  39.     Node *p16 = new Node(16);  
  40.     Node *p14 = new Node(14, p12, p16);  
  41.   
  42.     Node *p10 = new Node(10, p6, p14);  
  43.     root = p10;  
  44.   
  45.     return root;  
  46. }  
  47.   
  48. // 改变链接  
  49. Node *change(Node *p, bool asRight)  
  50. {  
  51.     if (!p)  
  52.         return NULL;  
  53.   
  54.     // 遍历左子树  
  55.     // 把左孩子与父节点的关系调整成双向链表的左右链接  
  56.     Node *pLeft = change(p->left, false);  
  57.     if (pLeft)  
  58.         pLeft->right = p;  
  59.     p->left = pLeft;  
  60.   
  61.     // 遍历右子树  
  62.     // 把右孩子与父节点的关系调整成双向链表的左右链接  
  63.     Node *pRight = change(p->right, true);  
  64.     if (pRight)  
  65.         pRight->left = p;  
  66.     p->right = pRight;  
  67.   
  68.     // 最关键的一段程序,需要仔细理解  
  69.     // 如果是右孩子,并且有左子孩子,则一直移动当前指针到左叶子节点  
  70.     // 如果是左孩子,并且有右子孩子,则一直移动当前指针到右叶子结点  
  71.     // 比如在构建12<=>14<=>16的时候,节点14是根节点10的右孩子asRight==true,它有左子孩子,此时当前指针一直向左移动到最左子孩子12上,12是根节点的后继  
  72.     // 比如在构建4<=>6=>8的时候,节点6是根节点10的左孩子asRight==false,它有右子孩子,此时当前指针一直向右移动到最右子孩子8上,8是根节点的前驱  
  73.     // 比如在构建6<=>10<=>14的时候,节点10是根节点asRight可为ture或false,此时asRight==false,有右子孩子,此时当前指针一直向右移动到最右子孩子16上  
  74.     Node *r = p;  
  75.     if (asRight)  
  76.     {// 右子树的开头节点是根节点的后继,即左叶子结点  
  77.         while (r->left)  
  78.             r = r->left;  
  79.     }else{// 左子树的最末尾节点是根节点的前驱,即右叶子结点  
  80.         while (r->right)  
  81.             r = r->right;  
  82.         }  
  83.     return r;  
  84. }  
  85.   
  86. void main(){  
  87.     Node *root = create();  
  88.     Node *tail = change(root, false); // 给出root节点,但是asRight==false,则返回二叉查找树最末尾节点  
  89.     while (tail)  
  90.     {  
  91.         cout << tail->data << " ";  
  92.         tail = tail->left;  
  93.     }  
  94.     cout << endl;  
  95.   
  96.     root = create();  
  97.     Node *head = change(root, true); // 给出root节点,但是asRight==ture,则返回二叉查找树最开头节点  
  98.     while (head)  
  99.     {  
  100.         cout << head->data << " ";  
  101.         head = head->right;  
  102.     }  
  103.     cout << endl;  
  104.     getchar();  
  105. }  
结果

 

总结

解一在构建二叉查找树使用了addBSTreeNode函数,此方法可以方便的自动建立二叉查找树的关系。这比解二一个节点一个节点手工建立要省事,而且可以复用。解一利用中序遍历时,拆解重构左右链接从而构造出排序的双向链表。解二代码量少,可以通过改变asRight变量控制双向链表的输出。这充分利用了链表的灵活性,更是建立在对二叉查找树的遍历和链表构建有充分的正确认识。从理解难易程度看,解一更好理解些。

0 0
原创粉丝点击