Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  lab9_tree
  4. //
  5. //  Created by AND on 17/05/2019.
  6. //  Copyright © 2019 Andrey Popov. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define N 10
  15. using namespace std;
  16.  
  17.  
  18. struct Tree {
  19.     int value;
  20.     Tree *left;
  21.     Tree *right;
  22. };
  23.  
  24. //th - tree head
  25. void add_node(Tree *th, int value) {
  26.     if (th == NULL) {
  27.         th = new Tree;
  28.         th->value = value;
  29.         th->left = NULL;
  30.         th->right = NULL;
  31.     }
  32.     else {
  33.         if (value > th->value) add_node(th->right, value);
  34.         else add_node(th->left, value);
  35.     }
  36. }
  37.  
  38.  
  39. void preorder(Tree *node){
  40.     if (node) {
  41.         cout << node->value << " ";
  42.         preorder(node->left);
  43.         preorder(node->right);
  44.     }
  45. }
  46.  
  47.  
  48. int tree_height(Tree *node){
  49.     int h1=0, h2=0;
  50.     if (node == NULL) return 0;
  51.     if (node->left){
  52.         h1 = tree_height(node->left);
  53.     }
  54.     if (node->right){
  55.         h2 = tree_height(node->right);
  56.     }
  57.     return (max(h1, h2) + 1);
  58. }
  59.  
  60.  
  61. int count_nodes(Tree *node){
  62.     if (node == NULL) return 0;
  63.     int c1 = 0, c2 = 0;
  64.     if (node->left){
  65.         c1 = count_nodes(node->left);
  66.     }
  67.     if (node->right){
  68.         c2 = count_nodes(node->right);
  69.     }
  70.     return (c1 + c2 + 1);
  71. }
  72.  
  73.  
  74. void copy_tree(Tree * th, Tree * new_tree) {
  75.     if (th){
  76.     new_tree = new Tree;
  77.     new_tree->value = th->value;
  78.     new_tree->left = NULL;
  79.     new_tree->right = NULL;
  80.     copy_tree(th->left, new_tree->left);
  81.     copy_tree(th->right, new_tree->right);
  82.     }
  83. }
  84.  
  85.  
  86. void mirror_tree(Tree * th, Tree * mirror_tree){
  87.     if (th){
  88.         mirror_tree = new Tree;
  89.         mirror_tree->value = th->value;
  90.         mirror_tree->left = NULL;
  91.         mirror_tree->right = NULL;
  92.         copy_tree(th->left, mirror_tree->right);
  93.         copy_tree(th->right, mirror_tree->left);
  94.     }
  95. }
  96.  
  97.  
  98. int compare_trees(Tree * th1, Tree * th2){
  99.     if (th1 != NULL & th2 != NULL){
  100.         if (th1->value == th2->value){
  101.             int b1 = compare_trees(th1->left, th2->left);
  102.             int b2 = compare_trees(th1->right, th2->right);
  103.             return b1 & b2;
  104.         }
  105.     }else return 0;
  106.     return 0;
  107. }
  108.  
  109.  
  110. void find_node(Tree * th, int value){
  111.     if (th == NULL) cout << "404 - not found";
  112.     else{
  113.         int root = th->value;
  114.         cout << root << " ";
  115.         if (value > root) find_node(th->right, value);
  116.         else if (value < root) find_node(th->left, value);
  117.     }
  118. }
  119.  
  120.  
  121. int main() {
  122.     int num;
  123.     Tree * my_tree = NULL;
  124.     Tree * new_tree = NULL;
  125.     for (int i = 0; i < 8; i++){
  126.         cin >> num;
  127.         add_node(my_tree, num);
  128.     }
  129.     cout << "\n";
  130.     preorder(my_tree);
  131.     cout << "\n";
  132.     copy_tree(my_tree, new_tree);
  133.     preorder(new_tree);
  134.     mirror_tree(my_tree, new_tree);
  135.     cout << "\n";
  136.     preorder(new_tree);
  137.     cout << "\n" << "height is\t" << tree_height(my_tree) << "\n";
  138.     cout << "quantity of nodes is\t" << count_nodes(my_tree) << "\n";
  139.     cout << "search 20\n";
  140.     find_node(my_tree, 20);
  141.     cout << "\n";
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement