Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int value;
- struct node *left,*right;
- };
- struct node * createNode(int val){
- struct node *newNode = malloc(sizeof(struct node));
- newNode->value=val;
- newNode->left=newNode->right=NULL;
- return newNode;
- }
- struct node *insertNode(struct node *root,int val){
- if(root==NULL){
- return createNode(val);
- }
- if(val<root->value){
- return insertNode(root->left,val);
- }
- else{
- return insertNode(root->right,val);
- }
- }
- void traverse(struct node *root){
- if(root==NULL) return;
- traverse(root->left);
- printf("%d->",root->value);
- traverse(root->right);
- }
- int main (void){
- struct node *root=NULL;
- int ch=0 , val,cx=0;
- while(1){
- printf("Choose From The Given Options \n1.Insert\t2.Delete\t3.Traverse\t4.Exit\n");
- scanf("%d",&ch);
- switch(ch){
- case 1:
- printf("Enter The Value/Key To Insert \n");
- scanf(" %d",&val);
- if (cx==0){
- root =insertNode(root,val);
- cx++;
- }
- else{
- insertNode(root,val);
- }
- break;
- case 2:break;
- case 3:traverse(root);
- printf("\n");
- break;
- case 4:exit(0);
- }}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement