#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;
template <typename KeyT, typename ValueT>
class Node
{
public:
Node(KeyT k, ValueT v)
{
key = k;
value = v;
right = NULL;
left = NULL;
}
Node<KeyT, ValueT> * lowest()
{
Node<KeyT, ValueT> * v = this;
if (right != NULL)
if (v->value > left->value) v = left;
if (left != NULL)
if (v->value > right->value) v = right;
return v;
}
Node<KeyT, ValueT> * searchByKey(KeyT k)
{
if (key == k)
return this;
Node<KeyT, ValueT> * n = NULL;
if (left != NULL)
n = left->searchByKey(k);
if (n != NULL) return n;
if (right!= NULL)
n = right->searchByKey(k);
if (n != NULL) return n;
return NULL;
}
Node<KeyT, ValueT> * getRight()
{
return right;
}
Node<KeyT, ValueT> * getLeft()
{
return left;
}
void setRight(Node<KeyT, ValueT> * nright)
{
right = nright;
}
void setLeft(Node<KeyT, ValueT> * nleft)
{
left = nleft;
}
KeyT getKey()
{
return key;
}
ValueT getValue()
{
return value;
}
private:
KeyT key;
ValueT value;
Node<KeyT, ValueT> * right;
Node<KeyT, ValueT> * left;
};
int main(int c, char * v[])
{
Node<int, int> * tree = new Node<int, int>(4, 6);
tree->setRight(new Node<int, int>(6, 7));
tree->setLeft(new Node<int, int>(2, 8));
Node<int, int> * result = NULL;
result = tree->lowest();
cout << setw(16) << "Lowest: " << "Key:" << result->getKey() << " Value:" << result->getValue() << endl;
result = tree->searchByKey(2);
cout << setw(16) << "By key 2: " << "Key:" << result->getKey() << " Value:" << result->getValue() << endl;
}
ICAgICNpbmNsdWRlIDxhbGdvcml0aG0+CiAgICAjaW5jbHVkZSA8aW9zdHJlYW0+CiAgICAjaW5jbHVkZSA8aW9tYW5pcD4KICAgIHVzaW5nIG5hbWVzcGFjZSBzdGQ7CgogICAgdGVtcGxhdGUgPHR5cGVuYW1lIEtleVQsIHR5cGVuYW1lIFZhbHVlVD4KICAgIGNsYXNzIE5vZGUKICAgIHsKICAgIHB1YmxpYzoKICAgICAgICBOb2RlKEtleVQgaywgVmFsdWVUIHYpCiAgICAgICAgewogICAgICAgICAgICBrZXkgPSBrOwogICAgICAgICAgICB2YWx1ZSA9IHY7CiAgICAgICAgICAgIHJpZ2h0ID0gTlVMTDsKICAgICAgICAgICAgbGVmdCA9IE5VTEw7CiAgICAgICAgfQoKICAgICAgICBOb2RlPEtleVQsIFZhbHVlVD4gKiBsb3dlc3QoKQogICAgICAgIHsKICAgICAgICAgICAgTm9kZTxLZXlULCBWYWx1ZVQ+ICogdiA9IHRoaXM7CgogICAgICAgICAgICBpZiAocmlnaHQgIT0gTlVMTCkKICAgICAgICAgICAgICAgIGlmICh2LT52YWx1ZSA+IGxlZnQtPnZhbHVlKSB2ID0gbGVmdDsKICAgICAgICAgICAgaWYgKGxlZnQgICE9IE5VTEwpCiAgICAgICAgICAgICAgICBpZiAodi0+dmFsdWUgPiByaWdodC0+dmFsdWUpIHYgPSByaWdodDsKCiAgICAgICAgICAgIHJldHVybiB2OwogICAgICAgIH0KCiAgICAgICAgTm9kZTxLZXlULCBWYWx1ZVQ+ICogc2VhcmNoQnlLZXkoS2V5VCBrKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGtleSA9PSBrKQogICAgICAgICAgICAgICAgcmV0dXJuIHRoaXM7CgogICAgICAgICAgICBOb2RlPEtleVQsIFZhbHVlVD4gKiBuID0gTlVMTDsKCiAgICAgICAgICAgIGlmIChsZWZ0ICE9IE5VTEwpCiAgICAgICAgICAgICAgICBuID0gbGVmdC0+c2VhcmNoQnlLZXkoayk7CiAgICAgICAgICAgIGlmIChuICE9IE5VTEwpIHJldHVybiBuOwogICAgICAgICAgICBpZiAocmlnaHQhPSBOVUxMKQogICAgICAgICAgICAgICAgbiA9IHJpZ2h0LT5zZWFyY2hCeUtleShrKTsKICAgICAgICAgICAgaWYgKG4gIT0gTlVMTCkgcmV0dXJuIG47CgogICAgICAgICAgICByZXR1cm4gTlVMTDsKICAgICAgICB9CgogICAgICAgIE5vZGU8S2V5VCwgVmFsdWVUPiAqIGdldFJpZ2h0KCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiByaWdodDsKICAgICAgICB9CgogICAgICAgIE5vZGU8S2V5VCwgVmFsdWVUPiAqIGdldExlZnQoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGxlZnQ7CiAgICAgICAgfQoKICAgICAgICB2b2lkIHNldFJpZ2h0KE5vZGU8S2V5VCwgVmFsdWVUPiAqIG5yaWdodCkKICAgICAgICB7CiAgICAgICAgICAgIHJpZ2h0ID0gbnJpZ2h0OwogICAgICAgIH0KCiAgICAgICAgdm9pZCBzZXRMZWZ0KE5vZGU8S2V5VCwgVmFsdWVUPiAqIG5sZWZ0KQogICAgICAgIHsKICAgICAgICAgICAgbGVmdCA9IG5sZWZ0OwogICAgICAgIH0KCiAgICAgICAgS2V5VCBnZXRLZXkoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGtleTsKICAgICAgICB9CgogICAgICAgIFZhbHVlVCBnZXRWYWx1ZSgpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgICAgfQoKICAgIHByaXZhdGU6CiAgICAgICAgS2V5VCAgIGtleTsKICAgICAgICBWYWx1ZVQgdmFsdWU7CgogICAgICAgIE5vZGU8S2V5VCwgVmFsdWVUPiAqIHJpZ2h0OwogICAgICAgIE5vZGU8S2V5VCwgVmFsdWVUPiAqIGxlZnQ7CiAgICB9OwoKICAgIGludCBtYWluKGludCBjLCBjaGFyICogdltdKQogICAgewogICAgICAgIE5vZGU8aW50LCBpbnQ+ICogdHJlZSA9IG5ldyBOb2RlPGludCwgaW50Pig0LCA2KTsKICAgICAgICB0cmVlLT5zZXRSaWdodChuZXcgTm9kZTxpbnQsIGludD4oNiwgNykpOwogICAgICAgIHRyZWUtPnNldExlZnQobmV3IE5vZGU8aW50LCBpbnQ+KDIsIDgpKTsKCiAgICAgICAgTm9kZTxpbnQsIGludD4gKiByZXN1bHQgPSBOVUxMOwoKICAgICAgICByZXN1bHQgPSB0cmV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