【100题】二叉排序树转成双向链表

来源:互联网 发布:淘宝怎么查自己的排名 编辑:程序博客网 时间:2024/06/09 15:35
#include <stdio.h>#include <iostream>using namespace std;struct BSTreeNode{    int m_nValue; // value of node    BSTreeNode *m_pLeft; // left child of node    BSTreeNode *m_pRight; // right child of node};typedef BSTreeNode DoubleList;DoubleList * pHead;// 创建二元查找树void createBinarySearchTree(BSTreeNode * & pCurrent, int value){    if (NULL == pCurrent)    {        BSTreeNode * pBSTree = new BSTreeNode();        pBSTree->m_pLeft = NULL;        pBSTree->m_pRight = NULL;        pBSTree->m_nValue = value;        pCurrent = pBSTree;    }    else     {        if ((pCurrent->m_nValue) > value)        {//如果比当前节点值小,则放到左子树            createBinarySearchTree(pCurrent->m_pLeft, value);        }        else if ((pCurrent->m_nValue) < value)        {//如果比当前节点值大,则放到右子树            createBinarySearchTree(pCurrent->m_pRight, value);        }        else        {//否则提示重复插入了。            cout<<"重复加入节点"<<endl;        }    }}BSTreeNode *ConvertNode(BSTreeNode *pNode,bool asRight){if(!pNode){return NULL;}BSTreeNode *pLeft = NULL;BSTreeNode *pRight = NULL;//如果左孩子不空,则递归转换左子树-->双向链表,并返回左子链表的最右端节点(最大值节点)if(pNode->m_pLeft){pLeft = ConvertNode(pNode->m_pLeft,false);}//将当前节点与左子链表的最右端连接起来。if(pLeft){pLeft->m_pRight = pNode;pNode->m_pLeft = pLeft;}//如果右孩子不空,则递归转换右子树-->双向链表,并返回右子链表的最左端的节点(最小值节点)if(pNode->m_pRight){pRight = ConvertNode(pNode->m_pRight,true);}//把当前节点与右子链表上最左端节点连接起来。if(pRight){pNode->m_pRight = pRight;pRight->m_pLeft = pNode;}//BSTreeNode *pTemp = pNode;//如果该分支是右子树,则将指针移动到最左侧if(asRight){while(pTemp->m_pLeft){pTemp = pTemp->m_pLeft;}}else//否则肯定是左子树,则移动指针到最右侧{while(pTemp->m_pRight){pTemp = pTemp->m_pRight;}}return pTemp;}BSTreeNode *Convert(BSTreeNode *pHeadOfTree){return ConvertNode(pHeadOfTree,true);}int main(){    BSTreeNode * pRoot = NULL;    pHead = NULL;    createBinarySearchTree(pRoot, 10);    createBinarySearchTree(pRoot, 4);    createBinarySearchTree(pRoot, 6);    createBinarySearchTree(pRoot, 8);    createBinarySearchTree(pRoot, 12);    createBinarySearchTree(pRoot, 14);    createBinarySearchTree(pRoot, 15);    createBinarySearchTree(pRoot, 16);pHead = Convert(pRoot);//从最左侧开始打印双向链表cout << "从左向右打印:";DoubleList *p = pHead;while(p != NULL){cout << p->m_nValue<<" ";p= p->m_pRight;}//从最右侧开始打印双向链表cout << endl <<"从右向左打印:";while(pHead->m_pRight != NULL){pHead= pHead->m_pRight;}while(pHead != NULL){cout << pHead->m_nValue<<" ";pHead = pHead->m_pLeft;}cout << endl;    return 0;}


 

运行结果:

从左向右打印:4 6 8 10 12 14 15 16
从右向左打印:16 15 14 12 10 8 6 4
请按任意键继续. . .

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司车辆超证了怎么办? 准迁证过期了5年怎么办 天津中派木业不发工资怎么办 在香港餐厅嫌冷怎么办 出国读研报到证怎么办 高中后出国留学档案怎么办 爱迪花园拆了怎么办 燕郊房子卖了户口怎么办 强制险单子丢了怎么办 车辆保险贴丢了怎么办 车险原单子丢了怎么办 车险保单丢了该怎么办? 审车保险单丢了怎么办 湖州公积金贷款买房具体信用怎么办 驾驶本扣分满了怎么办 摩托车被扣12分怎么办 驾照被扣12分怎么办 骑摩托被扣12分怎么办 摩托车没有驾照违章被扣分怎么办 高考登录密码丢了怎么办 高中档案遗失里面体检表怎么办 驾校不给补科一成绩单怎么办 外地驾照转广州体检表怎么办 驾照体检表签名签错怎么办 改完名字后护照怎么办 办健康证没有身份证怎么办 身份证掉了怎么办健康证 华师附小不搬了怎么办 健康证快到期了怎么办 老公想去日本打工怎么办 学生没有资产证明怎么办日本签证 在读证明学校不按模版怎么办 办日本签证没有户口本怎么办 日本大学留级续签失败怎么办 法国签证递交时间太晚怎么办 日本留学生签证更新拒签怎么办 永驻拒签了我该怎么办 越南签证拒签了怎么办 l1签证续签被拒怎么办 去日本跟团签证怎么办 手表里指针掉了怎么办