-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuptr_binary_tree.hpp
55 lines (47 loc) · 1.16 KB
/
uptr_binary_tree.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
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include "io.hpp"
#include <cstddef>
#include <memory>
#include <unordered_map>
#include <utility>
template<typename T>
struct Node;
template<typename T>
using Node_ptr = std::unique_ptr<Node<T>>;
template<typename T>
struct Node
{
const T data;
Node_ptr<T> left;
Node_ptr<T> right;
explicit Node(T data) : data(std::move(data))
{}
};
template<typename T>
void read_tree(Node_ptr<T>& root)
{
root.reset();
std::unordered_map<T, Node_ptr<T>*> map;
std::size_t n_edges;
read(n_edges);
for (std::size_t i = 0; i < n_edges; ++i)
{
T parent_data, child_data;
char which;
read(parent_data, child_data, which);
const auto ptr = (which == 'L') ? &Node<T>::left : &Node<T>::right;
if (const auto pos = map.find(parent_data); pos != map.end())
{
(**pos->second).*ptr = std::make_unique<Node<T>>(child_data);
map.emplace(child_data, &((**pos->second).*ptr));
}
else
{
root = std::make_unique<Node<T>>(parent_data);
(*root).*ptr = std::make_unique<Node<T>>(child_data);
map.emplace(parent_data, &root);
map.emplace(child_data, &((*root).*ptr));
}
}
}