-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap12.hpp
147 lines (131 loc) · 3.42 KB
/
chap12.hpp
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
143
144
145
146
147
#ifndef CHAP_12_H
#define CHAP_12_H
#include <cmath>
#include "chap2.hpp"
struct Node{
int value;
Node* left=nullptr;
Node* right=nullptr;
Node* parent=nullptr;
Node(int value_) : value(value_)
{}
Node()
{
value = -1000;
}
bool has_left(){return left != nullptr;};
bool has_right(){return right != nullptr;};
bool has_parent(){return parent != nullptr;};
std::string state()
{
std::string msg = "value " + std::to_string(value) + " left leaf: ";
if(has_left())
{
msg += std::to_string(left->value);
}
else
{
msg += "None";
}
msg += " right leaf: ";
if(has_right())
{
msg += std::to_string(right->value);
}
else
{
msg += "None";
}
return msg;
}
};
class BinarySearchTree
{
public:
/**
* @brief Construct a new Binary Search Tree object
*
*/
BinarySearchTree();
/**
* @brief Destroy the Binary Search Tree object
*
*/
~BinarySearchTree();
/**
* @brief recursively inserts a leaf into the tree starting from root position by respecting the
* binary search tree property
*
* @param node_ptr a node belonging to the tree
* @param value node value to be inserted
*/
void InsertLeaf(Node* node_ptr, int value);
/**
* @brief prints the content of a binary tree in a tring
*
* @param node_ptr node to print belonging to the tree
* @param msg tree structure to fillup
* @param depth representation of the current tree depth to compute spacing
*/
void Display(Node* node_ptr, std::string& msg, int depth);
/**
* @brief finds a node with the node containing the searched value
*
* @param returned_node node containing the searched value if any
* @param to_find value to search for
* @return true
* @return false
*/
bool Search(Node* returned_node, int to_find);
/**
* @brief retrieves the node with minimum value
*
* @return Node*
*/
Node* Minimum();
/**
* @brief retrieves the node with minimum value
*
* @return Node*
*/
Node* Maximum();
Node* root=nullptr; /// root node
private:
/**
* @brief walks through the tree recursively to find the queried value
*
* @param current current parent node
* @param to_find value to look for
* @return None
*/
void SearchTree(Node* current, Node* result_node, int to_find);
/**
* @brief utility to find the minimum node on the tree given
*
* @param current
* @param minimum_value
* @return Node*
*/
void FindMinimum(Node* current, Node* min_node, int minimum_value);
/**
* @brief utility to find the maximum node on the tree given
*
* @param current
* @param min_node
* @param minimum_value
*/
void FindMaximum(Node* current, Node* min_node, int minimum_value);
Node* empty_node_ = new Node(-1000);
};
namespace chap12
{
/**
* @brief creates a binary tree given a vector of ints and depth
*
* @param bst_ptr pointer binary search tree to be filled
* @param values input values vector
* @param depth depth of the tree
*/
void FillupTree(BinarySearchTree* bst_ptr, std::vector<int> values, int depth);
}
#endif