Skip to content

Instantly share code, notes, and snippets.

@tgvdinesh
Created January 19, 2017 03:38
Show Gist options
  • Save tgvdinesh/8e8a88fa697d12d222f21888080e1715 to your computer and use it in GitHub Desktop.
Save tgvdinesh/8e8a88fa697d12d222f21888080e1715 to your computer and use it in GitHub Desktop.
Tree DS implementation in C language
int main(){
node * root = NULL;
int a[100005],K,i = 0,j = 0, _element, present;
scanf("%d",&K);
for( j = 0; j < K;j++ ) {
scanf("%d",&a[i++]);
}
for( i = 0; i < K;i++ ){
root = addElement(root,a[i]);
}
int q;
scanf("%d", &q);
while (q--) {
scanf("%d",&_element);
present = isPresent(root, _element);
printf("%d\n", present);
}
return 0;
}
node * addElement(node * root, int x ){
if( root == NULL ) {
root = (node *) (malloc(sizeof(node)));
root->val = x;root->val1 = -1;
root->left = NULL; root->right = NULL;
return root;
}
if( x < root->val ) {
root->left = addElement(root->left,x);
}
else {
root->right = addElement(root->right,x);
}
return root;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
struct node {
struct node *left,*right;
int val, val1;
};
typedef struct node node;
node * addElement(node * root, int x);
/* Any extra functions you would like to add, code here */
int isPresent(node* root, int val){
/* For your reference
struct node {
struct node *left,*right;
int val;
};
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment