Binary search tree converted to ordered doubly linked list

Source: Internet
Author: User

First, the problem description Enter a binary search tree, and now you want to convert the two-fork search tree into a sorted doubly linked list. And in the process of conversion, you cannot create any new nodes, only the point of the node pointer in the tree can be adjusted to implement.   Second, realize the idea in the binary search tree, each node has two pointers to its left and right subtree, and the value of the left subtree node is always less than the value of the parent node, and the value of the right subtree node is always greater than the parent node value. In a doubly linked list, each node also has two pointers, which point to the previous node and the latter node, respectively. So the nodes of the two data structures are consistent, the binary search tree is a two-fork search tree, the two-way linked list is doubly linked list, just because two pointers to different points, by changing its pointer to the direction of the implementation is completely possible.   For example, the following two-fork search tree,  If using the middle sequence traversal, its traversal order is 1-2-3-4-5-6-7, through the appropriate pointer transformation operation, can become the two-way ordered chain list as follows:   from, we can see, in order to reduce the number of pointer changes, And to make the operation simpler, when converting to a sort doubly linked list, the pointer to the left Dial hand node is adjusted to the pointer to the previous node in the list, and the pointer to the right child node is adjusted to the pointer to the next node in the list. For example, for a pointer above the value 2, after the adjustment, its previous node is 1, the latter node is 3, and the node 2 of the left Dial hand node is 1, the right child node is originally 3.  for the operation of the tree, usually in the process of traversing the various nodes of the tree, through the node implementation of certain operations to complete, This algorithm is no exception. Since the conversion of the two-way linked list is also ordered, and we can see from the above, when we traverse the binary search tree in the middle sequence, its traversal of the node is ordered, so here I take a bit of the traversal order should be the middle order.   So how do we adjust the pointer so that the two-fork search tree becomes a two-way ordered list? When traversing to the root node, we can think of the tree as three parts: the root node, the Zoki right subtree of the root. such as the two-fork sorting tree, it is divided into root node 4, the Node 2 is the root of the left dial hand pair and the node 6 is the root of the right subtree. From the list of transformations, we can see that the left pointer of node 4 points to Node 3, the right pointer of node 3 points to Node 4, and because we are using the middle sequence traversal, so when we traverse to the node 4 o'clock, the node 4 of the tree has been transformed into an ordered doubly linked list, The node 3 is the tail node of the converted doubly linked list, so we should use a variable last_node to hold the last node pointer in order to be used when it is contiguous with the root node. The value of this variable last_node is then updated to point to the root node 4. For the right sub-tree of Node 4, take similar action. As for the specific implementation, we only need to recursively place all the subtreesThis can be done. The operation process is as follows:  : implementation code   [CPP]View Plaincopyprint?
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. Using Std::cout;
  5. Using Std::cin;
  6. Using Std::endl;
  7. struct Bsnode
  8. {
  9. //Define the node structure of binary search tree
  10. Bsnode *left;
  11. Bsnode *right;
  12. int data;
  13. };
  14. Define various data types to use
  15. typedef bsnode* Bstree;
  16. typedef bsnode* Dlist;
  17. typedef bsnode Dlnode;
  18. Insert a node with a value of data into a binary search tree
  19. Bstree Insertnode (bstree tree, int data);
  20. Turn the two-fork search tree into a doubly linked list and return to the head node.
  21. Dlist bstreetolist (bstree tree);
  22. Traverse the binary search tree for each node and adjust the pointer.
  23. void Convertnode (Bstree tree, Bsnode **last_node);
  24. Find the leftmost node of a binary search tree
  25. bsnode* Findleftmostnode (bstree tree);
  26. Binary search tree with middle order output
  27. void Printbitree (Bstree tree);
  28. Output link List
  29. void Printlist (dlist list);
  30. Bstree Insertnode (bstree tree, int data)
  31. {
  32. if (tree = = NULL)
  33. {
  34. //Find insertion point, insert
  35. Tree = new Bsnode;
  36. Tree->left = NULL;
  37. Tree->right = NULL;
  38. Tree->data = data;
  39. }
  40. //Insert in its right subtree
  41. Else if (Tree->data < data)
  42. Tree->right = Insertnode (tree->right, data);
  43. //Insert in its left subtree
  44. Else if (Tree->data > Data)
  45. Tree->left = Insertnode (tree->left, data);
  46. return tree;
  47. }
  48. Dlist bstreetolist (Bstree tree)
  49. {
  50. if (tree = = NULL)
  51. return NULL;
  52. //Find the leftmost node, the head node of the converted list
  53. Dlnode *head = Findleftmostnode (tree);
  54. Bsnode *last_node = NULL;
  55. //To convert
  56. Convertnode (tree, &last_node);
  57. return head;
  58. }
  59. bsnode* Findleftmostnode (Bstree tree)
  60. {
  61. if (tree = = NULL)
  62. return NULL;
  63. While (tree->left! = NULL)
  64. Tree = tree->left;
  65. return tree;
  66. }
  67. void Convertnode (Bstree tree, Bsnode **last_node)
  68. {
  69. if (tree = = NULL)
  70. return;
  71. //Convert the tree's left subtree, Last_node is the pointer to the last node of the linked list after conversion
  72. if (tree->left! = NULL)
  73. Convertnode (Tree->left, Last_node);
  74. //Adjust the left pointer of the tree to point to the previous node
  75. Tree->left = *last_node;
  76. //Adjustment points to the last node, right points to the next node .
  77. if (*last_node! = NULL)
  78. (*last_node)->right = tree;
  79. //Adjust the pointer to a node in the last linked list.
  80. *last_node = tree;
  81. //Convert the right subtree of a tree, Last_node is a pointer to the last node of the linked list after conversion
  82. if (tree->right! = NULL)
  83. Convertnode (Tree->right, Last_node);
  84. }
  85. void Printbstree (Bstree tree)
  86. {
  87. if (tree = = NULL)
  88. return;
  89. Printbstree (Tree->left);
  90. cout << tree->data << "";
  91. Printbstree (Tree->right);
  92. }
  93. void Printlist (dlist list)
  94. {
  95. Dlnode *node = list;
  96. While (node! = NULL)
  97. {
  98. cout << node->data << "";
  99. node = node->right;
  100. }
  101. }
  102. int main ()
  103. {
  104. Bstree tree = NULL;
  105. Srand (Time (NULL));
  106. cout << "Insert Data Order is:" << Endl;
  107. For (int i = 0; i <; ++i)
  108. {
  109. //Insert random 10 numbers to generate a two-fork sort tree
  110. int data = rand ()%100;
  111. cout << data << "";
  112. Tree = Insertnode (tree, data);
  113. }
  114. cout << "\nthe bstree is:" << Endl;
  115. Printbstree (tree);
  116. //To convert
  117. Tree = bstreetolist (tree);
  118. cout << "\nbitree to List:" << Endl;
  119. Printlist (tree);
  120. return 0;
  121. }
