Sayonara Player
Tree.h
1 
2 /* Copyright (C) 2011-2016 Lucio Carreras
3  *
4  * This file is part of sayonara player
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15 
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 
22 
23 #ifndef TREE_H
24 #define TREE_H
25 
26 
27 #include <QList>
28 #include <QString>
29 #include <algorithm>
30 
31 #include "Helper/Logger/Logger.h"
32 
33 template<typename T>
38 class Tree {
39 
40  public:
41  Tree* parent=nullptr;
42 
43  QList<Tree*> children;
44  T data;
45 
50  Tree(const T& data_){
51  data = data_;
52  parent = nullptr;
53  children.clear();
54  }
55 
56  virtual ~Tree(){
57  for(Tree* child : children){
58  delete child;
59  }
60 
61  children.clear();
62  data = T();
63  }
64 
68  Tree* copy(){
69  Tree* node = new Tree(this->data);
70 
71  for(Tree* child : children){
72  node->children << child->copy();
73  }
74 
75  return node;
76  }
77 
78 
84  Tree* add_child(Tree* node){
85 
86  node->parent = this;
87  this->children << node;
88 
89  this->sort(false);
90 
91  return node;
92  }
93 
94 
100  Tree* remove_child(Tree* deleted_node){
101 
102  deleted_node->parent = nullptr;
103 
104  for(int i=0; i < children.size(); i++){
105 
106  Tree* node = children[i];
107 
108  if(node == deleted_node){
109  deleted_node = children.takeAt(i);
110  i--;
111  }
112  }
113 
114  return deleted_node;
115  }
116 
117 
122  void move(Tree* new_parent){
123 
124  parent->remove_child(data);
125  new_parent->add_child(this);
126  }
127 
132  void sort(bool recursive){
133  int i;
134 
135  if(children.isEmpty()){
136  return;
137  }
138 
139  auto lambda = [](Tree* a, Tree* b){
140  return (a->data < b->data);
141  };
142 
143  std::sort(children.begin(), children.end(), lambda);
144 
145 
146  i=0;
147  for(Tree* child : children){
148  if(recursive){
149  child->sort(recursive);
150  }
151  i++;
152  }
153  }
154 
155 
156  void print(int lvl) const {
157 
158  for(Tree* child : children){
159 
160  QString str;
161  for(int i=0; i<lvl; i++){
162  str += " ";
163  }
164 
165  str += "|_";
166 
167  child->print(lvl+1);
168  }
169  }
170 
171 };
172 
173 
174 #endif // TREE_H
void move(Tree *new_parent)
move current node to a new parent
Definition: Tree.h:122
Tree * remove_child(Tree *deleted_node)
remove a node from the current node
Definition: Tree.h:100
The Tree class.
Definition: LibraryGenreView.h:45
void sort(bool recursive)
sort children of all nodes in ascending way according to their data
Definition: Tree.h:132
Tree * copy()
Definition: Tree.h:68
Tree(const T &data_)
Tree constructor.
Definition: Tree.h:50
Tree * add_child(Tree *node)
adds a child to the given node
Definition: Tree.h:84