Advertisement
kxcoze

SatieTrees

Nov 6th, 2020 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct TreeNode {
  6.     int data;
  7.     TreeNode *left;
  8.     TreeNode *right;
  9. };
  10.  
  11. TreeNode *root = NULL;
  12.  
  13. void buildTree();
  14. void insertFromChild(TreeNode*, int);
  15. void insertFromRoot(int);
  16. void destroyTree(TreeNode*);
  17. void preorderPrint(TreeNode*);
  18. void printLeaves(TreeNode*);
  19.  
  20. int main() {
  21.     setlocale(LC_ALL, "rus");
  22.  
  23.     buildTree();
  24.     //preorderPrint(root); // Вывод ключей всех узлов в прямом порядке
  25.     printLeaves(root); // Вывод ключей всех листьев
  26.     destroyTree(root);
  27.     return 0;
  28. }
  29.  
  30. void insertFromChild(TreeNode *leaf, int key) {
  31.     if (key < leaf->data) {
  32.         if (leaf->left != NULL)
  33.             insertFromChild(leaf->left, key);
  34.         else {
  35.             leaf->left = new TreeNode;
  36.             leaf->left->data = key;
  37.             leaf->left->left = NULL;
  38.             leaf->left->right = NULL;
  39.         }
  40.     }
  41.     else {
  42.         if (leaf->right != NULL)
  43.             insertFromChild(leaf->right, key);
  44.         else {
  45.             leaf->right = new TreeNode;
  46.             leaf->right->data = key;
  47.             leaf->right->left = NULL;
  48.             leaf->right->right = NULL;
  49.         }
  50.     }
  51. }
  52.  
  53. void insertFromRoot(int key) {
  54.     if (root != NULL) {
  55.         insertFromChild(root, key);
  56.     }
  57.     else {
  58.         root = new TreeNode;
  59.         root->data = key;
  60.         root->left = NULL;
  61.         root->right = NULL;
  62.     }
  63. }
  64.  
  65. void buildTree() {
  66.     int n;
  67.     cout << "Enter count of elements to insert in bintree:\n";
  68.     cin >> n;
  69.     cout << "Input elements:\n";
  70.     for (int i = 0; i < n; i++) {
  71.         int req;
  72.         cin >> req;
  73.         insertFromRoot(req);
  74.     }
  75. }
  76.  
  77. void destroyTree(TreeNode *leaf) {
  78.     if (leaf != NULL) {
  79.         destroyTree(leaf->left);
  80.         destroyTree(leaf->right);
  81.         delete leaf;
  82.     }
  83. }
  84.  
  85. void preorderPrint(TreeNode *root) {
  86.     if (root == NULL) {
  87.         return;
  88.     }
  89.     cout << root->data << ' ';
  90.     preorderPrint(root->left);
  91.     preorderPrint(root->right);
  92. }
  93.  
  94. void printLeaves(TreeNode *root) {
  95.     if (root == NULL) {
  96.         return;
  97.     }
  98.  
  99.     if (root->left == NULL && root->right == NULL) {
  100.         cout << root->data << ' ';
  101.     }
  102.     printLeaves(root->left);
  103.     printLeaves(root->right);
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement