dzungchaos

CTDL&TT: Cây và thao tác

May 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Tnode{
  7.     char word[20]; // D. li.u c.a nút
  8.     struct Tnode * left;
  9.     struct Tnode *right;
  10. };
  11.  
  12. typedef struct Tnode treeNode;
  13.  
  14. treeNode* makeTreeNode(char *word)
  15. {
  16.     treeNode* newNode = NULL;
  17.     newNode = (treeNode*)malloc(sizeof(treeNode));
  18.     if (newNode == NULL){
  19.         printf("Out of memory\n");
  20.     exit(1);
  21.     }
  22.     else {
  23.         newNode->left = NULL;
  24.         newNode->right= NULL;
  25.         strcpy(newNode->word,word);
  26.     }
  27.    
  28.     return newNode;
  29. }
  30.  
  31. int countNodes(treeNode *tree) {
  32.     /* the function counts the number of nodes of a tree*/
  33.     if( tree == NULL ) return 0;
  34.     else {
  35.         int ld = countNodes(tree->left);
  36.         int rd = countNodes(tree->right);
  37.    
  38.     return 1+ld+rd;
  39.     }
  40. }
  41.  
  42. int depth(treeNode *tree) {
  43.     /* the function computes the depth of a tree */
  44.     if( tree == NULL ) return 0;
  45.     int ld = depth(tree->left);
  46.     int rd = depth(tree->right);
  47.     /* if (ld > rd) return 1+ld; else return 1+rd; */
  48.    
  49.     return 1 + (ld > rd ? ld : rd);
  50. }
  51.  
  52. void freeTree(treeNode *tree)
  53. {
  54.     if( tree == NULL ) return;
  55.     freeTree(tree->left);
  56.     freeTree(tree->right);
  57.     free(tree);
  58.    
  59.     return;
  60. }
  61.  
  62. void printPreorder(treeNode *tree)
  63. {
  64.     if( tree != NULL )
  65.     {
  66.         printf("%s\n", tree->word);
  67.         printPreorder(tree->left);
  68.         printPreorder(tree->right);
  69.     }
  70. }
  71.  
  72. void printInorder(treeNode *tree)
  73. {
  74.     if( tree != NULL )
  75.     {
  76.         printInorder(tree->left);
  77.         printf("%s\n", tree->word);
  78.         printInorder(tree->right);
  79.     }
  80. }
  81.  
  82. void printPostorder(treeNode *tree)
  83. {
  84.     if( tree != NULL )
  85.     {
  86.         printPostorder(tree->left);
  87.         printPostorder(tree->right);
  88.         printf("%s\n", tree->word);
  89.     }
  90. }
  91.  
  92. treeNode *RandomInsert(treeNode *tree,char *word){
  93.     treeNode *newNode, *p;
  94.     newNode = makeTreeNode(word);
  95.     if ( tree == NULL ) return newNode;
  96.     if ( rand()%2 ==0 ){
  97.         p=tree;
  98.         while (p->left !=NULL) p=p->left;
  99.         p->left=newNode;
  100.         printf("Node %s is left child of %s \n",word,(*p).word);
  101.     }
  102.     else {
  103.         p=tree;
  104.         while (p->right !=NULL) p=p->right;
  105.         p->right=newNode;
  106.         printf("Node %s is right child of %s \n",word,(*p).word);
  107.     }
  108.    
  109.     return tree;
  110. }
  111.    
  112. int main() {
  113.     treeNode *randomTree=NULL;
  114.     char word[20] = "a";
  115.     while( strcmp(word,"~")!=0)
  116.     {
  117.         printf("\nEnter item (~ to finish): ");
  118.         scanf("%s", word);
  119.     if (strcmp(word,"~")==0)
  120.         printf("Cham dut nhap thong tin nut... \n");
  121.     else randomTree=RandomInsert(randomTree,word);
  122.     }
  123.    
  124.     printf("The tree in preorder:\n"); printPreorder(randomTree);
  125.     printf("The tree in postorder:\n"); printPostorder(randomTree);
  126.     printf("The tree in inorder:\n"); printInorder(randomTree);
  127.     printf("The number of nodes is: %d\n",countNodes(randomTree));
  128.     printf("The depth of the tree is: %d\n", depth(randomTree));
  129.     freeTree(randomTree);
  130.     getch();
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment