Skip to content

Commit

Permalink
updated some docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jan 21, 2024
1 parent f448231 commit d58aadb
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename T> class doubly_linked_list {

bool empty() { return tail == nullptr; }

size_t size(){return __size;}
size_t size() { return __size; }

doubly_list_iter<T> begin() { return doubly_list_iter<T>(root); }

Expand Down
79 changes: 62 additions & 17 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
#include <vector>
#include <string>
#include "../../visualization/tree_visual/tree_visualization.h"
#include <vector>
#endif

/*
*Class for AVL tree.
*/
template <typename T> class avl_tree {
public:
/*
*Contructor for AVL tree class.
*@param __elements: you can directly pass a vector<T> so you don't have to do
*insert multiple times.
*/
explicit avl_tree(std::vector<T> __elements = {}) noexcept : root(nullptr) {
if (!__elements.empty()) {
for (T &x : __elements) {
Expand All @@ -16,44 +24,74 @@ template <typename T> class avl_tree {
}
~avl_tree() noexcept {}

/*
*insert function.
*@param key: key to be inserted.
*/
void insert(T key) { root = __insert(root, key); }

/*
*remove function.
*@param key: key to be removed.
*/
void remove(T key) { root = __remove(root, key); }

/*
*inorder function.
*Returns vector<T>, the elements inorder.
*/
std::vector<T> inorder() {
std::vector<T> path;
__inorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

/*
*preorder function.
*Returns vector<T>, the elements preorder.
*/
std::vector<T> preorder() {
std::vector<T> path;
__preorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

/*
*postorder function.
*Returns vector<T>, the elements postorder.
*/
std::vector<T> postorder() {
std::vector<T> path;
__postorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

void visualize(){
/*
*visualize function
*Returns .dot file that can be previewed using graphviz in vscode.
*/
void visualize() {
std::string __generated = generate_visualization();
visualization::visualize(__generated);
}

private:
/*
*Struct for the node type pointer.
*@param info: the value of the node.
*@param height: height of each node.
*@param left: pointer to the left.
*@param right: pointer to the right.
*/
typedef struct node {
T info;
int64_t height;
struct node *left;
struct node *right;
} node;
node *root;

int64_t height(node *root) {
if (root == nullptr)
return 0;
Expand Down Expand Up @@ -167,32 +205,39 @@ template <typename T> class avl_tree {
}
}

std::string generate_visualization(){
std::string generate_visualization() {
std::string __generate = __inorder_gen(root);
return __generate;
}

std::string __inorder_gen(node *root){
std::string __inorder_gen(node *root) {
std::string __s;
if(std::is_same_v<T, char> || std::is_same_v<T, std::string>){
if(root -> left){
if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
if (root->left) {
__s += root->info;
__s += "->";
__s += "->";
__s += root->left->info;
__s += "\n";
__s += __inorder_gen(root->left);
__s += __inorder_gen(root->left);
}
if(root -> right){
if (root->right) {
__s += root->info;
__s += "->";
__s += "->";
__s += root->right->info;
__s += "\n";
__s += __inorder_gen(root->right);
}
}
else{
if(root -> left){__s += std::to_string(root->info) + "->" + std::to_string(root->left->info) + "\n" + __inorder_gen(root->left);}
if(root -> right){__s += std::to_string(root->info) + "->" + std::to_string(root->right->info) + "\n" + __inorder_gen(root->right);}
} else {
if (root->left) {
__s += std::to_string(root->info) + "->" +
std::to_string(root->left->info) + "\n" +
__inorder_gen(root->left);
}
if (root->right) {
__s += std::to_string(root->info) + "->" +
std::to_string(root->right->info) + "\n" +
__inorder_gen(root->right);
}
}
return __s;
}
Expand Down
87 changes: 69 additions & 18 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
#include <queue>
#include <vector>
#include <string>
#include <type_traits>
#include "../../visualization/tree_visual/tree_visualization.h"
#include <vector>
#endif

/*
*Class for BST tree.
*/
template <typename T> class bst {
public:
/*
*Contructor for BST tree class.
*@param __elements: you can directly pass a vector<T> so you don't have to do
*insert multiple times.
*/
explicit bst(std::vector<T> __elements = {}) noexcept : root(nullptr) {
if (!__elements.empty()) {
for (T &x : __elements) {
Expand All @@ -18,10 +26,22 @@ template <typename T> class bst {
}
~bst() noexcept {}

/*
*insert function.
*@param key: key to be inserted.
*/
void insert(T key) { root = __insert(root, key); }

/*
*remove function.
*@param key: key to be removed.
*/
void remove(T key) { root = __remove(root, key); }

/*
*level order function.
*Returns vector<vector<T>>, the level order of the Tree.
*/
std::vector<std::vector<T>> level_order() {
std::vector<std::vector<T>> levels;
if (!root) {
Expand All @@ -48,38 +68,62 @@ template <typename T> class bst {
return levels;
}

/*
*inorder function.
*Returns vector<T>, the elements inorder.
*/
std::vector<T> inorder() {
std::vector<T> path;
__inorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

/*
*preorder function.
*Returns vector<T>, the elements preorder.
*/
std::vector<T> preorder() {
std::vector<T> path;
__preorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

/*
*postorder function.
*Returns vector<T>, the elements postorder.
*/
std::vector<T> postorder() {
std::vector<T> path;
__postorder([&](node *callbacked) { path.push_back(callbacked->info); },
root);
return path;
}

void visualize(){
/*
*visualize function
*Returns .dot file that can be previewed using graphviz in vscode.
*/
void visualize() {
std::string __generated = generate_visualization();
visualization::visualize(__generated);
}

private:
struct node {
/*
*Struct for the node type pointer.
*@param info: the value of the node.
*@param left: pointer to the left.
*@param right: pointer to the right.
*/
typedef struct node {
T info;
node *right, *left;
};
node *right;
node *left;
} node;
node *root;

node *new_node(T &key) {
node *p = new node;
p->info = key;
Expand Down Expand Up @@ -157,32 +201,39 @@ template <typename T> class bst {
}
}

std::string generate_visualization(){
std::string generate_visualization() {
std::string __generate = __inorder_gen(root);
return __generate;
}

std::string __inorder_gen(node *root){
std::string __inorder_gen(node *root) {
std::string __s;
if(std::is_same_v<T, char> || std::is_same_v<T, std::string>){
if(root -> left){
if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
if (root->left) {
__s += root->info;
__s += "->";
__s += "->";
__s += root->left->info;
__s += "\n";
__s += __inorder_gen(root->left);
__s += __inorder_gen(root->left);
}
if(root -> right){
if (root->right) {
__s += root->info;
__s += "->";
__s += "->";
__s += root->right->info;
__s += "\n";
__s += __inorder_gen(root->right);
}
}
else{
if(root -> left){__s += std::to_string(root->info) + "->" + std::to_string(root->left->info) + "\n" + __inorder_gen(root->left);}
if(root -> right){__s += std::to_string(root->info) + "->" + std::to_string(root->right->info) + "\n" + __inorder_gen(root->right);}
} else {
if (root->left) {
__s += std::to_string(root->info) + "->" +
std::to_string(root->left->info) + "\n" +
__inorder_gen(root->left);
}
if (root->right) {
__s += std::to_string(root->info) + "->" +
std::to_string(root->right->info) + "\n" +
__inorder_gen(root->right);
}
}
return __s;
}
Expand Down
Loading

0 comments on commit d58aadb

Please sign in to comment.