#### Problem Statement:-

Convert given binary tree into inordered and preordered threaded binary tree.Analyze time and space complexity of the algorithm.

Code:-

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202``` ```//============================================================================ // Name : ThreadedBinaryTree.cpp // Author : VNKDJ5 //============================================================================= #include using namespace std; class TBT; class node { node *left,*right; int data; bool rbit,lbit; public: node() { left=NULL; right=NULL; rbit=lbit=0; } node(int d) { left=NULL; right=NULL; rbit=lbit=0; data=d; } friend class TBT; }; class TBT { node *root; //acts as a dummy node public: TBT() //dummy node initialization { root=new node(9999); root->left=root; root->rbit=1; root->lbit=0; root->right=root; } void create(); void insert(int data); node *inorder_suc(node *); void inorder_traversal(); node * preorder_suc(node *c); void preorder_traversal(); }; //-------------------------------------------- void TBT::preorder_traversal() { node *c=root->left; while(c!=root) { cout<<" "<data; c=preorder_suc(c); } } void TBT::inorder_traversal() { node *c=root->left; while(c->lbit==1) c=c->left; while(c!=root) { cout<<" "<data; c=inorder_suc(c); } } node* TBT::inorder_suc(node *c) { if(c->rbit==0) return c->right; else c=c->right; while(c->lbit==1) { c=c->left; } return c; } node *TBT::preorder_suc(node *c) { if(c->lbit==1) { return c->left; } while(c->rbit==0) { c=c->right; } return c->right; } //-------- Create Method void TBT::create() { int n; cout<<"\nEnter number of nodes:"; cin>>n; for(int i=0;i>info; this->insert(info); } } void TBT::insert(int data) { if(root->left==root&&root->right==root) //no node in tree { node *p=new node(data); p->left=root->left; p->lbit=root->lbit; //0 p->rbit=0; p->right=root->right; root->left=p; root->lbit=1; cout<<"\nInserted start"<left; while(1) { if(cur->datarbit==0) { p->right=cur->right; p->rbit=cur->rbit; p->lbit=0; p->left=cur; cur->rbit=1; cur->right=p; cout<<"\nInserted right "<right; } if(cur->data>data) //insert left { node *p=new node(data); if(cur->lbit==0) { p->left=cur->left; p->lbit=cur->lbit; p->rbit=0; p->right=cur; //successor cur->lbit=1; cur->left=p; cout<<"\nInserted left"<left; } } } int main() { TBT t1; int value; int choice; do { cout<<"\n1.Create Tree\n2.Insert into tree\n3.Preorder\n4.Inrder\n0.Exit\nEnter your choice: "; cin>>choice; switch(choice) { case 1: t1.create(); break; case 2: cout<<"\nEnter Number(data): "; cin>>value; t1.insert(value); break; case 3: cout<<"\nPreorder traversal of TBT\n"; t1.preorder_traversal(); break; case 4: cout<<"\nInoder Traversal of TBT\n"; t1.inorder_traversal(); break; default: cout<<"\nWrong choice"; } }while(choice!=0); return 0; } ```

Output:-

 Output TBT Program