Skip to content

Commit

Permalink
added documentation
Browse files Browse the repository at this point in the history
+ some tree functions and tests
  • Loading branch information
spirosmaggioros committed Jan 22, 2024
1 parent f80f361 commit 0a06c86
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 34 deletions.
18 changes: 18 additions & 0 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ template <typename T> class avl_tree {
*/
void insert(T key) { root = __insert(root, key); }

/*
*search function.
*@param key: key to be searched.
*/
bool search(T key) { return __search(root, key); }

/*
*remove function.
*@param key: key to be removed.
Expand Down Expand Up @@ -180,6 +186,18 @@ template <typename T> class avl_tree {
}
return root;
}
bool __search(node *root, T key) {
while (root) {
if (root->info < key) {
root = root->right;
} else if (root->info > key) {
root = root->left;
} else {
return true;
}
}
return false;
}

void __inorder(std::function<void(node *)> callback, node *root) {
if (root) {
Expand Down
19 changes: 19 additions & 0 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ template <typename T> class bst {
*/
void insert(T key) { root = __insert(root, key); }

/*
*search function.
*@param key: key to be searched.
*/
bool search(T key) { return __search(root, key); }

/*
*remove function.
*@param key: key to be removed.
Expand Down Expand Up @@ -144,6 +150,19 @@ template <typename T> class bst {
return root;
}

bool __search(node *root, T &key) {
while (root) {
if (root->info < key) {
root = root->right;
} else if (root->info > key) {
root = root->left;
} else {
return true;
}
}
return false;
}

node *__remove(node *root, T &key) {
if (!root) {
return root;
Expand Down
58 changes: 45 additions & 13 deletions tests/tree/avl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

TEST_CASE("checking insertions and traversals") {
avl_tree<int> a1;
avl_tree<char> a2;
avl_tree<std::string> a3;

a1.insert(10);
a1.insert(15);
Expand All @@ -15,7 +13,33 @@ TEST_CASE("checking insertions and traversals") {
std::vector<int> v = {-150, 4, 10, 15};
std::vector<int> inorder = a1.inorder();
REQUIRE(v == inorder);
}

TEST_CASE("checking search") {
avl_tree<std::string> a;
a.insert("hello");
a.insert("world");
a.insert("i'm");
a.insert("spirosmag");
REQUIRE(a.search("world") == true);
REQUIRE(a.search("im") == false);
}

TEST_CASE("checking removals") {
avl_tree<int> a1;
a1.insert(10);
a1.insert(2);
a1.insert(190);
a1.insert(-20);
a1.insert(23);
a1.remove(190);
std::vector<int> v = {-20, 2, 10, 23};
std::vector<int> inorder = a1.inorder();
REQUIRE(v == inorder);
}

TEST_CASE("checking inorder") {
avl_tree<char> a2;
a2.insert('g');
a2.insert('d');
a2.insert('w');
Expand All @@ -24,6 +48,7 @@ TEST_CASE("checking insertions and traversals") {
std::vector<char> _inorder = a2.inorder();
REQUIRE(_v == _inorder);

avl_tree<std::string> a3;
a3.insert("hello");
a3.insert("from");
a3.insert("mars");
Expand All @@ -33,15 +58,22 @@ TEST_CASE("checking insertions and traversals") {
REQUIRE(__v == __inorder);
}

TEST_CASE("checking removals") {
avl_tree<int> a1;
a1.insert(10);
a1.insert(2);
a1.insert(190);
a1.insert(-20);
a1.insert(23);
a1.remove(190);
std::vector<int> v = {-20, 2, 10, 23};
std::vector<int> inorder = a1.inorder();
REQUIRE(v == inorder);
TEST_CASE("checking preorder") {
avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(-5);
a.insert(2);
std::vector<int> __preorder = {5, -5, 2, 10};
REQUIRE(a.preorder() == __preorder);
}

TEST_CASE("checking postorder") {
avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(-5);
a.insert(2);
std::vector<int> __postorder = {-5, 2, 10, 5};
REQUIRE(a.postorder() == __postorder);
}
66 changes: 53 additions & 13 deletions tests/tree/bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

TEST_CASE("checking insertions and traversals") {
bst<int> b1;
bst<char> b2;
bst<std::string> b3;

b1.insert(2);
b1.insert(19);
Expand All @@ -15,7 +13,42 @@ TEST_CASE("checking insertions and traversals") {
std::vector<int> v = {2, 5, 19};
std::vector<int> inorder = b1.inorder();
REQUIRE(v == inorder);
}

TEST_CASE("checking removals") {
bst<int> b1;
b1.insert(10);
b1.insert(2);
b1.insert(190);
b1.insert(-20);
b1.insert(23);

b1.remove(190);
std::vector<int> v = {-20, 2, 10, 23};
std::vector<int> inorder = b1.inorder();
REQUIRE(v == inorder);
}

TEST_CASE("checking search") {
bst<int> b;
b.insert(1);
b.insert(10);
b.insert(-5);
b.insert(8);
REQUIRE(b.search(-5) == true);
REQUIRE(b.search(-8) == false);

bst<std::string> b1;
b1.insert("hello");
b1.insert("i'm");
b1.insert("spiros");
b1.insert("maggioros");
REQUIRE(b1.search("spiros") == true);
REQUIRE(b1.search("im") == false);
}

TEST_CASE("checking inorder") {
bst<char> b2;
b2.insert('g');
b2.insert('k');
b2.insert('a');
Expand All @@ -24,6 +57,7 @@ TEST_CASE("checking insertions and traversals") {
std::vector<char> _inorder = b2.inorder();
REQUIRE(_v == _inorder);

bst<std::string> b3;
b3.insert("hello");
b3.insert("abraham");
b3.insert("cheers");
Expand All @@ -32,16 +66,22 @@ TEST_CASE("checking insertions and traversals") {
REQUIRE(__v == __inorder);
}

TEST_CASE("checking removals") {
bst<int> b1;
b1.insert(10);
b1.insert(2);
b1.insert(190);
b1.insert(-20);
b1.insert(23);
TEST_CASE("checking preorder") {
bst<char> b;
b.insert('g');
b.insert('a');
b.insert('b');
b.insert('w');
std::vector<char> pre = {'g', 'a', 'b', 'w'};
REQUIRE(b.preorder() == pre);
}

b1.remove(190);
std::vector<int> v = {-20, 2, 10, 23};
std::vector<int> inorder = b1.inorder();
REQUIRE(v == inorder);
TEST_CASE("checking postorder") {
bst<char> b;
b.insert('g');
b.insert('a');
b.insert('b');
b.insert('w');
std::vector<char> post = {'a', 'b', 'w', 'g'};
REQUIRE(b.postorder() == post);
}
8 changes: 0 additions & 8 deletions tools/cpp_api/linked_list_api.cc

This file was deleted.

128 changes: 128 additions & 0 deletions tutorial/avl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
### Mini Tutorial for the AVL tree class

avl_tree<T> -- creates an avl tree.

avl tree contains:
- insert
- remove
- search
- inorder
- preorder
- postorder
- visualize

### **insert**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
//creates a tree with elements {4,5,10,13};
```

### **remove**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);
//removes the element 4 from the tree.
```

### **remove**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);
//removes the element 4 from the tree.
```

### **search**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);

//returns true if an element is in the tree.
if(a.search(4)){
std::cout<< "element 4 found in the tree" << '\n';
}
```

### **inorder**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);

//returns the elements in inorder fashion.
std::vector<int> in = a.inorder();
```

### **preorder**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);

//returns the elements in preorder fashion.
std::vector<int> pre = a.preorder();
```

### **postorder**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);

//returns the elements in postorder fashion.
std::vector<int> in = a.postorder();
```

### **visualize**:
```cpp
#include <algoplus/avl>

avl_tree<int> a;
a.insert(10);
a.insert(5);
a.insert(4);
a.insert(13);
a.remove(4);

//returns a .dot file that can easily be previewed using
//vscode plugin for graphviz or local command line tools.
a.visualize();
```
Loading

0 comments on commit 0a06c86

Please sign in to comment.