123
This commit is contained in:
parent
9fa9b7bdbe
commit
3f261da232
32
a3/binary_search_tree.c
Normal file
32
a3/binary_search_tree.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Node {
|
||||
int data;
|
||||
struct Node* left;
|
||||
struct Node* right;
|
||||
} Node;
|
||||
|
||||
Node* newNode(int data) {
|
||||
Node* node = (Node*)malloc(sizeof(Node));
|
||||
node->data = data;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return(node);
|
||||
}
|
||||
|
||||
Node* insert(Node* node, int data) {
|
||||
if (node == NULL) {
|
||||
return(newNode(data));
|
||||
}
|
||||
else {
|
||||
if (data <= node->data) node->left = insert(node->left, data);
|
||||
else node->right = insert(node->right, data);
|
||||
return(node);
|
||||
}
|
||||
}
|
||||
|
||||
int search(Node* root, int x) {
|
||||
if(root == NULL || root->data == x) return root;
|
||||
else if(root->data < x) return search(root->right, x);
|
||||
else return search(root->left, x);
|
||||
}
|
Loading…
Reference in New Issue
Block a user