0% found this document useful (0 votes)
23 views10 pages

2 Adsl

The document discusses an assignment on binary trees. It includes code to: 1) Construct a binary tree from a prefix expression and perform recursive and non-recursive inorder and postorder traversals. 2) Construct a binary tree from a postfix expression and perform recursive inorder and preorder traversals. 3) The document contains the student's name, division, roll number and output from running the code on sample expressions.

Uploaded by

sohamkawadkar31
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)
23 views10 pages

2 Adsl

The document discusses an assignment on binary trees. It includes code to: 1) Construct a binary tree from a prefix expression and perform recursive and non-recursive inorder and postorder traversals. 2) Construct a binary tree from a postfix expression and perform recursive inorder and preorder traversals. 3) The document contains the student's name, division, roll number and output from running the code on sample expressions.

Uploaded by

sohamkawadkar31
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/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy