-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (58 loc) · 1.57 KB
/
main.cpp
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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left_link;
Node* right_link;
};
/* method declarations */
Node* get_new_node(int data);
void insert(int data, Node* &root_address);
bool is_present(int data, Node* root_address);
Node* root_address = NULL; //Not best practice but I have made this global for ease of use.
int main() {
insert(15, root_address);
insert(10, root_address);
insert(20, root_address);
insert(18, root_address);
cout<<is_present(10, root_address)<<endl;
cout<<is_present(12, root_address)<<endl;
cout<<is_present(18, root_address)<<endl;
}
void insert(int data, Node* &root_address) {
if (root_address == NULL) {
root_address = get_new_node(data);
}
else if (data <= root_address -> data) {
insert(data, root_address -> left_link);
}
else {
insert(data, root_address -> right_link);
}
}
Node *get_new_node(int data) {
Node* new_node_address = new Node();
new_node_address -> data = data;
new_node_address -> left_link = NULL;
new_node_address -> right_link = NULL;
return new_node_address;
}
bool is_present(int data, Node* root_address) {
bool search_result;
if (root_address == NULL) {
search_result = false;
}
else if (data == root_address -> data) {
search_result = true;
}
else if (data < root_address -> data){
search_result = is_present(data, root_address -> left_link);
}
else if (data > root_address -> data) {
search_result = is_present(data, root_address -> right_link);
}
else {
search_result = false;
}
return search_result;
}