/* Name: 二元查找树转变成排序的双向链表(树) Copyright: Author: yifi Date: 23/11/16 17:06 Description: */ #include <iostream> using namespace std; typedef struct _BSTreeNode{ int m_Value; _BSTreeNode* pLeft; _BSTreeNode* pRight; }BSTreeNode,*pBSTreeNode; void addBSTreeNode(BSTreeNode *&pCurrent,int value); void inOrderBSTree(BSTreeNode* pBSTree); void convertToDoubleList(BSTreeNode* pCurrent); BSTreeNode *pHead=NULL;//指向循环队列头结点 BSTreeNode *pIndex=NULL;//指向前一个结点 int main() { BSTreeNode *pRoot=NULL; addBSTreeNode(pRoot,10); addBSTreeNode(pRoot,6); addBSTreeNode(pRoot,14); addBSTreeNode(pRoot,4); addBSTreeNode(pRoot,8); addBSTreeNode(pRoot,12); addBSTreeNode(pRoot,16); inOrderBSTree(pRoot); return 0; } void addBSTreeNode(BSTreeNode *&pCurrent,int value) { if (pCurrent == NULL) { BSTreeNode* pTemp = new BSTreeNode(); pTemp->m_Value = value; pTemp->pLeft = NULL; pTemp->pRight = NULL; pCurrent = pTemp; } else if (pCurrent->m_Value < value) { addBSTreeNode(pCurrent->pRight,value); } else if (pCurrent->m_Value > value) { addBSTreeNode(pCurrent->pLeft,value); } else { cout << "Node Repeated" << endl; } } void inOrderBSTree(BSTreeNode* pBSTree) { if (pBSTree == NULL) { return ; } if (pBSTree->pLeft != NULL) { inOrderBSTree(pBSTree->pLeft); } convertToDoubleList(pBSTree); if (pBSTree->pRight != NULL) { inOrderBSTree(pBSTree->pRight); } } void convertToDoubleList(BSTreeNode* pCurrent) { pCurrent->pLeft = pIndex; if (pIndex == NULL) { pHead = pCurrent; } else { pIndex->pRight = pCurrent; } pIndex = pCurrent; cout << pCurrent->m_Value << " "; }