📌  相关文章
📜  按排序顺序打印二叉树级别|设置2(使用设置)

📅  最后修改于: 2021-04-29 06:45:57             🧑  作者: Mango

给定一棵树,按排序顺序打印级别顺序遍历。

例子 :

Input :     7
          /    \
        6       5
       / \     / \
      4  3    2   1
Output : 
7
5 6
1 2 3 4 

Input :     7
          /    \
        16       1
       / \      
      4   13    
Output :
7 
1 16
4 13

我们在下面的文章中讨论了基于优先级队列的解决方案。

按排序顺序打印二叉树级别|设置1(使用优先级队列)

在这篇文章中,讨论了一个基于集合(使用平衡二叉搜索树实现)的解决方案。

方法 :
1.开始遍历树。
2.将所有节点存储在一个集合(或任何其他类似的数据结构)中。
3.打印集合的元素。

C++
// CPP code to print level order 
// traversal in sorted order
#include 
using namespace std;
  
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int dat = 0)
        : data(dat), left(nullptr), 
          right(nullptr)
    {
    }
};
  
// Function to print sorted 
// level order traversal
void sorted_level_order(Node* root)
{
    queue q;
    set s;
  
    q.push(root);
    q.push(nullptr);
  
    while (q.empty() == false) {
        Node* tmp = q.front();
        q.pop();
  
        if (tmp == nullptr) {
            if (s.empty() == true)
                break;
            for (set::iterator it =
                 s.begin();it != s.end(); ++it) 
                cout << *it << " ";
            q.push(nullptr);
            s.clear();
        }
        else {
            s.insert(tmp->data);
  
            if (tmp->left != nullptr)
                q.push(tmp->left);
            if (tmp->right != nullptr)
                q.push(tmp->right);
        }
    }
}
  
// Driver code
int main()
{
    Node* root = new Node(7);
    root->left = new Node(6);
    root->right = new Node(5);
    root->left->left = new Node(4);
    root->left->right = new Node(3);
    root->right->left = new Node(2);
    root->right->right = new Node(1);    
    sorted_level_order(root);    
    return 0;    
}


Java
// Java code to print level order 
// traversal in sorted order
import java.util.*; 
import java.util.HashSet; 
  
class GFG 
{ 
static class Node 
{ 
    int data; 
    Node left, right; 
};
static Node newNode(int data) 
{ 
    Node node = new Node(); 
    node.data = data; 
    node.left = node.right = null; 
    return (node); 
} 
  
// Function to print sorted 
// level order traversal
static void sorted_level_order(Node root)
{
    Queue q = new LinkedList<>(); 
    Set s = new HashSet();
    q.add(root);
    q.add(null);
  
    while (!q.isEmpty())
    {
        Node tmp = q.peek(); 
            q.remove(); 
  
        if (tmp == null)
        {
            if (s.isEmpty())
                break;
            Iterator value = s.iterator(); 
            while (value.hasNext())
            { 
                System.out.print(value.next() + " "); 
            } 
            q.add(null);
            s.clear();
        }
        else
        {
            s.add(tmp.data);
  
            if (tmp.left != null)
                q.add(tmp.left);
            if (tmp.right != null)
                q.add(tmp.right);
        }
    }
}
  
// Driver Code
public static void main(String[] args) 
{
    Node root = newNode(7);
    root.left = newNode(6);
    root.right = newNode(5);
    root.left.left = newNode(4);
    root.left.right = newNode(3);
    root.right.left = newNode(2);
    root.right.right = newNode(1); 
    sorted_level_order(root); 
}
}
  
// This code is contributed by SHUBHAMSINGH10


Python3
# Python3 program to print level order 
# traversal in sorted order
  
# Helper function that allocates a new 
# node with the given data and None 
# left and right poers.                                     
class newNode: 
  
    # Construct to create a new node 
    def __init__(self, key): 
        self.data = key
        self.left = None
        self.right = None
  
# Function to print sorted 
# level order traversal
def sorted_level_order( root):
  
    q = []
    s = set()
  
    q.append(root)
    q.append(None)
  
    while (len(q)):
        tmp = q[0]
        q.pop(0)
  
        if (tmp == None): 
            if (not len(s)):
                break
            for i in s: 
                print(i, end = " ")
            q.append(None)
            s = set()
          
        else :
            s.add(tmp.data)
  
            if (tmp.left != None):
                q.append(tmp.left)
            if (tmp.right != None):
                q.append(tmp.right)
  
# Driver Code 
if __name__ == '__main__':
      
    """ 
    Let us create Binary Tree shown
    in above example """
    root = newNode(7)
    root.left = newNode(6)
    root.right = newNode(5)
    root.left.left = newNode(4)
    root.left.right = newNode(3)
    root.right.left = newNode(2)
    root.right.right = newNode(1)
    sorted_level_order(root)
  
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# code to print level order 
// traversal in sorted order
using System;
using System.Collections.Generic;
  
class GFG 
{ 
public class Node 
{ 
    public int data; 
    public Node left, right; 
};
  
static Node newNode(int data) 
{ 
    Node node = new Node(); 
    node.data = data; 
    node.left = node.right = null; 
    return (node); 
} 
  
// Function to print sorted 
// level order traversal
static void sorted_level_order(Node root)
{
    Queue q = new Queue(); 
    SortedSet s = new SortedSet();
    q.Enqueue(root);
    q.Enqueue(null);
  
    while (q.Count != 0)
    {
        Node tmp = q.Peek(); 
            q.Dequeue(); 
  
        if (tmp == null)
        {
            if (s.Count == 0)
                break;
            foreach (int v in s)
            { 
                Console.Write(v + " "); 
            } 
            q.Enqueue(null);
            s.Clear();
        }
        else
        {
            s.Add(tmp.data);
  
            if (tmp.left != null)
                q.Enqueue(tmp.left);
            if (tmp.right != null)
                q.Enqueue(tmp.right);
        }
    }
}
  
// Driver Code
public static void Main(String[] args) 
{
    Node root = newNode(7);
    root.left = newNode(6);
    root.right = newNode(5);
    root.left.left = newNode(4);
    root.left.right = newNode(3);
    root.right.left = newNode(2);
    root.right.right = newNode(1); 
    sorted_level_order(root); 
}
}
  
// This code is contributed by 29AjayKumar


输出:
7 5 6 1 2 3 4