📜  打印所有可能的 N 节点完整二叉树

📅  最后修改于: 2021-09-22 10:35:39             🧑  作者: Mango

给定一个整数N ,任务是打印所有可能的具有N 个节点的完整二叉树。除了 NULL 之外,节点处的值不会成为不同全二叉树的标准。

例子:

方法:解决问题的最简单方法是使用递归并检查每个子树是否有奇数个节点,因为满二叉树有奇数个节点。请按照以下步骤解决问题:

  • 初始化一个 hashMap,比如存储所有完整二叉树的hm
  • 通过执行以下步骤,创建一个函数,比如allPossibleBFT ,参数为N
    1. 创建一个列表,比如包含类节点的列表
    2. 如果N =1 ,则在列表中添加节点(0, NULL, NULL)。
    3. 现在检查N是否为奇数,然后使用变量x在范围[0, N-1] 中迭代并执行以下步骤:
      • 初始化一个变量,假设yN – 1 – x
      • x为参数递归调用函数allPossibleBFT并将其分配给节点left
        1. y为参数递归调用函数allPossibleBFT ,并将其分配给节点right
        2. 现在创建一个参数为(0, NULL, NULL)的新节点
        3. 分配Node.left,Node.right右图所示。
        4. 节点添加到列表中。
    4. 执行上述步骤后,在 hashMap hm 中插入列表。
  • 执行完所有步骤后,打印列表中的完整二叉树。

下面是上述方法的实现:

Java
// JAVA program for the above approach
import java.util.*;
import java.io.*;
  
class GFG {
    // Class for creating node and
    // its left and right child
    public static class Node {
        int data;
        Node left;
        Node right;
        Node(int data, Node left, Node right)
        {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }
    
    // Function to traverse the tree and add all 
    // the left and right child in the list al
    public static void display(Node node, List al)
    {
        // If node = null then terminate the function
        if (node == null) {
            return;
        }
       // If there is left child of Node node
       // then insert it into the list al
        if (node.left != null) {
            al.add(node.left.data);
        }
       // Otherwise insert null in the list
        else {
            al.add(null);
        }
         
       // Similarly, if there is right child
       // of Node node then insert it into
       // the list al
        if (node.right != null) {
            al.add(node.right.data);
        }
       // Otherwise insert null
        else {
            al.add(null);
        }
        
        // Recursively call the function
        // for left child and right child
        // of the Node node
        display(node.left, al);
        display(node.right, al);
    }
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int n = 7;
         
        // Function Call
        List list = allPossibleFBT(n);
        
       // Print all possible binary full trees
        for (Node root: list) {
            List al = new ArrayList<>();
            al.add(root.data);
            display(root, al);
            System.out.println(al);
        }
    }
      // Save tree for all n before recursion.
    static HashMap > hm = new HashMap<>();
    public static List allPossibleFBT(int n)
    {
      // Check whether tree exists for given n value or not.
        if (!hm.containsKey(n)) {
              
            // Create a list containing nodes
            List list = new LinkedList<>();
            
            // If N=1, Only one tree can exist 
            // i.e. tree with root.
            if (n == 1) {
               
                list.add(new Node(0, null, null));
            }
            
            // Check if N is odd because binary full
            // tree has N nodes
            else if (n % 2 == 1) {
                
                // Iterate through all the nodes that
                // can be in the left subtree
                for (int x = 0; x < n; x++) {
                    
                   // Remaining Nodes belongs to the
                   // right subtree of the node
                    int y = n - 1 - x;
                    
                  // Iterate through all left Full Binary Tree
                 //  by recursively calling the function
                    for (Node left: allPossibleFBT(x)) {
                        
                      // Iterate through all the right Full
                      // Binary tree by recursively calling
                      // the function
                        for (Node right: allPossibleFBT(y)) {
                            
                           // Create a new node
                            Node node = new Node(0, null, null);
                            
                           // Modify the left node 
                            node.left = left;
                            
                           // Modify the right node
                            node.right = right;
                            
                           // Add the node in the list
                            list.add(node);
                        }
                    }
                }
            }
            
          //Insert tree in HashMap. 
            hm.put(n, list);
        }
        return hm.get(n);
    }
}


输出
[0, 0, 0, null, null, 0, 0, null, null, 0, 0, null, null, null, null]
[0, 0, 0, null, null, 0, 0, 0, 0, null, null, null, null, null, null]
[0, 0, 0, 0, 0, null, null, null, null, 0, 0, null, null, null, null]
[0, 0, 0, 0, 0, null, null, 0, 0, null, null, null, null, null, null]
[0, 0, 0, 0, 0, 0, 0, null, null, null, null, null, null, null, null]

时间复杂度: O(2 N )
空间复杂度: O(2 N )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程