Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 KB | None | 0 0
  1. //10)   Определите структуру TreeNode – узел дерева:
  2. //
  3. //
  4. //struct TreeNode
  5. //{
  6. //    char * name;      //имя узла
  7. //    TreeNode * nodes; //список дочерних узлов
  8. //    TreeNode * next;  //следующий узел на том же уровне
  9. //}
  10. //        * root;   //корневой узел (первый узел на нулевом уровне)
  11. //
  12. //Определите следующие функции: AddNode() – добавление нового дочернего узла,
  13. //FindNode() – поиск узла по его имени,
  14. //DelTree() – удаление всего дерева,
  15. //PrintTree() – вывод дерева (имен узлов) на экран.
  16.  
  17. //                    +-----------+                                        +-----------+
  18. //                    |           |                                        |           |
  19. //                    |           +--------------------------------------> |           |
  20. //                    |           |                                        |           |
  21. //                    +-----+-----+                                        +-----+-----+
  22. //              +-------^   ^   ^-------+                            +-------^   ^   ^-------+
  23. //              |           |           |                            |           |           |
  24. //              |           |           |                   +--------+--+  +-----+-----+  +--+--------+
  25. //              |           |           |                   |           |  |           |  |           |
  26. //              |           |           |                   |           |  |           |  |           |
  27. //              |           |           |                   |           |  |           |  |           |
  28. //              |           |           |                   +-----------+  +-----------+  +-----------+
  29. //              |           |           |
  30. //              |           |           |
  31. //     +--------+--+  +-----+-----+  +--+--------+                     +-----------+                   +-----------+
  32. //     |           |  |           |  |           |                     |           |                   |           |
  33. //     |           |  |           |  |           +-------------------->+           +------------------>+           |
  34. //     |           |  |           |  |           |                     |           |                   |           |
  35. //     +-----------+  +-----------+  +------+----+                     +-----+-----+                   +------+----+
  36. //                                    +--------^   ^                     +------^   ^                    +-------^   ^
  37. //                                    |            |                     |          |                    |           |
  38. //                                    |            |                     |          |                    |           |
  39. //                                    |            |                     |          |                    |           |
  40. //                                    +---------+-+   +------+----+      +---------+-+  +-----+-----+    +---------+-+  +------+----+
  41. //                                    |           |   |           |      |           |  |           |    |           |  |           |
  42. //                                    |           |   |           |      |           |  |           |    |           |  |           |
  43. //                                    |           |   |           |      |           |  |           |    |           |  |           |
  44. //                                    +-----------+   +-----------+      +-----------+  +-----------+    +-----------+  +-----------+
  45.  
  46. #include <iostream>
  47. using namespace std;
  48.  
  49. struct TreeNode
  50. {
  51.     char* name;         //name of the node
  52.     TreeNode* nodes;    //list of child nodes
  53.     TreeNode* next;     //next node on this level
  54. } *root;
  55.  
  56. void GetWriteNodeName();
  57. TreeNode CreateNodeSameLevel(TreeNode);
  58. void GetAmountCreateChildNodes(TreeNode);
  59. TreeNode CreateChildNode(TreeNode);
  60. char NeedOfCreatingChildNode();
  61. TreeNode AddNode();
  62.  
  63. int main() {
  64.  
  65.     int choice;
  66.     printf("Please enter what action u would like to do\n");
  67.     printf("1) Add Node\n");
  68.     printf("2) Find Node\n");
  69.     printf("3) Dell all tree\n");
  70.     printf("4) Print all tree\n");
  71.     printf("Enter your value: ");
  72.     cin >> choice;
  73.  
  74.     switch (choice)
  75.     {
  76.         case 1: AddNode(); break;
  77.     }
  78. }
  79.  
  80. TreeNode AddNode(){
  81.     char need_of_creation;
  82.     TreeNode created_node;
  83.  
  84.     printf("Create next node at this level? [y/n]: ");
  85.     cin >> need_of_creation;
  86.  
  87.     if (need_of_creation == 'y'){
  88.         TreeNode NewNodeSameLevel = CreateNodeSameLevel(created_node);
  89.         printf("Next node created! \n");
  90.     }
  91.  
  92.     GetAmountCreateChildNodes(created_node);
  93.     return created_node;
  94. }
  95.  
  96. void GetWriteNodeName(TreeNode node){
  97.     printf("Enter name of the new node: ");
  98.     cin >> node.name;
  99. }
  100.  
  101. TreeNode CreateChildNode(TreeNode base_node){
  102.     TreeNode new_child_node;
  103.     GetWriteNodeName(new_child_node);
  104.  
  105.     return new_child_node;
  106. }
  107.  
  108. char NeedOfCreatingChildNode(){
  109.     char need_of_child;
  110.     printf("Need of creating child node? [y/n]: ");
  111.     cin >> need_of_child;
  112.  
  113.     return need_of_child;
  114. }
  115.  
  116. void GetAmountCreateChildNodes(TreeNode created_node){
  117.     int amount_of_child_nodes;
  118.     char need_of_child = NeedOfCreatingChildNode();
  119.  
  120.     if (&need_of_child){
  121.         printf("Enter amount of child nodes: ");
  122.         cin >> amount_of_child_nodes;
  123.  
  124.         TreeNode child_nodes_list[amount_of_child_nodes];
  125.         for (int i=0; i < amount_of_child_nodes; i++){
  126.             child_nodes_list[i] = CreateChildNode(created_node);
  127.         }
  128.  
  129.         // create next node to a last child node
  130.         char need_of_child = NeedOfCreatingChildNode();
  131.         if (&need_of_child == "y"){
  132.             CreateNodeSameLevel(child_nodes_list[amount_of_child_nodes - 1]);
  133.         }
  134.     }
  135. }
  136.  
  137. TreeNode CreateNodeSameLevel(TreeNode base_node){
  138.     TreeNode NewNodeSameLevel;
  139.     GetWriteNodeName(NewNodeSameLevel);
  140.     base_node.next = &NewNodeSameLevel;
  141.  
  142.     return NewNodeSameLevel;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement