-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.cpp
68 lines (65 loc) · 2.49 KB
/
avl.cpp
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
template <class T1, class T2>
inline void avl<T1, T2>::display(){
/**
* this function displayes the contect of the tree in preoreder
* parameters : none
* returns : none
* excpetions : none
* */
if(root) root->display(); // if there is a root display it
else cout << "[]"; // if not display "[]"
cout << endl; // print a new line any way
}
template <class T1, class T2>
inline T2 avl<T1, T2>::lookUp(T1 k){
/**
* this function is used to get the value associated with the given key
* parameters : k:<T1> "the key to look up"
* returns : T2 "the value associated with the given key"
* exceptions : "key not found"
* */
if(root) return root->lookUp(k); // if the root exists search in it
else throw "key not found"; // if not then there can't be such a key
}
template <class T1, class T2>
inline bool avl<T1, T2>::containsKey(T1 k){
/**
* this function checks if some key exists in the tree or not
* parameters : k<T1> "the key to search for"
* returns : bool "true if found / false if not found"
* exceptions : none
* */
if(root) return root->containsKey(k); // if there is a root, search there, and return the result
else return false; // if not then there can't be such a key
}
template <class T1, class T2>
inline void avl<T1, T2>::modify(T1 k, T2 v){
/**
* this function modifies the value associated with the given key to the given value
* parameters : k<T1> "key to update" v<T2> "new value"
* returns : none
* exceptions : "key not found"
* */
if(root) root->modify(k, v); // if there is a root modify there
else throw "key not found"; // if not then there can't be such a key
}
template <class T1, class T2>
inline void avl<T1, T2>::insert(T1 k, T2 v){
/**
* this function adds the given key-value pair to the tree
* parameters : k<T1> "key to insert" v<T2> "value to associate with it"
* returns : none
* exceptions : "key already exists"
* */
root = bst<T1, T2>::insert(root, k, v); // insert a new node, then reset the root in case of re-balance
}
template <class T1, class T2>
inline void avl<T1, T2>::set(T1 k, T2 v){
/**
* this function mdofies the value associated with given key to the given value if key is found and if not found add the key value pair to the tree
* parameters : k<T1> "key to insert" v<T2> "value to associate with it"
* returns : none
* exceptions : none
* */
root = bst<T1, T2>::set(root, k, v); // set the given node, then reset the root in case of re-balance
}