The results are as follows: Four, code analysis because the binary sorting tree is not allowed to have the same elements, in the randomly generated 10 number, there are two of the same 3 and 73, so actually inserted into the two fork sorting tree node only 8, and then we are in the middle order to traverse the output binary sorting tree data, Then output the converted linked list of data, found that the order is consistent, thus proving the correctness of the algorithm. The core functions of the algorithm are Bstreetolist,convertnode and Findleftmostnode. As we can see in the function bstreetolist, we have a variable last_node used to record the converted list of the end nodes, because in the convention we return the 1th node of the list (counting from 1), and Last_node points to the last node, We can take the pointer from the end of the tail to get the first node pointer, but here I do not, because it needs to traverse each node once, the time complexity of O (n). Instead, a pointer to the leftmost node of the two-fork sort tree is found before the transformation. Because the sort binary tree is ordered, the leftmost node is the smallest node, and our algorithm does not delete or add nodes, that is, the address of the node is not changed, so the leftmost node is the 1th node of the converted list, its time complexity is O (logn). Five, time complexity and space complexity the algorithm starts from the root point to the left, finds the leftmost node, has a time complexity of O (Logn), and then iterates through each node in the two-fork sort tree for pointer transformation, with a time complexity of O (n), so the total time complexity is O (n). As for the spatial complexity, because the Convertnode function makes recursive calls, its function has two open parameters, and the function call layer in the function stack does not exceed the height of the tree, so its space complexity is O (logn).

Binary search tree converted to ordered doubly linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.