Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tree and binarySearch
//clang 3.8.0 #include <stdlib.h> #include <stdio.h> struct node{ int data; struct node *left; struct node *right; }; typedef struct node Node; int BinarySearch(Node *n,int value){ if(n==NULL)return NULL; if(value==n->data){ return n->data; } else if(value<n->data) return BinarySearch(n->left,value); else if(value>n->data) return BinarySearch(n->right,value); } Node* createNode(int value){ Node *newNode; newNode=malloc(sizeof(Node)); newNode->data=value; newNode->left=NULL; newNode->right=NULL; return newNode; } Node* insert(Node *n,int value){ if(n==NULL)return createNode(value); if(value<n->data) n->left=insert(n->left,value); if(value>n->data) n->right=insert(n->right,value); return n; } void inorder(Node *n){ if(n==NULL) return; inorder(n->left); printf("%d->",n->data); inorder(n->right); } int main(void) { Node *root=createNode(5); insert(root,7); insert(root,6); insert(root,3); insert(root,2); insert(root,1); inorder(root); printf("\n%d",BinarySearch(root,2)); return 0; }
run
|
edit
|
history
|
help
1
Does fgets() read past a leading newline?
Ashar
Bucles: Múltiplos de 4 hasta N tecleado por el usuario
Run code before/after main in C (Clang)
Vectores: Dos vectores rellenos al azar
Lee hora y suma un segundo
Bit string printer
Herout (67) - 4
Vectores: Llena vector con números al azar
Cuadrado asteriscos solido