-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst.cpp
94 lines (65 loc) · 1.43 KB
/
tst.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* NAME: NWANKWO CHUKWUEBUKA JUSTIN
DATE: 10/8/2016
*/
#include <iostream>
using namespace std;
struct node{
char ch;
int val;
node * left;
node * mid;
node * right;
node(char alpha){
ch = alpha;
val = -2;
}
};
int get(node * root, string key, int index){
if(root == NULL)
return -1;
if(index == key.size() - 1)
return root->val;
if(key[index] < root->ch)
return get(root->left, key, index);
else if(key[index] > root->ch)
return get(root->right, key, index);
else
return get(root->mid, key, ++index);
}
node * insert(node *& root, string key, int value, int index){
if(root == NULL){
root = new node(key[index]);
root->left = NULL;
root->mid = NULL;
root->right = NULL;
}
if(key[index] < root->ch){
cout<<"in left\n";
root->left = insert(root->left, key, value, index);
}
else if(key[index] > root->ch){
cout<<"in right\n";
root->right = insert(root->right, key, value, index);
}
else{
if(index < key.size() - 1){
cout<<"in mid\n";
root->mid = insert(root->mid, key, value, ++index);
}
else
root->val = value;
}
return root;
}
int main(){
node * root = NULL;
root = insert(root, "shell", 5, 0);
cout<<"\n\n\n";
cout << get(root, "shell", 0)<<endl;
cout << get(root, "love", 0)<<endl;
root = insert(root, "sea", 7, 0);
cout<<endl<<endl;
root = insert(root, "seabird", 21, 0);
cout<< get(root, "sea", 0)<<endl;
cout<< get(root, "seabird", 0)<<endl;
}