Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct treeNode {
  6. struct treeNode* leftPtr; // pointer to left subtree
  7. char data; // node value
  8. struct treeNode* rightPtr; // pointer to right subtree
  9. };
  10.  
  11. typedef struct treeNode TreeNode;
  12. typedef TreeNode* TreeNodePtr;
  13.  
  14. void insertNode(TreeNodePtr* treePtr, char value);
  15. void inOrder(TreeNodePtr treePtr);
  16. void preOrder(TreeNodePtr treePtr);
  17. void postOrder(TreeNodePtr treePtr);
  18.  
  19. int main()
  20. {
  21. TreeNodePtr rootPtr[10] = { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL }; // each tree holds a vocabulary , tree is empty initially
  22. char item; // varaible to hold charactor
  23. char sentence[100]; // hold sentence input by user
  24. int charCounter = 0; // counter for charator in each vocabulary
  25. char* vocabulary[10]; // each vocabulary contains 10 charactor at most
  26. int vocabularyCounter = 0; // counter for vocabulary in sentence
  27. int vocabularyNumber = 0; // number of vocabulary this sentence owns
  28. char* nextTokenPtr = NULL; // for function token_s , points to remaining sentence to be tokenized
  29.  
  30. printf("Please enter a sentence : ");
  31. fgets(sentence, 100, stdin); // read sentence from user
  32. sentence[strlen(sentence) - 1] = '\0'; // cover newline charactor
  33.  
  34. // tolenize the sentence
  35. vocabulary[vocabularyCounter] = strtok_s(sentence, " ", &nextTokenPtr); // first token
  36.  
  37. while (vocabulary[vocabularyCounter] != NULL)
  38. {
  39. printf("%s\n", vocabulary[vocabularyCounter]);
  40. vocabulary[++vocabularyCounter] = strtok_s(NULL, " ", &nextTokenPtr); // obtain next token
  41. }
  42. vocabularyNumber = vocabularyCounter;
  43.  
  44. // for each vocabulary , build the tree
  45. for (vocabularyCounter = 0; vocabularyCounter < vocabularyNumber; vocabularyCounter++)
  46. {
  47. charCounter = 0;
  48.  
  49. // build the tree , using charactors in vocabulary
  50. while (vocabulary[vocabularyCounter][charCounter] != '\0')
  51. {
  52. item = vocabulary[vocabularyCounter][charCounter++];
  53. insertNode(&(rootPtr[vocabularyCounter]), item);
  54. }
  55. }
  56.  
  57. // for each vocabulary , print the tree
  58. for (vocabularyCounter = 0; vocabularyCounter < vocabularyNumber; vocabularyCounter++)
  59. {
  60. // traverse hte tree preOrder
  61. printf("\n\nThe preOrder traversal is : ");
  62. preOrder(rootPtr[vocabularyCounter]);
  63.  
  64. // traverse hte tree inOrder
  65. printf("\n\nThe inOrder traversal is : ");
  66. inOrder(rootPtr[vocabularyCounter]);
  67.  
  68. // traverse hte tree preOrder
  69. printf("\n\nThe postOrder traversal is : ");
  70. postOrder(rootPtr[vocabularyCounter]);
  71. printf("\n");
  72. }
  73. return 0;
  74. }
  75.  
  76. // insert node into tree
  77. void insertNode(TreeNodePtr* treePtr, char value)
  78. {
  79. if (*treePtr == NULL) // if tree is empty or the correct location has been found
  80. {
  81. *treePtr = malloc(sizeof(TreeNode));
  82.  
  83. // if the memory is allocated , then assign data
  84. if (*treePtr != NULL)
  85. {
  86. (*treePtr)->data = value;
  87. (*treePtr)->leftPtr = NULL;
  88. (*treePtr)->rightPtr = NULL;
  89. }
  90. else
  91. {
  92. printf("%c not inserted. No memory avaliable.\n", value);
  93. }
  94. }
  95. else // tree is not empty
  96. {
  97. if (value < (*treePtr)->data) // data to insert is less than data in the current node
  98. {
  99. insertNode(&((*treePtr)->leftPtr), value);
  100. }
  101. else if (value > (*treePtr)->data) // data to insert is greater than data in the current node
  102. {
  103. insertNode(&((*treePtr)->rightPtr), value);
  104. }
  105. else // duplicate data value ignored
  106. {
  107. printf("dup %c ", value);
  108. }
  109. }
  110. } // end function insertNode
  111.  
  112. // begin inorder traversal of tree
  113. void inOrder(TreeNodePtr treePtr)
  114. {
  115. // if tree is not empty , then traverse
  116. if (treePtr != NULL)
  117. {
  118. inOrder(treePtr->leftPtr);
  119. printf("%c ", treePtr->data);
  120. inOrder(treePtr->rightPtr);
  121. }
  122. }
  123.  
  124. // begin preorder traversal of tree
  125. void preOrder(TreeNodePtr treePtr)
  126. {
  127. // if tree is not empty , then traverse
  128. if (treePtr != NULL)
  129. {
  130. printf("%c ", treePtr->data);
  131. preOrder(treePtr->leftPtr);
  132. preOrder(treePtr->rightPtr);
  133. }
  134. }
  135.  
  136. // begin postorder traveral of tree
  137. void postOrder(TreeNodePtr treePtr)
  138. {
  139. // if tree is not empty , then traverse
  140. if (treePtr != NULL)
  141. {
  142. postOrder(treePtr->leftPtr);
  143. postOrder(treePtr->rightPtr);
  144. printf("%c ", treePtr->data);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement