0% found this document useful (0 votes)
6 views4 pages

Amazingness Oct9

The document contains C++ code for implementing an expression tree and set operations. It includes classes for creating and evaluating expression trees, as well as managing sets with operations like insertion, union, intersection, and difference. Additionally, it demonstrates tree traversal methods such as preorder, inorder, and postorder.

Uploaded by

micocoy lakas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Amazingness Oct9

The document contains C++ code for implementing an expression tree and set operations. It includes classes for creating and evaluating expression trees, as well as managing sets with operations like insertion, union, intersection, and difference. Additionally, it demonstrates tree traversal methods such as preorder, inorder, and postorder.

Uploaded by

micocoy lakas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

#include<iostream> //expression tree case '-':


return (evaluate(ptr->left) -
evaluate(ptr->right));
using namespace std;
case '*':
return (evaluate(ptr->left) *
enum OpType {OPERATOR, OPERAND}; evaluate(ptr->right));
case '/':
struct InfoNode{ return (evaluate(ptr->left) /
OpType whichType; evaluate(ptr->right));

union
{ }

char operation; }

int operand; return 0;

}; }

};
int main(){

struct TreeNode{ TreeNode *node1 = new TreeNode();

InfoNode info; node1->info.whichType = OPERAND;

TreeNode *left; node1->info.operand = 9;

TreeNode *right;
}; TreeNode *node2 = new TreeNode();
node2->info.whichType = OPERAND;

class myExpression{ node2->info.operand = 6;

private:
TreeNode *root; TreeNode *node3 = new TreeNode();
node3->info.whichType = OPERAND;

public: node3->info.operand = 3;

int evaluate(TreeNode *ptr);


}; TreeNode *nodeAdd = new TreeNode();
nodeAdd->info.whichType = OPERATOR;

int myExpression::evaluate(TreeNode *ptr){ nodeAdd->info.operation = '+';

switch(ptr->info.whichType){ nodeAdd->left = node2;

case OPERAND: nodeAdd->right = node3;

return ptr->info.operand;
case OPERATOR: TreeNode *nodeDiv = new TreeNode();

switch(ptr->info.operation) nodeDiv->info.whichType = OPERATOR;

{ nodeDiv->info.operation = '/';

case '+': nodeDiv->left = node1;

return (evaluate(ptr->left) + nodeDiv->right = nodeAdd;


evaluate(ptr->right));
2

head = node;
myExpression expr; }
int result = expr.evaluate(nodeDiv);
// Remove a string from the set
cout << "9 / (6 + 3) is = " << result << void remove(string x) {
endl;
Set *p = head, *q = nullptr;
while (p != nullptr && p->data != x) {
delete node1;
q = p;
delete node2;
p = p->next;
delete node3;
}
delete nodeDiv;
if (p == nullptr) return; // Element not
delete nodeAdd; found
return 0;
} if (q == nullptr) {
head = head->next; // Remove the
head
#include <iostream> //sets
} else {
#include <string>
q->next = p->next; // Bypass the
using namespace std; node to remove it
struct Set { }
string data; delete p;
Set *next; }
}; bool ismember(string x) {
Set *p = head;
class setADT { while (p != nullptr) {
private: if (p->data == x) return true;
Set *head; p = p->next;
}
public: return false;
// Initialize an empty set }
setADT() : head(nullptr) {} setADT unionSet(setADT& B) {
setADT result = *this; // Start with
// Insert a string into the set (avoids the current set
duplicates) Set *p = B.head;
void insert(string x) { while (p != nullptr) {
if (ismember(x)) return; // Avoid result.insert(p->data); // Insert
inserting duplicates elements from B
Set* node = new Set; p = p->next;
node->data = x; }
node->next = head; return result;
3

}
int main() {
// Intersection of this set with another setADT set1, set2;
set (Set 1 ∩ Set 2)
set1.insert("apple");
setADT intersection(setADT& B) {
set1.insert("banana");
setADT result;
set1.insert("cherry");
Set *p = head;
while (p != nullptr) {
set2.insert("banana");
if (B.ismember(p->data)) {
set2.insert("cherry");
result.insert(p->data); // Insert
common elements set2.insert("date");

}
p = p->next; cout << "Set 1: ";

} set1.printSet();

return result;
} cout << "Set 2: ";
set2.printSet();

// Difference of this set with another set setADT resultUnion =


(Set 1 - Set 2) set1.unionSet(set2);

setADT difference(setADT& B) { cout << "Union of Set 1 and Set 2: ";

setADT result; resultUnion.printSet();

Set *p = head; setADT resultIntersection =


set1.intersection(set2);
while (p != nullptr) {
cout << "Intersection of Set 1 and Set 2:
if (!B.ismember(p->data)) { ";
result.insert(p->data); // Insert resultIntersection.printSet();
elements only in Set 1
setADT resultDifference =
} set1.difference(set2);
p = p->next; cout << "Difference of Set 1 and Set 2
(Set 1 - Set 2): ";
}
resultDifference.printSet();
return result;
}
return 0;
void printSet() {
}
Set *p = head;
while (p != nullptr) {
#include <iostream> //trees
cout << p->data << " ";
using namespace std;
p = p->next;
}
// Node class represents each node in the
cout << endl; tree
} class Node {
}; public:
4

int item; postOrder(r->rightChild);


Node* leftChild; cout << r->item << " ";
Node* rightChild; }

Node(int x) : item(x), leftChild(nullptr), // Helper function to delete the tree


rightChild(nullptr) {}
void destroyTree(Node* r) {
};
if (r == nullptr)
return;
class Tree {
destroyTree(r->leftChild);
public:
destroyTree(r->rightChild);
Node* root;
delete r;
}
Tree() : root(nullptr) {}
};

// Function to create a new node


int main() {
Node* createNode(int value) {
Tree tree;
return new Node(value);
tree.root = tree.createNode(1);
}
tree.root->leftChild = tree.createNode(2);
tree.root->rightChild =
// Preorder traversal: root -> left -> right tree.createNode(3);
void preOrder(Node* r) { tree.root->leftChild->leftChild =
tree.createNode(4);
if (r == nullptr)
tree.root->leftChild->rightChild =
return; tree.createNode(5);
cout << r->item << " ";
preOrder(r->leftChild); cout << "Preorder traversal: ";
preOrder(r->rightChild); tree.preOrder(tree.root);
} cout << endl;

void inOrder(Node* r) { cout << "Inorder traversal: ";


if (r == nullptr) tree.inOrder(tree.root);
return; cout << endl;
inOrder(r->leftChild);
cout << r->item << " "; cout << "Postorder traversal: ";
inOrder(r->rightChild); tree.postOrder(tree.root);
} cout << endl;
void postOrder(Node* r) { tree.destroyTree(tree.root);
if (r == nullptr) return 0;
return; }
postOrder(r->leftChild);

You might also like