dzungchaos

CTDL&TT: BST

Jul 5th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node
  5. {
  6.     int data;
  7.     Node* left;
  8.     Node* right;
  9. } node_t;
  10.  
  11.  
  12.  
  13. void Free( node_t* root )
  14. {
  15.     if ( root == NULL )
  16.         return;
  17.  
  18.     Free( root->left );
  19.     Free( root->right );
  20.     free( root );
  21. }
  22.  
  23. int LeftOf( const int value, const node_t* root )
  24. {
  25.     // Nếu bạn muốn cây BST cho phép giá trị trùng lặp, hãy sử dụng dòng code thứ 2
  26.     return value < root->data;
  27. //    return value <= root->data;
  28. }
  29.  
  30. int RightOf( const int value, const node_t* root )
  31. {
  32.     return value > root->data;
  33. }
  34.  
  35. node_t* Insert( node_t* root, const int value )
  36. {
  37.     if ( root == NULL )
  38.     {
  39.         node_t* node = (node_t*)malloc( sizeof( node_t ) );
  40.         node->left = NULL;
  41.         node->right = NULL;
  42.         node->data = value;
  43.         return node;
  44.     }
  45.     if ( LeftOf( value, root ) )
  46.         root->left = Insert( root->left, value );
  47.     else if ( RightOf( value, root ) )
  48.         root->right = Insert( root->right, value );
  49.     return root;
  50. }
  51.  
  52. bool Search( const node_t* root, int value )
  53. {
  54.     if ( root == NULL )
  55.         return false;
  56.     if(root->data == value){
  57.         return true;
  58.     }else if ( LeftOf( value, root ) ){
  59.         return Search( root->left, value );
  60.     }else if( RightOf( value, root ) ){
  61.         return Search( root->right, value );
  62.     }
  63. }
  64.  
  65. int LeftMostValue( const node_t* root )
  66. {
  67.     while ( root->left != NULL )
  68.         root = root->left;
  69.     return root->data;
  70. }
  71.  
  72. node_t* Delete( node_t* root, int value )
  73. {
  74.     if ( root == NULL )
  75.         return root;
  76.     if ( LeftOf( value, root ) )
  77.         root->left = Delete( root->left, value );
  78.     else if ( RightOf( value, root ) )
  79.         root->right = Delete( root->right, value );
  80.     else
  81.     {
  82.         // root->data == value, delete this node
  83.         if ( root->left == NULL )
  84.         {
  85.             node_t* newRoot = root->right;
  86.             free( root );
  87.             return newRoot;
  88.         }
  89.         if ( root->right == NULL )
  90.         {
  91.             node_t* newRoot = root->left;
  92.             free( root );
  93.             return newRoot;
  94.         }
  95.         root->data = LeftMostValue( root->right );
  96.         root->right = Delete( root->right, root->data );
  97.     }
  98.     return root;
  99. }
  100.  
  101. void PreOrder(node_t* root){
  102.     if(root != NULL)
  103.     {
  104.         printf("%d ", root->data);
  105.         PreOrder(root->left);
  106.         PreOrder(root->right);
  107.     }
  108. }
  109.  
  110. void InOrder(node_t* root){
  111.     if(root != NULL)
  112.     {
  113.         InOrder(root->left);
  114.         printf("%d ", root->data);
  115.         InOrder(root->right);
  116.     }
  117. }
  118.  
  119. void PostOrder(node_t* root){
  120.     if(root != NULL)
  121.     {
  122.         PostOrder(root->left);
  123.         PostOrder(root->right);
  124.         printf("%d ", root->data);
  125.     }
  126. }
  127.  
  128.  
  129. int main()
  130. {
  131.     node_t* root = NULL;
  132.  
  133.     root = Insert( root, 25 );
  134.     root = Insert( root, 15 );
  135.     root = Insert( root, 50 );
  136.     root = Insert( root, 10 );
  137.     root = Insert( root, 22 );
  138.     root = Insert( root, 35 );
  139.     root = Insert( root, 70 );
  140.     root = Insert( root, 4 );
  141.     root = Insert( root, 12 );
  142.     root = Insert( root, 18 );
  143.     root = Insert( root, 24 );
  144.     root = Insert( root, 31 );
  145.     root = Insert( root, 44 );
  146.     root = Insert( root, 66 );
  147.     root = Insert( root, 90 );
  148.     printf("\nDuyet preorder : ");
  149.     PreOrder(root);
  150.     printf("\nDuyet inorder  : ");
  151.     InOrder(root);
  152.     printf("\nDuyet postorder:");
  153.     PostOrder(root);
  154.  
  155.     printf("\n==Thu them phan tu 15 vao BTS==\n");
  156.     Insert(root, 15);
  157.     printf("\nDuyet preorder : ");
  158.     PreOrder(root);
  159.     printf("\nDuyet inorder  : ");
  160.     InOrder(root);
  161.     printf("\nDuyet postorder:");
  162.     PostOrder(root);
  163.  
  164.  
  165.     printf("\n==Thu xoa phan tu 50 khoi BTS==\n");
  166.     Delete(root, 50);
  167.     printf("\nDuyet preorder : ");
  168.     PreOrder(root);
  169.     printf("\nDuyet inorder  : ");
  170.     InOrder(root);
  171.     printf("\nDuyet postorder:");
  172.     PostOrder(root);
  173.  
  174.  
  175.  
  176.  
  177.     Free( root );
  178.     root = NULL;
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment