Version: 1.0
tree.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_TREE_H_
8 #define GRAPHGEN_TREE_H_
9 
10 #include <algorithm>
11 #include <memory>
12 #include <unordered_map>
13 #include <vector>
14 
15 // BDRAG - This data structure allows to manage a Binary Directed Rooted Acyclic Graph,
16 // that can be both a standard tree or a DAG, with the sole limitation that every node
17 // shall have two children. In our case exactly two or zero.
18 //template<typename T>
19 //struct tree {
20 // struct node
21 // {
22 // T data;
23 // node *left = nullptr, *right = nullptr;
24 //
25 // node() {}
26 // node(T d) : data(move(d)) {}
27 // node(T d, node* l, node* r) : data(move(d)), left(l), right(r) {}
28 //
29 // bool isleaf() const {
30 // return left == nullptr && right == nullptr;
31 // }
32 // };
33 //
34 // // Vector of unique pointers to tree nodes. This is useful to free memory when the tree is converted to DAG
35 // std::vector<std::unique_ptr<node>> nodes;
36 //
37 // // Creates and returns new node updating the nodes vector
38 // template<typename... Args>
39 // node* make_node(Args&&... args) {
40 // nodes.emplace_back(std::make_unique<node>(std::forward<Args>(args)...));
41 // return nodes.back().get();
42 // }
43 //
44 //private:
45 // node *root_ = nullptr;
46 //public:
47 //
48 // node* GetRoot() const {
49 // return root_;
50 // }
51 //
52 // node* GetRoot() {
53 // return root_;
54 // }
55 //
56 // void SetRoot(node *root) {
57 // root_ = root;
58 // }
59 //
60 // friend void swap(tree& t1, tree& t2) {
61 // using std::swap;
62 // swap(t1.root_, t2.root_);
63 // swap(t1.nodes, t2.nodes);
64 // }
65 //
66 // // Recursive function to copy a tree. This is required by the copy constructor
67 // // It works for both trees and dags
68 // node *MakeCopyRecursive(node *n, std::unordered_map<node*, node*> &copies, std::vector<node*>& tracked_nodes) {
69 // if (n == nullptr)
70 // return nullptr;
71 //
72 // if (!copies[n]) {
73 // node *nn = make_node();
74 // nn->data = n->data;
75 // nn->left = MakeCopyRecursive(n->left, copies, tracked_nodes);
76 // nn->right = MakeCopyRecursive(n->right, copies, tracked_nodes);
77 // copies[n] = nn;
78 // auto it = find(begin(tracked_nodes), end(tracked_nodes), n);
79 // if (it != end(tracked_nodes)) {
80 // *it = nn;
81 // }
82 // }
83 // return copies[n];
84 // }
85 //
86 // tree() {}
87 //
88 // // Copy constructor
89 // tree(const tree& t) {
90 // std::unordered_map<node*, node*> copies;
91 // std::vector<node*> tracked_nodes;
92 // root_ = MakeCopyRecursive(t.root_, copies, tracked_nodes);
93 // }
94 // tree(const tree& t, std::vector<node*>& tracked_nodes) { // Allows to track where the nodes in a tree have been copied to
95 // std::unordered_map<node*, node*> copies;
96 // root_ = MakeCopyRecursive(t.root_, copies, tracked_nodes);
97 // }
98 // tree(tree&& t) {
99 // swap(*this, t);
100 // }
101 // // Copy assignment
102 // tree& operator=(tree t) {
103 // swap(*this, t);
104 // return *this;
105 // }
106 //
107 // node* make_root() { return root_ = make_node(); }
108 //};
109 
110 #endif // !GRAPHGEN_TREE_H_
111