-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
82 lines (70 loc) · 1.98 KB
/
Tree.cpp
File metadata and controls
82 lines (70 loc) · 1.98 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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Tree.h"
#include "Strings.h"
#include "secEnter.h"
void addNode(treeNode*& node, char* data, bool leaf) {
bool flagRoot = false;
treeNode** tmp;
if (node == nullptr) {
tmp = &node;
flagRoot = true;
}
else {
if (!leaf) tmp = &node->leftLeaf;
else tmp = &node->rightLeaf;
if (*tmp != nullptr) return;
}
*tmp = (treeNode*)malloc(sizeof(treeNode));
(*tmp)->data = (char*)calloc(buffSizeStr1024, sizeof(char));
strcpy((*tmp)->data, data);
if (flagRoot) {
node->key = (char*)calloc(buffSizeStr1024, sizeof(char));
*node->key = '*';
}
else {
(*tmp)->key = (char*)calloc(buffSizeStr1024, sizeof(char));
if (*node->key != '*') {
strcpy((*tmp)->key, node->key);
*((*tmp)->key + my_strlen((*tmp)->key)) = leaf + '0';
}
else {
*((*tmp)->key) = leaf + '0';
}
}
(*tmp)->leftLeaf = nullptr;
(*tmp)->rightLeaf = nullptr;
}
void cleanTree(treeNode* node/*root at first request*/) {
if (node->leftLeaf != nullptr) cleanTree(node->leftLeaf);
if (node->rightLeaf != nullptr) cleanTree(node->rightLeaf);
free(node->data);
free(node->key);
free(node);
}
void addAnswToTree(treeNode*& node, char* data, char* newData, bool leaf/*left or right. where data have to go*/) {
addNode(node, node->data, !leaf);
addNode(node, data, leaf);
strcpy(node->data, newData);
}
treeNode* findLeaf(char* way, treeNode* node) {
if (*(way + 1) == '\0') {
return node;
}
if (*way == '0') return findLeaf(way + 1, node->leftLeaf);
if (*way == '1') return findLeaf(way + 1, node->rightLeaf);
}
treeNode* findObject(treeNode* node/*root at 1st request*/) {
if (node->leftLeaf && node->rightLeaf) {
printf("%s ?\n1 - Yes\n0 - No\n", node->data);
if (securitedEnter(0, 1)) return findObject(node->rightLeaf);
else return findObject(node->leftLeaf);
}
else {
printf("Is it %s ?\n1 - Yes\n0 - No\n", node->data);
if (securitedEnter(0, 1)) return nullptr;
else return node;
}
}