DS3

Practical Name : Write menu driven program using ‘C’ for Binary Search Tree. The menu includes - Create a Binary Search Tree , Insert element in a Binary Search Tree and Display


#include <stdio.h> #include <stdlib.h> struct node {

int key;


struct node *left, *right;


};


struct node* newNode(int item)


{ struct node* temp


= (struct node*)malloc(sizeof(struct node)); temp->key = item;

temp->left = temp->right = NULL; return temp;

}


void inorder(struct node* root)


{


if (root != NULL) { inorder(root->left); printf("%d \n", root->key); inorder(root->right);

} }

 

struct node* insert(struct node* node, int key)


{


if (node == NULL)


return newNode(key); if (key < node->key)

node->left = insert(node->left, key); else if (key > node->key)

node->right = insert(node->right, key); return node;

}


int main()


{


struct node* root = NULL; root = insert(root, 54); insert(root, 32);

insert(root, 23);

insert(root, 43);

insert(root, 74);

insert(root, 66);

insert(root, 89); inorder(root);

return 0;


}


Post a Comment

Previous Post Next Post