Lab 5 DSA
Lab 5 DSA
2023BCD0014
1) Binary tree to threaded binary tree
Algorithm:
1. Initialize:
• Create a dummy node (def) with data = -1, left pointing to the
new root, and right pointing to itself.
• Create the root of the threaded binary tree (troot) using the
original binary tree’s root data.
2. Convert Using Level Order Traversal:
• Use a queue to process nodes level by level.
• For each node:
o If it has a left child, create a new threaded node, set left
pointers, and mark lflag = true.
o If it has a right child, create a new threaded node, set
right pointers, and mark rflag = true.
3. Return the dummy node (def) as the entry point of the threaded
binary tree.
Code:
#include<bits/stdc++.h>
using namespace std;
struct BTNode{
int data;
BTNode *left = NULL,*right = NULL;
};
struct TBTNode{
int data;
TBTNode *left = NULL, *right = NULL;
bool lflag = false,rflag = false;
};
TBTNode* BTtoTBT(BTNode* root){
TBTNode *def = new TBTNode(),*troot = new TBTNode;
if(root == NULL) return def;
def->data = -1;
def->lflag = true;
def->left = troot;def->right = def;
troot->data = root->data;
troot->left = troot->right = def;
queue<pair<BTNode*,TBTNode*>> q;
q.push({root,troot});
while(!q.empty()){
auto [bnode,tnode] = q.front();q.pop();
if(bnode->left){
TBTNode* newNode = new TBTNode();
newNode->data = bnode->left->data;
newNode->left = tnode->left;
newNode->right = tnode;
tnode->left = newNode;
tnode->lflag = true;
q.push({bnode->left,newNode});
}
if(bnode->right){
TBTNode* newNode = new TBTNode();
newNode->data = bnode->right->data;
newNode->left = tnode;
newNode->right = tnode->right;
tnode->right = newNode;
tnode->rflag = true;
q.push({bnode->right,newNode});
}
}
return def;
}
void print(TBTNode* root){
if(root->left == NULL) return;
queue<TBTNode*> q;
q.push(root->left);
while(!q.empty()){
TBTNode* temp = q.front();q.pop();
cout<<"Node "<<temp->data<<": "<<endl;
cout<<"Left flag = "<<temp->lflag<<", Right flag = "<<temp-
>rflag<<endl;
cout<<"Left is pointing to "<<temp->left->data<<"\nright is
pointing to "<<temp->right->data<<"\n\n";
if(temp->lflag) q.push(temp->left);
if(temp->rflag) q.push(temp->right);
}
return;
}
BTNode* input(){
int val;cin>>val;
if(val == -1) return NULL;
BTNode* root = new BTNode();
root->data = val;
queue<BTNode*> q;
q.push(root);
while(!q.empty()){
auto cur = q.front();q.pop();
int v1,v2;cin>>v1>>v2;
BTNode* temp1 = new BTNode(),* temp2 = new BTNode();
if(v1 != -1){
temp1->data = v1;
cur->left = temp1;
q.push(temp1);
}
if(v2 != -1){
temp2->data = v2;
cur->right = temp2;
q.push(temp2);
}
}
return root;
}
int main(){
BTNode* root = input();
TBTNode* node = BTtoTBT(root);
print(node);
}
Input:
1 2 3 4 5 -1 6 -1 -1 -1 7 -1 -1 -1 -1
Received Output:
Node 1:
Left flag = 1, Right flag = 1 Left is pointing to 2
right is pointing to 3
Node 2:
Left flag = 1, Right flag = 1 Left is pointing to 4
right is pointing to 5
Node 3:
Left flag = 0, Right flag = 1
Left is pointing to 1 right is pointing to 6
Node 4:
Left flag = 0, Right flag = 0
Left is pointing to -1
right is pointing to 2
Node 5:
Left flag = 0, Right flag Left is pointing to 2 right is pointing to 7
Node 6:
Left flag = 0, Right flag Left is pointing to 3 right is pointing to -1
Node 7:
Left flag = 0, Right flag Left is pointing to 5 right is pointing to 1
Ex 2:
Algorithm:
• Build the Threaded Binary Tree
1. Read the root node value. If -1, return dummy.
2. Assign the value to root, set its left and right pointers to
dummy.
3. Use a queue to insert nodes level by level.
4. For each node:
o Read left child value:
▪ If not -1, create a new node, set left pointers, update
lflag, and enqueue it.
o Read right child value:
▪ If not -1, create a new node, set right pointers,
update rflag, and enqueue it.
• Inorder Traversal
1. If lflag is true, move left and recurse.
2. Print the node’s data.
3. If rflag is true, move right and recurse.
Code:
#include<bits/stdc++.h>
using namespace std;
struct TBTNode{
int data;
TBTNode *left = NULL, *right = NULL;
bool lflag = false, rflag = false;
};
void inorder(TBTNode *root){
if(root->lflag) inorder(root->left);
cout<<root->data<<" ";
if(root->rflag) inorder(root->right);
}
TBTNode* input(){
TBTNode *dummy = new TBTNode(),* root = new TBTNode();
dummy->data = -1;
dummy->lflag = true;
dummy->left = root; dummy->right = dummy;
int val;cin>>val;
if(val == -1) return dummy;
root->data = val;
root->left = root->right = dummy;
queue<TBTNode*> q;
q.push(root);
while(!q.empty()){
TBTNode *cur = q.front();q.pop();
cin>>val;
if(val != -1){
cur->lflag = true;
TBTNode *temp = new TBTNode();
temp->data = val;
temp->left = cur->left;
temp->right = cur;
cur->left = temp;
q.push(temp);
}
cin>>val;
if(val != -1){
cur->rflag = true;
TBTNode *temp = new TBTNode();
temp->data = val;
temp->left = cur;
temp->right = cur->right;
cur->right = temp;
q.push(temp);
}
}
return dummy;
}
int main(){
TBTNode *root = input();
cout<<"Inorder:\n";
inorder(root->left);
}
Input:
10 6 13 5 9 -1 20 -1 -1 -1 -1 -1 -1
Received Output: Inorder:
5 6 9 10 13 20
Code:
#include<bits/stdc++.h>
using namespace std;
struct TBTNode{
int data;
TBTNode *left = NULL, *right = NULL;
bool lflag = false, rflag = false;
};
void inorder(TBTNode *root){
if(root->lflag) inorder(root->left);
cout<<root->data<<" ";
if(root->rflag) inorder(root->right);
}
bool search(TBTNode *root,int val){
if(root == NULL) return false;
if(root->data == val) return true;
if(search(root->left,val) || search(root->right,val)) return true;
return false;
}
TBTNode* leftmost(TBTNode* root) {
while(root && root->left) root = root->left;
return root;
}
TBTNode* succ(TBTNode* root){
if(root->right && !root->rflag) return leftmost(root->right);
return root->right;
}
TBTNode* rightmost(TBTNode* root) {
while(root && root->right) root = root->right;
return root;
}
TBTNode* pred(TBTNode* root){
if(root->left && !root->lflag) return rightmost(root->left);
return root->left;
}
TBTNode* deletenode(TBTNode *root, int val){
TBTNode *cur = root->left,*par = root;
while(cur != root && cur->data != val){
par = cur;
if(val < cur->data) cur = cur->lflag?cur->left:root;
else cur = cur->rflag?cur->right:root;
}
if(!cur->lflag && !cur->rflag){
if(par->left == cur){
par->left = cur->left;
par->lflag = false;
}
else{
par->right = cur->right;
par->rflag = false;
}
delete cur;
}
else if(cur->lflag && !cur->rflag){
if(par->left == cur) par->left = cur->left;
else par->right = cur->left;
delete cur;
}
else if(!cur->lflag && cur->rflag){
if(par->left == cur) par->left = cur->right;
else par->right = cur->right;
delete cur;
}
else{
TBTNode *s = succ(cur);
int v = s->data;
if(s->lflag){
TBTNode* p = pred(s);
if(p->left == s) p->left = s->left;
else p->right = s->left;
}
else{
if(par->left == s) par->left = s->right;
else par->right = s->right;
}
cur->data = v;
delete s;
}
return root;
}
TBTNode* insert(TBTNode *root){
cout<<"Enter the value to insert: \n";
int val;cin>>val;
TBTNode *temp = root->left;
while(1){
if(temp->data > val){
if(!temp->lflag){
TBTNode* newNode = new TBTNode();
newNode->data = val;
newNode->left = temp->left;
newNode->right = temp;
temp->lflag = true;
temp->left = newNode;
break;
}
temp = temp->left;
}
else{
if(!temp->rflag){
TBTNode* newNode = new TBTNode();
newNode->data = val;
newNode->left = temp;
newNode->right = temp->right;
temp->rflag = true;
temp->right = newNode;
break;
}
temp = temp->right;
}
}
return root;
}
TBTNode* input(){
TBTNode *dummy = new TBTNode(),* root = new TBTNode();
dummy->data = -1;
dummy->lflag = true;
dummy->left = root;dummy->right = dummy;
root->left = root->right = dummy;
cout<<"Enter the number of nodes followed by the nodes:\n";
int n;cin>>n;n--;
int val;cin>>val;
if(val == -1) return dummy;
root->data = val;
while(n--){
TBTNode* newNode = new TBTNode(),* temp = root;
cin>>newNode->data;
while(1){
if(newNode->data < temp->data){
if(!temp->lflag){
newNode->left = temp->left;
newNode->right = temp;
temp->lflag = true;
temp->left = newNode;
break;
}
temp = temp->left;
}
else{
if(!temp->rflag){
newNode->left = temp;
newNode->right = temp->right;
temp->rflag = true;
temp->right = newNode;
break;
}
temp = temp->right;
}
}
}
cout<<"Enter 1 for inserting:\nEnter 2 for deleting:\nEnter 3 for
searching:\n";
return dummy;
}
int main(){
TBTNode* dummy = input();
cout<<"Initial Inorder:\n";
inorder(dummy->left);
cout<<"\n\n";
while(1){
int a;cin>>a;
switch(a){
case 1:dummy = insert(dummy);
break;
case 2:
cout<<"Enter the value to delete: \n";
int val;cin>>val;
dummy = deletenode(dummy,val);
break;
case 3:
cout<<"Enter the value to search: \n";
cin>>val;
cout<<search(dummy->left,val)?"Found":"Not Found";
break;
default: return 0;
}
cout<<"Inorder:\n";
inorder(dummy->left);
cout<<"\n\n";
}
}
7
20 10 30 5 16 14 17
1 13
1 15
2 17
216
0
Received Output:
Enter the number of nodes followed by the nodes:
Enter 1 for inserting:
Enter 2 for deleting:
Enter 3 for searching:
Initial Inorder:
5 10 14 16 17 20 30
Enter the value to insert: Inorder:
5 10 13 14 16 17 20 30
Enter the value to insert: Inorder:
5 10 13 14 15 16 17 20 30