Algorithm
Algo Create Binary Tree
INPUT: ROOT IS POINTER TO THE ROOT NODE OF THE TREE
OUTPUT: ELEMENTS OF BINARY TREE
DATA STRUCTURE: BINARY TREE USING LINKEDLIST
STEP 1: START
STEP 2: PRINT("ENTER THE ROOT NODE")
STEP 3: ROOT=GETNODE(NODE)
STEP 4: ROOT->DATA=ITEM
STEP 5: ROOT->LC=NULL
STEP 6: ROOT->RC=NULL
STEP 7: PRINT("ENTER THE REMAING ELEMENTS:")
STEP 8: NEWNODE(ROOT,ITEM)
STEP 9: NEWNODE(ROOT,ITEM)
STEP 10: PRINT("DOES NODE ITEM HAS A LEFT CHILD Y/N")
STEP 11: IF(FLAG=Y)THEN
STEP 12: IF(PTR->LC=NULL)THEN
STEP 13: PTR->LC=GETNODE(NODE)
STEP 14: PTR->RC=NULL
STEP 15: PTR->LC=NULL
STEP 16: ENDIF
STEP 17: NEWNODE(PTR,ITEM)
STEP 18: PRINT("DOES NODE ITEM HAS A RIGHT CHILD Y/N")
STEP 19: IF(FLAG=Y)THEN
STEP 20: IF(PTR->RC=NULL)THEN
STEP 21: PTR->RC=GETNODE(NODE)
STEP 22: PTR->RC=NULL
STEP 23: PTR->LC=NULL
STEP 24: ENDIF
STEP 25: NEWNODE(PTR,ITEM)
STEP 26: END
Algo Preorder
STEP 1: START
STEP 2: PREORDER(ROOT)
STEP 3: IF(ROOT==NULL)THEN
STEP 4: RETURN ROOT
STEP 5: ENDIF
STEP 6: PRINT(ROOT->DATA)
STEP 7: PREORDER(PTR->LC)
STEP 8: PREORDER(PTR->RC)
STEP 9: END
Algo Inorder
STEP 1: START
STEP 2: PREORDER(ROOT)
STEP 3: IF(ROOT==NULL)THEN
STEP 4: RETURN ROOT
STEP 5: ENDIF
STEP 6: PREORDER(PTR->LC)
STEP 7: PRINT(ROOT->DATA)
STEP 8: PREORDER(PTR->RC)
STEP 9: END
Algo Postorder
STEP 1: START
STEP 2: PREORDER(ROOT)
STEP 3: IF(ROOT==NULL)THEN
STEP 4: RETURN ROOT
STEP 5: ENDIF
STEP 6: PREORDER(PTR->LC)
STEP 7: PREORDER(PTR->RC)
STEP 8: PRINT(ROOT->DATA)
STEP 9: END
Code
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *lc;
struct node *rc;
};
char ch;int n;
int item;
struct node *news, *node1, *node2;
struct node *newnode(struct node *,int);
struct node *getnode();
struct node *postordertraversal(struct node *);
struct node *preordertraversal(struct node *);
struct node *inordertraversal(struct node *);
int main()
{
struct node *tree;
tree = malloc(sizeof(struct node));
printf("ENTER THE ROOT NODE");
scanf("%d", &tree->data);
// tree->lc = NULL;
// tree->rc = NULL;
// printf("ENTER THE ELEMENTS AND -1 FOR EXIT");
newnode(tree,tree->data);
printf("preorder Traversal: ");
preordertraversal(tree);
printf("\n");
printf("inorder traversal: ");
inordertraversal(tree);
printf("\n");
printf("postorder traversal: ");
postordertraversal(tree);
return 0;
}
struct node *newnode(struct node *ptr,int n)
{
// if(ptr==NULL)
// {
// ptr=malloc(sizeof(struct node));
// }
printf("DOES TREE %d HAS LEFT CHILD Y/N :",n);
scanf("%c", &ch);
if ((getchar()) != 'n')
{
// if (ptr->lc == NULL)
// {
struct node *news=malloc(sizeof(struct node));
ptr->lc=news;
// ptr = news;
printf("ENTER THE NODE DATA");
scanf("%d", &news->data);
n=ptr->data;
// ptr->lc = NULL;
// ptr->rc = NULL;
// }
newnode(ptr->lc,news->data);
}
printf("DOES TREE %d HAS RIGHT CHILD Y/N : ",n );
scanf("%c", &ch);
if ((getchar()) != 'n')
{
struct node *news=malloc(sizeof(struct node));
ptr->rc=news;
// ptr = ptr->rc;
printf("ENTER THE NODE DATA");
scanf("%d", &news->data);
n=ptr->data;
// ptr->lc = NULL;
// ptr->rc = NULL;
newnode(ptr->rc,news->data);
}
}
struct node *getnode(int n){
news=malloc(sizeof(struct node));
// news->data=n;
return news;
}
struct node *preordertraversal(struct node *root)
{
if (root == NULL)
return root;
printf("%d ", root->data);
preordertraversal(root->lc);
preordertraversal(root->rc);
}
struct node *inordertraversal(struct node *root)
{
if (root == NULL)
return root;
inordertraversal(root->lc);
printf("%d ", root->data);
inordertraversal(root->rc);
}
struct node *postordertraversal(struct node *root)
{
if (root == NULL)
return root;
postordertraversal(root->lc);
postordertraversal(root->rc);
printf("%d ", root->data);
}
Output
user@computer$ :
ENTER THE ROOT NODE1
DOES TREE 1 HAS LEFT CHILD Y/N :y
ENTER THE NODE DATA2
DOES TREE 2 HAS LEFT CHILD Y/N :y
ENTER THE NODE DATA4
DOES TREE 4 HAS LEFT CHILD Y/N :n
DOES TREE 4 HAS RIGHT CHILD Y/N : n
DOES TREE 2 HAS RIGHT CHILD Y/N : y
ENTER THE NODE DATA5
DOES TREE 5 HAS LEFT CHILD Y/N :n
DOES TREE 5 HAS RIGHT CHILD Y/N : n
DOES TREE 1 HAS RIGHT CHILD Y/N : y
ENTER THE NODE DATA3
DOES TREE 3 HAS LEFT CHILD Y/N :y
ENTER THE NODE DATA6
DOES TREE 6 HAS LEFT CHILD Y/N :n
DOES TREE 6 HAS RIGHT CHILD Y/N : n
DOES TREE 3 HAS RIGHT CHILD Y/N : y
ENTER THE NODE DATA7
DOES TREE 7 HAS LEFT CHILD Y/N :n
DOES TREE 7 HAS RIGHT CHILD Y/N : n
preorder Traversal: 1 2 4 5 3 6 7
inorder traversal: 4 2 5 1 6 3 7
postorder traversal: 4 5 2 6 7 3 1