-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
97 lines (85 loc) · 1.75 KB
/
Copy pathbinary_tree.cpp
File metadata and controls
97 lines (85 loc) · 1.75 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <queue>
#include <string>
using namespace std;
template <class T>
class Binary {
T node;
Binary<T>* leftChild = nullptr;
Binary<T>* rightChild = nullptr;
public:
Binary<T>(T node);
T getNode();
void insert(T el);
void print();
bool contains(T el);
T getMax();
T getMin();
};
template <class T>
T Binary<T>::getNode() {
return node;
}
template <class T>
void Binary<T>::insert(T el){
if(el < node){
if(leftChild != nullptr){
leftChild->insert(el);
} else {
leftChild = new Binary<T>(el);
}
} else {
if(rightChild != nullptr){
rightChild->insert(el);
} else {
this->rightChild = new Binary<T>(el);
}
}
}
template <class T>
void Binary<T>::print(){
cout << node;
if(leftChild != nullptr){
leftChild->print();
} else {
cout << endl;
}
if(rightChild != nullptr){
rightChild->print();
} else {
cout << endl;
}
}
template <class T>
Binary<T>::Binary(T node) : node(node), leftChild(nullptr), rightChild(nullptr){
}
template <class T>
bool Binary<T>::contains(T el) {
if(node == el){
return true;
}
if(el < node){
if(leftChild != nullptr && leftChild->contains(el)){
return true;
}
} else {
if(rightChild != nullptr && rightChild->contains(el)){
return true;
}
}
return false;
}
template <class T>
T Binary<T>::getMax() {
if(rightChild == nullptr){
return node;
}
return rightChild->getMax();
}
template <class T>
T Binary<T>::getMin() {
if(leftChild == nullptr){
return node;
}
return leftChild->getMin();
}