Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. //Program code 7.13
  2. // Function to create Inorder threaded Binary search tree
  3.  
  4. #include<conio.h>
  5. #include<iostream.h>
  6.  
  7. class TBTNode
  8. {
  9. public:
  10. int data,Lbit,Rbit;
  11. TBTNode *Left,*Right;
  12. };
  13. class TBTree
  14. {
  15. public:
  16. TBTNode *curr,*temp,*Head,*Root;
  17. //public:
  18. TBTree()
  19. {
  20. Root=NULL;
  21. }
  22. void create();
  23. void Inorder(TBTNode *);
  24. void Preorder(TBTNode *);
  25. };
  26.  
  27.  
  28. // Traverse a threaded tree in preorder
  29.  
  30. void TBTree :: Preorder (TBTNode *Root)
  31. {
  32. TBTNode *temp ;
  33. int flag =0;
  34. temp =Root;
  35. while (temp !=Head)
  36. {
  37. if (flag == 0)
  38. cout<<temp->data<<" ";
  39. if (temp->Lbit==0 && flag==0) // go Left till Lbit is 1
  40. {
  41. temp=temp->Left;
  42. }
  43. else
  44. if(temp->Rbit==0) // go to Right by child
  45. {
  46. temp=temp->Right;
  47. flag=0;
  48. }
  49. else // go to Right by thread
  50. {
  51. temp=temp->Right;
  52. flag=1;
  53. }
  54. }
  55. }
  56. void TBTree :: create( )
  57. {
  58. char ans;
  59. int flag;
  60. TBTNode *node,*temp;
  61. Head = new TBTNode; // create head
  62. Head->Left=Head;
  63. Head->Right=Head;
  64. Head->Rbit=Head -> Lbit = 1;
  65. // create root for TBST
  66. Root = new TBTNode;
  67. cout<<endl<<"\n Enter data for root = ";
  68. cin >> Root -> data;
  69. Root->Left=Head;
  70. Root->Right=Head;
  71. // attach root to Left of Head
  72. Head->Left=Root;
  73. // make thread bit of root 0
  74. Root->Lbit=Root->Rbit=1;
  75. do
  76. {
  77. // create new node for a tree
  78. node = new TBTNode;
  79. cout <<endl<<"\n Enter next Data = ";
  80. cin >> node -> data;
  81. node->Lbit=node->Rbit = 1;
  82. temp = Root;
  83. while(1)
  84. {
  85. if(node->data<temp->data)
  86. {
  87. if(temp->Lbit==1) // checking leaf node and attach
  88. {
  89. node->Left=temp->Left;
  90. node->Right=temp;
  91. // attach node to Left of temp
  92. temp->Lbit=0;
  93. temp->Left=node;
  94. break;
  95. }
  96. else
  97. temp=temp->Left;
  98. }
  99. else
  100. {
  101. if(temp->Rbit==1) // is thread
  102. {
  103. node->Left=temp;
  104. node->Right=temp->Right;
  105. // attaching node to Right of temp
  106. temp->Right=node;
  107. temp->Rbit=0; break;
  108. }
  109. else
  110. temp=temp->Right;
  111. }
  112. } // end of while
  113. cout<<"\n\n\t Do you want to add more data ?[y/n]";
  114. cin >> ans;
  115. }while(ans=='y'||ans=='Y');
  116. } // end of create
  117.  
  118. void TBTree::Inorder(TBTNode *)
  119. {
  120. TBTNode *temp;
  121. temp=Root;
  122. int flag=0;
  123. if(Root==NULL)
  124. {
  125. cout<<"Tree not present";
  126. }
  127. else
  128. {
  129. while(temp!=Head)
  130. {
  131. if (temp->Lbit==0 && flag==0) // go to left till Lbit is 1(till child)
  132. {
  133. temp=temp->Left;
  134. }
  135. else
  136. {
  137. cout<<temp->data<<" "; // display data
  138. if(temp->Rbit==0) // go to right by child
  139. {
  140. temp=temp->Right;
  141. flag=0;
  142. }
  143. else // go to Right by thread
  144. {
  145. temp=temp->Right;
  146. flag=1;
  147. }
  148. }
  149. }
  150. }
  151. }
  152.  
  153.  
  154. void main()
  155. {
  156. TBTree t;
  157. clrscr();
  158. cout<<"\n\t ********* Threaded Binary Tree **********";
  159. t.create();
  160. cout<<endl<<"\n\n\t Inorder Traversal =";
  161. t.Inorder(t.Root);
  162. cout<<endl<<"\n\n\t Preorder traversal =";
  163. t.Preorder(t.Root);
  164. getch();
  165. }
  166.  
  167. /********************* OUTPUT ***********************
  168.  
  169. ********* Threaded Binary Tree **********
  170.  
  171. Enter data for root = 6
  172.  
  173.  
  174. Enter next Data = 4
  175.  
  176.  
  177. Do you want to add more data ?[y/n]y
  178.  
  179.  
  180. Enter next Data = 2
  181.  
  182.  
  183. Do you want to add more data ?[y/n]y
  184.  
  185.  
  186. Enter next Data = 3
  187.  
  188.  
  189. Do you want to add more data ?[y/n]y
  190.  
  191.  
  192. Enter next Data = 9
  193.  
  194.  
  195. Do you want to add more data ?[y/n]y
  196.  
  197.  
  198. Enter next Data = 8
  199.  
  200.  
  201. Do you want to add more data ?[y/n]n
  202.  
  203.  
  204.  
  205. Inorder Traversal =2 3 4 6 8 9
  206.  
  207.  
  208. Preorder traversal =6 4 2 3 9 8
  209.  
  210. ******************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement