2 Adsl

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name- Soham Kawadkar Div: SY-9 Roll no.

- 22230143 A

ASSIGNMENT 2 ADSL
a) Construct binary tree using prefix expression and perform recursive inorder and
postorder traversal of a tree.
CODE:
#include <iostream>
using namespace std;

struct Node {
char data;
Node* left;
Node* right;
};

Node* createNode(char data) {


Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}

Node* constructTreeFromPrefix(string prefix, int& index) {


if (index >= prefix.size()) {
return nullptr;
}

char symbol = prefix[index];


index++;

Node* newNode = createNode(symbol);

if (isalnum(symbol)) {
return newNode;
}

newNode->left = constructTreeFromPrefix(prefix, index);


newNode->right = constructTreeFromPrefix(prefix, index);

return newNode;
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

void recursiveInorder(Node* root) {


if (root) {
recursiveInorder(root->left);
cout << root->data << " ";
recursiveInorder(root->right);
}
}

void recursivePostorder(Node* root) {


if (root) {
recursivePostorder(root->left);
recursivePostorder(root->right);
cout << root->data << " ";
}
}

int main() {
string prefix = "*+AB-CD";
int index = 0;
Node* root = constructTreeFromPrefix(prefix, index);

cout << "Recursive Inorder Traversal: ";


recursiveInorder(root);
cout << endl;

cout << "Recursive Postorder Traversal: ";


recursivePostorder(root);
cout << endl;

return 0;
}
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

b) Construct binary tree using prefix expression and perform non-recursive inorder and
postorder traversal of a tree.

CODE:
#include <iostream>
#include <stack>
using namespace std;

struct Node {
char data;
Node* left;
Node* right;
};

Node* createNode(char data) {


Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}

Node* constructTreeFromPrefix(string prefix, int& index) {


if (index >= prefix.size()) {
return nullptr;
}

char symbol = prefix[index];


index++;

Node* newNode = createNode(symbol);

if (isalnum(symbol)) {
return newNode;
}

newNode->left = constructTreeFromPrefix(prefix, index);


newNode->right = constructTreeFromPrefix(prefix, index);

return newNode;
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

void nonRecursiveInorder(Node* root) {


stack<Node*> s;
Node* current = root;

while (current != nullptr || !s.empty()) {


while (current != nullptr) {
s.push(current);
current = current->left;
}

current = s.top();
s.pop();

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

current = current->right;
}
}

void nonRecursivePostorder(Node* root) {


stack<Node*> s1, s2;
s1.push(root);
while (!s1.empty()) {
Node* current = s1.top();
s1.pop();
s2.push(current);

if (current->left) {
s1.push(current->left);
}
if (current->right) {
s1.push(current->right);
}
}

while (!s2.empty()) {
cout << s2.top()->data << " ";
s2.pop();
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

int main() {
string prefix = "*+AB-CD";
int index = 0;
Node* root = constructTreeFromPrefix(prefix, index);

cout << "Non-Recursive Inorder Traversal: ";


nonRecursiveInorder(root);
cout << endl;

cout << "Non-Recursive Postorder Traversal: ";


nonRecursivePostorder(root);
cout << endl;

return 0;
}
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

c) Construct binary tree using Postfix expression and perform recursive inorder & preorder
traversal.

CODE:
#include <iostream>
using namespace std;

struct Node {
char data;
Node* left;
Node* right;
};

Node* createNode(char data) {


Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}

Node* constructTreeFromPostfix(string postfix, int& index) {


if (index < 0) {
return nullptr;
}

char symbol = postfix[index];


index--;

Node* newNode = createNode(symbol);

if (isalnum(symbol)) {
return newNode;
}

newNode->right = constructTreeFromPostfix(postfix, index);


newNode->left = constructTreeFromPostfix(postfix, index);

return newNode;
}
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

void recursiveInorder(Node* root) {


if (root) {
recursiveInorder(root->left);
cout << root->data << " ";
recursiveInorder(root->right);
}
}

void recursivePreorder(Node* root) {


if (root) {
cout << root->data << " ";
recursivePreorder(root->left);
recursivePreorder(root->right);
}
}

int main() {
string postfix = "AB+CD-*";
int index = postfix.size() - 1;
Node* root = constructTreeFromPostfix(postfix, index);

cout << "Recursive Inorder Traversal: ";


recursiveInorder(root);
cout << endl;

cout << "Recursive Preorder Traversal: ";


recursivePreorder(root);
cout << endl;

return 0;
}
OUTPUT:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

FAQs:
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A
Name- Soham Kawadkar Div: SY-9 Roll no.- 22230143 A

You might also like