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

//TreeAPI.h #ifndef _TREE_API_H_ #define _TREE_API_H_ typedef struct _BSTreeNode { int m_nValue; struct _BSTreeNode* m_pLeft; struct _BSTreeNode* m_pRight; }BSTreeNode; void createRandArr(int *randArr, int len); bool insertBST(BSTreeNode* &root, int data); void displayBST2(BSTreeNode* &rootList, BSTreeNode* root); void convertToDoubleList(BSTreeNode* &rootList, BSTreeNode* pCurr); void freeBST(BSTreeNode* pRoot); void freeDoubleList(BSTreeNode* pRoot); void testBST(void); #endif //TreeAPI.cpp #include "StdAfx.h" #include "TreeAPI.h" #include <time.h> #include <stdlib.h> void createRandArr(int *randArr, int len) { srand((int)time(0)); for (int i = 0; i < len; ++i) { randArr[i] = rand()%100; } } bool insertBST(BSTreeNode* &root, int data) { if (!root) // root == NULL { root = (BSTreeNode*)malloc(sizeof(BSTreeNode)); root->m_nValue = data; root->m_pLeft = NULL; root->m_pRight = NULL; return false; } //malloc memory BSTreeNode* newNode = (BSTreeNode*)malloc(sizeof(BSTreeNode)); newNode->m_nValue = data; newNode->m_pLeft = NULL; newNode->m_pRight = NULL; BSTreeNode* p = root; while(p != NULL) { if (data == p->m_nValue) //exist before { free(newNode); newNode = NULL; return false; } else if (data < p->m_nValue) { if (p->m_pLeft != NULL) { p = p->m_pLeft; } else { p->m_pLeft = newNode; return true; } } else { if (p->m_pRight != NULL) { p = p->m_pRight; } else { p->m_pRight = newNode; return true; } } } return true; } void displayBST2(BSTreeNode* &rootList, BSTreeNode* root) { if (root->m_pLeft != NULL) { displayBST2(rootList,root->m_pLeft); } printf("%d ",root->m_nValue); convertToDoubleList(rootList, root); if (root->m_pRight != NULL) { displayBST2(rootList, root->m_pRight); } } void convertToDoubleList(BSTreeNode* &rootList, BSTreeNode* pCurr) { if (NULL == rootList) { rootList = (BSTreeNode*)malloc(sizeof(BSTreeNode)); rootList->m_nValue = pCurr->m_nValue; rootList->m_pLeft = NULL; rootList->m_pRight = NULL; return; } BSTreeNode* newNode = (BSTreeNode*)malloc(sizeof(BSTreeNode)); newNode->m_nValue = pCurr->m_nValue; newNode->m_pLeft = NULL; newNode->m_pRight = NULL; BSTreeNode* pNode = rootList; //go to end while (pNode->m_pRight != NULL) { pNode = pNode->m_pRight; } newNode->m_pLeft = pNode; pNode->m_pRight = newNode; } void testBST(void) { int arr[20]; int len = sizeof(arr)/ sizeof(int); int i; BSTreeNode* root = NULL; BSTreeNode* rootList = NULL; //create arr createRandArr(arr, len); //dsp arr printf("dsp arr:/n"); for (i = 0; i < len; ++i) { printf("%d ",arr[i]); } printf("/n"); for (i = 0; i < len; ++i) { insertBST(root, arr[i]); } //dsp sequential printf("/ndsp root:/n"); displayBST2(rootList, root); BSTreeNode* pNode = rootList; printf("/n/ndsp list seq/n"); while (pNode != NULL) { printf("%d ", pNode->m_nValue); pNode = pNode->m_pRight; } //dsp reverse printf("/n/ndsp list reverse/n"); pNode = rootList; while (pNode->m_pRight != NULL) { pNode = pNode->m_pRight; } while(pNode != NULL) { printf("%d ", pNode->m_nValue); pNode = pNode->m_pLeft; } printf("/n"); //free mem freeBST(root); freeDoubleList(rootList); printf("/n"); } void freeBST(BSTreeNode* pRoot) { //del left child if (pRoot->m_pLeft != NULL) { freeBST(pRoot->m_pLeft); } //del right child if (pRoot->m_pRight != NULL) { freeBST(pRoot->m_pRight); } //free root free(pRoot); } void freeDoubleList(BSTreeNode* pRoot) { BSTreeNode* pNode = pRoot; BSTreeNode* pNext = pNode->m_pRight; while(pNode != NULL && pNext != NULL) { free(pNode); pNode = pNext; pNext = pNext->m_pRight; } free(pNode); pNode = NULL; } //main.cpp #include "stdafx.h" #include "TreeAPI.h" #include <stdlib.h> int main(int argc, char* argv[]) { testBST(); return 0; } //result  

你可能感兴趣的:(struct,list,api,tree,null)