-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctiontree.h
More file actions
168 lines (161 loc) · 3.36 KB
/
functiontree.h
File metadata and controls
168 lines (161 loc) · 3.36 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#ifndef FUNCTIONTREE_H_
#define FUNCTIONTREE_H_
#include <stdexcept>
#include "Symbol.h"
/**
* FunctionTree builds a tree from a sequence of symbols.
* The location where the symbol is added is dependent upon
* the rank of the symbol and THE STATE OF THE CLIENT PROGRAM.
* The rank indicates the order of the operation in a defined
* order of operations (PEMDAS). The state of the client program
* dictates how a leaf is added to another leaf (left or right)
**/
template<class T>
class FunctionTree
{
public:
typedef Symbol<T> Symbol;
class Node
{
friend class FunctionTree<T>;
public:
Node(){_left=NULL;_right=NULL;_data=0;}
Node(Symbol* sym):_symbol(sym){_left=NULL;_right=NULL;_data=0;}
virtual ~Node(){}
T &GetNodeContents(){return _data;}
void SetNodeContents(T const& data){_data = data;}
void Fire()
{
T arg1 = (_left!=NULL? _left->GetNodeContents():0);
T arg2 = (_right!=NULL? _right->GetNodeContents():0);
_data = _symbol->Fire(arg1, arg2);
}
Symbol* GetSymbol(){return _symbol;}
void SetSymbol(Symbol *symbol){_symbol = symbol;}
bool IsLeaf()
{
if(_symbol->GetRank()<0)
return true;
else
return false;
}
private:
Symbol *_symbol;
T _data;
Node *_right;
Node *_left;
};
FunctionTree(){_root = NULL;}
struct ParentRightChildPair
{
ParentRightChildPair(Node *p,Node *r):_parent(p),_rightChild(r){}
Node *_parent;
Node *_rightChild;
};
virtual ~FunctionTree()
{
if(_root != NULL)
{
Clear(&_root);
}
}
void Clear(){Clear(&_root);}
void Insert(Symbol *symbol)
{
if(_root==NULL)
{
_root = new Node(symbol);
}
else
{
if(symbol->GetRank()<0)
AddLeaf(symbol);
else
AddOperator(symbol);
}
}
T Evaluate()
{
Evaluate(_root);
return _root->GetNodeContents();
}
private:
void Evaluate(Node *current)
{
if(current->_left!=NULL)
{
Evaluate(current->_left);
}
if(current->_right!=NULL)
{
Evaluate(current->_right);
}
current->Fire();
}
void AddLeaf(Symbol *symbol)
{
Node *right_most = GetRightMost(_root);
right_most->_right = new Node(symbol);
}
Node *GetRightMost(Node *parent)
{
if(parent->_right != NULL)
return GetRightMost(parent->_right);
else
return parent;
}
void AddOperator(Symbol *symbol)
{
ParentRightChildPair nodesToRotate(NULL,_root);
FindFirstLesserRightChild(nodesToRotate,symbol);
if(nodesToRotate._parent == NULL)
{
AddAsNewRoot(symbol);
}
else
{
Rotate(nodesToRotate,symbol);
}
}
void AddAsNewRoot(Symbol *symbol)
{
Node *newRoot = new Node(symbol);
newRoot->_left = _root;
_root = newRoot;
}
void Rotate(ParentRightChildPair &nodesToRotate, Symbol *symbol)
{
Node *newParent = new Node(symbol);
nodesToRotate._parent->_right = newParent;
newParent->_left = nodesToRotate._rightChild;
}
void FindFirstLesserRightChild(ParentRightChildPair ¤t,Symbol *symbol)
{
if(!(*(current._rightChild->GetSymbol()) < *symbol))
{
if(current._rightChild->_right != NULL)
{
Node *newParent = current._rightChild;
current._rightChild = current._rightChild->_right;
current._parent = newParent;
FindFirstLesserRightChild(current,symbol);
}
}
}
void Clear(Node **root)
{
if((*root)->_left!=NULL)
{
Clear(&(*root)->_left);
}
if((*root)->_right!=NULL)
{
Clear(&(*root)->_right);
}
delete *root;
*root = NULL;
}
Node *_root;
};
#endif