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 <algorithm>
29 
30 #include "Helper/Logger/Logger.h"
31 
32 template<typename T>
37 class Tree {
38 
39  public:
40  Tree* parent=nullptr;
41 
42  QList<Tree*> children;
43  T data;
44 
49  Tree(const T& data_){
50  data = data_;
51  parent = nullptr;
52  children.clear();
53  }
54 
55  virtual ~Tree(){
56  for(Tree* child : children){
57  delete child;
58  }
59 
60  children.clear();
61  data = T();
62  }
63 
67  Tree* copy(){
68  Tree* node = new Tree(this->data);
69 
70  for(Tree* child : children){
71  node->children << child->copy();
72  }
73 
74  return node;
75  }
76 
77 
83  Tree* add_child(Tree* node){
84 
85  node->parent = this;
86  this->children << node;
87 
88  this->sort(false);
89 
90  return node;
91  }
92 
93 
99  Tree* remove_child(Tree* deleted_node){
100 
101  deleted_node->parent = nullptr;
102 
103  for(int i=0; i < children.size(); i++){
104 
105  Tree* node = children[i];
106 
107  if(node == deleted_node){
108  deleted_node = children.takeAt(i);
109  i--;
110  }
111  }
112 
113  return deleted_node;
114  }
115 
116 
121  void move(Tree* new_parent){
122 
123  parent->remove_child(data);
124  new_parent->add_child(this);
125  }
126 
131  void sort(bool recursive){
132  int i;
133 
134  if(children.isEmpty()){
135  return;
136  }
137 
138  auto lambda = [](Tree* a, Tree* b){
139  return (a->data < b->data);
140  };
141 
142  std::sort(children.begin(), children.end(), lambda);
143 
144 
145  i=0;
146  for(Tree* child : children){
147  if(recursive){
148  child->sort(recursive);
149  }
150  i++;
151  }
152  }
153 
154 
155  void print(int lvl) const {
156 
157  for(Tree* child : children){
158 
159  QString str;
160  for(int i=0; i<lvl; i++){
161  str += " ";
162  }
163 
164  str += "|_";
165 
166  child->print(lvl+1);
167  }
168  }
169 
170 };
171 
172 
173 #endif // TREE_H
void move(Tree *new_parent)
move current node to a new parent
Definition: Tree.h:121
Tree * remove_child(Tree *deleted_node)
remove a node from the current node
Definition: Tree.h:99
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:131
Tree * copy()
Definition: Tree.h:67
Tree(const T &data_)
Tree constructor.
Definition: Tree.h:49
Tree * add_child(Tree *node)
adds a child to the given node
Definition: Tree.h:83
Definition: org_mpris_media_player2_adaptor.h:20