-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBTreeMenu.cpp
More file actions
142 lines (128 loc) · 2.56 KB
/
RBTreeMenu.cpp
File metadata and controls
142 lines (128 loc) · 2.56 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
133
134
135
136
137
138
139
140
141
142
#include "RBTreeMenu.h"
#include "Common.h"
int Getcolor(RBTreeNode* node)
{
if (node->IsRed == true)
{
return 4;
}
return 0;
}
void PaintNode(RBTreeNode* node)
{
int backGround = 7;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
int color = Getcolor(node);
SetConsoleTextAttribute(hStdOut, (WORD)((backGround << 4) | color));
}
void PrintTree(RBTreeNode* node, RBTree* tree, const int indent)
{
if (node == tree->Nil || node == nullptr)
{
return;
}
if (node->Right != nullptr)
{
PrintTree(node->Right, tree, indent + 4);
}
if (indent)
{
std::cout << std::setw(indent) << ' ';
}
if (node->Right == tree->Nil)
{
std::cout << " /*\n" << std::setw(indent) << ' ';
}
if (node->Right != nullptr && node->Right != tree->Nil)
{
std::cout << " /\n" << std::setw(indent) << ' ';
}
PaintNode(node);
if (node != tree->Nil)
{
std::cout << node->Data << "\n ";
}
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, (WORD)((7 << 4) | 0));
if (node->Left == tree->Nil)
{
std::cout << std::setw(indent) << ' ' << " \\\n";
std::cout << std::setw(indent) << ' ' << " *\n";
PrintTree(node->Left, tree, indent + 4);
}
if (node->Left != nullptr && node->Left != tree->Nil)
{
std::cout << std::setw(indent) << ' ' << " \\\n";
PrintTree(node->Left, tree, indent + 4);
}
}
void RBTreeMenu(RBTree* tree)
{
enum class Menu
{
Add = 1,
Remove = 2,
Find = 3,
Exit = 0
};
InitTree(tree);
bool endProgramm = false;
while (!endProgramm)
{
std::cout << "Here is Red-Black tree:" << std::endl;
PrintTree(tree->Root, tree, 2);
std::cout << "\n1 - Add\n"
<< "2 - Remove\n"
<< "3 - Find\n"
<< "0 - Exit\n"
<< std::endl;
int userChoose = GetValue();
switch (static_cast<Menu>(userChoose))
{
case Menu::Add:
{
Add(tree, GetValue());
break;
}
case Menu::Remove:
{
int data = GetValue();
if (FindNode(data, tree) == nullptr)
{
std::cout << "Not found" << std::endl;
system("pause");
break;
}
RemoveNode(FindNode(data, tree), tree);
break;
}
case Menu::Find:
{
int key = GetValue();
if (FindNode(key, tree) == nullptr)
{
std::cout << "Not found" << std::endl;
system("pause");
break;
}
std::cout << "Node with value " << key
<< " has address: "
<< FindNode(key, tree) << std::endl;
system("pause");
break;
}
case Menu::Exit:
{
endProgramm = true;
break;
}
default:
{
std::cout << "Unknown command. Choose one from the list" << std::endl;
system("pause");
break;
}
}
system("cls");
}
}