Advertisement
Aminpro

[ECP1026] In Lab 2, Question 3

Dec 14th, 2012
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. //https://www.facebook.com/AminproPastebin
  2. // Thank you Joshua Soo for sharing and helping with the code :)
  3. //           ^ ini orang life saver
  4. //And also to Dato Absyarie Syafiq for the skeleton to speed up the coding ^^
  5.  
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<ctype.h>
  10. #include<string.h>
  11.  
  12. struct BTnode *root, *parent1, *parent2; // Global
  13.  
  14. struct BTnode {
  15.         char val;
  16.         struct BTnode *left;
  17.         struct BTnode *right;
  18. };
  19.  
  20. struct BTnode* newBTnode(char val);
  21. void freeBT(struct BTnode *root);
  22. void inorder(struct BTnode*node);        
  23.  
  24. void growTree(void){
  25.  
  26.         root = newBTnode('G');
  27.         root->left = newBTnode('B');
  28.         root->right = newBTnode('K');
  29.  
  30.         parent1 = root->left;
  31.         parent1->left = newBTnode('A');
  32.         parent1->right = newBTnode('C');
  33.  
  34.         parent2 = root->right;
  35.         parent2->left = newBTnode('J');
  36.         parent2->right = newBTnode('M');
  37. }
  38.  
  39. void inorder(struct BTnode *node){
  40.         if (node){
  41.                 inorder(node->left);
  42.                 printf("%c\n",node->val);
  43.                 inorder(node->right);
  44.                 printf("\t");
  45.         }
  46. }
  47.  
  48.  
  49. int main(void){
  50.  
  51.         growTree();
  52.         inorder(root);
  53.     freeBT(root);
  54.     return 0;
  55. }
  56.  
  57. struct BTnode* newBTnode(char val) {
  58.         struct BTnode* newnode  ;
  59.         newnode = (struct BTnode*)malloc(sizeof(struct BTnode));
  60.         newnode->val = val;
  61.         newnode->left = NULL;
  62.         newnode->right = NULL;
  63.         return newnode;
  64. }
  65. void freeBT(struct BTnode *root) {
  66.         if (root) {
  67.                 freeBT(root->left);
  68.                 freeBT(root->right);
  69.                 free(root);
  70.         }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement