-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinarysearchtree.cpp
75 lines (70 loc) · 1.64 KB
/
binarysearchtree.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
#include "binarysearchtree.h"
#include <stack>
Node::Node() {
}
Node::Node(long long k, Node* _l, Node* _r, QColor col) {
key = k;
left = _l;
right = _r;
color = col;
size = 1;
height = 1;
}
Node::~Node() {
}
Node *Node::getL() {
return this->left;
}
Node *Node::getR() {
return this->right;
}
long long Node::getKey() {
return this->key;
}
QColor Node::getColor() {
return this->color;
}
void Node::updateSize() {
if (left != nullptr) left->updateSize();
if (right != nullptr) right->updateSize();
size = (left != nullptr ? left->size : 0) + (right != nullptr ? right->size : 0) + 1;
}
BinarySearchTree::BinarySearchTree() {
root = nullptr;
height = 0;
}
BinarySearchTree::BinarySearchTree(int h, Node* r) {
root = r;
height = h;
}
BinarySearchTree::~BinarySearchTree() {
if (root == nullptr) return;
std::stack<Node*> st;
st.push(root);
while (!st.empty()) {
auto t = st.top();
st.pop();
if (t->getL() != nullptr) st.push(t->getL());
if (t->getR() != nullptr) st.push(t->getR());
delete t;
}
}
Node* BinarySearchTree::getRoot() {
return root;
}
void BinarySearchTree::insert(long long val) {
Q_UNUSED(val);
}
void BinarySearchTree::erase(long long val) {
Q_UNUSED(val);
}
Node* BinarySearchTree::find(long long val) {
Q_UNUSED(val);
return nullptr;
}
void BinarySearchTree::updateHeight(Node *head) {
if (head == nullptr) return;
updateHeight(head->left);
updateHeight(head->right);
head->height = 1 + std::max(head->left == nullptr ? 0 : head->left->height, head->right == nullptr ? 0 : head->right->height);
}