#include<iostream>
using namespace std;
struct tTreeNode{
int value;
tTreeNode *left;
tTreeNode *right;
bool leftthread;
bool rightthread;
};
void TBT_insert(tTreeNode *,int);
tTreeNode* insuc(tTreeNode *);
void inorder(tTreeNode *);
int main(void){
tTreeNode head;
head.leftthread=true;
head.rightthread=false;
head.left=&head;
head.right=&head;
TBT_insert(&head,25);
TBT_insert(&head,20);
TBT_insert(&head,40);
TBT_insert(&head,10);
TBT_insert(&head,23);
TBT_insert(&head,30);
TBT_insert(&head,60);
TBT_insert(&head,55);
TBT_insert(&head,70);
inorder(&head);
return 0;
}
void TBT_insert(tTreeNode *head,int x){
tTreeNode *ptr;
tTreeNode *p=new tTreeNode;
p->leftthread=true;
p->rightthread=true;
p->value=x;
if(head->leftthread==true){
p->right=head;
p->left=head;
head->left=p;
head->leftthread=false;
}
else{
ptr=head->left;
while(ptr!=head){
if((ptr->value)>x){
if(ptr->leftthread==false)
ptr=ptr->left;
else{
p->left=ptr->left;
p->right=ptr;
ptr->leftthread=false;
ptr->left=p;
break;
}
}
else if(ptr->value<x){
if(ptr->rightthread==false)
ptr=ptr->right;
else{
p->right=ptr->right;
p->left=ptr;
ptr->right=false;
ptr->right=p;
break;
}
}
}
}
}
void inorder(tTreeNode *head){
tTreeNode *ptr=head;
do{
ptr=insuc(ptr);
if(ptr!=head)
cout<<"Visit"<<ptr->value<<endl;
}while(ptr!=head);
}
tTreeNode* insuc(tTreeNode *s){
tTreeNode* t=s->right;
if(!s->rightthread)
while(!t->leftthread)
t=t->left;
return t;
}
I2luY2x1ZGU8aW9zdHJlYW0+CgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgpzdHJ1Y3QgdFRyZWVOb2RlewogICAgICAgIAogICAgIGludCB2YWx1ZTsgICAKICAgICB0VHJlZU5vZGUgKmxlZnQ7CiAgICAgdFRyZWVOb2RlICpyaWdodDsgCiAgICAgYm9vbCBsZWZ0dGhyZWFkOwogICAgIGJvb2wgcmlnaHR0aHJlYWQ7CiAgICAg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AgICAgICAgICAgICAgCiAgICAgICBoZWFkLT5sZWZ0PXA7CiAgICAgICBoZWFkLT5sZWZ0dGhyZWFkPWZhbHNlOwogICAgICAgCiAgICAgICB9CiAgICAgZWxzZXsKICAgICAgICAgIHB0cj1oZWFkLT5sZWZ0OwogICAgICAgICAgCiAgICAgICAgICB3aGlsZShwdHIhPWhlYWQpewogICAgICAgICAgCiAgICAgICAgICAKICAgICAgICAgIGlmKChwdHItPnZhbHVlKT54KXsKICAgICAgICAgICAgICBpZihwdHItPmxlZnR0aHJlYWQ9PWZhbHNlKSAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgIHB0cj1wdHItPmxlZnQ7ICAgICAgICAKICAgICAgICAgICAgICBlbHNleyAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgcC0+bGVmdD1wdHItPmxlZnQ7ICAgICAgICAKICAgICAgICAgICAgICAgICAgcC0+cmlnaHQ9cHRyOyAgICAgICAgIAogICAgICAgICAgICAgICAgICBwdHItPmxlZnR0aHJlYWQ9ZmFsc2U7CiAgICAgICAgICAgICAgICAgIHB0ci0+bGVmdD1wOyAKICAgICAgICAgICAgICAgICAgYnJlYWs7ICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICBlbHNlIGlmKHB0ci0+dmFsdWU8eCl7CiAgICAgICAgICAgICAgIGlmKHB0ci0+cmlnaHR0aHJlYWQ9PWZhbHNlKQogICAgICAgICAgICAgICAgICBwdHI9cHRyLT5yaWdodDsKICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgIHAtPnJpZ2h0PXB0ci0+cmlnaHQ7CiAgICAgICAgICAgICAgICAgICBwLT5sZWZ0PXB0cjsKICAgICAgICAgICAgICAgICAgIHB0ci0+cmlnaHQ9ZmFsc2U7CiAgICAgICAgICAgICAgICAgICBwdHItPnJpZ2h0PXA7CiAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgIAogICAgICAgICAgfQogICAgICAgICAgfSAgICAgCiAgICAgICAgICAKICAgICB9CiAgICAgCiAgICAgCnZvaWQgaW5vcmRlcih0VHJlZU5vZGUgKmhlYWQpewogICAgIHRUcmVlTm9kZSAqcHRyPWhlYWQ7CiAgICAgZG97CiAgICAgICAgICBwdHI9aW5zdWMocHRyKTsgICAgIAogICAgICAgICAgaWYocHRyIT1oZWFkKSAgICAgCiAgICAgICAgICAgIGNvdXQ8PCJWaXNpdCI8PHB0ci0+dmFsdWU8PGVuZGw7ICAgCiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICB9d2hpbGUocHRyIT1oZWFkKTsKICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgIAp9ICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgIAp0VHJlZU5vZGUqIGluc3VjKHRUcmVlTm9kZSAqcyl7CiAgICAgICAgICAgdFRyZWVOb2RlKiB0PXMtPnJpZ2h0OwogICAgICAgICAgIGlmKCFzLT5yaWdodHRocmVhZCkKICAgICAgICAgICB3aGlsZSghdC0+bGVmdHRocmVhZCkKICAgICAgICAgICAgICB0PXQtPmxlZnQ7ICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgcmV0dXJuIHQ7ICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgIAogICAgICAgICAgIAogICAgICAgICAgIH0=