-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVarBinNode1.cpp
More file actions
51 lines (42 loc) · 1012 Bytes
/
Copy pathVarBinNode1.cpp
File metadata and controls
51 lines (42 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//node implementation with the composite design pattern
class VarBinNode
{
public:
virtual ~VarBinNode() {}
virtual bool isLeaf() =0;
virtual void traverse() =0;
};
class LeafNode : public VarBinNode
{
private:
Operand var;
public:
LeafNode(const Operand& val) {var=val;}
bool isLeaf() {return true;}
Operand value() {return var;}
void traverse() {cout << "Leaf: " << value() << endl;}
};
class IntlNode : public VarBinNode
{
private:
VarBinNode* lc;
VarBinNode* rc;
Operator opx;
public:
IntlNode(const Operator& op, VarBinNode* l, VarBinNode* r)
{opx=op;lc=l;rc=r;}
bool isLeaf() {return false;}
VarBinNode* left() {return lc;}
VarBinNode* right() {return rc;}
Operator value() {return opx;}
void traverse()
{
cout << "Internal: " << value() << endl;
if (left() != NULL) left()->traverse();
if (right() != NULL) right()->traverse();
}
};
void traverse(VarBinNode* root)
{
if (root != NULL) root->traverse();
}