Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //10) Определите структуру TreeNode – узел дерева:
- //
- //
- //struct TreeNode
- //{
- // char * name; //имя узла
- // TreeNode * nodes; //список дочерних узлов
- // TreeNode * next; //следующий узел на том же уровне
- //}
- // * root; //корневой узел (первый узел на нулевом уровне)
- //
- //Определите следующие функции: AddNode() – добавление нового дочернего узла,
- //FindNode() – поиск узла по его имени,
- //DelTree() – удаление всего дерева,
- //PrintTree() – вывод дерева (имен узлов) на экран.
- // +-----------+ +-----------+
- // | | | |
- // | +--------------------------------------> | |
- // | | | |
- // +-----+-----+ +-----+-----+
- // +-------^ ^ ^-------+ +-------^ ^ ^-------+
- // | | | | | |
- // | | | +--------+--+ +-----+-----+ +--+--------+
- // | | | | | | | | |
- // | | | | | | | | |
- // | | | | | | | | |
- // | | | +-----------+ +-----------+ +-----------+
- // | | |
- // | | |
- // +--------+--+ +-----+-----+ +--+--------+ +-----------+ +-----------+
- // | | | | | | | | | |
- // | | | | | +-------------------->+ +------------------>+ |
- // | | | | | | | | | |
- // +-----------+ +-----------+ +------+----+ +-----+-----+ +------+----+
- // +--------^ ^ +------^ ^ +-------^ ^
- // | | | | | |
- // | | | | | |
- // | | | | | |
- // +---------+-+ +------+----+ +---------+-+ +-----+-----+ +---------+-+ +------+----+
- // | | | | | | | | | | | |
- // | | | | | | | | | | | |
- // | | | | | | | | | | | |
- // +-----------+ +-----------+ +-----------+ +-----------+ +-----------+ +-----------+
- #include <iostream>
- using namespace std;
- struct TreeNode
- {
- char* name; //name of the node
- TreeNode* nodes; //list of child nodes
- TreeNode* next; //next node on this level
- } *root;
- void GetWriteNodeName();
- TreeNode CreateNodeSameLevel(TreeNode);
- void GetAmountCreateChildNodes(TreeNode);
- TreeNode CreateChildNode(TreeNode);
- char NeedOfCreatingChildNode();
- TreeNode AddNode();
- int main() {
- int choice;
- printf("Please enter what action u would like to do\n");
- printf("1) Add Node\n");
- printf("2) Find Node\n");
- printf("3) Dell all tree\n");
- printf("4) Print all tree\n");
- printf("Enter your value: ");
- cin >> choice;
- switch (choice)
- {
- case 1: AddNode(); break;
- }
- }
- TreeNode AddNode(){
- char need_of_creation;
- TreeNode created_node;
- printf("Create next node at this level? [y/n]: ");
- cin >> need_of_creation;
- if (need_of_creation == 'y'){
- TreeNode NewNodeSameLevel = CreateNodeSameLevel(created_node);
- printf("Next node created! \n");
- }
- GetAmountCreateChildNodes(created_node);
- return created_node;
- }
- void GetWriteNodeName(TreeNode node){
- printf("Enter name of the new node: ");
- cin >> node.name;
- }
- TreeNode CreateChildNode(TreeNode base_node){
- TreeNode new_child_node;
- GetWriteNodeName(new_child_node);
- return new_child_node;
- }
- char NeedOfCreatingChildNode(){
- char need_of_child;
- printf("Need of creating child node? [y/n]: ");
- cin >> need_of_child;
- return need_of_child;
- }
- void GetAmountCreateChildNodes(TreeNode created_node){
- int amount_of_child_nodes;
- char need_of_child = NeedOfCreatingChildNode();
- if (&need_of_child){
- printf("Enter amount of child nodes: ");
- cin >> amount_of_child_nodes;
- TreeNode child_nodes_list[amount_of_child_nodes];
- for (int i=0; i < amount_of_child_nodes; i++){
- child_nodes_list[i] = CreateChildNode(created_node);
- }
- // create next node to a last child node
- char need_of_child = NeedOfCreatingChildNode();
- if (&need_of_child == "y"){
- CreateNodeSameLevel(child_nodes_list[amount_of_child_nodes - 1]);
- }
- }
- }
- TreeNode CreateNodeSameLevel(TreeNode base_node){
- TreeNode NewNodeSameLevel;
- GetWriteNodeName(NewNodeSameLevel);
- base_node.next = &NewNodeSameLevel;
- return NewNodeSameLevel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement