-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
132 lines (110 loc) · 2.65 KB
/
tree.cpp
File metadata and controls
132 lines (110 loc) · 2.65 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
#include <iostream>
#include <string>
#include "tree.hpp"
#include "well_formed.hpp"
#include "tableaux.hpp"
using namespace std;
Node* create_node(char symbol)
{
Node *new_node = new Node;
if (new_node == NULL)
return NULL;
new_node->symbol = symbol;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
Node* copy_tree(Node* root)
{
if (root)
{
Node *left = copy_tree(root->left);
Node *right = copy_tree(root->right);
Node *new_root = create_node(root->symbol);
new_root->left = left;
new_root->right = right;
return new_root;
}
return NULL;
}
void print_tree(Node* root)
{
if (root)
{
print_tree(root->left);
cout << root->symbol << " ";
print_tree(root->right);
}
}
void print_formula(Node* root)
{
if (root)
{
bool left_brackets = root->left && is_bin_symbol(root->left->symbol);
bool right_brackets = root->right && is_bin_symbol(root->right->symbol);
// print left side with brackets if needed
if (left_brackets) cout << "(";
print_formula(root->left);
if (left_brackets) cout << ")";
if (root->left) cout << ' ';
cout << root->symbol;
if (root->right && root->symbol != '*') cout << ' '; // we don't want negation separated from letter
// print right side with brackets if needed
if (right_brackets) cout << "(";
print_formula(root->right);
if (right_brackets) cout << ")";
}
}
void free_tree(Node* root)
{
if (root)
{
free_tree(root->left);
free_tree(root->right);
delete root;
}
}
Tnode* create_tnode(Sign s, Node *root)
{
Tnode *tnode = new Tnode;
if (tnode == NULL)
return NULL;
tnode->sign = s;
tnode->root = root;
tnode->left = NULL;
tnode->right = NULL;
tnode->used = false;
tnode->closed = false;
return tnode;
}
void t_free_tree(Tnode* root)
{
if (root)
{
t_free_tree(root->left);
t_free_tree(root->right);
delete root;
}
}
//customized print2DUtil for signed formula
static const int COUNT = 7;
void print_signed_2D(Tnode *troot, int space)
{
if(troot == NULL)
return ;
space += COUNT;
print_signed_2D(troot->right, space);
printf("\n");
for(int i = COUNT; i < space ; i++)
printf(" ");
cout << "(" << troot->number << ") ";
troot->sign ? cout << "T " : cout << "F ";
print_formula(troot->root);
cout<<'\n';
print_signed_2D(troot->left, space);
}
void print_enumerated_2D(Tnode* node)
{
find_numbers(node);
print_signed_2D(node, 0);
}