-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchNode.h
121 lines (94 loc) · 2.66 KB
/
BinarySearchNode.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Created by abasiy on ۱۰/۱۲/۲۰۲۳.
//
#ifndef DATASTRUCTURE_BINARYSEARCHNODE_H
#define DATASTRUCTURE_BINARYSEARCHNODE_H
#include "iostream"
using namespace std;
template <class T>
class BinarySearchNode{
private:
BinarySearchNode<T> *left;
BinarySearchNode<T> *right;
BinarySearchNode<T> *parent;
bool isLeftChild;
bool isRightChild;
public:
T data;
BinarySearchNode(T data, BinarySearchNode<T> *parent = nullptr,
BinarySearchNode<T> *left = nullptr, BinarySearchNode<T> *right = nullptr){
this->data = data;
this->parent = parent;
this->left = left;
this->right = right;
this->isLeftChild = false;
this->isRightChild = false;
}
void setIsLeftChild(bool isLeftChild) {
this->isLeftChild = isLeftChild;
}
void setIsRightChild(bool isRightChild) {
this->isRightChild = isRightChild;
}
~BinarySearchNode(){
this->left = nullptr;
this->right = nullptr;
this->parent = nullptr;
}
bool isLeaf(){
return ( !isLeftChild && !isRightChild);
}
bool hasLeftChild(){
return (isLeftChild);
}
bool hasRightChild(){
return (isRightChild);
}
bool hasSingleChild(){
return (this->hasRightChild() ^ this->hasLeftChild());
}
bool hasChild(){
return (hasLeftChild() || hasRightChild());
}
void setLeftChild(T data){
this->isLeftChild = true;
this->left = new BinarySearchNode<T>(data, this);
}
void setLeftChild(BinarySearchNode<T> *node){
this->left = node;
}
void setRightChild(T data){
this->isRightChild = true;
this->right = new BinarySearchNode<T>(data, this);
}
void setRightChild(BinarySearchNode<T> *node){
this->right = node;
}
BinarySearchNode *getLeftChild() const{
return this->left;
}
BinarySearchNode *getRightChild() const{
return this->right;
}
void setRightThread(BinarySearchNode<T> *thread_ptr){
this->right = thread_ptr;
this->isRightChild = false;
}
void setLeftThread(BinarySearchNode<T> *thread_ptr){
this->left = thread_ptr;
this->isLeftChild = false;
}
BinarySearchNode *getRightThread(){
return (!hasRightChild() ? this->right : nullptr);
}
BinarySearchNode *getLeftThread(){
return (!hasLeftChild() ? this->left : nullptr);
}
void setParent(BinarySearchNode<T> *parent_node){
this->parent = parent_node;
}
BinarySearchNode<T> *getParent(){
return this->parent;
}
};
#endif //DATASTRUCTURE_BINARYSEARCHNODE_H