Download Beneath the silver glow of a crescent moon, the traveler wandered through the silent fores and more Cheat Sheet Labour Law in PDF only on Docsity!
CS F211: DATA STRUCTURES & ALGORITHMS
ND
SEMESTER 2024-25)
BST, AND AVL TREES
Chittaranjan Hota, PhD Senior Professor, Computer Sc. BITS-Pilani Hyderabad Campus hota[AT]hyderabad.bits-pilani.ac.in
IMPLEMENTATION OF HASH MAP #include #include #include #include using namespace std; class HashMap { //Hash Table Class private: static const int TABLE_SIZE = 10; vector<list<pair<string, int>>> table; int hashFunction(const string &key) { hash hashFunc; return hashFunc(key) % TABLE_SIZE; } public: HashMap() { table.resize(TABLE_SIZE); } void insert (const string &key, int value) { int index = hashFunction(key); // Check if key already exists, update it for (auto &p : table[index]) { if (p.first == key) { p.second = value; return; } } // If key not found, insert new key-value table[index].emplace_back(key, value); } void remove (const string &key) { int index = hashFunction(key); auto &chain = table[index]; for(auto it=chain.begin(); it!=chain.end();++it){ if (it->first == key) { chain.erase(it); return; } } cout << key << " not found."; } int search (const string &key) { int index = hashFunction(key); for (auto &p : table[index]) { if (p.first == key) return p.second; } throw runtime_error("Key not found"); } void display () { for (int i = 0; i < TABLE_SIZE; i++) { cout << "Index " << i << ": "; for (auto &p : table[i]) cout << p.first << p.second; cout << endl; } } }; int main() { HashMap map; map.insert("Riddhi", 25); map.insert("Aryan", 30); map.insert("Mahadevan", 22); map.insert("Saket", 40); map.insert("Purva", 29); map.display(); map.remove("Purva"); map.display(); return 0 ; } Lab11 Next week
DNS LOOKUP USING HASH MAPS
- Each DNS server keeps track of its immediate
children servers using a hashmap.
Lab11 Next week
IMPL. OF LINEAR PROBING & PERFORMANCE
void insert (const string &key, int value) {
int index = hashFunction(key);
int originalIndex = index;
bool found = false;
while (table[index].isOccupied && !table[index].isDeleted) {
if (table[index].key == key){// Key exists, update value
table[index].value = value;
return;
index = (index + 1) % TABLE_SIZE;
if (index == originalIndex) { // Full table check
cout << "Hash table is full! Cannot insert.\n";
return;
// Insert new key-value pair
table[index].key = key;
table[index].value = value;
table[index].isOccupied = true;
table[index].isDeleted = false;
- In the worst case, searches, insertions
and removals on a hash table take?
time.
- The worst case occurs when all the
keys inserted into the map collide.
- The load factor a = n / N affects the
performance of a hash table. Default
load factor is 0. 75.
Rehashing
BINARY SEARCH TREE (BST) & ORDERED MAPS
If node with key 8 has a left child with key 3, what would be the type? To get an ascending order, what is needed?
Keys are: 20, 10, 6, 2, 8, 15, 40, 30 and 25
ORDER OF INSERTION IS IMPORTANT (a) if values are inserted in the order 37 , 24 , 42 , 7 , 2 , 40 , 42 , 32 , 120 , (b) If the order is 120 , 42 , 42 , 7 , 2 , 32 , 37 , 24 , 40 template typename SearchTree::TPos SearchTree::finder(const K& k, const TPos& v) { if (v.isExternal()) return v; // Key not found if (k < (v).key()) return finder(k, v.left()); else if ((v).key() < k) return finder(k, v.right()); else return v; } template typename SearchTree::Iterator SearchTree::find(const K& k) { TPos v = finder(k, root()); if (v.isInternal()) { return Iterator(v); // Found it } else return end(); // Didn't find it } Lab 12 : Next to next week’s lab…
- 1
- What happens to the Balance Factors and how will you handle the imbalance?
AVL TREES
- Adelson-Velsky and Landis (Soviet mathematicians and computer scientists)
- BST with height-balance property.
BALANCING THE TREE THROUGH ROTATIONS
(LL Case)
/ \
\
\
(RR Case)
/ \
(Right rotate at 30)
(Left rotate at 10)
\
(LR Case)
/ \
\
(RL Case)
\
\
/ \
(LR 30) (RR 5 0) (RR 5 0) (LR 30)