linked_impl.h
Go to the documentation of this file.
1 /*
2  * This file is part of the alglib project.
3  *
4  * (c) Divyanshu Kakwani <divkakwani@gmail.com>
5  *
6  * For the full copyright and license information, please view the LICENSE file
7  * that was distributed with this source code.
8  */
9 
10 #include <memory>
11 #include <vector>
12 #include <stdexcept>
13 
14 #pragma once
15 
56 // Forward declaration
57 template<typename value_t> class Tree;
58 
59 template<typename value_t>
60 struct Node {
61 
62  typedef value_t value_type;
63 
64  std::shared_ptr<Node> parent;
65 
66  value_type val;
67 
68  std::vector<std::shared_ptr<Tree<value_t>>> subtrees;
69 
72  Node(const value_type& value) {
73  val = value;
74  parent = 0;
75  // Keep the subtrees list empty
76  }
77 
78  std::shared_ptr<Tree<value_t>> get_ith_subtree(int i) {
79  if(i >= subtrees.size())
80  throw std::out_of_range("i is too big");
81  return subtrees[i];
82  }
83 
84  void set_ith_subtree(std::shared_ptr<Tree<value_type>> pT, int i) {
85  if(i > subtrees.size())
86  throw std::out_of_range("i is too big");
87  if(subtrees.size() == i)
88  subtrees.push_back(pT);
89  else
90  subtrees[i] = pT;
91  }
92 
93  value_type get_val() {
94  return val;
95  }
96  void set_val(const value_type& value) {
97  val = value;
98  }
99 
101  return parent;
102  }
103  void set_parent(std::shared_ptr<Node<value_type>> new_parent) {
104  parent = new_parent;
105  }
106 
107 };
108 
109 template<typename value_t>
110 struct Tree {
111 
112  public:
113  typedef value_t value_type;
114 
116 
117  explicit Tree(const Node<value_type>& root_node) : root(root_node) {}
118 
119  // Iterators
120  class PostIter;
121  class PreIter;
122  class InIter;
123 
124  // Inspection of the tree
125  PreIter prebegin() const {}
126  PreIter preend() const {}
127  InIter inbegin() const {}
128  InIter inend() const {}
129  PostIter postbegin() const {}
130  PostIter postend() const {}
131 };
132 
133 // Node n(3);
134 // Tree T(n);
135 // Tree.root.set_ith_subtree(a tree or a node)
136 
137 
value_type val
Definition: linked_impl.h:66
void set_parent(std::shared_ptr< Node< value_type >> new_parent)
Definition: linked_impl.h:103
std::shared_ptr< Tree< value_t > > get_ith_subtree(int i)
Definition: linked_impl.h:78
InIter inend() const
Definition: linked_impl.h:128
PreIter prebegin() const
Definition: linked_impl.h:125
Definition: linked_impl.h:60
value_type get_val()
Definition: linked_impl.h:93
Node(const value_type &value)
Definition: linked_impl.h:72
std::vector< std::shared_ptr< Tree< value_t > > > subtrees
Definition: linked_impl.h:68
Node< value_type > & get_parent()
Definition: linked_impl.h:100
Node< value_type > & root
Definition: linked_impl.h:115
void set_val(const value_type &value)
Definition: linked_impl.h:96
value_t value_type
Definition: linked_impl.h:113
PostIter postend() const
Definition: linked_impl.h:130
A generic tree data structre.
Definition: linked_impl.h:57
Tree(const Node< value_type > &root_node)
Definition: linked_impl.h:117
InIter inbegin() const
Definition: linked_impl.h:127
void set_ith_subtree(std::shared_ptr< Tree< value_type >> pT, int i)
Definition: linked_impl.h:84
PreIter preend() const
Definition: linked_impl.h:126
value_t value_type
Definition: linked_impl.h:62
PostIter postbegin() const
Definition: linked_impl.h:129
std::shared_ptr< Node > parent
Definition: linked_impl.h:64