把二元查找树转变成排序的双向链表

来源:互联网 发布:网络诈骗能抓到人吗 编辑:程序博客网 时间:2024/05/21 09:16

本文转自:http://zhedahht.blog.163.com/blog/static/254111742007127104759245/

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

 比如将二元查找树
    
                                        10
                                          /    \
                                        6       14
                                      /  \     /  \
                                    4     8  12    16
转换成双向链表 
4=6=8=10=12=14=16

分析:在二叉树中,每个节点都有两个指向子节点的指针。在双向链表中,每个节点也有两个指针,他们分别指向前一个节点和后一个节点。由于这两种节点的结构相似,同时二叉搜索树也是一种排序的数据结构,因此在理论上有可能实现二叉搜索树和排序的双向链表的转换。在搜索二叉树中,左子结点的值总是小于父节点的值,右子结点的值总是大于父节点的值。因此我们在转换成排序双向链表时,原先指向左子结点的指针调整为链表中指向前一个节点的指针,原先指向右子结点的指针调整为链表中指向后一个节点指针。接下来我们考虑该如何转换。
由于要求转换之后的链表时排好序的,我们可以中序遍历树中的每一个节点,这时因为中序遍历算法的特点是按照从小到大的顺序遍历二叉树的每一个节点。当遍历到根节点的时候,我们把树看成三部分:值为10的节点、根节点值为6的左子树、根节点值为14的右子树。根据排序链表的定义,值为10的节点将和它的左子树的最大一个节点(值为8的节点)链接起来,同时他还将和右子树最小的节点(即值为12的节点)链接起来。按照中序遍历的顺序,当我们遍历转换到根节点(值为10的节点)时,它的左子树已经转换成一个排序的链表了,并且处在链表中的最后一个节点时当前值最大的节点。我们把值为8的节点和根节点链接起来,此时链表中的最后一个节点就是10了。接着我们去遍历转换右子树,并把根节点和右子树中最小的节点链接起来。至于怎么去转换它的左子树和右子树,由于遍历和转换过程是一样的,我们和自然地想到可以用递归。
基于以上的分析,代码如下:
#include <iostream>#include <vector>#include <stdio.h>using namespace std;struct BinaryTreeNode{int m_nValue;BinaryTreeNode* m_pLeft;BinaryTreeNode* m_pRight;};//创建节点BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = pNode->m_pRight = NULL;return pNode;}//添加pLeft到pParent的左孩子bool BinaryTreeAddLeftNode(BinaryTreeNode* pParent,BinaryTreeNode* pLeft){if(NULL == pParent || NULL == pLeft)return false;if(pParent->m_pLeft != NULL){printf("%d has left child\n",pParent->m_nValue);return false;}else{pParent->m_pLeft = pLeft;return true;}}//添加pRight到pParent的右孩子bool BinaryTreeAddRightNode(BinaryTreeNode* pParent,BinaryTreeNode* pRight){if(NULL == pParent || NULL == pRight)return false;if(pParent->m_pRight != NULL){printf("%d has right child\n",pParent->m_nValue);return false;}else{pParent->m_pRight = pRight;return true;}}void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList){    if(pNode == NULL)        return;    BinaryTreeNode *pCurrent = pNode;    if (pCurrent->m_pLeft != NULL)        ConvertNode(pCurrent->m_pLeft, pLastNodeInList);    pCurrent->m_pLeft = *pLastNodeInList;     if(*pLastNodeInList != NULL)        (*pLastNodeInList)->m_pRight = pCurrent;    *pLastNodeInList = pCurrent;    if (pCurrent->m_pRight != NULL)        ConvertNode(pCurrent->m_pRight, pLastNodeInList);}BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree){    BinaryTreeNode *pLastNodeInList = NULL;    ConvertNode(pRootOfTree, &pLastNodeInList);    // pLastNodeInList指向双向链表的尾结点,    // 我们需要返回头结点    BinaryTreeNode *pHeadOfList = pLastNodeInList;    while(pHeadOfList != NULL && pHeadOfList->m_pLeft != NULL)        pHeadOfList = pHeadOfList->m_pLeft;    return pHeadOfList;}void Print(BinaryTreeNode* pRoot){while(pRoot != NULL){cout<<pRoot->m_nValue<<" ";pRoot = pRoot->m_pRight;}cout<<endl;}int main(){//构建树BinaryTreeNode* pRoot = CreateBinaryTreeNode(10);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);BinaryTreeAddLeftNode(pRoot,pNode6);BinaryTreeAddRightNode(pRoot,pNode14);BinaryTreeAddLeftNode(pNode6,pNode4);BinaryTreeAddRightNode(pNode6,pNode8);BinaryTreeAddLeftNode(pNode14,pNode12);BinaryTreeAddRightNode(pNode14,pNode16);//测试程序BinaryTreeNode* pHead = Convert(pRoot);Print(pHead);}
在上面的代码中,我们用pLastNodeInList指向已经转换好的链表的最后一个节点(也是最大的节点)。当我们遍历到值为10的节点的时候,它的左子树都已经转换好了,因此pLastNodeInList指向值为8的节点。接着把根节点链接到链表中之后,值为10的节点成了链表中最后一个极点(新的值最大的节点),于是pLastNodeInList指向了这个值为10的节点。接下来把pLastNodeInList作为参数传入函数递归遍历右子树。我们找到右子树中最左边的子节点(值为12的节点,在右子树中值最小),并把该节点和值为10的节点链接起来。