Skip to content

Commit

Permalink
added splay tree class(Beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Feb 3, 2024
1 parent ad3d67d commit 317e427
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 8 deletions.
14 changes: 14 additions & 0 deletions examples/tree/splay_tree.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifdef __cplusplus
#include "../../src/classes/tree/splay_tree.h"
#include <iostream>
#endif

int main() {
splay_tree<int> s;
s.insert(10);
s.insert(5);
s.insert(14);
s.insert(3);
s.insert(20);
s.visualize();
}
8 changes: 4 additions & 4 deletions examples/tree/unnamed.dot
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
digraph Tree {
"20,36"->"3,41"
"3,41"->"0,1"
"3,41"->"10,15"
"20,36"->"29,99"
20->14
14->5
5->3
5->10
}
19 changes: 17 additions & 2 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ template <typename T> class avl_tree {
*@brief insert function.
*@param key: key to be inserted.
*/
void insert(T key) { root = __insert(root, key); }
void insert(T key) {
root = __insert(root, key);
__size++;
}

/**
*@brief clear function
*Erase all the nodes from the tree.
*/
void clear() {
root = std::make_shared<node>(nullptr);
__size = 0;
return;
}

Expand Down Expand Up @@ -72,11 +76,20 @@ template <typename T> class avl_tree {
return Iterator(ino.size(), ino);
}

/**
* @brief size function
*
* @return size_t the size of the tree
*/
size_t size() { return __size; }
/**
*@brief remove function.
*@param key: key to be removed.
*/
void remove(T key) { root = __remove(root, key); }
void remove(T key) {
root = __remove(root, key);
__size--;
}

/**
*@brief inorder function.
Expand Down Expand Up @@ -142,7 +155,9 @@ template <typename T> class avl_tree {
std::shared_ptr<node> right;
node(T key) : info(key), left(nullptr), right(nullptr), height(0) {}
} node;

std::shared_ptr<node> root;
size_t __size;

int64_t height(std::shared_ptr<node> root) {
if (root == nullptr)
Expand Down
19 changes: 17 additions & 2 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ template <typename T> class bst {
*@brief insert function.
*@param key: key to be inserted.
*/
void insert(T key) { root = __insert(root, key); }
void insert(T key) {
root = __insert(root, key);
__size++;
}

/**
*@brief search function.
Expand All @@ -46,7 +49,10 @@ template <typename T> class bst {
*@brief remove function.
*@param key: key to be removed.
*/
void remove(T key) { root = __remove(root, key); }
void remove(T key) {
root = __remove(root, key);
__size--;
}

class Iterator;

Expand All @@ -70,6 +76,13 @@ template <typename T> class bst {
return Iterator(ino.size(), ino);
}

/**
* @brief size function
*
* @return size_t the size of the tree
*/
size_t size() { return __size; }

/**
*@brief inorder function.
*@returns vector<T>, the elements inorder.
Expand Down Expand Up @@ -134,7 +147,9 @@ template <typename T> class bst {
std::shared_ptr<node> left;
node(T key) : info(key), right(nullptr), left(nullptr) {}
} node;

std::shared_ptr<node> root;
size_t __size;

std::shared_ptr<node> new_node(T &key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
Expand Down
11 changes: 11 additions & 0 deletions src/classes/tree/interval_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ template <typename T> class interval_tree {
void insert(std::pair<T, T> p) {
interval i = interval(p);
root = __insert(root, i);
__size++;
}

/**
Expand All @@ -56,6 +57,7 @@ template <typename T> class interval_tree {
void remove(std::pair<T, T> p) {
interval i = interval(p);
root = __remove(root, i);
__size--;
}

/**
Expand Down Expand Up @@ -91,6 +93,13 @@ template <typename T> class interval_tree {
return Iterator(ino.size(), ino);
}

/**
* @brief size function
*
* @return size_t the size of the tree
*/
size_t size() { return __size; }

/**
*@brief inorder function.
*@returns vector<pair<T,T>>, the elements inorder.
Expand Down Expand Up @@ -184,7 +193,9 @@ template <typename T> class interval_tree {
node(interval n)
: i(new interval(n)), max(n.high), right(nullptr), left(nullptr) {}
};

std::shared_ptr<node> root;
size_t __size;

std::shared_ptr<node> new_node(interval i) {
std::shared_ptr<node> p = std::make_shared<node>(i);
Expand Down
Loading

0 comments on commit 317e427

Please sign in to comment.