Wednesday, July 23, 2014

BST ( Binary search tree basic operations )

Practice the following programs yourself after under standing the code by following functions. so you can able to under stand the steps to build the BST and operations on it.
courtesy to stanford meterial
http://cslibrary.stanford.edu/110/BinaryTrees.html
http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ ===> helps to understand tree traversal
 
file:bin.h
--------------------
#include
struct node
{
int data;
struct node *left;
struct node *right;
};

file:bin_oper.c
---------------------

#include
#include "bin.h"
struct node * insert (struct node *root, int data);
void preorder(struct node*);
void inorder(struct node*);
void postorder(struct node*);
int search (struct node* root, int data);
void pathtravel(struct node* root,int path[],int pathlen);
void printpath(int path[],int len);
int haspathsum(struct node* root,int sum);
void mirror(struct node *root);
int main()
{
 struct node *root=NULL;
 int option,value;
 int path[100],len=0,pathsum=0;
 do
 {
  printf("\nenter the operation\n");
  scanf("%d",&option);
   switch (option)
   {

    case 1:
     printf("\nInserting into the BST\n");
     /* printf("Enter the data\n");
     scanf("%d",&value); */
     root = insert(root,4);
     insert(root,2);
     insert(root,5);
     insert(root,1);
     insert(root,3);
     break;
    case 2:
     printf("\npreorder traversal\n");
     preorder(root);
     break;
    case 3:
     printf("\ninorder traversal\n");
     inorder(root);
     break;
    case 4:
     printf("\npostorder traversal\n");
     postorder(root);
     break;
    case 5:
     printf("\nsearching the BST\n");
     printf("\nEnter the data need to be searched\n");
     scanf("%d",&value);
     if(search(root,value))
      printf("\nfound\n");
     else
      printf("\nnot found\n");
     break;
    case 6:
     printf("\nprinting the path of the tree\n");
     pathtravel(root,path,len);
     break;
    case 7:
     printf("\nChecking the path sum of the BST\n");
     printf("\nEnter the pathsum value:\t");
     scanf("%d",&pathsum);
     printf("\nchecking any of the the path sum equal to\t%d\n",pathsum);
     int value= haspathsum(root,pathsum);
     if (value == 1)
     printf("\nhaving path\n");
     else 
     printf("\nhaving no path sum\n");
     break;
    case 8:
     printf("\nfinding the mirror of the tree\n");
     mirror(root);
     printf("\nMirror of the tree was found\n");
     break;
    case 9:
     printf("\nfinding the maximum depth of the tree\n");
     value = maxDepth(root);
     printf("\nMaximum depth is %d \n",value);
     break;
    case 10:
     printf("\nfinding the minimum value in the BST\n");
     value = minValue(root);
     printf("\nminimum value in the BST is\t %d \n",value);
     value = rminValue(root);
     printf("\nrecursive minimum value in the BST is\t %d \n",value);
     break;
    case 11:
     printf("\nfinding the Maximum value in the BST\n");
     value = maxValue(root);
     printf("\nmaximum value in the BST is\t %d \n",value);
     value = rmaxValue(root);
     printf("\nrecursive maximum value in the BST is\t %d \n",value);
    break;

   }
 } while (option != -1);
 return 0;
}

struct node * insert (struct node* root, int data)
{
 if (root == NULL)
 {
  root = (struct node *)malloc(sizeof(struct node));
  root->data = data;
  root->left = NULL;
  root->right = NULL;
  return root;
 }
 else if(root->data > data)
 {
  root->left = insert(root->left,data);
 }
 else 
 {
  root->right = insert(root->right,data);
 }
 return (root);
}

void preorder(struct node* root)
{
 /* Root left right */
 if (root== NULL)
  return;
 printf("\n%d",root->data);
 preorder(root->left);
 preorder(root->right);
}
void inorder(struct node* root)
{
 if (root== NULL)
  return;
 /* left right Root */
 inorder(root->left);
 printf("\n%d",root->data);
 inorder(root->right);
}

void postorder(struct node* root)
{
 if (root==NULL)
  return;
 /* left right Root */
 postorder(root->left);
 postorder(root->right);
 printf("\n%d",root->data);
}
int search (struct node* root, int data)
{
 if(root == NULL)
  return 0;
 if (root->data == data)
  return 1;
 else if(root->data > data)
  search(root->left, data);
 else 
  search (root->right, data);
}

void pathtravel(struct node* root,int path[],int pathlen)
{
 if(root == NULL)
 {
  return;
 }
 path[pathlen++]=root->data;
 if(root->left == NULL && root->right == NULL)
 {
  printpath(path,pathlen);
 }
 pathtravel(root->left,path,pathlen);
 pathtravel(root->right,path,pathlen);
}
void printpath(int path[],int len)
{
 int i=0;
 printf("\npath\n");
 for(i=0;idata);
  printf("\ncalling from root->data = %d", root->data);
  return (haspathsum(root->left,sum) || haspathsum(root->right,sum));

  /*
  printf("\ncalling from root->data = %d", root->data);
  value = haspathsum(root->left,sum);
  if(value)
  {
   printf("\nhaving path\n");
   exit(0);
  }
  value = haspathsum(root->right,sum);
  if(value)
  {
   printf("\nhaving path\n");
   exit(0);

  }
  */
 }
}
int size(struct node * root)
{
 if (root == NULL)
  return;
 return (size(root->left)+1+size(root->right));
}
void mirror(struct node *root)
{
 if (root == NULL)
 {
  return;
 }
 mirror(root->left);
 mirror(root->right);
 struct node *temp= root->left;
 root->left=root->right;
 root->right=temp;
}
int maxDepth(struct node* root)
{
 if(root == NULL)
 {
  return;
 }
 int ldepth = 1+ maxDepth(root->left);
 int rdepth = 1+ maxDepth(root->right);
 if(ldepth>rdepth)
 {
  return ldepth;
 }
 else
 {
  return rdepth;
 }
}
int maxValue(struct node* root)
{
 struct node* temp=root;
 while(temp->right!=NULL)
  temp = temp->right;
 return temp->data;
}
int minValue(struct node* root)
{
 struct node* temp=root;
 while(temp->left!=NULL)
  temp = temp->left;
 return temp->data;
}
int rmaxValue(struct node* root)
{
 if(root->right!=NULL)
  rmaxValue(root->right);
 else
  return (root->data);
}
int rminValue(struct node* root)
{
 if(root->left!=NULL)
  rminValue(root->left);
 else
  return (root->data);
} 
 
 
Question 1:

Reference:
http://codercareer.blogspot.in/p/binary-tree-interview-questions.html 
http://cslibrary.stanford.edu/110/BinaryTrees.html

 
http://www.crazyforcode.com/tree/ 

No comments:

Post a Comment