当前位置:   article > 正文

数据结构 - 把二叉查找树转变成排序的双向链表(C++)_将二叉搜索树转换为双向排序链表c++

将二叉搜索树转换为双向排序链表c++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

  1. /*
  2. * 把二元查找树转变成排序的双向链表 - by Chimomo
  3. *
  4. * 【思路】按照二叉树的中序遍历正好是按从小到大地顺序遍历全部节点。在遍历的过程中,更改它的逻辑结构。
  5. */
  6. #include <iostream>
  7. using namespace std;
  8. /**
  9. * The binary search tree node.
  10. */
  11. struct BSTreeNode {
  12. int m_nValue; // The value of node.
  13. BSTreeNode *m_pLeft; // The left child of node.
  14. BSTreeNode *m_pRight; // The right child of node.
  15. };
  16. typedef BSTreeNode DoublyLinkedList;
  17. DoublyLinkedList *pHead = nullptr;
  18. DoublyLinkedList *pDoublyLinkedList = nullptr;
  19. /**
  20. * Construct binary search tree recursively.
  21. * @param pCurrent The current pointer. Use & here since pCurrent will be changed in the function body.
  22. * @param value The value.
  23. */
  24. void ConstructBSTree(BSTreeNode *&pCurrent, int value) {
  25. if (pCurrent == nullptr) {
  26. auto pTree = new BSTreeNode();
  27. pTree->m_pLeft = nullptr;
  28. pTree->m_pRight = nullptr;
  29. pTree->m_nValue = value;
  30. pCurrent = pTree;
  31. } else {
  32. if (value > pCurrent->m_nValue) {
  33. ConstructBSTree(pCurrent->m_pRight, value);
  34. } else if (value < pCurrent->m_nValue) {
  35. ConstructBSTree(pCurrent->m_pLeft, value);
  36. } else {
  37. cout << "Invalid input, there already have the value!" << endl;
  38. }
  39. }
  40. }
  41. /**
  42. * Convert binary search tree node to doubly linked list node.
  43. * @param pCurrent The current pointer.
  44. */
  45. void ConvertBSTreeNodeToDoublyLinkedListNode(BSTreeNode *pCurrent) {
  46. pCurrent->m_pLeft = pDoublyLinkedList;
  47. if (pDoublyLinkedList == nullptr) {
  48. pHead = pCurrent;
  49. } else {
  50. pDoublyLinkedList->m_pRight = pCurrent;
  51. }
  52. pDoublyLinkedList = pCurrent;
  53. }
  54. /**
  55. * Traverse binary search tree (in-order) to convert binary tree to doubly linked list.
  56. * @param pCurrent The current pointer.
  57. */
  58. void TraverseBSTreeInOrder(BSTreeNode *pCurrent) {
  59. if (pCurrent == nullptr) {
  60. return;
  61. } else {
  62. if (pCurrent->m_pLeft != nullptr) {
  63. TraverseBSTreeInOrder(pCurrent->m_pLeft);
  64. }
  65. ConvertBSTreeNodeToDoublyLinkedListNode(pCurrent);
  66. if (pCurrent->m_pRight != nullptr) {
  67. TraverseBSTreeInOrder(pCurrent->m_pRight);
  68. }
  69. }
  70. }
  71. /**
  72. * Test program.
  73. * @param argc The argument count.
  74. * @param argv The argument array.
  75. * @return The int.
  76. */
  77. int main(int argc, char *argv[]) {
  78. int value;
  79. BSTreeNode *pBSTree = nullptr;
  80. cout << "Please input integers, -1 to end:";
  81. cin >> value;
  82. while (-1 != value) {
  83. ConstructBSTree(pBSTree, value);
  84. cin >> value;
  85. }
  86. TraverseBSTreeInOrder(pBSTree);
  87. cout << "Print BST:";
  88. while (pHead) {
  89. cout << pHead->m_nValue << ' ';
  90. pHead = pHead->m_pRight;
  91. }
  92. return 0;
  93. }
  94. // Output:
  95. /*
  96. Please input integers, -1 to end:6 7 8 5 1 3 9 2 4 0 10 -1
  97. 6 7 8 5 1 3 9 2 4 0 10 -1
  98. Print BST:0 1 2 3 4 5 6 7 8 9 10
  99. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/397231
推荐阅读
相关标签
  

闽ICP备14008679号