# Binary search tree converted to ordered doubly linked list

Source: Internet
Author: User

http://blog.csdn.net/ljianhui/article/details/22338405

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);
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);
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);
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

Related Keywords:

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: info-contact@alibabacloud.com 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.