-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavlTree_impl_public.h
148 lines (142 loc) · 3.52 KB
/
avlTree_impl_public.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Alexis Francis & Jillian Hanson
//This is avlTree_impl_public.h, where we implement most of the public functions
//from the avlTree.h file
#ifndef AVLTREE_AVLTREE_IMPL_PUBLIC_H
#define AVLTREE_AVLTREE_IMPL_PUBLIC_H
#include <queue>
#include <utility>
#include <iostream>
#include"avlTree.h"
using namespace std;
//Node Constructor
template<class K, class V>
Node<K,V>::Node(K key, V value) {
this->key = key;
this->value = value;
this->left = nullptr;
this->right = nullptr;
this->height = 0;
}
//AvlTree Constructor
template<class K, class V>
AVLTree<K,V>::AVLTree() {
this->size = 0;
this->root = nullptr;
}
//getSize
template<class K, class V>
int AVLTree<K,V>::getSize() {
return this->size;
}
//isEmpty
template<class K, class V>
bool AVLTree<K,V>::isEmpty() {
if(getSize() > 0) {
return false;
}
else {
return true;
}
}
//insert
template<class K, class V>
void AVLTree<K,V>::insert(K key, V value){
root = insert_h(key, value, root);
size++;
}
//update
template<class K, class V>
void AVLTree<K,V>::update(K key, V value){
Node<K,V> *n = getNode(key, root);
if(n != nullptr){
n->value = value;
}
else{
cout << "Throwing tantrum now" << endl;
}
}
//get
template<class K, class V>
V AVLTree<K,V>::get(K key) {
Node<K,V> *n = getNode(key, root);
if(n != nullptr){
return n->value;
}
else{
cout << "Throwing tantrum now" << endl;
}
}
//contains
template<class K, class V>
bool AVLTree<K,V>::contains(K key){
return getNode(key, root) != nullptr;
}
//remove
template<class K, class V>
void AVLTree<K,V>::remove(K key){
root = remove_h(key, root);
}
//getKeys
template<class K, class V>
vector<K> AVLTree<K,V>::getKeys(){
vector<pair<K,V>> get = getItems();
vector<K> toReturn;
for (int i=0; i < get.size(); i++) {
toReturn.push_back(get.at(i).first);
}
return toReturn;
}
//getItems
template<class K, class V>
vector<pair<K,V>> AVLTree<K,V>::getItems(){
return inOrder();
}
//preOrder
template<class K, class V>
vector<pair<K,V>> AVLTree<K,V>::preOrder() {
vector<pair<K,V>> v;
preorder_h(v, root);
return v;
}
//inOrder
template<class K, class V>
vector<pair<K,V>> AVLTree<K,V>::inOrder(){
vector<pair<K,V>> v;
inorder_h(v, root);
return v;
}
//postOrder
template<class K, class V>
vector<pair<K,V>> AVLTree<K,V>::postOrder(){
vector<pair<K,V>> v;
postorder_h(v, root);
return v;
}
//levelOrder
template <class K, class V>
void AVLTree<K,V>::levelOrder() {
queue<pair<Node<K,V>*,int>> q;
q.push(make_pair(root,0));
int currentLevel = -1;
while (!q.empty()) {
// update current pair
pair<Node<K,V>*,int> current = q.front();
q.pop();
// if starting a new level, announce it
if (currentLevel < current.second) {
currentLevel = current.second;
cout << "***** STARTING LEVEL " << currentLevel << " *****" << endl;
}
// print the key of the current Node
Node<K,V>* currentNode = current.first;
cout << currentNode->key << endl;
// enqueue the children
if (currentNode->left != nullptr) {
q.push(make_pair(currentNode->left, currentLevel+1));
}
if (currentNode->right != nullptr) {
q.push(make_pair(currentNode->right, currentLevel+1));
}
}
}
#endif