-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecurseFunctions.cpp
More file actions
109 lines (98 loc) · 2.39 KB
/
RecurseFunctions.cpp
File metadata and controls
109 lines (98 loc) · 2.39 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
#ifndef RECURSEFUNCTIONS_CPP
#define RECURSEFUNCTIONS_CPP
#include <iostream>
#include "BinaryTree.h"
#include <Windows.h>
#include <string>
using namespace std;
template <class MyType = int>
void PushItem(MyType _data, int _key, TreeItem<MyType>* ¤t)
{
if (current == nullptr)
{
current = new TreeItem<MyType>(_data, _key);
return;
}
if (current->key < _key)
{
PushItem(_data, _key, current->right);
}
if (current->key > _key)
{
PushItem(_data, _key, current->left);
}
}
template <class MyType = int>
void HeightRecurse(int &temp, int &max, TreeItem<MyType>* current)
{
if ((current->left == nullptr) && (current->right == nullptr))
{
max = (temp > max) ? temp : max;
temp--;
return;
}
if (current->left != nullptr) HeightRecurse(++temp, max, current->left);
if (current->right != nullptr) HeightRecurse(++temp, max, current->right);
--temp;
}
template <class MyType = int>
void ShowRecurse(const int interval, int &temp, const int limit, const TreeItem<MyType>* current, int &switcher) // Show only a limit floor
{
if (((current->left == nullptr) && (current->right == nullptr)) || (temp == limit))
{
if (temp == limit)
{
SetColor(switcher);
cout << current->key << string(interval, ' ');
}
temp--;
return;
}
if (current->left != nullptr) ShowRecurse(interval, ++temp, limit, current->left, switcher);
if (current->right != nullptr) ShowRecurse(interval, ++temp, limit, current->right, switcher);
--temp;
}
template <class MyType = int>
void FBypass(const TreeItem<MyType>* current)
{
cout << "Data: " << current->data << endl;
cout << "Key : " << current->key << endl;
cout << "---\n";
if ((current->left == nullptr) && (current->right == nullptr))
{
return;
}
if (current->left != nullptr) FBypass(current->left);
if (current->right != nullptr) FBypass(current->right);
}
// a few needed not recurse functions
inline bool isOdd(int integer)
{
if (integer % 2 == 0)
return false;
else
return true;
}
inline void SetColor(int &switcher)
{
if (switcher == 0)
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, (WORD)(0 << 4 | 4));
switcher++;
}
else
{
if (isOdd(switcher))
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, (WORD)(0 << 4 | (1+switcher > 15) ? switcher - 14 : 1+switcher));
switcher++;
} else
if (!isOdd(switcher))
{
switcher++;
}
}
}
#endif