fork download
  1. #include<iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct tTreeNode{
  8.  
  9. int value;
  10. tTreeNode *left;
  11. tTreeNode *right;
  12. bool leftthread;
  13. bool rightthread;
  14.  
  15. };
  16.  
  17.  
  18. void TBT_insert(tTreeNode *,int);
  19. tTreeNode* insuc(tTreeNode *);
  20. void inorder(tTreeNode *);
  21.  
  22.  
  23. int main(void){
  24. tTreeNode head;
  25. head.leftthread=true;
  26. head.rightthread=false;
  27. head.left=&head;
  28. head.right=&head;
  29.  
  30. TBT_insert(&head,25);
  31. TBT_insert(&head,20);
  32. TBT_insert(&head,40);
  33. TBT_insert(&head,10);
  34. TBT_insert(&head,23);
  35. TBT_insert(&head,30);
  36. TBT_insert(&head,60);
  37. TBT_insert(&head,55);
  38. TBT_insert(&head,70);
  39.  
  40.  
  41. inorder(&head);
  42.  
  43.  
  44. return 0;
  45. }
  46.  
  47.  
  48.  
  49. void TBT_insert(tTreeNode *head,int x){
  50. tTreeNode *ptr;
  51. tTreeNode *p=new tTreeNode;
  52. p->leftthread=true;
  53. p->rightthread=true;
  54. p->value=x;
  55.  
  56.  
  57. if(head->leftthread==true){
  58.  
  59. p->right=head;
  60. p->left=head;
  61. head->left=p;
  62. head->leftthread=false;
  63.  
  64. }
  65. else{
  66. ptr=head->left;
  67.  
  68. while(ptr!=head){
  69.  
  70.  
  71. if((ptr->value)>x){
  72. if(ptr->leftthread==false)
  73. ptr=ptr->left;
  74. else{
  75. p->left=ptr->left;
  76. p->right=ptr;
  77. ptr->leftthread=false;
  78. ptr->left=p;
  79. break;
  80. }
  81. }
  82. else if(ptr->value<x){
  83. if(ptr->rightthread==false)
  84. ptr=ptr->right;
  85. else{
  86. p->right=ptr->right;
  87. p->left=ptr;
  88. ptr->right=false;
  89. ptr->right=p;
  90. break;
  91. }
  92. }
  93.  
  94. }
  95. }
  96.  
  97. }
  98.  
  99.  
  100. void inorder(tTreeNode *head){
  101. tTreeNode *ptr=head;
  102. do{
  103. ptr=insuc(ptr);
  104. if(ptr!=head)
  105. cout<<"Visit"<<ptr->value<<endl;
  106.  
  107. }while(ptr!=head);
  108.  
  109.  
  110. }
  111.  
  112. tTreeNode* insuc(tTreeNode *s){
  113. tTreeNode* t=s->right;
  114. if(!s->rightthread)
  115. while(!t->leftthread)
  116. t=t->left;
  117.  
  118. return t;
  119.  
  120.  
  121. }
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
Visit10
Visit20
Visit23
Visit25
Visit70
Visit55
Visit60
Visit30
Visit40