-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVarBinNode.cpp
More file actions
51 lines (43 loc) · 1 KB
/
VarBinNode.cpp
File metadata and controls
51 lines (43 loc) · 1 KB
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
//Node implementation with simple inheritance
class VarBinNode
{
public:
virtual ~VarBinNode() {}
virtual bool isLeaf() =0;
};
class LeafNode : public VarBinNode
{
private:
Operand var;
public:
LeafNode(const Operand& val) {var = val;}
bool isLeaf() {return true;}
Operand value() {return var;}
};
class IntlNode : public VarBinNode
{
private:
VarBinNode* left;
VarBinNode* right;
Operator opx;
public:
IntlNode(const Operator& op, VarBinNode* l, VarBinNode* r)
{opx=op;left=l;right=r;}
bool isLeaf() {return false;}
VarBinNode* leftchild() {return left;}
VarBinNode* rightchild() {return right;}
Operator value() {return opx;}
};
void traverse(VarBinNode* root)
{
if (root == NULL) return;
if (root->isLeaf())
cout << "Leaf: " << ((LeafNode *)root)->value() << endl;
else
{
cout << "Internal: "
<< ((IntlNode *)root)->value() << endl;
traverse(((IntlNode *)root)->leftchild());
traverse(((IntlNode *)root)->rightchild());
}
}