0% found this document useful (0 votes)
5 views1 page

Exp 9 DS

Ds experiments

Uploaded by

robincyril712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Exp 9 DS

Ds experiments

Uploaded by

robincyril712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insertNode(struct Node* root, int data) {


if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insertNode(root->left, data);
} else {
root->right = insertNode(root->right, data);
}
return root;
}

void inorderTraversal(struct Node* root) {


if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

int main() {
struct Node* root = NULL;
int data, i;

printf("Enter 6 nodes for the BST:\n");


for (i = 0; i < 6; i++) {
scanf("%d", &data);
root = insertNode(root, data);
}

printf("Inorder Traversal: ");


inorderTraversal(root);
printf("\n");

return 0;
}

You might also like