Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "treehead.h"
- int LAST_ID = 0;
- struct tree *insert (struct tree *p, char *name, int direction)
- {
- struct tree *temporary;
- temporary = (struct tree*) malloc(sizeof(struct tree));
- temporary->name = *name;
- temporary->ID = LAST_ID + 1;
- temporary->leftchild = temporary->rightchild = NULL;
- if (p == NULL)
- {
- return temporary;
- }
- else
- {
- if (direction == 0) // to the left
- {
- p->leftchild = temporary;
- p->childscount += 1;
- p->child_ids[p->childscount] = temporary->ID;
- p->leftchild->parentscount += 1;
- p->leftchild->parent_ids[p->leftchild->parentscount] = p->ID;
- LAST_ID += 1;
- return temporary;
- }
- else if(direction == 1) // insert in the middle
- {
- p->midchild = temporary;
- p->childscount += 1;
- p->child_ids[p->childscount] = temporary->ID;
- p->midchild->parentscount += 1;
- p->midchild->parent_ids[p->midchild->parentscount] = p->ID;
- LAST_ID += 1;
- return temporary;
- }
- else // to the right
- {
- p->rightchild = temporary;
- p->childscount += 1;
- p->child_ids[p->childscount] = temporary->ID;
- p->rightchild->parentscount += 1;
- p->rightchild->parent_ids[p->rightchild->parentscount] = p->ID;
- LAST_ID += 1;
- return temporary;
- }
- }
- }
- void inorder(struct tree *p)
- {
- inorder(p->leftchild);
- printf("%d ", p->name, p->ID);
- inorder(p->midchild);
- inorder(p->rightchild);
- }
- void preorder(struct tree *p)
- {
- printf("%d%d The name of the tree and its ID is: ", p->name, p->ID);
- preorder(p->leftchild);
- preorder(p->midchild);
- preorder(p->rightchild);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement