(*)~reworking this project
This commit is contained in:
14
Makefile
14
Makefile
@@ -1,17 +1,11 @@
|
|||||||
CC=clang++
|
CC=clang++
|
||||||
CFLAGS=-pedantic -Wall -ggdb
|
CFLAGS=-pedantic -Wall -ggdb
|
||||||
VFLAGS=--show-leak-kinds=all --leak-check=full
|
VFLAGS=--show-leak-kinds=all --leak-check=full
|
||||||
|
OUT=
|
||||||
|
|
||||||
list: list.cpp
|
$(OUT): $(OUT).cpp
|
||||||
$(CC) $(CFLAGS) $^ -o $@
|
$(CC) $(CFLAGS) $^ -o $@
|
||||||
|
|
||||||
.PHONY: memcheck_list
|
.PHONY: memcheck
|
||||||
memcheck_list: list
|
memcheck: $(OUT)
|
||||||
valgrind $(VFLAGS) ./$^;
|
|
||||||
|
|
||||||
btree: btree.cpp
|
|
||||||
$(CC) $(CFLAGS) $^ -o $@
|
|
||||||
|
|
||||||
.PHONY: memcheck_btree
|
|
||||||
memcheck_btree: btree
|
|
||||||
valgrind $(VFLAGS) ./$^;
|
valgrind $(VFLAGS) ./$^;
|
||||||
|
|||||||
46
btree.cpp
46
btree.cpp
@@ -47,11 +47,49 @@ BinaryTree<T> *insert(BinaryTree<T> *tree, T value)
|
|||||||
}
|
}
|
||||||
*node = new BinaryTree<T>;
|
*node = new BinaryTree<T>;
|
||||||
(*node)->value = value;
|
(*node)->value = value;
|
||||||
(*node)->left = (*node)->right = nullptr;
|
(*node)->left = nullptr;
|
||||||
|
(*node)->right = nullptr;
|
||||||
(*node)->compare = tree->compare;
|
(*node)->compare = tree->compare;
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void rightRotate(BinaryTree<T> **tree)
|
||||||
|
{
|
||||||
|
auto left = *tree->left;
|
||||||
|
*tree->left = left->right;
|
||||||
|
left->right = *tree;
|
||||||
|
*tree = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void leftRotate(BinaryTree<T> **tree)
|
||||||
|
{
|
||||||
|
auto right = (*tree)->right;
|
||||||
|
(*tree)->right = right->left;
|
||||||
|
right->left = *tree;
|
||||||
|
*tree = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::ostream &operator<<(std::ostream &ostream, const BinaryTree<T> *btree)
|
||||||
|
{
|
||||||
|
if (!btree)
|
||||||
|
return ostream;
|
||||||
|
ostream << btree->value << "(";
|
||||||
|
if (btree->left)
|
||||||
|
ostream << btree->left;
|
||||||
|
if (btree->left && btree->right)
|
||||||
|
ostream << ", ";
|
||||||
|
if (btree->right)
|
||||||
|
ostream << btree->right;
|
||||||
|
ostream << ")";
|
||||||
|
return ostream;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test()
|
||||||
|
{}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
auto tree = new BinaryTree<int>;
|
auto tree = new BinaryTree<int>;
|
||||||
@@ -69,9 +107,9 @@ int main(void)
|
|||||||
for (int i = 0; i <= 5; ++i)
|
for (int i = 0; i <= 5; ++i)
|
||||||
tree = insert(tree, i * 2);
|
tree = insert(tree, i * 2);
|
||||||
|
|
||||||
std::cout << tree->left->value << ", "
|
leftRotate(&tree);
|
||||||
<< tree->value << ", "
|
std::cout << tree << std::endl;
|
||||||
<< tree->right->value << std::endl;
|
|
||||||
delete tree;
|
delete tree;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user