You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

101 lines
3.0 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* avlTester.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pohl <pohl@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/17 13:33:39 by pohl #+# #+# */
/* Updated: 2021/11/26 11:23:47 by pohl ### ########.fr */
/* */
/* ************************************************************************** */
#include "AvlTree.hpp"
#include <iostream>
#include <cstdlib>
void display( AvlNode<int, char> *node )
{
if (node)
std::cout << "Node (" << node->getKey() << ") = " << node->getVal() << std::endl;
else
std::cout << "Node is NULL" << std::endl;
}
void action_loop(AvlTree<int, char> &tree)
{
std::string input;
int key;
char value;
while (1)
{
std::cout << "[d]elete, [i]nsert, [r]otate, [q]uit, [mi]multipleinsert: ";
std::cin >> input;
if (input == "d" || input == "delete")
{
std::cout << "Please enter the key of the node you want to delete: ";
std::cin >> key;
if (!tree.deleteNode(key))
std::cout << "Node not found" << std::endl;
}
else if (input == "i" || input == "insert")
{
std::cout << "enter key (0 for random): ";
std::cin >> key;
if (key != 0)
{
std::cout << "And now the value: ";
std::cin >> value;
}
if (key == 0)
{
key = rand() % 94 + 32;
std::cout << "Inserting " << key << " with value " << static_cast<char>(key) << std::endl;
tree.insertNode(AvlNode<int, char>(key, static_cast<char>(key)));
}
else
tree.insertNode(AvlNode<int, char>(key, value));
}
else if (input == "mi")
{
std::cout << "How many? " << std::endl;
std::cin >> key;
for (int i = key; i > 0; i--)
{
key = rand() % 94 + 32;
std::cout << "Inserting " << key << " with value " << static_cast<char>(key) << std::endl;
tree.insertNode(AvlNode<int, char>(key, static_cast<char>(key)));
}
}
else if (input == "q")
exit(0);
else if (input == "r")
{
std::cout << "Please enter the key of the node you want to rotate: ";
std::cin >> key;
std::cout << "Do you want to rotate it [l]eft or [r]ight? ";
std::cin >> value;
AvlNode<int, char> *node = tree.findNode(key);
tree.rotate(node, value == 'l' ? 1 : 0);
}
tree.printTree();
}
}
int avlTester( void )
{
AvlTree<int, char> tree;
AvlNode<int, char> to_insert = AvlNode<int, char>(3, 'c');
int tmp;
tree.insertNode(to_insert);
for (int i = 0; i < 15; i++)
{
tmp = rand() % 94 + 32;
tree.insertNode(AvlNode<int, char>(tmp, static_cast<char>(tmp)));
}
tree.printTree();
action_loop(tree);
return (0);
}