6 #include <rudiments/stdio.h>
7 #include <rudiments/private/nodeinlines.h>
9 #define AVLTREE_TEMPLATE template <class valuetype>
11 #define AVLTREE_CLASS avltree<valuetype>
13 #define AVLTREENODE_TEMPLATE template <class valuetype>
15 #define AVLTREENODE_CLASS avltreenode<valuetype>
19 AVLTREE_CLASS::avltree() {
28 AVLTREE_CLASS::~avltree() {
34 void AVLTREE_CLASS::insert(valuetype value) {
48 stdoutput.
printf(
"----------------------------------------"
49 "---------------------------------------\n");
55 top->insert(node,&top);
59 first->getLeftChild();
60 first=first->getLeftChild()) {}
64 last->getRightChild();
65 last=last->getRightChild()) {}
89 stdoutput.
printf(
"----------------------------------------"
90 "---------------------------------------\n");
114 bool AVLTREE_CLASS::remove(valuetype value) {
116 return (current)?remove(current):false;
121 bool AVLTREE_CLASS::removeAndDelete(valuetype value) {
123 return (current)?removeAndDelete(current):false;
128 bool AVLTREE_CLASS::removeAndArrayDelete(valuetype value) {
130 return (current)?removeAndArrayDelete(current):false;
135 bool AVLTREE_CLASS::removeAll(valuetype value) {
137 while (remove(value)) {
145 bool AVLTREE_CLASS::removeAllAndDelete(valuetype value) {
147 while (removeAndDelete(value)) {
155 bool AVLTREE_CLASS::removeAllAndArrayDelete(valuetype value) {
157 while (removeAndArrayDelete(value)) {
190 uint64_t AVLTREE_CLASS::getLength()
const {
223 return (node)?node->
getNext():NULL;
239 stdoutput.
printf(
"find ");
241 stdoutput.
printf(
" from ");
245 node_print(
"(null)");
254 int32_t result=current->
compare(value);
259 stdoutput.
printf(
" - %d\n",result);
264 }
else if (result==0) {
266 }
else if (result>0) {
273 stdoutput.
printf(
" success!\n");
275 stdoutput.
printf(
" failed\n");
277 stdoutput.
printf(
"} find\n\n");
293 void AVLTREE_CLASS::clear() {
297 stdoutput.
printf(
"clearing %d nodes {\n",length);
301 AVLTREENODE_CLASS *node=top;
305 if (node->getRightChild()) {
306 node=node->getRightChild();
308 while (node->getLeftChild()) {
309 node=node->getLeftChild();
313 AVLTREENODE_CLASS *p=node->getParent();
315 if (p->getLeftChild()==node) {
316 p->setLeftChild(NULL);
318 p->setRightChild(NULL);
324 stdoutput.
printf(
" clearing %lld\n",i);
339 stdoutput.
printf(
"} cleared %d nodes\n\n",i);
351 void AVLTREE_CLASS::clearAndDelete() {
355 stdoutput.
printf(
"clearing %d nodes {\n",length);
359 AVLTREENODE_CLASS *node=top;
363 if (node->getRightChild()) {
364 node=node->getRightChild();
366 while (node->getLeftChild()) {
367 node=node->getLeftChild();
371 AVLTREENODE_CLASS *p=node->getParent();
373 if (p->getLeftChild()==node) {
374 p->setLeftChild(NULL);
376 p->setRightChild(NULL);
382 stdoutput.
printf(
" clearing %lld\n",i);
385 delete node->getValue();
393 stdoutput.
printf(
"} cleared %d nodes\n\n",i);
405 void AVLTREE_CLASS::clearAndArrayDelete() {
409 stdoutput.
printf(
"clearing %d nodes {\n",length);
413 AVLTREENODE_CLASS *node=top;
417 if (node->getRightChild()) {
418 node=node->getRightChild();
420 while (node->getLeftChild()) {
421 node=node->getLeftChild();
425 AVLTREENODE_CLASS *p=node->getParent();
427 if (p->getLeftChild()==node) {
428 p->setLeftChild(NULL);
430 p->setRightChild(NULL);
436 stdoutput.
printf(
" clearing %lld\n",i);
439 delete[] node->getValue();
447 stdoutput.
printf(
"} cleared %d nodes\n\n",i);
459 void AVLTREE_CLASS::print()
const {
467 AVLTREENODE_CLASS::avltreenode(valuetype value) {
478 AVLTREENODE_CLASS::~avltreenode() {
483 void AVLTREENODE_CLASS::setValue(valuetype value) {
489 valuetype AVLTREENODE_CLASS::getValue()
const {
495 AVLTREENODE_CLASS *AVLTREENODE_CLASS::getParent() {
501 AVLTREENODE_CLASS *AVLTREENODE_CLASS::getLeftChild() {
507 AVLTREENODE_CLASS *AVLTREENODE_CLASS::getRightChild() {
513 uint8_t AVLTREENODE_CLASS::getLeftHeight() {
519 uint8_t AVLTREENODE_CLASS::getRightHeight() {
525 AVLTREENODE_CLASS *AVLTREENODE_CLASS::getPrevious() {
535 AVLTREENODE_CLASS *node=left;
538 while (node->right) {
547 if (parent->right==
this) {
554 AVLTREENODE_CLASS *node=parent;
559 if (node->parent->right==node) {
570 AVLTREENODE_CLASS *AVLTREENODE_CLASS::getNext() {
580 AVLTREENODE_CLASS *node=right;
592 if (parent->left==
this) {
599 AVLTREENODE_CLASS *node=parent;
604 if (node->parent->left==node) {
615 int32_t AVLTREENODE_CLASS::compare(valuetype value)
const {
616 return node_compare(this->value,value);
622 return node_compare(this->value,peer->value);
627 void AVLTREENODE_CLASS::print()
const {
628 uint16_t indentlevel=0;
629 print(
"top",&indentlevel);
634 void AVLTREENODE_CLASS::print(
const char *name, uint16_t *indentlevel)
const {
636 for (uint16_t i=0; i<*indentlevel; i++) {
639 stdoutput.
printf(
"<%s value=\"",name);
641 stdoutput.
printf(
"\" lh=\"%d\" rh=\"%d\" bf=\"%d\"",
642 leftheight,rightheight,leftheight-rightheight);
643 if (!left && !right) {
649 left->print(
"left ",indentlevel);
652 right->print(
"right",indentlevel);
655 for (uint16_t i=0; i<*indentlevel; i++) {
658 stdoutput.
printf(
"</%s>\n",name);
664 void AVLTREENODE_CLASS::setParent(AVLTREENODE_CLASS *node) {
670 void AVLTREENODE_CLASS::setLeftChild(AVLTREENODE_CLASS *node) {
676 void AVLTREENODE_CLASS::setRightChild(AVLTREENODE_CLASS *node) {
694 if (node->
compare(location->value)<=0) {
696 if (location->left) {
697 location=location->left;
701 stdoutput.
printf(
"insert ");
702 node_print(node->value);
703 stdoutput.
printf(
" to left of ");
704 node_print(location->value);
705 stdoutput.
printf(
" {\n\n");
708 location->setLeftChild(node);
712 }
else if (node->
compare(location->value)>0) {
714 if (location->right) {
715 location=location->right;
719 stdoutput.
printf(
"insert ");
720 node_print(node->value);
721 stdoutput.
printf(
" to right of ");
722 node_print(location->value);
723 stdoutput.
printf(
" {\n\n");
726 location->setRightChild(node);
732 node->setParent(location);
735 adjustParentHeights(node);
738 node->parent->balance(treetop);
741 stdoutput.
printf(
"} insert\n\n");
750 stdoutput.
printf(
"detach ");
752 stdoutput.
printf(
" {\n\n");
755 while (top->parent) { top=top->parent; }
756 top->print(); stdoutput.
printf(
"\n");
764 stdoutput.
printf(
"less simple case: 2 children\n\n");
776 while (successor->left) {
777 successor=successor->left;
781 stdoutput.
printf(
"swap ");
783 stdoutput.
printf(
" and ");
784 node_print(successor->value);
791 bool successorwasimmediaterightchild=(right==successor);
800 successor->parent=parent;
801 if (successor->parent) {
802 if (successor->parent->left==
this) {
803 successor->parent->left=successor;
805 successor->parent->right=successor;
813 successor->left=left;
814 if (successor->left) {
815 successor->left->parent=successor;
817 if (successorwasimmediaterightchild) {
818 successor->right=
this;
819 successor->right->parent=successor;
821 successor->right=right;
822 if (successor->right) {
823 successor->right->parent=successor;
828 if (parent->left==successor) {
837 successor->leftheight=leftheight;
838 successor->rightheight=rightheight;
854 leftheight=temp.leftheight;
855 rightheight=temp.rightheight;
859 while (top->parent) { top=top->parent; }
860 top->print(); stdoutput.
printf(
"\n");
870 stdoutput.
printf(
"simple case: 1 or 0 children\n\n");
888 if (parent->left==
this) {
890 parent->leftheight--;
893 parent->rightheight--;
898 child->parent=parent;
908 adjustParentHeights(p);
931 while (top->parent) { top=top->parent; }
932 top->print(); stdoutput.
printf(
"\n");
937 stdoutput.
printf(
"} detach\n\n");
946 while (node->parent) {
949 uint8_t height=((node->leftheight>node->rightheight)?
951 node->rightheight)+1;
959 if (node->parent->left==node) {
960 if (node->parent->leftheight==height) {
963 node->parent->leftheight=height;
965 if (node->parent->rightheight==height) {
968 node->parent->rightheight=height;
983 stdoutput.
printf(
"balance at ");
985 stdoutput.
printf(
" {\n\n");
988 while (top->parent) { top=top->parent; }
989 top->print(); stdoutput.
printf(
"\n");
998 if ((node->leftheight>node->rightheight &&
999 node->leftheight-node->rightheight>1) ||
1000 (node->rightheight>node->leftheight &&
1001 node->rightheight-node->leftheight>1)) {
1003 #ifdef DEBUG_AVLTREE
1004 stdoutput.
printf(
"imbalance at ");
1005 node_print(node->value);
1006 stdoutput.
printf(
"\n\n");
1012 if (node->rightheight>node->leftheight) {
1013 if (node->right->rightheight>
1014 node->right->leftheight) {
1015 node=node->leftRotate(treetop);
1017 node=node->rightLeftRotate(treetop);
1019 }
else if (node->leftheight>node->rightheight) {
1020 if (node->left->leftheight>
1021 node->left->rightheight) {
1022 node=node->rightRotate(treetop);
1024 node=node->leftRightRotate(treetop);
1028 #ifdef DEBUG_AVLTREE
1030 while (top->parent) { top=top->parent; }
1031 top->print(); stdoutput.
printf(
"\n");
1036 #ifdef DEBUG_AVLTREE
1037 stdoutput.
printf(
"no imbalance at ");
1038 node_print(node->value);
1039 stdoutput.
printf(
"\n\n");
1047 #ifdef DEBUG_AVLTREE
1049 stdoutput.
printf(
"continuing at ");
1050 node_print(node->value);
1051 stdoutput.
printf(
"\n\n");
1057 #ifdef DEBUG_AVLTREE
1058 stdoutput.
printf(
"} balance\n\n");
1079 #ifdef DEBUG_AVLTREE
1080 stdoutput.
printf(
"left rotation at ");
1082 stdoutput.
printf(
"\n\n");
1089 uint8_t starheight=b->leftheight;
1100 #ifdef DEBUG_AVLTREE
1101 stdoutput.
printf(
"(new tree top)\n\n");
1105 b->parent=a->parent;
1111 a->rightheight=starheight;
1119 adjustParentHeights(a);
1145 #ifdef DEBUG_AVLTREE
1146 stdoutput.
printf(
"right-left rotation at ");
1148 stdoutput.
printf(
" {\n\n");
1149 stdoutput.
printf(
"right part\n\n");
1159 uint8_t starheight=b->rightheight;
1169 c->leftheight=starheight;
1177 adjustParentHeights(c);
1179 #ifdef DEBUG_AVLTREE
1181 while (top->parent) { top=top->parent; }
1182 top->print(); stdoutput.
printf(
"\n");
1186 leftRotate(treetop);
1188 #ifdef DEBUG_AVLTREE
1189 stdoutput.
printf(
"} right-left\n\n");
1215 #ifdef DEBUG_AVLTREE
1216 stdoutput.
printf(
"right rotation at ");
1218 stdoutput.
printf(
"\n\n");
1225 uint8_t starheight=b->rightheight;
1236 #ifdef DEBUG_AVLTREE
1237 stdoutput.
printf(
"(new tree top)\n\n");
1241 b->parent=c->parent;
1247 c->leftheight=starheight;
1255 adjustParentHeights(c);
1281 #ifdef DEBUG_AVLTREE
1282 stdoutput.
printf(
"left-right rotation at ");
1284 stdoutput.
printf(
" {\n\n");
1285 stdoutput.
printf(
"left part\n\n");
1295 uint8_t starheight=b->leftheight;
1305 a->rightheight=starheight;
1313 adjustParentHeights(a);
1315 #ifdef DEBUG_AVLTREE
1317 while (top->parent) { top=top->parent; }
1318 top->print(); stdoutput.
printf(
"\n");
1322 rightRotate(treetop);
1324 #ifdef DEBUG_AVLTREE
1325 stdoutput.
printf(
"} left-right\n\n");
valuetype getValue() const
avltreenode< valuetype > * getPrevious()
avltreenode< valuetype > * getNext()
avltreenode< valuetype > * getRightChild()
avltreenode< valuetype > * getLeftChild()
int32_t compare(valuetype value) const
size_t printf(const char *format,...)