-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMap.h
151 lines (131 loc) · 3.77 KB
/
MyMap.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
149
150
151
//
// MyMap.h
// Proj. 4
//
// Created by Faith Yu on 8/3/2017.
// Copyright © 2017 Faith Yu. All rights reserved.
//
#ifndef MyMap_h
#define MyMap_h
#include<stdlib.h>
// MyMap.h
// Skeleton for the MyMap class template. You must implement the first six
// member functions.
template<typename KeyType, typename ValueType>
class MyMap
{
public:
MyMap() // constructor
: m_size(0), m_root(nullptr)
{}
~MyMap() // destructor; deletes all of the tree's nodes
{
clear();
}
void clear() // deletes all of the trees nodes producing an empty tree
{
deleteTree(m_root);
}
int size() const // return the number of associations in the map
{
if(m_root == nullptr)
return 0;
else
return m_size;
}
void associate(const KeyType& key, const ValueType& value)
{
// if the tree is empty, we create a new Node
if(m_root == nullptr)
{
m_root = new Node(key, value); // size increment is down below
}
else
{
Node* curr = m_root;
for(;;)
{
if(curr->m_key == key)
{
curr->m_value = value;
return; // we do not increment the size
}
else if (curr->m_key < key) // if curr value is smaller than key
{
if(curr->m_right == nullptr)
{
curr->m_right = new Node(key, value);
break;
}
else
curr = curr->m_right;
}
else if (curr->m_key > key) // if curr value is greater than key
{
if(curr->m_left == nullptr)
{
curr->m_left = new Node(key, value);
break;
}
else
curr = curr->m_left;
}
}
}
m_size++;
}
const ValueType* find(const KeyType& key) const
{
Node* curr = m_root;
while(curr != nullptr)
{
if(curr->m_key == key)
return &(curr->m_value);
else if(curr->m_key > key)
curr = curr->m_left;
else if(curr->m_key < key)
curr = curr->m_right;
}
// if there is no matching key value
return nullptr;
}
// for a modifiable map, return a pointer to modifiable ValueType
ValueType* find(const KeyType& key)
{
return const_cast<ValueType*>(const_cast<const MyMap*>(this)->find(key));
}
// C++11 syntax for preventing copying and assignment
MyMap(const MyMap&) = delete;
MyMap& operator=(const MyMap&) = delete;
private:
// private class Node
struct Node
{
Node(KeyType key, ValueType value)
:m_key(key),m_value(value)
{
m_left = m_right = nullptr;
}
~Node()
{
delete m_left;
delete m_right;
}
KeyType m_key;
ValueType m_value;
Node* m_left;
Node* m_right;
};
Node* m_root;
size_t m_size;
void deleteTree(struct Node* node)
{
if (node == NULL) return;
/* first delete both subtrees */
deleteTree(node->m_left);
deleteTree(node->m_right);
/* then delete the node */
free(node);
}
};
#endif /* MyMap_h */