DSA_Practical 6
DSA_Practical 6
DSA_Practical 6
#include <iostream>
struct Node {
int data;
Node *left;
Node *right;
bool isThreaded; // true if right pointer is a thread
class InOrderThreadedBinaryTree
{ private: Node *root;
createThreaded(root->left);
if (root->left != nullptr)
{ Node* prev = root->left;
while (prev->right != nullptr) {
prev = prev->right;
}
prev->right = root;
prev->isThreaded = true;
}
createThreaded(root->right);
}
void preOrderTraverse(Node* root) {
if (root == nullptr) return;
if (!root->isThreaded) {
preOrderTraverse(root->left);
}
public:
InOrderThreadedBinaryTree() : root(nullptr) {}
void createThreads() {
createThreaded(root);
}
void preOrder() {
preOrderTraverse(root);
}
};
int main() {
InOrderThreadedBinaryTree tree;
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(3); tree.insert(7);
tree.insert(12);
tree.insert(18);
return 0;
}
Output:-