Showing posts with label DataStructures. Show all posts
Showing posts with label DataStructures. Show all posts

Sunday, 3 September 2017

Implement a C program to print all root to leaf paths of a BST



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode  {
          int data;
          struct tnode * leftc;
          struct tnode * rightc;
};

void insert(struct tnode **, int num);
void inorder(struct tnode * s);
int max(int a, int b);
int height(struct tnode* node);
int diameter(struct tnode * tree);

int main()  {
          struct tnode * root=NULL;
          char ch[5];
          int num;
          do {
                   printf("Enter the element to be inserted in the tree\n");
                   scanf("%d",&num);
                   insert(&root, num);
                   printf("Do you want to insert another element?\n");
                   scanf("%s",ch);
          }while(strcmp(ch,"yes")==0);
          printf("The elements in the tree are");
          inorder(root);
          printf("\n");
          printf("The diameter of the tree is %d\n",diameter(root));
          return 0;
}

void insert(struct tnode ** s, int num) {
          if((*s) == NULL) {
                   (*s) = (struct tnode *) malloc( sizeof (struct tnode));
                   (*s)->data = num;
                   (*s)->leftc = NULL;
                   (*s)->rightc = NULL;
          }
          else    {
                   if(num < (*s)->data)
                     insert(&( (*s)->leftc ), num);
                   else
                     insert(&( (*s)->rightc ), num);
          }
}

void inorder(struct tnode * s)  {
          if(s != NULL)
          {
                   inorder(s->leftc);
                   printf(" %d",s->data);
                   inorder(s->rightc);
          }
}

int diameter(struct tnode * tree)  {
   if (tree == NULL)
     return 0;
  int lefthigh = height(tree->leftc);
  int righthigh = height(tree->rightc);
  int leftdia = diameter(tree->leftc);
  int rightdia = diameter(tree->rightc);
return max(lefthigh + righthigh + 1, max(leftdia, rightdia));
}

int height(struct tnode* node)  {
   if(node == NULL)
       return 0;
   return 1 + max(height(node->leftc), height(node->rightc));
}

int max(int a, int b)  {
  return (a >= b)? a: b;
}

Implement a C program to find the maximum depth or height of a BST



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tnode {
          int data;
          struct tnode * leftc;
          struct tnode * rightc;
};

void insert(struct tnode **, int num);
void inorder(struct tnode *);
int findMaxDepth(struct tnode * s);

int main() {
          struct tnode * root=NULL;
          char ch[5];
          int num;
          do {
                   printf("Enter the element to be inserted in the tree\n");
                   scanf("%d",&num);
                   insert(&root, num);
                   printf("Do you want to insert another element?\n");
                   scanf("%s",ch);
          }while(strcmp(ch,"yes")==0);
          printf("The elements in the tree are");
          inorder(root);
          printf("\n");
          printf("The maximum depth of the tree is %d\n",findMaxDepth(root));
          return 0;
}

void insert(struct tnode ** s, int num) {
          if((*s) == NULL) {
                   (*s) = (struct tnode *) malloc( sizeof (struct tnode));
                   (*s)->data = num;
                   (*s)->leftc = NULL;
                   (*s)->rightc = NULL;
          }
          else    {
                   if(num < (*s)->data)
                     insert(&( (*s)->leftc ), num);
                   else
                     insert(&( (*s)->rightc ), num);
          }
}

void inorder(struct tnode * s) {
   if(s!=NULL)
    {
        inorder(s->leftc);
        printf(" %d", s->data);
        inorder(s->rightc);
    }
}


int findMaxDepth(struct tnode * s) {
   if (s==NULL)
       return 0;
   else
   {
       int lDepth = findMaxDepth(s->leftc);
       int rDepth = findMaxDepth(s->rightc);
       if (lDepth > rDepth)
           return(lDepth+1);
       else return(rDepth+1);
   }
}

C-Program to know whether the character is vowel or not

#include<stdio.h> int main() {   char c;   printf("Enter a character to know whether it is vowel or not\n");   scanf("...