Algorithm
INPUT: ROOT IS POINTER TO THE ROOT NODE OF THE TREE
OUTPUT: ELEMENTS OF BINARY SEARCH 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 REMAINING ELEMENTS:")
STEP 8: NEWNODE(ROOT,ITEM)
STEP 9: NEWNODE(ROOT,ITEM)
STEP 10: IF(PTR->DATA>ITEM)THEN
STEP 11: IF(PTR->LC=NULL)THEN
STEP 12: PTR->LC=GETNODE(NODE)
STEP 13: PTR->DATA=ITEM
STEP 14: PTR->RC=NULL
STEP 15: PTR->LC=NULL
STEP 16: ENDIF
STEP 17: ELSE
STEP 18: NEWNODE(PTR,ITEM)
STEP 19: ENDIF
STEP 20: IF(PTR->DATA<ITEM)
STEP 21: IF(PTR->RC=NULL)THEN
STEP 21: PTR->RC=GETNODE(NODE)
STEP 22: PTR->RC=NULL
STEP 23: PTR->LC=NULL
STEP 24: PTR->DATA=ITEM
STEP 24: ENDIF
STEP 25: ELSE
STEP 26: NEWNODE(PTR,ITEM)
STEP 27: ENDIF
STEP 28: 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, *ptr1;
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");
ptr1 = tree;
n = tree->data;
while (n != -1)
{
printf("ENTER THE VALUE OF NODE %d :", n);
scanf("%d", &n);
newnode(tree, n);
}
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("%d", ptr->data);
// printf("ENTER THE VALUE OF NODE %d :", n);
// scanf("%d", &n);
if (n == -1)
{
return 1;
}
if (ptr->data > n)
{
if (ptr->lc == NULL)
{
// if (ptr->lc == NULL)
// {
struct node *news = malloc(sizeof(struct node));
ptr->lc = news;
ptr = news;
news->data = n;
// ptr->data=news->data;
// ptr->lc = NULL;
// ptr->rc = NULL;
// }
}
else
{
newnode(ptr->lc, n);
}
}
// printf("DOES TREE %d HAS RIGHT CHILD Y/N : ",n );
// scanf("%c", &ch);
else if (ptr->data < n)
{
if (ptr->rc == NULL)
{
struct node *news = malloc(sizeof(struct node));
ptr->rc = news;
// ptr = ptr->rc;
ptr = news;
news->data = n;
// ptr->lc = NULL;
// ptr->rc = NULL;
}
else
{
newnode(ptr->rc, n);
}
}
}
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 NODE28
ENTER THE VALUE OF NODE 28 :42
ENTER THE VALUE OF NODE 42 :18
ENTER THE VALUE OF NODE 18 :17
ENTER THE VALUE OF NODE 17 :23
ENTER THE VALUE OF NODE 23 :13
ENTER THE VALUE OF NODE 13 :85
ENTER THE VALUE OF NODE 85 :36
ENTER THE VALUE OF NODE 36 :19
ENTER THE VALUE OF NODE 19 :81
ENTER THE VALUE OF NODE 81 :55
ENTER THE VALUE OF NODE 55 :-1
preorder Traversal: 28 18 17 13 23 19 42 36 85 81 55
inorder traversal: 13 17 18 19 23 28 36 42 55 81 85
postorder traversal: 13 17 19 23 18 36 55 81 85 42 28