AVLTree/Windows_AVLTree/AVLTree.cpp

58 lines
1023 B
C++
Raw Permalink Normal View History

#include "AVLTree.hpp"
using namespace Lenyiin;
void test_AVLTree()
{
//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
AVLTree<int, int> t;
for (const auto& e : a)
{
t.Insert(std::make_pair(e, e));
}
t.InOrder();
t.PreOrder();
t.PostOrder();
t.LevelOrder();
std::cout << t.IsBalance() << std::endl;
auto ret = t.Find(6);
if (ret == nullptr)
{
std::cout << "û<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>" << std::endl;
}
else
{
std::cout << "<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>" << std::endl;
std::cout << ret->_kv.first << ":" << ret->_kv.second << std::endl;
}
auto pre = t.FindPredecessor(ret);
if (pre != nullptr)
{
std::cout << ret->_kv.first << " <20><>ǰ<EFBFBD><C7B0><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>: " << pre->_kv.first << std::endl;
}
else
{
std::cout << "ǰ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>Ϊ<EFBFBD><EFBFBD>" << std::endl;
}
auto suc = t.FindSuccessor(ret);
if (suc != nullptr)
{
std::cout << ret->_kv.first << " <20>ĺ<EFBFBD><C4BA>̽ڵ<CCBD><DAB5><EFBFBD>: " << suc->_kv.first << std::endl;
}
else
{
std::cout << "<EFBFBD><EFBFBD><EFBFBD>̽ڵ<EFBFBD>Ϊ<EFBFBD><EFBFBD>" << std::endl;
}
}
int main()
{
test_AVLTree();
return 0;
}