-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.cpp
More file actions
107 lines (98 loc) · 4.02 KB
/
Copy pathbinarySearchTree.cpp
File metadata and controls
107 lines (98 loc) · 4.02 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
/*
Author - Brandon Streets-McLeod
Last modified - 03/05/2021
This is the binary search tree file, it is used to build binary search trees to find all variables within a C file.
*/
#include "binarySearchTree.h"//includes all function definitions
#include "symbolTableProcess.h"//includes all function definitions
using namespace std;
node::node() //default constructor
{
ident;
root;
*left;
*right;
}
node::node(identifier identifierName, node *left, node *right) //constructor taking an initial value
{
ident = identifierName;
root = identifierName;
*left = *left;
*right = *right;
}
node::~node() //destructor
{
delete left;
delete right;
}
//updates a nodes count if there is multiple references to the identifier
void update(node **tree, identifier structNode)//update function that updates count property of the node
{
if (*tree != nullptr)
{
update(&((*tree)->left), structNode);//recursion to go through each left node of the tree
if (((*tree)->ident).identName == structNode.identName)//checks if the trees identifier value is equal to the nodes identifier value
{
((*tree)->ident).count++;//increments counter
}
update(&((*tree)->right), structNode);//recursion to go through each right node of the tree
}
}
//change insert so that it stores pointers to structs with data in
void insert (node **tree , identifier *nodes)
{
if (*tree == nullptr)
{
*tree = new node;//creates new instance of node if it doesnt already exist
(*tree)->ident = *nodes;//assigns values
(*tree)->left = nullptr;//assigns values
(*tree)->right = nullptr;//assigns values
}
else
{
if (((nodes)->identName) < ((*tree)->ident).identName)//checks if value is less than the root node
{
insert(&((*tree)->left), nodes);//calls insert function with values
}
else if ((nodes)->identName > ((*tree)->ident).identName)//checks if value is greater than the root node
{
insert(&((*tree)->right), nodes);//calls insert function with values
}
}
}
//puts node in order and outputs them to console
void inorder (node *tree)
{
if (tree != nullptr)//checks if tree isnt a null pointer
{
inorder(tree->left);//uses recursion to call itself to organise all left nodes
cout << (tree->ident).identName;//outputs root node
inorder(tree->right);//uses recursion to call itself to organise all right nodes
}
}
//fills up the identifier struct to store all data from each line of symbol table
void fillStruct(string identName, int lineNum, string identType, string identReturnType, int count, node **tree)
{
identifier nodes;//creates the struct named nodes
identName = remove_spaces(identName);//assigns values to each property of nodes
nodes.identName = identName;//assigns values to each property of nodes
nodes.lineNum = lineNum;//assigns values to each property of nodes
nodes.identType = identType;//assigns values to each property of nodes
nodes.identReturnType = identReturnType;//assigns values to each property of nodes
nodes.count = count;//assigns values to each property of nodes
insert(&(*tree), &nodes);//inserts data into tree
update(&(*tree), nodes);//updates the count property of the struct if its not the first occurence of the variable
}
//uses same theory as inorder function so reference through each node and use pointers to get struct and print data in order to file
void printToFile(node *tree)
{
if (tree != nullptr)//checks if tree isnt a null pointer
{
ofstream outAllFile("identifiers.txt",ios_base::app);//opens file stream with the type to append on the end of the file
printToFile(tree->left);//uses recursion to call itself to organise all left nodes
outAllFile << (tree->ident).identName << " , " << (tree->ident).lineNum << " , " << (tree->ident).identType << " , " << (tree->ident).identReturnType << ", " << (tree->ident).count << endl;//outputs to file
//cout << tree->ident;//outputs root node
printToFile(tree->right);//uses recursion to call itself to organise all right nodes
outAllFile.close();//closes file
}
}