• 二叉树



    1. // BTree.cpp : Defines the entry point for the console application.
    2. //
    3. #include "stdafx.h"
    4. #include <iostream>
    5. #include <stack>
    6. using namespace std;
    7. typedef struct BTree_node {
    8. int data ;
    9. BTree_node * left;
    10. BTree_node * right;
    11. } BTree ;
    12. BTree *create_node ( int data )
    13. {
    14. BTree *node = new BTree();
    15. node->data = data ;
    16. node->left = node->right = NULL ;
    17. return node;
    18. }
    19. /*
    20. ·ÇµÝ¹é·½Ê½²åÈë
    21. */
    22. void insert_node ( BTree * root , int data )
    23. {
    24. BTree * node = root ;
    25. BTree * last = root ;
    26. while (node != NULL ) {
    27. last = node ;
    28. if ( data < node->data )
    29. node = node->left;
    30. else
    31. node = node->right;
    32. }
    33. if( data < last->data )
    34. last->left = create_node ( data );
    35. else
    36. last->right = create_node ( data );
    37. }
    38. /*
    39. µÝ¹é+ÆÕָͨÕ뷽ʽ²åÈë
    40. */
    41. void insert_node_recursive ( BTree *node , int data )
    42. {
    43. if ( data < node->data ) {
    44. if ( node->left == NULL ) {
    45. node->left = create_node ( data ) ;
    46. return ;
    47. }else
    48. insert_node_recursive( node->left, data );
    49. }else {
    50. if (node->right == NULL ) {
    51. node->right = create_node ( data ) ;
    52. return ;
    53. }else {
    54. insert_node_recursive( node->right, data );
    55. }
    56. }
    57. }
    58. /*
    59. µÝ¹é+¶þ¼¶Ö¸Õ뷽ʽ²åÈë
    60. */
    61. void insert_node_recursive_ptr2tptr (BTree **node , int data )
    62. {
    63. if( *node == NULL ){
    64. *node = create_node ( data ) ;
    65. return ;
    66. }
    67. if ( data < (*node)->data )
    68. insert_node_recursive_ptr2tptr( &((*node)->left), data );
    69. else
    70. insert_node_recursive_ptr2tptr( &((*node)->right), data );
    71. }
    72. /*
    73. µÝ¹é+Ö¸ÕëÒýÓõķ½Ê½²åÈë
    74. */
    75. void insert_node_recursive_reference (BTree* &node , int data )
    76. {
    77. if( node == NULL ){
    78. node = create_node ( data ) ;
    79. return ;
    80. }
    81. if ( data < node->data )
    82. insert_node_recursive_reference( node->left, data );
    83. else
    84. insert_node_recursive_reference( node->right, data );
    85. }
    86. // Ïȸù±éÀú(µÝ¹é)
    87. void traversal_DLR_recursive ( BTree * node )
    88. {
    89. if( node == NULL ) return ;
    90. cout<< node->data <<",";
    91. traversal_DLR_recursive ( node->left );
    92. traversal_DLR_recursive ( node->right );
    93. }
    94. // Ïȸù±éÀú(·ÇµÝ¹é)
    95. void traversal_DLR ( BTree * node )
    96. {
    97. stack<BTree *> last;
    98. while ( node != NULL ) {
    99. cout<<node->data<<",";
    100. if ( node->left != NULL ) {
    101. if ( node->right != NULL ) {
    102. last.push(node->right);
    103. }
    104. node = node->left;
    105. } else if ( node->right != NULL ) {
    106. node = node->right;
    107. } else {
    108. if ( last.empty() ) return;
    109. node = last.top();
    110. last.pop();
    111. }
    112. }
    113. }
    114. // Öиù±éÀú(µÝ¹é)
    115. void traversal_LDR_recursive ( BTree * node )
    116. {
    117. if( node == NULL ) return ;
    118. traversal_LDR_recursive ( node->left );
    119. cout<< node->data <<",";
    120. traversal_LDR_recursive ( node->right );
    121. }
    122. // Öиù±éÀú(·ÇµÝ¹é)
    123. void traversal_LDR ( BTree * node )
    124. {
    125. stack<BTree *> last;
    126. while ( node != NULL ) {
    127. if( !last.empty() && node == last.top() ) {
    128. cout<<node->data<<",";
    129. if ( node->right != NULL ) {
    130. last.pop();
    131. node = node->right;
    132. } else {
    133. last.pop();
    134. if ( last.empty() ) return;
    135. node = last.top();
    136. }
    137. } else if ( node->left != NULL ) {
    138. last.push(node);
    139. node = node->left;
    140. } else if ( node->right != NULL ) {
    141. cout<<node->data<<",";
    142. node = node->right;
    143. } else {
    144. cout<<node->data<<",";
    145. if ( last.empty() ) return;
    146. node = last.top();
    147. }
    148. }
    149. }
    150. // ºó¸ù±éÀú(µÝ¹é)
    151. void traversal_LRD_recursive ( BTree * node )
    152. {
    153. if( node == NULL ) return ;
    154. traversal_LRD_recursive ( node->left );
    155. traversal_LRD_recursive ( node->right );
    156. cout<< node->data <<",";
    157. }
    158. // ºó¸ù±éÀú(·ÇµÝ¹é,Õ»¸¨Öú)
    159. void traversal_LRD ( BTree * node )
    160. {
    161. stack<BTree *> last;
    162. stack<BTree *> last_left;
    163. stack<BTree *> last_right;
    164. /*
    165. if ( root->left != NULL ) {
    166. BTree * node = root->left;
    167. while ( node != NULL ) {
    168. if ( !last_left.empty() && node == last_left.top() ) {
    169. if ( node->right != NULL ) {
    170. node = node->right;
    171. } else {
    172. cout<<node->data<<",";
    173. last_left.pop();
    174. if ( last_right.empty() ) return;
    175. node = last_right.top();
    176. }
    177. } else if ( node->left != NULL ) {
    178. last_left.push(node);
    179. if ( node->right != NULL ) {
    180. last_left.push( node->right );
    181. }
    182. node = node->left;
    183. } else if( node->right != NULL ) {
    184. node = node->right;
    185. } else {
    186. cout<<node->data<<",";
    187. if ( last.empty() ) return;
    188. node = last.top();
    189. }
    190. }
    191. } else if ( root->right != NULL ) {
    192. BTree * node = root->right;
    193. while ( node != NULL ) {
    194. if ( !last_right.empty() && node == last_right.top() ) {
    195. if ( node->right != NULL ) {
    196. node = node->right;
    197. } else {
    198. cout<<node->data<<",";
    199. last_right.pop();
    200. if ( last_right.empty() ) return;
    201. node = last_right.top();
    202. }
    203. } else if ( node->left != NULL ) {
    204. last_right.push(node);
    205. if ( node->right != NULL ) {
    206. last_right.push( node->right );
    207. }
    208. node = node->left;
    209. } else if( node->right != NULL ) {
    210. node = node->right;
    211. } else {
    212. cout<<node->data<<",";
    213. if ( last.empty() ) return;
    214. node = last.top();
    215. }
    216. }
    217. }
    218. cout<<root->data<<endl;
    219. */
    220. while ( node != NULL ) {
    221. if( !last.empty() && node == last.top() ) {
    222. if ( node->right != NULL ) {
    223. //last.push(node->left);
    224. node = node->right;
    225. } else {
    226. cout<<node->data<<",";
    227. last.pop();
    228. if ( last.empty() ) return;
    229. node = last.top();
    230. }
    231. } else if ( node->left != NULL ) {
    232. last.push(node);
    233. node = node->left;
    234. } else if ( node->right != NULL ) {
    235. //cout<<node->data<<",";
    236. node = node->right ;
    237. } else {
    238. cout<<node->data<<",";
    239. if ( last.empty() ) return;
    240. node = last.top();
    241. }
    242. }
    243. }
    244. int main(int argc, char* argv[])
    245. {
    246. BTree * root =create_node ( 6 ) ;
    247. BTree * &node = root ;
    248. //²åÈë(ÆÕͨ)
    249. insert_node ( root , 19 );
    250. insert_node ( root , 4 );
    251. //²åÈë(µÝ¹é)
    252. insert_node_recursive( root , 3 );
    253. //²åÈë(¶þ¼¶Ö¸Õë)
    254. insert_node_recursive_ptr2tptr(&root , 5 );
    255. //²åÈë(ÒýÓÃ)
    256. insert_node_recursive_reference ( node , 10 );
    257. cout<<"Ïȸù±éÀú(µÝ¹é)£º"<<endl;
    258. traversal_DLR_recursive ( root );
    259. cout<<endl<<"=============="<<endl;
    260. cout<<"Ïȸù±éÀú(·ÇµÝ¹é,Õ»¸¨Öú)£º"<<endl;
    261. traversal_DLR ( root );
    262. cout<<endl<<"=============="<<endl;
    263. cout<<"Öиù±éÀú(µÝ¹é)£º"<<endl;
    264. traversal_LDR_recursive ( root );
    265. cout<<endl<<"=============="<<endl;
    266. cout<<"Öиù±éÀú(·ÇµÝ¹é£¬Õ»¸¨Öú)£º"<<endl;
    267. traversal_LDR ( root );
    268. cout<<endl<<"=============="<<endl;
    269. cout<<"ºó¸ù±éÀú(µÝ¹é)£º"<<endl;
    270. traversal_LRD_recursive ( root );
    271. cout<<endl<<"=============="<<endl;
    272. cout<<"ºó¸ù±éÀú(·ÇµÝ¹é£¬Õ»¸¨Öú)£º"<<endl;
    273. traversal_LRD ( root );
    274. cout<<endl<<"=============="<<endl;
    275. return 0;
    276. }





  • 相关阅读:
    iPad用户使用Mac和Windows应用软件-记Parallels Access使用体验
    用ipad维护Linux服务器
    Class Model of Quick Time Plugin
    vm_write
    [转]Getting a Packet Trace
    [原]调试没有符号的 iOS 应用
    [转]编译 JavaScriptCore For iOS
    [转]ARM64 Function Calling Conventions
    [转]如何选择职业道路
    [转]Native Java Bytecode Debugging without Source Code
  • 原文地址:https://www.cnblogs.com/fysola/p/4862554.html
Copyright © 2020-2023  润新知