SHARE
TWEET

Untitled

a guest Feb 17th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "treehead.h"
  2.  
  3. int LAST_ID = 0;
  4.  
  5. struct tree *insert (struct tree *p, char *name, int direction)
  6. {
  7.     struct tree *temporary;
  8.     temporary = (struct tree*) malloc(sizeof(struct tree));
  9.     temporary->name = *name;
  10.     temporary->ID = LAST_ID + 1;
  11.    
  12.     temporary->leftchild = temporary->rightchild = NULL;
  13.  
  14.     if (p == NULL)
  15.     {
  16.         return temporary;
  17.     }
  18.     else
  19.     {
  20.         if (direction == 0) // to the left
  21.         {
  22.             p->leftchild = temporary;
  23.             p->childscount += 1;
  24.             p->child_ids[p->childscount] = temporary->ID;
  25.             p->leftchild->parentscount += 1;
  26.             p->leftchild->parent_ids[p->leftchild->parentscount] = p->ID;
  27.             LAST_ID += 1;
  28.             return temporary;
  29.         }
  30.         else if(direction == 1) // insert in the middle
  31.         {
  32.             p->midchild = temporary;
  33.             p->childscount += 1;
  34.             p->child_ids[p->childscount] = temporary->ID;
  35.             p->midchild->parentscount += 1;
  36.             p->midchild->parent_ids[p->midchild->parentscount] = p->ID;
  37.             LAST_ID += 1;
  38.             return temporary;
  39.         }
  40.         else // to the right
  41.         {
  42.             p->rightchild = temporary;
  43.             p->childscount += 1;
  44.             p->child_ids[p->childscount] = temporary->ID;
  45.             p->rightchild->parentscount += 1;
  46.             p->rightchild->parent_ids[p->rightchild->parentscount] = p->ID;
  47.             LAST_ID += 1;
  48.             return temporary;
  49.         }
  50.     }
  51. }
  52. void inorder(struct tree *p)
  53. {
  54.         inorder(p->leftchild);
  55.         printf("%d ", p->name, p->ID);
  56.         inorder(p->midchild);
  57.         inorder(p->rightchild);
  58. }
  59. void preorder(struct tree *p)
  60. {
  61.         printf("%d%d The name of the tree and its ID is: ", p->name, p->ID);
  62.         preorder(p->leftchild);
  63.         preorder(p->midchild);
  64.         preorder(p->rightchild);
  65. }
RAW Paste Data
Top