-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AVL tree needs change to shared_ptr in order to generate the iterator. Every data structure need to be converted to smart pointers.
- Loading branch information
1 parent
0d246bd
commit 9bad71a
Showing
6 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifdef __cplusplus | ||
#include <iostream> | ||
#include <vector> | ||
#endif | ||
|
||
template <typename T> class avl_iter { | ||
public: | ||
avl_iter() noexcept { | ||
__elements = _get_inorder(root); | ||
index = 0; | ||
} | ||
avl_iter(const node *p) noexcept : root(p) {} | ||
~avl_iter(); | ||
|
||
avl_iter &operator++() { | ||
if (index >= 0 && index < __elements.size()) { | ||
index++; | ||
} | ||
return *this; | ||
} | ||
|
||
avl_iter operator++(int) { | ||
avl_iter it = *this; | ||
++*this; | ||
return it; | ||
} | ||
|
||
bool operator!=(const avl_iter &it) { return root != it.root; } | ||
T operator*() { return __elements[index]; } | ||
|
||
private: | ||
typedef struct node { | ||
T info; | ||
int64_t height; | ||
struct node *left; | ||
struct node *right; | ||
} node; | ||
node *root; | ||
std::vector<T> __elements; | ||
int64_t index; | ||
|
||
std::vector<T> __get_inorder(node *root) { | ||
std::vector<T> p; | ||
__inorder(root, p); | ||
return p; | ||
} | ||
|
||
void __inorder(node *root, std::vector<T> &p) { | ||
if (root) { | ||
__inorder(root->left, p); | ||
p.push_back(root->info); | ||
__inorder(root->right, p); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters