0% found this document useful (0 votes)
2 views

21469 Assignment 5

This document contains C++ code for an Inordered Threaded Binary Search Tree (TBST) implementation. It includes classes for tree nodes and the TBST itself, with methods for creating nodes, performing inorder and preorder traversals, and a main function for user interaction. The program allows users to insert nodes and display the tree in both traversal orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

21469 Assignment 5

This document contains C++ code for an Inordered Threaded Binary Search Tree (TBST) implementation. It includes classes for tree nodes and the TBST itself, with methods for creating nodes, performing inorder and preorder traversals, and a main function for user interaction. The program allows users to insert nodes and display the tree in both traversal orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CODE:

#include <iostream>

#include <climits>

using namespace std;

class TreeNode {

public:

int data;

TreeNode *left, *right;

bool left_thread, right_thread;

TreeNode() {

data = 0;

left = right = nullptr;

left_thread = right_thread = true;

TreeNode(int data) {

this->data = data;

left = right = nullptr;

left_thread = right_thread = true;

};

class InorderedTBST {

private:

TreeNode *root;

TreeNode *dummyNode;

public:

InorderedTBST() {
root = nullptr;

dummyNode = nullptr;

void create() {

int element;

cout << "Enter the data of TreeNode: ";

cin >> element;

TreeNode *newNode = new TreeNode(element);

if (root == nullptr) {

root = newNode;

dummyNode = new TreeNode(INT_MIN);

dummyNode->left = root;

dummyNode->left_thread = false;

dummyNode->right = dummyNode;

dummyNode->right_thread = false;

root->left = dummyNode;

root->right = dummyNode;

} else {

TreeNode *current = root;

while (true) {

if (newNode->data < current->data) {

if (!current->left_thread) {

current = current->left;

} else {

break;

} else {

if (!current->right_thread) {

current = current->right;
} else {

break;

if (newNode->data < current->data) {

newNode->left = current->left;

newNode->right = current;

current->left = newNode;

current->left_thread = false;

} else {

newNode->right = current->right;

newNode->left = current;

current->right = newNode;

current->right_thread = false;

TreeNode* find_successor(TreeNode *current) {

if (current->right_thread)

return current->right;

current = current->right;

while (!current->left_thread)

current = current->left;

return current;

void inorder(TreeNode *current) {

if (current == nullptr) {

cout << "There is NO inorder threaded binary search tree created yet !!" << endl;
return;

while (!current->left_thread)

current = current->left;

while (current != dummyNode) {

cout << current->data << " ";

current = find_successor(current);

cout << endl;

void inorder() {

inorder(root);

void preorder(TreeNode *current) {

if (current == nullptr) {

cout << "There is NO inorder threaded binary search tree created yet !!" << endl;

return;

cout << current->data << " ";

bool flag = false;

while (current != dummyNode) {

while (!current->left_thread && !flag) {

current = current->left;

cout << current->data << " ";

if (!current->right_thread) {

current = current->right;

cout << current->data << " ";

flag = false;
} else {

current = current->right;

flag = true;

cout << endl;

void preorder() {

preorder(root);

};

int main() {

InorderedTBST tree;

int choice;

do {

cout << "\n1. Insert Node";

cout << "\n2. Inorder Traversal";

cout << "\n3. Preorder Traversal";

cout << "\n4. Exit";

cout << "\nEnter your choice: ";

cin >> choice;

switch (choice) {

case 1:

tree.create();

break;

case 2:

cout << "Inorder Traversal: ";

tree.inorder();

break;
case 3:

cout << "Preorder Traversal: ";

tree.preorder();

break;

case 4:

cout << "Exiting..." << endl;

break;

default:

cout << "Invalid choice! Try again." << endl;

} while (choice != 4);

return 0;

OUTPUT:

You might also like