58 lines
1023 B
C++
58 lines
1023 B
C++
#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 << "没有找到" << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "找到了" << 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 << " 的前驱节点是: " << pre->_kv.first << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "前驱节点为空" << std::endl;
|
|
}
|
|
|
|
auto suc = t.FindSuccessor(ret);
|
|
if (suc != nullptr)
|
|
{
|
|
std::cout << ret->_kv.first << " 的后继节点是: " << suc->_kv.first << std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "后继节点为空" << std::endl;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
test_AVLTree();
|
|
|
|
return 0;
|
|
} |