Skip to content

Commit

Permalink
created documentation for BST.
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jan 22, 2024
1 parent 7f6e2f2 commit 2007437
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 33 deletions.
12 changes: 11 additions & 1 deletion examples/tree/avl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {
std::cout << x << " ";
}
std::cout << '\n';

// you can now visualize with Algo+
avl_tree<int> b2;
b2.insert(10);
Expand All @@ -35,4 +35,14 @@ int main() {
b2.insert(-8);

b2.visualize();

avl_tree<int> testing_avl;
testing_avl.insert(10);
testing_avl.visualize();
testing_avl.insert(5);
testing_avl.visualize();
testing_avl.insert(-5);
testing_avl.visualize();
testing_avl.insert(2);
testing_avl.visualize();
}
5 changes: 5 additions & 0 deletions examples/unnamed.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph Tree {
5->-5
-5->2
5->10
}
11 changes: 11 additions & 0 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
#include <queue>
#include <string>
#include <vector>
#endif
Expand Down Expand Up @@ -30,6 +31,15 @@ template <typename T> class avl_tree {
*/
void insert(T key) { root = __insert(root, key); }

/*
*clear function
*Erase all the nodes from the tree.
*/
void clear() {
free(root);
return;
}

/*
*search function.
*@param key: key to be searched.
Expand Down Expand Up @@ -186,6 +196,7 @@ template <typename T> class avl_tree {
}
return root;
}

bool __search(node *root, T key) {
while (root) {
if (root->info < key) {
Expand Down
30 changes: 0 additions & 30 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,6 @@ template <typename T> class bst {
*/
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) {
return levels;
}
std::queue<T> q;
q.push(root);
while (!q.empty()) {
int64_t size = q.size();
std::vector<T> row;
for (int64_t i = 0; i < size; i++) {
T current = q.front();
row.push_back(current);
q.pop();
if (current->right) {
q.push(current->right);
}
if (current->left) {
q.push(current->left);
}
}
levels.push_back(row);
}
return levels;
}

/*
*inorder function.
*Returns vector<T>, the elements inorder.
Expand Down
2 changes: 1 addition & 1 deletion tests/tree/avl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ TEST_CASE("checking postorder") {
a.insert(2);
std::vector<int> __postorder = {-5, 2, 10, 5};
REQUIRE(a.postorder() == __postorder);
}
}
2 changes: 1 addition & 1 deletion tests/tree/bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ TEST_CASE("checking postorder") {
b.insert('w');
std::vector<char> post = {'a', 'b', 'w', 'g'};
REQUIRE(b.postorder() == post);
}
}
128 changes: 128 additions & 0 deletions tutorial/bst.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
### Mini Tutorial for the BST class

bst<T> -- creates an avl tree.

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

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

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

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

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

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

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

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

bst<int> b;
b.insert(10);
b.insert(5);
b.insert(4);
b.insert(13);
b.remove(4);

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

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

bst<int> b;
b.insert(10);
b.insert(5);
b.insert(4);
b.insert(13);
b.remove(4);

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

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

bst<int> b;
b.insert(10);
b.insert(5);
b.insert(4);
b.insert(13);
b.remove(4);

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

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

bst<int> b;
b.insert(10);
b.insert(5);
b.insert(4);
b.insert(13);
b.remove(4);

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

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

bst<int> b;
b.insert(10);
b.insert(5);
b.insert(4);
b.insert(13);
b.remove(4);

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

0 comments on commit 2007437

Please sign in to comment.