Advertisement
qberik

Untitled

Oct 31st, 2022
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. #ifndef TREE_H
  2. #define TREE_H
  3. #include "exc.h"
  4. #include "log.h"
  5. #include <cstdint>
  6. #include <type_traits>
  7. #include <vector>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14. template<class T>
  15. class btree{
  16. public:
  17. struct node;
  18. btree();
  19. ~btree();
  20.  
  21. void insert(T);
  22. btree<T>::node* remove(T);
  23. node *search(T);
  24. void destroy_tree();
  25. void simmetrichniy();
  26. void obratniy();
  27. void pryamoy();
  28.  
  29. void vprint( std::ostream& out = std::cout );
  30.  
  31. void left_right();
  32. void right_left();
  33.  
  34.  
  35. struct node{
  36. T value;
  37. node *left;
  38. node *right;
  39. };
  40.  
  41.  
  42. node*min_element( node* leaf);
  43. void destroy_tree(node *leaf);
  44. void insert(T key, node *leaf);
  45. btree<T>::node* remove(T key, node *leaf);
  46. node *search(T key, node *leaf);
  47. void simmetrichniy(node *leaf);
  48. void obratniy(node *leaf);
  49. void pryamoy(node *leaf);
  50.  
  51. node *root;
  52. node *_parrent;
  53. };
  54.  
  55.  
  56.  
  57.  
  58. template<class T>
  59. typename btree<T>::node* btree<T>::remove(T key, node *leaf){
  60. INFO( "Уладение элемента" );
  61. if( leaf->value == key ){
  62. if( leaf->left == nullptr && leaf->right == nullptr ){
  63. return nullptr;
  64. }
  65. if( leaf->left == nullptr ){
  66. return leaf->right;
  67. }
  68. if( leaf->right == nullptr ){
  69. return leaf->left;
  70. }
  71.  
  72. node* minleaf = min_element(leaf->right);
  73. leaf->value = leaf->value;
  74.  
  75. leaf->right = remove( leaf->value ,leaf->right );
  76.  
  77. return leaf;
  78. }
  79.  
  80.  
  81. if( key < leaf->value ){
  82. if( leaf->left == nullptr ){
  83. WARN("Элемент не найден");
  84. return leaf;
  85. }
  86. leaf->left = remove( key, leaf->left );
  87. return leaf;
  88. }
  89.  
  90.  
  91. if( key > leaf->value ){
  92. if( leaf->right == nullptr ){
  93. WARN("Элемент не найден");
  94. return leaf;
  95. }
  96.  
  97. leaf->right = remove( key, leaf->right );
  98. return leaf;
  99. }
  100.  
  101. ERROR("unexpexted behaviour");
  102. return nullptr;
  103.  
  104. }
  105.  
  106. template<class T>
  107. typename btree<T>::node* btree<T>::min_element(node* leaf) {
  108. INFO( "Поиск левого элемента" );
  109. if (leaf->left == nullptr) return leaf;
  110.  
  111. return min_element(leaf->left);
  112. }
  113.  
  114. template<class T>
  115. typename btree<T>::node* btree<T>::remove(T key){
  116. INFO( "Уладение элемента" );
  117. if (root == nullptr) {
  118. return nullptr;
  119. }
  120.  
  121. root = remove(key, root );
  122.  
  123. ERROR("unexpexted behaviour");
  124. return nullptr;
  125. }
  126.  
  127. template<class T>
  128. void btree<T>::left_right(){
  129. INFO( "Поиск левого элемента правого поддерева" );
  130. if( root->left == nullptr ){
  131. cout << "Левого поддерева нет" << endl;
  132. }else{
  133. node *n = root->left;
  134. while( n->right != nullptr ){
  135. n = n->right;
  136. }
  137. cout << "Самый правый в левом поддереве " << n->value << endl;
  138. }
  139. }
  140.  
  141.  
  142.  
  143. template<class T>
  144. void btree<T>::right_left(){
  145. INFO( "Поиск правого элемента левого поддерева" );
  146. if( root->right == nullptr ){
  147. cout << "Правого поддерева нет" << endl;
  148. }else{
  149. node *n = root->right;
  150. while( n->left != nullptr ){
  151. n = n->left;
  152. }
  153. cout << "Самый левый в правом поддереве " << n->value << endl;
  154. }
  155. }
  156.  
  157.  
  158.  
  159.  
  160. template<class T>
  161. void btree<T>::vprint(std::ostream& out ){
  162. INFO( "Горизонтальная печать дерева" );
  163.  
  164. vector< vector<string> > some;
  165. node* p = root;
  166. vector<node*> depth;
  167. vector<node*> next_depth;
  168. depth.push_back( root );
  169. vector<string> layer;
  170. bool flag = true;
  171. while ( flag ) {
  172. layer.clear();
  173. next_depth.clear();
  174. for( auto p : depth ){
  175. if( p != nullptr ){
  176. string in;
  177. in += p->value;
  178. layer.push_back( in );
  179. if( p->left != nullptr || 1 ){
  180. next_depth.push_back( p->left );
  181. }
  182.  
  183. if( p->right != nullptr || 1 ){
  184. next_depth.push_back( p->right );
  185. }
  186. }else{
  187. layer.push_back("-");
  188. next_depth.push_back( nullptr );
  189. next_depth.push_back( nullptr );
  190. }
  191. }
  192. depth = next_depth;
  193. some.push_back( layer );
  194.  
  195. flag = false;
  196. for( auto i : depth ){
  197. if( i != nullptr ){
  198. flag = true;
  199. }
  200. }
  201. }
  202.  
  203.  
  204.  
  205.  
  206. //falsy print
  207. int max_len = -1;
  208. int len = 0;
  209. for( auto l : some ){
  210. len = 0;
  211. for( auto e: l ){
  212. len += e.size();
  213. len ++;
  214. }
  215. len--;
  216. if( len > max_len ){
  217. max_len = len;
  218. }
  219. }
  220.  
  221.  
  222. layer.clear();
  223. int sep = 0;
  224. for( int _i = 0; _i < some.size(); _i++ ){
  225. int index = some.size() - 1 - _i;
  226. string line = "";
  227. for( int j = 0; j < sep; j++ ){
  228. line += ' ';
  229. }
  230. sep = 2 * sep + 1;
  231. for( auto elem: some[ index ] ){
  232. line += elem;
  233. for( int j = 0; j < sep; j++ ){
  234. line += ' ';
  235. }
  236. }
  237. layer.insert( layer.begin(), line);
  238. }
  239.  
  240.  
  241. for( auto l : layer ){
  242. out << l << endl;
  243. }
  244.  
  245.  
  246.  
  247. out << endl;
  248.  
  249. }
  250.  
  251.  
  252. template<class T>
  253. btree<T>::btree(){
  254. INFO( "Создание дерева" );
  255. root = nullptr;
  256. }
  257.  
  258. template<class T>
  259. btree<T>::~btree(){
  260. INFO( "Удаление дерева" );
  261. destroy_tree();
  262. }
  263.  
  264.  
  265.  
  266. template<class T>
  267. void btree<T>::insert(T key, node *leaf){
  268. INFO( "Вставка в дерево" );
  269.  
  270. if(key < leaf->value){
  271. if(leaf->left != nullptr){
  272. insert(key, leaf->left);
  273. }else{
  274. leaf->left = new node;
  275. leaf->left->value = key;
  276. leaf->left->left = nullptr;
  277. leaf->left->right = nullptr;
  278. }
  279. }else if(key > leaf->value){ // ??? >=
  280. if(leaf->right != nullptr){
  281. insert(key, leaf->right);
  282. }else{
  283. leaf->right = new node;
  284. leaf->right->value = key;
  285. leaf->right->right = nullptr;
  286. leaf->right->left = nullptr;
  287. }
  288. }
  289.  
  290. }
  291.  
  292. template<class T>
  293. void btree<T>::insert(T key){
  294. INFO( "Вставка в дерево" );
  295. if(root != nullptr){
  296. insert(key, root);
  297. }else{
  298. root = new node;
  299. root->value = key;
  300. root->left = nullptr;
  301. root->right = nullptr;
  302. }
  303. }
  304.  
  305. template<class T>
  306. typename btree<T>::node* btree<T>::search(T key, btree<T>::node *leaf){
  307. INFO( "поиск по дереву" );
  308. if(leaf != nullptr){
  309. if(key == leaf->value){
  310. return leaf;
  311. }
  312. if(key < leaf->value){
  313. return search(key, leaf->left);
  314. }else{
  315. return search(key, leaf->right);
  316. }
  317. }else{
  318. WARN("Элемент не найден");
  319. return nullptr;
  320. }
  321. }
  322.  
  323. template<class T>
  324. typename btree<T>::node *btree<T>::search(T key){
  325. return search(key, root);
  326. }
  327.  
  328. template<class T>
  329. void btree<T>::destroy_tree(){
  330. destroy_tree(root);
  331. }
  332.  
  333. template<class T>
  334. void btree<T>::simmetrichniy(){
  335. simmetrichniy(root);
  336. cout << "\n";
  337. }
  338.  
  339. template<class T>
  340. void btree<T>::simmetrichniy(node *leaf){
  341. if(leaf != nullptr){
  342. simmetrichniy(leaf->left);
  343. cout << leaf->value << ",";
  344. simmetrichniy(leaf->right);
  345. }
  346. }
  347.  
  348. template<class T>
  349. void btree<T>::destroy_tree(node *leaf){
  350. INFO( "Очитка дерева" );
  351. root = nullptr;
  352. }
  353.  
  354. template<class T>
  355. void btree<T>::obratniy(){
  356. obratniy(root);
  357. cout << "\n";
  358. }
  359.  
  360. template<class T>
  361. void btree<T>::obratniy(node *leaf){
  362. if(leaf != nullptr){
  363. obratniy(leaf->left);
  364. obratniy(leaf->right);
  365. cout << leaf->value << ",";
  366. }
  367. }
  368.  
  369. template<class T>
  370. void btree<T>::pryamoy(){
  371. pryamoy(root);
  372. cout << "\n";
  373. }
  374.  
  375. template<class T>
  376. void btree<T>::pryamoy(node *leaf){
  377. if(leaf != nullptr){
  378. cout << leaf->value << ",";
  379. pryamoy(leaf->left);
  380. pryamoy(leaf->right);
  381. }
  382. }
  383.  
  384.  
  385. #endif
  386.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement