Language: C++

#include <algorithm>
#include <iostream>
#include <string>

template<typename K>
class AvlNode
{
public:
	int height;
	int size;
	K key;
	AvlNode<K>* lch;
	AvlNode<K>* rch;
	AvlNode(K key) : key(key), height(1) { }
	~AvlNode<K>() { }
	static AvlNode<K>* update(AvlNode<K>* r)
	{
		if (!r) return nullptr;
		r->height = std::max((r->lch ? r->lch->height : 0), (r->rch ? r->rch->height : 0)) + 1;
		r->size = (r->lch ? r->lch->size : 0) + (r->rch ? r->rch->size : 0) + 1;
		return r;
	}
	static AvlNode<K>* rotateLeft(AvlNode<K>* r)
	{
		AvlNode<K>* p = r->rch;
		r->rch = p->lch;
		p->lch = update(r);
		return update(p);
	}
	static AvlNode<K>* rotateRight(AvlNode<K>* r)
	{
		AvlNode<K>* p = r->lch;
		r->lch = p->rch;
		p->rch = update(r);
		return update(p);
	}
	static int bias(AvlNode<K>* r)
	{
		return r ? ((r->rch ? r->rch->height : 0) - (r->lch ? r->lch->height : 0)) : 0;
	}
	static AvlNode<K>* balance(AvlNode<K>* r)
	{
		int bias = AvlNode<K>::bias(r);
		if (bias > 1) {
			// 右のほうが高い場合
			if (AvlNode<K>::bias(r->rch) < 0) {
				// 子は左の方が高い場合
				r->rch = rotateRight(r->rch);
			}
			r = rotateLeft(r);
		} else if (bias < -1) {
			// 左のほうが高い場合
			if (AvlNode<K>::bias(r->lch) > 0) {
				// 子は右の方が高い場合
				r->lch = rotateLeft(r->lch);
			}
			r = rotateRight(r);
		}
		return r;
	}
	static AvlNode<K>* find(AvlNode<K>* n, K key)
	{
		if (!n) return nullptr;
		if (key < n->key) return find(n->lch, key);
		else if (key > n->key) return find(n->rch, key);
		else return n;
	}
	static AvlNode<K>* minNode(AvlNode<K>* r)
	{
		if (!r) return nullptr;
		while (r->lch) r = r->lch;
		return r;
	}
	static void clearAll(AvlNode<K>* r)
	{
		if (!r) return;
		clearAll(r->lch);
		clearAll(r->rch);
		delete r;
	}
	template<typename K>
	static AvlNode<K>* at(AvlNode<K>* r, int index)
	{
		int i = r->lch ? r->lch->size : 0;
		if (index < i) return at(r->lch, index);
		if (i < index) return at(r->rch, index - i - 1);
		return r;
	}
	template<typename K>
	static AvlNode<K>* insert(AvlNode<K>* n, K key)
	{
		if (!n) return new AvlNode(key);
		if (key < n->key) n->lch = insert(n->lch, key);
		else if (key > n->key) n->rch = insert(n->rch, key);
		else return n;
		return balance(update(n));
	}
	template<typename K>
	static AvlNode<K>* erase(AvlNode<K>* n, K key)
	{
		if (!n) return nullptr;
		if (key < n->key) n = AvlNode<K>::erase(n->lch, key);
		else if (key > n->key) n = AvlNode<K>::erase(n->rch, key);
		else {
			if (n->lch && n->rch) {
				AvlNode<K>* min = AvlNode<K>::minNode(n->rch);
				n->key = min->key;
				n->rch = AvlNode<K>::erase(n->rch, min->key);
			} else if (n->lch) {
				AvlNode<K>* ret = n->lch;
				delete n;
				return balance(update(ret));
			} else if (n->rch) {
				AvlNode<K>* ret = n->rch;
				delete n;
				return balance(update(ret));
			} else {
				delete n;
				return nullptr;
			}
		}
		return balance(update(n));
	}
};

template<typename K>
class AvlTree
{
	AvlNode<K>* root;
public:
	AvlTree<K>() { }
	~AvlTree<K>()
	{
		AvlNode<K>::clearAll(root);
	}
	bool contains(K key)
	{
		return AvlNode<K>::find(this->root, key);
	}
	void erase(K key)
	{
		root = AvlNode<K>::erase(this->root, key);
	}
	void insert(K key)
	{
		root = AvlNode<K>::insert(this->root, key);
	}
	K at(int index)
	{
		AvlNode<K>* node = AvlNode<K>::at(this->root, index);
		return node->key;
	}
};

using namespace std;

int main()
{
	string s("aguihwnvkdnaowhurgjjdjjdjdhhguwknjnaih");
	int k = 5;
//	cin >> s >> k;
	AvlTree<string> tree;

	for (unsigned int i = 0; i < s.length(); ++i) {
		for (unsigned int j = i; j < s.length(); ++j) {
			tree.insert(s.substr(i, j));
		}
	}

	cout << tree.at(k) << endl;

	return 0;
}


Compiler output:
a.cc:81:18: error: declaration of template parameter 'K' shadows template parameter
   81 |         template<typename K>
      |                  ^~~~~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<typename K>
      |          ^~~~~~~~
a.cc:89:18: error: declaration of template parameter 'K' shadows template parameter
   89 |         template<typename K>
      |                  ^~~~~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<typename K>
      |          ^~~~~~~~
a.cc:98:18: error: declaration of template parameter 'K' shadows template parameter
   98 |         template<typename K>
      |                  ^~~~~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<typename K>
      |          ^~~~~~~~