-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_made_with_class.cpp
More file actions
66 lines (60 loc) · 1.17 KB
/
tree_made_with_class.cpp
File metadata and controls
66 lines (60 loc) · 1.17 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
#include <iostream>
using namespace std;
class BST{
int key;
BST *left, *right;
public:
// Basic Constructor, if not added, program gives errors
BST(){
key = 0;
left = right = NULL;
}
// Constructor which acts to create a new Node
BST(int val){
key = val;
left = right = NULL;
}
BST* insert(BST *parent, int val){
if(parent == NULL){
return new BST(val);
}
if (val <= parent -> key ){
parent -> left = insert(parent -> left, val);
}else{
parent -> right = insert(parent -> right, val);
}
return parent;
}
void inorder(BST *root){
if(root == NULL){
return;
}
inorder(root -> left);
cout << root -> key << endl;
inorder(root -> right);
}
BST* search(BST *root, int val){
if(root == NULL){
cout << "Value not Found" << endl;
return root;
}
if(root -> key == val){
cout << "Value Found" << endl;
return root;
}
if(val < root -> key){
return search(root -> left, val);
}else{
return search(root -> right, val);
}
}
};
int main(){
BST tree, *root = NULL;
root = tree.insert(root, 20);
tree.insert(root, 30);
tree.insert(root, 10);
tree.search(root, 12);
tree.inorder(root);
// tree.search(root, 30);
}