Advertisement
Doragonroudo

EGCO112 #5 - 9/3/18 - Binary Tree

Mar 9th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. //
  2. //  test.c
  3. //  
  4. //
  5. //  Created by 6272 on 3/9/2561 BE.
  6. //
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #define NL printf("\n")
  11.  
  12. struct treeNode
  13. {
  14.     struct treeNode *leftPtr;
  15.     int data;
  16.     struct treeNode *rightPtr;
  17. };
  18. typedef struct treeNode TreeNode;
  19. typedef TreeNode *TreeNodePtr;
  20.  
  21. void insertNode(TreeNodePtr *root, int value)
  22. {
  23.     TreeNodePtr newNode, c;
  24.     newNode = (TreeNodePtr)malloc(sizeof(TreeNode));
  25.     if (newNode)
  26.     {
  27.         newNode->data = value;
  28.         newNode->leftPtr = NULL;
  29.         newNode->rightPtr = NULL;
  30.         c = *root;
  31.         printf("DEBUG : Inserting %d\n", value);
  32.         if (c == NULL) *root = newNode;
  33.         else
  34.         {
  35.             while (c)
  36.             {
  37.                 if (c->data >= value)
  38.                 {
  39.                     printf("DEBUG : Going Left\n");
  40.                     if (c->leftPtr == NULL)
  41.                     {
  42.                         printf("DEBUG : Assign\n");
  43.                         c->leftPtr = newNode;
  44.                         break;
  45.                     }
  46.                     else
  47.                     {
  48.                         c = c->leftPtr;
  49.                     }
  50.                 }
  51.                 else
  52.                 {
  53.                     printf("DEBUG : Going Right\n");
  54.                     if (c->rightPtr == NULL)
  55.                     {
  56.                         printf("DEBUG : Assign\n");
  57.                         c->rightPtr = newNode;
  58.                         break;
  59.                     }
  60.                     else
  61.                     {
  62.                         c = c->rightPtr;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. void inOrder(TreeNodePtr treePtr)
  71. {
  72.     // if tree is not empty, then traverse
  73.     if (treePtr != NULL)
  74.     {
  75.         inOrder(treePtr->leftPtr); //Recursion to the left
  76.         printf("%3d", treePtr->data); //print the value
  77.         inOrder(treePtr->rightPtr); //Recursion to the right
  78.     } // end if
  79. } // end function inOrder
  80.  
  81. void preOrder (TreeNodePtr treePtr)
  82. {
  83.     if (treePtr != NULL)
  84.     {
  85.         printf("%3d", treePtr->data); //print the value
  86.         preOrder(treePtr->leftPtr); //Recursion to the left
  87.         preOrder(treePtr->rightPtr); //Recursion to the right
  88.     } // end if
  89. }
  90.  
  91. void postOrder (TreeNodePtr treePtr)
  92. {
  93.     if (treePtr != NULL)
  94.     {
  95.         postOrder(treePtr->leftPtr); //Recursion to the left
  96.         postOrder(treePtr->rightPtr); //Recursion to the right
  97.         printf("%3d", treePtr->data); //print the value
  98.     } // end if
  99. }
  100.  
  101. void lab (TreeNodePtr treePtr, int a)
  102. {
  103.     a++;
  104.     if (treePtr != NULL)
  105.     {
  106.         lab(treePtr->rightPtr, a); //Recursion to the right
  107.         int i;
  108.         for(i=0; i<a; i++)
  109.         {
  110.             printf("     ");
  111.         }
  112.         printf("%3d\n", treePtr->data); //print the value
  113.         lab(treePtr->leftPtr, a); //Recursion to the left
  114.     } // end if
  115. }
  116.  
  117. int main(int argc, const char *argv[])
  118. {
  119.     int i;
  120.     TreeNodePtr root;
  121.     root = NULL;
  122.     for (i=1; i<argc; i++)
  123.     {
  124.         insertNode(&root, atoi(argv[i]));
  125.     }
  126.     printf("inOrder\n");
  127.     inOrder(root);
  128.     NL;
  129.     printf("preOrder\n");
  130.     preOrder(root);
  131.     NL;
  132.     printf("postOrder\n");
  133.     postOrder(root);
  134.     NL;
  135.     printf("lab\n");
  136.     lab(root, -1);
  137.     NL;
  138.    
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement