-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
107 lines (90 loc) · 2.34 KB
/
tree.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "tree.h"
#include "myassert.h"
#include "verificator.h"
void TreeCtor (BinaryTree_t* myTree)
{
MYASSERT(myTree, ERR_BAD_POINTER_TREE, return)
myTree->Root = NULL;
myTree->Size = 0;
for (size_t i = 0; i < SIZE_OF_VARIABLES; i++)
{
myTree->Variables[i].Name[0] = '\0';
myTree->Variables[i].Number = 0;
}
myTree->ChangeOptimize = 0;
}
EnumOfErrors TreeDtor (BinaryTree_t* myTree)
{
MYASSERT(myTree, ERR_BAD_POINTER_TREE, return ERR_BAD_POINTER_TREE)
if (myTree->Root == NULL)
{
return ERR_OK;
}
//Verify(myTree);
RecFree (myTree->Root);
return ERR_OK;
}
void RecFree (Node_t* CurrentNode)
{
if (!CurrentNode) return;
if (CurrentNode->Left)
{
RecFree (CurrentNode->Left);
}
if (CurrentNode->Right)
{
RecFree (CurrentNode->Right);
}
free(CurrentNode);
}
void InitNode(Node_t* NewNode)
{
MYASSERT(NewNode, ERR_BAD_POINTER_NODE, return)
NewNode->Value.Number = NAN;
NewNode->Type = INIT;
NewNode->Right = NULL;
NewNode->Left = NULL;
NewNode->Parent = NULL;
}
Node_t* CreateNode (BinaryTree_t* myTree)
{
Node_t* NewNode = (Node_t*) calloc (1, sizeof (Node_t));
MYASSERT(NewNode, ERR_BAD_CALLOC, return NULL)
InitNode(NewNode);
myTree->Size++;
return NewNode;
}
Node_t* DiffCreateNode (BinaryTree_t* myTree, EnumOfType NewType, NodeValue_t NewValue, Node_t* LeftNode, Node_t* RightNode)
{
Node_t* NewNode = (Node_t*) calloc (1, sizeof (NewNode[0]));
MYASSERT(NewNode, ERR_BAD_CALLOC, return NULL)
InitNode(NewNode);
NewNode->Left = LeftNode;
NewNode->Right = RightNode;
NewNode->Type = NewType;
if (LeftNode) LeftNode->Parent = NewNode;
if (RightNode) RightNode->Parent = NewNode;
if (NewNode->Type == NUMBER)
{
NewNode->Value.Number = NewValue.Number;
}
if ((NewNode->Type == OPERATOR)||(NewNode->Type == VARIABLE))
{
NewNode->Value.Index = NewValue.Index;
}
myTree->Size++;
return NewNode;
}
bool Compare (double x, double y)
{
if (((isnan (x) == 1) && (isnan (y) == 1)) || (fabs (x - y) < EPSILONE))
return 1;
else
return 0;
}