【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
请按任意键继续. . .
- 【100题】二叉排序树转成双向链表
- 【100题】二叉排序树转成双向链表----中序遍历法
- C语言强化(一)二叉排序树转成排序的双向链表
- 二叉排序树转双向链表
- 二叉排序树和双向链表
- 二叉排序树转双向链表
- 微软100道算法题第一题------二叉排序树变成排序的双向链表
- 二叉排序树向双向链表的转换
- 二叉排序树转化为有序双向链表
- 二叉排序树转化为双向链表
- 二叉排序树换成一个双向链表
- 二叉排序树转化为双向链表
- 二叉排序树与双向链表的转换
- 把二叉排序树变成排序的双向链表
- 将二叉排序树BST转换成排序的双向链表
- 二叉排序树转换成排序的双向链表
- 二叉排序树转换成排序的双向链表
- 面试题27:二叉排序树转双向链表
- Android开发之Service
- 监听接收短信
- 【菜鸟C++学习笔记】13.while语句
- oracle存储过程和函数的调用
- [PHP]如果客户端禁用cookie,session还能用吗?
- 【100题】二叉排序树转成双向链表
- SMON功能介绍 1
- Blazeds学习
- Perl 判断一个字符串中所有字符是否在另外一个字符串中出现
- oracle trim函数用法详解
- stm32f1的存储器与复位
- MySQL 的Performance Troubleshooting呢,Jeremy提供了以下思路和经验:
- 实习总结(六)---开源精神(TLD算法)
- python 学习笔记之字典