1 Star 0 Fork 0

xiaqiu / TreeVisualizer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Trees.h 14.44 KB
一键复制 编辑 原始数据 按行查看 历史
deevroman 提交于 2019-11-09 09:02 . Add clear tree
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
#include <iostream>
#include <vector>
#include <utility>
#include <QVariant>
#include <QDebug>
#ifndef TREES_H
#define TREES_H
template<typename T>
class AVLTree {
public:
struct node {
T key;
node *left = nullptr;
node *right = nullptr;
int height = 1;
node() {}
node(T key) :
key(key) {}
};
node *root = nullptr;
private:
size_t getHeight(node *p) {
if (p == nullptr) {
return 0;
} else {
return p->height;
}
}
int bfactor(node *p) {
return getHeight(p->right) - getHeight(p->left);
}
void fixHeight(node *p) {
p->height = std::max(getHeight(p->left), getHeight(p->right)) + 1;
}
void rotateright(node *&p) {
node *q = p->left;
p->left = q->right;
q->right = p;
fixHeight(p);
fixHeight(q);
p = q;
}
void rotateleft(node *&q) {
node *p = q->right;
q->right = p->left;
p->left = q;
fixHeight(q);
fixHeight(p);
q = p;
}
void balance(node *&p) {
fixHeight(p);
if (bfactor(p) == 2) {
if (bfactor(p->right) < 0) {
rotateright(p->right);
}
rotateleft(p);
} else if (bfactor(p) == -2) {
if (bfactor(p->left) > 0) {
rotateleft(p->left);
}
rotateright(p);
}
}
void INSERT(node *&cur, T &key) {
if (cur == nullptr) {
cur = new node(key);
return;
}
if (key < cur->key) {
INSERT(cur->left, key);
} else if (cur->key < key) {
INSERT(cur->right, key);
} else if (key == cur->key) {
return;
}
balance(cur);
}
node *findMin(node *p) {
if (p->left != nullptr) {
return findMin(p->left);
} else {
return p;
}
}
void removeMin(node *&p) {
if (p->left == nullptr) {
p = p->right;
return;
}
removeMin(p->left);
balance(p);
}
void REMOVE(node *&cur, T key) {
if (cur == nullptr) {
return;
}
if (key < cur->key) {
REMOVE(cur->left, key);
} else if (key > cur->key) {
REMOVE(cur->right, key);
} else {
node *q = cur->left;
node *r = cur->right;
delete cur;
if (r == nullptr) {
cur = q;
return;
}
node *min = findMin(r);
removeMin(r);
min->right = r;
min->left = q;
balance(min);
cur = min;
return;
}
balance(cur);
}
T GETMAX(node *now) {
if (now == nullptr) {
return 0;
}
if (now->right == nullptr) {
return now->key;
}
return GETMAX(now->right);
}
void CLEAR(node *now) {
if (now == nullptr) {
return;
}
CLEAR(now->left);
CLEAR(now->right);
delete now;
}
public:
void clear() {
CLEAR(root);
root = nullptr;
}
void insert(T key) {
INSERT(root, key);
}
T getMax() {
return GETMAX(root);
}
void remove(T key) {
REMOVE(root, key);
}
};
template<typename T>
class SplayTree {
public:
struct node {
T key;
node *left = nullptr;
node *right = nullptr;
node *parent = nullptr;
node() {}
node(T key, node *left, node *right) :
key(key), left(left), right(right) {}
};
node *root = nullptr;
private:
void setParent(node *child, node *parent) {
if (child != nullptr) {
child->parent = parent;
}
}
void keepParent(node *v) {
setParent(v->left, v);
setParent(v->right, v);
}
void rotate(node *parent, node *child) {
node *gparent = parent->parent;
if (gparent != nullptr) {
if (gparent->left == parent) {
gparent->left = child;
} else {
gparent->right = child;
}
}
if (parent->left == child) {
parent->left = child->right;
child->right = parent;
} else {
parent->right = child->left;
child->left = parent;
}
keepParent(child);
keepParent(parent);
child->parent = gparent;
}
void splay(node *v) {
if (v->parent == nullptr) {
return;
}
node *parent = v->parent;
node *gparent = parent->parent;
if (gparent == nullptr) {
rotate(parent, v);
} else {
bool zigzig = (gparent->left == parent) == (parent->left == v);
if (zigzig) {
rotate(gparent, parent);
rotate(parent, v);
} else {
rotate(parent, v);
rotate(gparent, v);
}
splay(v);
}
}
node *find(node *v, T key) {
if (v == nullptr) {
return nullptr;
}
if (key == v->key) {
splay(v);
return v;
}
if (key < v->key && v->left != nullptr) {
return find(v->left, key);
}
if (key > v->key && v->right != nullptr) {
return find(v->right, key);
}
splay(v);
return v;
}
std::pair<node *, node *> split(T key) {
if (root == nullptr) {
return {nullptr, nullptr};
}
root = find(root, key);
if (root->key == key) {
setParent(root->left, nullptr);
setParent(root->right, nullptr);
return {root->left, root->right};
}
if (root->key < key) {
node *right = root->right;
root->right = nullptr;
setParent(right, nullptr);
return {root, right};
} else {
node *left = root->left;
root->left = nullptr;
setParent(left, nullptr);
return {left, root};
}
}
void INSERT(node *&v, T key) {
auto p = split(key);
v = new node(key, p.first, p.second);
keepParent(v);
root = v;
}
node *merge(node *left, node *right) {
if (right == nullptr) {
return left;
}
if (left == nullptr) {
return right;
}
right = find(right, left->key);
right->left = left;
left->parent = right;
return right;
}
void REMOVE(node *&v, T key) {
v = find(v, key);
if (v == nullptr || v->key != key) {
return;
}
setParent(v->left, nullptr);
setParent(v->right, nullptr);
delete v;
root = v = merge(v->left, v->right);
}
void CLEAR(node *now) {
if (now == nullptr) {
return;
}
CLEAR(now->left);
CLEAR(now->right);
delete now;
}
public:
void clear() {
CLEAR(root);
root = nullptr;
}
void insert(T key) {
INSERT(root, key);
}
void remove(T key) {
REMOVE(root, key);
}
};
#define NIL &sentinel
template<class T>
class RedBlackTree {
enum color {
BLACK, RED
};
public:
struct node {
node *left;
node *right;
node *parent;
color color;
T key;
};
node sentinel = {NIL, NIL, 0, BLACK, 0};
node *root = NIL;
void rotateLeft(node *x) {
node *y = x->right;
x->right = y->left;
if (y->left != NIL) y->left->parent = x;
if (y != NIL) y->parent = x->parent;
if (x->parent) {
if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
} else {
root = y;
}
y->left = x;
if (x != NIL) x->parent = y;
}
void rotateRight(node *x) {
node *y = x->left;
x->left = y->right;
if (y->right != NIL) y->right->parent = x;
if (y != NIL) y->parent = x->parent;
if (x->parent) {
if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
} else {
root = y;
}
y->right = x;
if (x != NIL) x->parent = y;
}
void insertFixup(node *x) {
while (x != root && x->parent->color == RED) {
if (x->parent == x->parent->parent->left) {
node *y = x->parent->parent->right;
if (y->color == RED) {
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
if (x == x->parent->right) {
x = x->parent;
rotateLeft(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateRight(x->parent->parent);
}
} else {
node *y = x->parent->parent->left;
if (y->color == RED) {
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
if (x == x->parent->left) {
x = x->parent;
rotateRight(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateLeft(x->parent->parent);
}
}
}
root->color = BLACK;
}
node *insert(T data) {
node *current, *parent, *x;
current = root;
parent = 0;
while (current != NIL) {
if (data == current->key) return (current);
parent = current;
current = (data < current->key) ?
current->left : current->right;
}
x = new node();
x->key = data;
x->parent = parent;
x->left = NIL;
x->right = NIL;
x->color = RED;
if (parent) {
if (data < parent->key)
parent->left = x;
else
parent->right = x;
} else {
root = x;
}
insertFixup(x);
return (x);
}
void deleteFixup(node *x) {
while (x != root && x->color == BLACK) {
if (x == x->parent->left) {
node *w = x->parent->right;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotateLeft(x->parent);
w = x->parent->right;
}
if (w->left->color == BLACK && w->right->color == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (w->right->color == BLACK) {
w->left->color = BLACK;
w->color = RED;
rotateRight(w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
rotateLeft(x->parent);
x = root;
}
} else {
node *w = x->parent->left;
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rotateRight(x->parent);
w = x->parent->left;
}
if (w->right->color == BLACK && w->left->color == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (w->left->color == BLACK) {
w->right->color = BLACK;
w->color = RED;
rotateLeft(w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
rotateRight(x->parent);
x = root;
}
}
}
x->color = BLACK;
}
void deleteNode(node *z) {
node *x, *y;
if (!z || z == NIL) return;
if (z->left == NIL || z->right == NIL) {
y = z;
} else {
y = z->right;
while (y->left != NIL) y = y->left;
}
if (y->left != NIL)
x = y->left;
else
x = y->right;
x->parent = y->parent;
if (y->parent)
if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
else
root = x;
if (y != z) z->key = y->key;
if (y->color == BLACK)
deleteFixup(x);
free(y);
}
node *findNode(T data) {
node *current = root;
while (current != NIL)
if (data == current->key)
return (current);
else
current = (data < current->key) ?
current->left : current->right;
return nullptr;
}
void remove(T key) {
auto now = findNode(key);
if (now != nullptr) {
deleteNode(now);
}
}
void CLEAR(node *now) {
if (now == nullptr) {
return;
}
if (now->left == now && now->right == now) {
return;
}
CLEAR(now->left);
CLEAR(now->right);
delete now;
}
void clear() {
CLEAR(root);
root = NIL;
}
};
#undef NIL
template<typename T>
int getSize(RedBlackTree<int>::node *now) {
if (now == now->left && now == now->right) {
return 0;
}
return getSize<RedBlackTree<int>::node *>(now->left) + getSize<RedBlackTree<int>::node *>(now->right) + 1;
}
template<typename T>
int getSize(T now) {
if (now == nullptr) {
return 0;
}
return getSize(now->left) + getSize(now->right) + 1;
}
#endif // TREES_H
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mrxiao_com/TreeVisualizer.git
git@gitee.com:mrxiao_com/TreeVisualizer.git
mrxiao_com
TreeVisualizer
TreeVisualizer
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891