• something aboout binary search tree~


    i implement the preorder , inorder ,postorder traversal in recursive way and iterative way , print tree , delete node , insert node.... with cpp..

    1 // Tree.cpp : 定义控制台应用程序的入口点。
    2  //
    3  
    4 #include "stdafx.h"
    5 #include <iostream>
    6 #include <stack>
    7 #include <queue>
    8 #include <vector>
    9 #include <string>
    10
    11
    12  using namespace std;
    13
    14 class Node
    15 {
    16 public:
    17 Node *left_child;
    18 Node *right_child;
    19 int depth;
    20 int content;
    21 Node() : left_child ( NULL ) , right_child ( NULL ) , content ( 0 ) , depth( -1 )
    22 {}
    23 Node( int i ) : left_child ( NULL ) , right_child ( NULL ) , content ( i ) , depth ( -1 )
    24 {}
    25 ~Node()
    26 {}
    27 };
    28
    29 typedef Node * Tree;
    30
    31 Tree Insert(Tree tree, int ch)
    32 {
    33 if( tree == NULL )
    34 {
    35 tree = new Node(ch);
    36 }
    37 else
    38 {
    39 if( tree->content < ch)
    40 {
    41 tree->right_child = Insert (tree->right_child , ch);
    42 }
    43 else if( tree->content > ch)
    44 {
    45 tree->left_child = Insert (tree->left_child , ch);
    46 }
    47 }
    48
    49 return tree;
    50 }
    51
    52 void Inorder(Tree tree)
    53 {
    54 if( tree != NULL )
    55 {
    56 if( tree->left_child != NULL )
    57 {
    58 Inorder ( tree->left_child );
    59 }
    60 cout << tree->content<< endl;
    61 if( tree->right_child != NULL )
    62 {
    63 Inorder ( tree->right_child );
    64 }
    65 }
    66 }
    67
    68
    69 void Preorder(Tree tree)
    70 {
    71 if( tree != NULL )
    72 {
    73 cout << tree->content << endl;
    74 if( tree->left_child != NULL )
    75 {
    76 Preorder ( tree->left_child );
    77 }
    78 if( tree->right_child != NULL )
    79 {
    80 Preorder ( tree->right_child );
    81 }
    82 }
    83 }
    84
    85
    86 void Postorder(Tree tree)
    87 {
    88 if( tree != NULL )
    89 {
    90 if( tree->left_child != NULL )
    91 {
    92 Postorder ( tree->left_child );
    93 }
    94 if( tree->right_child != NULL )
    95 {
    96 Postorder ( tree->right_child );
    97 }
    98 cout << tree->content<< endl;
    99 }
    100 }
    101
    102
    103 void Levelorder ( Tree tree )
    104 {
    105 queue <Node> toprint;
    106
    107 if( tree != NULL )
    108 {
    109 Node tmp;
    110 toprint.push( *tree );
    111 while( !toprint.empty() )
    112 {
    113 tmp = toprint.front();
    114 toprint.pop();
    115 cout << tmp.content << endl;
    116 if ( tmp.left_child != NULL )
    117 {
    118 toprint.push ( *tmp.left_child );
    119 }
    120 if ( tmp.right_child != NULL )
    121 {
    122 toprint.push ( *tmp.right_child );
    123 }
    124 }
    125 }
    126 }
    127
    128
    129 void Preorder_iterative ( Tree tree )
    130 {
    131 if ( tree != NULL )
    132 {
    133 stack <Node> toprint;
    134 Node tmp = *tree;
    135 toprint.push ( *tree );
    136 while ( !toprint.empty() )
    137 {
    138 tmp = toprint.top();
    139 toprint.pop();
    140 cout << tmp.content;
    141 while ( tmp.left_child != NULL )
    142 {
    143 if ( tmp.right_child != NULL )
    144 {
    145 // cout <<endl<< "before:"<<tmp.left_child->content<<endl;
    146 toprint.push(*(tmp.right_child));
    147 // cout <<endl<< "after:"<< tmp.left_child->content<<endl;
    148 }
    149
    150 tmp = *(tmp.left_child);
    151 cout << tmp.content;
    152 }
    153 }
    154 }
    155 }
    156
    157 void Postorder_iterative ( Tree tree )
    158 {
    159 stack < Node > toprint;
    160 if ( tree != NULL )
    161 {
    162 Node flag ('0');
    163 Node tmp = *tree;
    164 toprint.push ( tmp );
    165 toprint.push ( flag ); //add a flag: if flag equals 0, don't print it otherwise print it;
    166
    167 while ( !toprint.empty() )
    168 {
    169 flag = toprint.top();
    170 toprint.pop();
    171 tmp = toprint.top();
    172 toprint.pop();
    173 Tree left = tmp.left_child;
    174 Tree right = tmp.right_child;
    175 if ( ( left == NULL && right == NULL ) || ( flag.content == '1' ) )
    176 {
    177 cout << tmp.content;
    178 continue;
    179 }
    180 else
    181 {
    182 flag.content = '1';
    183 toprint.push ( tmp );
    184 toprint.push ( flag );
    185
    186 if( right != NULL )
    187 {
    188 Node r = *right;
    189 Node rf ( '0' );
    190 toprint.push ( r );
    191 toprint.push ( rf );
    192 }
    193
    194 if( left != NULL )
    195 {
    196 Node l = *left;
    197 Node lf ( '0' );
    198 toprint.push ( l );
    199 toprint.push ( lf );
    200 }
    201 }
    202 }
    203 }
    204 }
    205
    206
    207 // each node will be pushed onto stack twice so we need a flag the hold the times to decide whether print the node.
    208 void Inorder_iterative ( Tree tree )
    209 {
    210
    211 stack < Node > toprint;
    212 if ( tree != NULL )
    213 {
    214 Node flag ('0');
    215 Node tmp = *tree;
    216 toprint.push ( tmp );
    217 toprint.push ( flag );
    218
    219 while ( !toprint.empty() )
    220 {
    221 flag = toprint.top();
    222 toprint.pop();
    223 tmp = toprint.top();
    224 toprint.pop();
    225 Tree left = tmp.left_child;
    226 Tree right = tmp.right_child;
    227 if ( ( left == NULL && right == NULL ) || ( flag.content == '1' ) )
    228 {
    229 cout << tmp.content;
    230 continue;
    231 }
    232 else
    233 {
    234 if( right != NULL )
    235 {
    236 Node r = *right;
    237 Node rf ( '0' );
    238 toprint.push ( r );
    239 toprint.push ( rf );
    240 }
    241
    242 flag.content = '1';
    243 toprint.push ( tmp );
    244 toprint.push ( flag );
    245 if( left != NULL )
    246 {
    247 Node l = *left;
    248 Node lf ( '0' );
    249 toprint.push ( l );
    250 toprint.push ( lf );
    251 }
    252 }
    253 }
    254 }
    255 }
    256
    257 //compute depth for each node and return the max depth
    258 int GetDepth ( Tree tree , int base )
    259 {
    260 int max = 0;
    261 if( tree != NULL )
    262 {
    263 tree->depth = base;
    264 max = base;
    265 if ( tree->left_child != NULL )
    266 {
    267 int tmp = GetDepth ( tree->left_child , base+1 );
    268 if ( tmp > max )
    269 {
    270 max = tmp;
    271 }
    272 }
    273 if ( tree->right_child != NULL )
    274 {
    275 int tmp = GetDepth ( tree->right_child , base+1 );
    276 if ( tmp > max )
    277 {
    278 max = tmp;
    279 }
    280 }
    281 }
    282 return max;
    283 }
    284
    285 void PrintTree ( Tree tree )
    286 {
    287 if ( tree != NULL )
    288 {
    289 int max = GetDepth ( tree , 0 );
    290 stack < Node > toprint;
    291 vector < int > accesstimes ( max + 1 , 0 ); //it holds the number of node that haven't be print
    292 accesstimes [0] = 1;
    293 Node tmp = *tree;
    294 toprint.push ( tmp );
    295 while ( !toprint.empty() )
    296 {
    297 tmp = toprint.top ();
    298 toprint.pop();
    299 int d = tmp.depth;
    300 string line;
    301 line.erase();
    302 for(int i = 1 ; i <= 2*d ; i ++)
    303 {
    304 if( (i/2)*2==i && i > 1 )
    305 {
    306 int v = i/2;
    307 if ( accesstimes [ v ] >0 )
    308 {
    309 //cout << "|";
    310 line += '|';
    311 }
    312 else
    313 {
    314 line += ' ';
    315 }
    316
    317 }
    318 else
    319 {
    320 //cout << " ";
    321 line += ' ';
    322 }
    323 }
    324
    325
    326 //cout << "_" << tmp.content;
    327 line += "_";
    328
    329 cout << line <<tmp.content << endl;
    330
    331 int this_node_depth = tmp.depth;
    332 accesstimes [ this_node_depth ] --;
    333
    334 if ( tmp.right_child != NULL )
    335 {
    336 Node r = *tmp.right_child;
    337 toprint.push ( r );
    338 accesstimes [ this_node_depth+1 ] ++;
    339 }
    340 if ( tmp.left_child != NULL )
    341 {
    342 Node l = *tmp.left_child;
    343 toprint.push ( l );
    344 accesstimes [ this_node_depth+1 ] ++;
    345 }
    346 }
    347
    348 }
    349 }
    350
    351 Node * SearchTree ( Tree tree , int target )
    352 {
    353 if( tree == NULL )
    354 {
    355 return NULL;
    356 }
    357 else
    358 {
    359 if ( tree->content == target )
    360 return tree;
    361 else if( tree->content < target )
    362 {
    363 return SearchTree ( tree->right_child , target );
    364 }
    365 else
    366 {
    367 return SearchTree ( tree->left_child , target );
    368 }
    369 }
    370 }
    371
    372 int FindMax ( Tree tree )
    373 {
    374 if ( tree != NULL )
    375 {
    376 Node * max = tree;
    377 while ( max->right_child != NULL )
    378 {
    379 max = max->right_child;
    380 }
    381
    382 return max->content;
    383 }
    384 else
    385 {
    386 return 0;
    387 }
    388 }
    389
    390 int FindMin ( Tree tree )
    391 {
    392 if ( tree != NULL )
    393 {
    394 Node * min = tree;
    395 while ( min->left_child != NULL )
    396 {
    397 min = min->left_child;
    398 }
    399
    400 return min->content;
    401 }
    402 else
    403 {
    404 return 0;
    405 }
    406 }
    407
    408
    409 Node * DeleteNode ( Tree tree , int target )
    410 {
    411 if ( tree != NULL )
    412 {
    413 if ( target == tree->content )
    414 {
    415 if ( tree->left_child && tree->right_child )
    416 {
    417 tree->content = FindMax ( tree->left_child );
    418 tree->left_child = DeleteNode ( tree->left_child , tree->content );
    419 }
    420 else if ( tree->left_child != NULL )
    421 {
    422 Node * tmp = tree;
    423 tree = tree->left_child;
    424 delete tmp;
    425 }
    426 else if ( tree->right_child != NULL )
    427 {
    428 Node * tmp = tree;
    429 tree = tree->right_child;
    430 delete tmp;
    431 }
    432
    433 }
    434 else if ( target < tree->content )
    435 {
    436 tree->left_child = DeleteNode ( tree->left_child , target );
    437 }
    438 else if ( target > tree->content )
    439 {
    440 tree->right_child = DeleteNode ( tree->right_child , target );
    441 }
    442 }
    443 return tree;
    444 }
    445 /*
    446 */
    447
    448 int _tmain(int argc, _TCHAR* argv[])
    449 {
    450 Tree tree = NULL;
    451 for ( int i = 0 ; i < 40 ; i++ )
    452 {
    453 tree = Insert ( tree , rand()%100);
    454 }
    455
    456 //Inorder ( tree );
    457 // cout << endl;
    458 //Preorder ( tree );
    459 // cout << endl;
    460 // Inorder_iterative ( tree );
    461 // cout << endl;
    462 // Inorder ( tree );
    463 // cout << endl;
    464 PrintTree (tree);
    465 tree = DeleteNode ( tree , 34 );
    466 PrintTree (tree);
    467
    468
    469 //Levelorder ( tree );
    470 cout << endl;
    471 getchar();
    472 return 0;
    473 }
    474
    475
  • 相关阅读:
    Java8 Time
    Java8 Stream
    Java8 Lambda
    Thinking in java 阅读
    String 中的 split 进行字符串分割
    Kubernetes 学习(九)Kubernetes 源码阅读之正式篇------核心组件之 Scheduler
    Kubernetes 学习(八)Kubernetes 源码阅读之初级篇------源码及依赖下载
    Golang(八)go modules 学习
    SQLAIchemy(二)ORM 相关
    SQLAIchemy 学习(一)Session 相关
  • 原文地址:https://www.cnblogs.com/kking/p/1847867.html
Copyright © 2020-2023  润新知