将BST转换为有序的双向链表!
生活随笔
收集整理的這篇文章主要介紹了
将BST转换为有序的双向链表!
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在二叉樹中,每個結點都有兩個指向子結點的指針. 在雙向鏈表中, 每個結點也有兩個指針,它們分別指向前一個結點和后一個結點.由于這兩種結構的相似性, 同時二叉搜索樹也是一種排過序的數據結構, 因此在理論上有可能實現二叉搜索樹和排序的雙向鏈表之間的轉換.
下面的文件BST_to_DL.cpp將BST轉換為排序過的雙向鏈表,請參加代碼:
#include "binary_tree.h"void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList){if(pNode == NULL)return;BinaryTreeNode* pCurrent = pNode;//首先處理左子樹if(pCurrent->pLeft)ConvertNode(pNode->pLeft, pLastNodeInList);//完成pCurrent與pLastNodeInList的拼接和更替pCurrent->pLeft = *pLastNodeInList;if(*pLastNodeInList != NULL)(*pLastNodeInList)->pRight = pCurrent;*pLastNodeInList = pCurrent;//遍歷到右子樹if(pCurrent->pRight)ConvertNode(pCurrent->pRight,pLastNodeInList);
}BinaryTreeNode* Convert(BinaryTreeNode* pRoot){BinaryTreeNode* pLastNodeInList = NULL;ConvertNode(pRoot, &pLastNodeInList);//pLastNodeInList指向雙向鏈表的尾結點,我們逆序返回它的頭結點BinaryTreeNode* pHeadOfList = pLastNodeInList;while(pHeadOfList != NULL && pHeadOfList->pLeft != NULL)pHeadOfList = pHeadOfList->pLeft;return pHeadOfList;
}//========================測試代碼 =============================
void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList){BinaryTreeNode* pNode = pHeadOfList;printf("The nodes from left to right are:\n");while(pNode != NULL){printf("%d\t", pNode->value);if(pNode->pRight == NULL)break;pNode = pNode->pRight;}printf("\nThe nodes from right to left are:\n");while(pNode != NULL){printf("%d\t", pNode->value);if(pNode->pLeft == NULL)break;pNode = pNode->pLeft;}printf("\n");
}void DestroyList(BinaryTreeNode* pHeadOfList){BinaryTreeNode* pNode = pHeadOfList;while(pNode != NULL){BinaryTreeNode* pNext = pNode->pRight;delete pNode;pNode = pNext;}
}void Test(const char* testName, BinaryTreeNode* pRoot){if(testName != NULL)printf("%s begins: \n", testName);PrintTree(pRoot);BinaryTreeNode* pHeadOfList = Convert(pRoot);PrintDoubleLinkedList(pHeadOfList);
}// 10
// / \
// 6 14
// /\ /\
// 4 8 12 16
void Test1(){BinaryTreeNode* pNode10 = 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);ConnectTreeNodes(pNode10, pNode6, pNode14);ConnectTreeNodes(pNode6, pNode4, pNode8);ConnectTreeNodes(pNode14, pNode12, pNode16);Test("Test1", pNode10);DestroyList(pNode4);
}// 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
void Test2(){BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);ConnectTreeNodes(pNode5, pNode4, NULL);ConnectTreeNodes(pNode4, pNode3, NULL);ConnectTreeNodes(pNode3, pNode2, NULL);ConnectTreeNodes(pNode2, pNode1, NULL);Test("Test2", pNode5);DestroyList(pNode1);
}// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
void Test3(){BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);ConnectTreeNodes(pNode1,NULL,pNode2);ConnectTreeNodes(pNode2,NULL,pNode3);ConnectTreeNodes(pNode3,NULL,pNode4);ConnectTreeNodes(pNode4,NULL,pNode5);Test("Test3",pNode1);DestroyList(pNode1);
}//樹中只有1個結點
void Test4(){BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);Test("Test4", pNode1);DestroyList(pNode1);
}//樹中沒有結點
void Test5(){Test("Test5",NULL);
}int main(int argc, char** argv){Test1();Test2();Test3();Test4();Test5();return 0;
}
總結
以上是生活随笔為你收集整理的将BST转换为有序的双向链表!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BST(binary search tr
- 下一篇: 中体骏彩C++面试题