-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryNode.h
69 lines (54 loc) · 1.48 KB
/
BinaryNode.h
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
//
// Created by abasiy on ۲۳/۱۱/۲۰۲۳.
//
#ifndef DATASTRUCTURE_BINARYNODE_H
#define DATASTRUCTURE_BINARYNODE_H
template <class T>
class BinaryNode {
private:
BinaryNode<T> * left_child;
BinaryNode<T> * right_child;
public:
T data;
BinaryNode<T> * parent;
BinaryNode(T data,
BinaryNode<T> *parent = nullptr,
BinaryNode<T> *leftChild = nullptr,
BinaryNode<T> *rightChild = nullptr
) {
this->data = data;
this->parent = parent;
this->left_child = leftChild;
this->right_child = rightChild;
}
~BinaryNode(){
this->right_child = nullptr;
this->left_child = nullptr;
this->parent = nullptr;
};
bool isLeaf(){
return (left_child == nullptr && right_child == nullptr);
}
bool hasChild(){
return (left_child != nullptr || right_child != nullptr);
}
void setRightChild(T value){
if(this->right_child != nullptr) {
delete this->right_child;
}
this->right_child = new BinaryNode<T>(value, this);
}
void setLeftChild(T value){
if(this->left_child != nullptr) {
delete this->left_child;
}
this->left_child = new BinaryNode<T>(value, this);
}
BinaryNode<T> *getLeftChild() const {
return left_child;
}
BinaryNode<T> *getRightChild() const {
return right_child;
}
};
#endif //DATASTRUCTURE_BINARYNODE_H