-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTreeImlement.c
68 lines (59 loc) · 1.6 KB
/
binaryTreeImlement.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
struct binaryTreeStruct {
int key;
struct binaryTreeStruct *left, *right;
};
struct binaryTreeStruct * createNewBinaryTree(int value) {
struct binaryTreeStruct *temp = (struct binaryTreeStruct *)malloc(sizeof(struct binaryTreeStruct *));
temp->key = value;
temp->left = temp->right = NULL;
return temp;
}
struct binaryTreeStruct * searchTree(struct binaryTreeStruct *root, int key) {
if(root == NULL || root->key == key) {
return root;
}
if(key > root->key) {
return searchTree(root->right, key);
}
return searchTree(root->left, key);
}
struct binaryTreeStruct * insertNode(struct binaryTreeStruct *node, int key) {
if(node == NULL) {
return createNewBinaryTree(key);
}
if(key > node->key){
node->right = insertNode(node->right, key);
} else if(key < node->key) {
node->left = insertNode(node->left, key);
}
//
return node;
}
void postOrder(struct binaryTreeStruct* root) {
if(root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%d\n", root->key);
}
}
int main(int argc, char *argv[]){
struct binaryTreeStruct* root = NULL;
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
// Search for a node with key 60
if (searchTree(root, 60) != NULL) {
printf("60 found");
}
else {
printf("60 not found");
}
printf("\n");
postOrder(root);
}