-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionTree.h
More file actions
83 lines (75 loc) · 1.51 KB
/
FunctionTree.h
File metadata and controls
83 lines (75 loc) · 1.51 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
#pragma once
#ifndef FUNCTIONTREE_H_
#define FUNCTIONTREE_H_
#include<stack>
#include "BinaryTree.h"
#include "Node.h"
template<class T>
class FunctionTree: public BinaryTree<T, Node<T> >
{
public:
typedef Node<T> node;
FunctionTree(void){}
~FunctionTree(void){}
virtual void Insert(T key)
{
}
virtual void Delete(T key){}
void InitializeFunctionTree(T key)
{
if(_root == NULL)
{
_root = new node(key);
_nonTerminatedBranches.push(_root);
}
}
void AddChildOperation(T key)
{
if(_root==NULL)
InitializeFunctionTree(key);
else
{
node *child = new node(key);
AddChildToCurrentBranch(child);
_nonTerminatedBranches.push(child);
}
}
void AddChildOperand(T key)
{
node *child = new node(key);
AddChildToCurrentBranch(child);
node* current_branch = _nonTerminatedBranches.top();
if(current_branch->IsFull())
_nonTerminatedBranches.pop();
}
void AddChildToCurrentBranch(node *child)
{
if(!_nonTerminatedBranches.empty())
{
node *parent = _nonTerminatedBranches.top();
if(parent->GetLeft()==NULL)
{
parent->SetLeftChild(child);
}
else if(parent->GetRight()==NULL)
{
parent->SetRightChild(child);
}
else
{
throw std::logic_error("attempted to add child to full parent");
}
}
else if(_root==NULL)
{
throw std::logic_error("attempted to add operation to uninitialized tree");
}
else
{
throw std::logic_error("attempted to add operation to complete tree");
}
}
private:
std::stack<node *> _nonTerminatedBranches;
};
#endif