Tree with multiple child nodes and next node

后端 未结 3 1008
难免孤独
难免孤独 2020-12-31 11:37

I want to build a tree with the following characteristics:

  1. Every node can have 1 \"next node\".
  2. Every node can have multiple child nodes.
  3. The
3条回答
  •  礼貌的吻别
    2020-12-31 12:26

    Below is method that develops a node with multiple nodes.

    Reference: https://www.geeksforgeeks.org/generic-tree-level-order-traversal/

    /* Let us create below tree 
    *            10 
    *    / / \ \ 
    *    2 34 56 100 
    *    / \         | / | \ 
    *    77 88   1 7 8 9 
    */
    
    
    
     // CPP program to do level order traversal 
     // of a generic tree 
    
     #include  
     using namespace std; 
    
     // Represents a node of an n-ary tree 
    
     struct Node 
     { 
       int key; 
       vectorchild; 
     }; 
    
    // Utility function to create a new tree node 
    
     Node *newNode(int key) 
     { 
        Node *temp = new Node; 
        temp->key = key; 
        return temp; 
     } 
    
    
    // Driver program 
    
    int main() 
    { 
    /* Let us create below tree 
    *            10 
    *    / / \ \ 
    *    2 34 56 100 
    *    / \         | / | \ 
    *    77 88   1 7 8 9 
    */
    Node *root = newNode(10); 
    (root->child).push_back(newNode(2)); 
    (root->child).push_back(newNode(34)); 
    (root->child).push_back(newNode(56)); 
    (root->child).push_back(newNode(100)); 
    (root->child[0]->child).push_back(newNode(77)); 
    (root->child[0]->child).push_back(newNode(88)); 
    (root->child[2]->child).push_back(newNode(1)); 
    (root->child[3]->child).push_back(newNode(7)); 
    (root->child[3]->child).push_back(newNode(8)); 
    (root->child[3]->child).push_back(newNode(9)); 
    
    
    return 0; 
    } 
    

提交回复
热议问题