-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.c
More file actions
80 lines (71 loc) · 1.92 KB
/
binary_search_tree.c
File metadata and controls
80 lines (71 loc) · 1.92 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by Ethan Sawyer on 7/5/20.
//
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *leftChild;
struct Node *rightChild;
};
struct Node *new_Node(int value) {
struct Node *this = (struct Node *) malloc(sizeof(struct Node));
if (this == NULL) {
return NULL; // Out of memory...
}
this->value = value;
this->leftChild = NULL;
this->rightChild = NULL;
return this;
}
void Node_print(struct Node *this) {
printf("Node[%d]", this->value);
}
/*
* The four cases are:
*
* The tree is empty, so the new element will be at the root of the tree.
* The element is already in the tree, so there’s nothing to do.
* The element is not already in the tree, meaning it belongs in either the
* left or the right subtree of the tree, depending on its value.
*/
struct Node *Node_add(struct Node *this, int value) {
if (this == NULL) {
this = new_Node(value);
} else if (value < this->value) {
this->leftChild = Node_add(this->leftChild, value);
} else if (value > this->value) {
this->rightChild = Node_add(this->rightChild, value);
} else {
// Already in tree
}
return this;
}
struct Tree {
struct Node *root;
};
struct Tree *new_Tree() {
struct Tree *this = (struct Tree *) malloc(sizeof(struct Tree));
if (this == NULL) {
return NULL; // Out of memory...
}
this->root = NULL;
return this;
}
void Tree_add(struct Tree *this, int value) {
this->root = Node_add(this->root, value);
}
void Tree_print(struct Tree *this) {
Node_print(this->root);
}
//int main(int argc, char *argv[]) {
// struct Node* root = NULL;
// root = Node_add(root, 20);
// root = Node_add(root, 10);
// root = Node_add(root, 5);
// root = Node_add(root, 15);
// root = Node_add(root, 30);
// root = Node_add(root, 40);
// root = Node_add(root, 20);
// Node_print(root);
//}