HERE IS THE CODE
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct node {
struct node *leftChild;
int data;
struct node *rightChild;
};
void insert(int, struct node **);
void inorderTraversal(struct node *);
void main() {
int numberOfNodes, i, currentNode;
struct node *root = NULL;
printf("Enter number of node\n");
scanf("%d", &numberOfNodes);
printf("Enter the node\n");
for (i=0;i<numberOfNodes;i++) {
scanf("%d", ¤tNode);
insert(currentNode, &root);
}
inorderTraversal(root);
}
void insert(int currentNode, struct node **root) {
if (*root == NULL) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp -> data = currentNode;
temp -> leftChild = NULL;
temp -> rightChild = NULL;
*root = temp;
} else if (currentNode < (*root) -> data) {
insert(currentNode, &((*root)->leftChild));
} else {
insert(currentNode, &((*root)->rightChild));
}
}
void inorderTraversal(struct node *root) {
if (root == NULL) {
return;
}
inorderTraversal(root-> leftChild);
printf("%d\t", root -> data);
inorderTraversal(root -> rightChild);
}
No comments:
Post a Comment