📜  树排序

📅  最后修改于: 2021-04-27 05:26:34             🧑  作者: Mango

树排序是一种基于二进制搜索树数据结构的排序算法。它首先根据输入列表或数组的元素创建一个二叉搜索树,然后对创建的二叉搜索树执行有序遍历,以按排序顺序获得元素。
算法:

Step 1: Take the elements input in an array.
Step 2: Create a Binary search tree by inserting data items from the array into the
        binary search tree.
Step 3: Perform in-order traversal on the tree to get the elements in sorted order.
C++
// C++ program to implement Tree Sort
#include
 
using namespace std;
 
struct Node
{
    int key;
    struct Node *left, *right;
};
 
// A utility function to create a new BST Node
struct Node *newNode(int item)
{
    struct Node *temp = new Node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Stores inoder traversal of the BST
// in arr[]
void storeSorted(Node *root, int arr[], int &i)
{
    if (root != NULL)
    {
        storeSorted(root->left, arr, i);
        arr[i++] = root->key;
        storeSorted(root->right, arr, i);
    }
}
 
/* A utility function to insert a new
   Node with given key in BST */
Node* insert(Node* node, int key)
{
    /* If the tree is empty, return a new Node */
    if (node == NULL) return newNode(key);
 
    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left  = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);
 
    /* return the (unchanged) Node pointer */
    return node;
}
 
// This function sorts arr[0..n-1] using Tree Sort
void treeSort(int arr[], int n)
{
    struct Node *root = NULL;
 
    // Construct the BST
    root = insert(root, arr[0]);
    for (int i=1; i


Java
// Java program to
// implement Tree Sort
class GFG
{
 
    // Class containing left and
    // right child of current
    // node and key value
    class Node
    {
        int key;
        Node left, right;
 
        public Node(int item)
        {
            key = item;
            left = right = null;
        }
    }
 
    // Root of BST
    Node root;
 
    // Constructor
    GFG()
    {
        root = null;
    }
 
    // This method mainly
    // calls insertRec()
    void insert(int key)
    {
        root = insertRec(root, key);
    }
     
    /* A recursive function to
    insert a new key in BST */
    Node insertRec(Node root, int key)
    {
 
        /* If the tree is empty,
        return a new node */
        if (root == null)
        {
            root = new Node(key);
            return root;
        }
 
        /* Otherwise, recur
        down the tree */
        if (key < root.key)
            root.left = insertRec(root.left, key);
        else if (key > root.key)
            root.right = insertRec(root.right, key);
 
        /* return the root */
        return root;
    }
     
    // A function to do
    // inorder traversal of BST
    void inorderRec(Node root)
    {
        if (root != null)
        {
            inorderRec(root.left);
            System.out.print(root.key + " ");
            inorderRec(root.right);
        }
    }
    void treeins(int arr[])
    {
        for(int i = 0; i < arr.length; i++)
        {
            insert(arr[i]);
        }
         
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        GFG tree = new GFG();
        int arr[] = {5, 4, 7, 2, 11};
        tree.treeins(arr);
        tree.inorderRec(tree.root);
    }
}
 
// This code is contributed
// by Vibin M


C#
// C# program to
// implement Tree Sort
using System;
public class GFG
{
 
  // Class containing left and
  // right child of current
  // node and key value
  public class Node
  {
    public int key;
    public Node left, right;
 
    public Node(int item)
    {
      key = item;
      left = right = null;
    }
  }
 
  // Root of BST
  Node root;
 
  // Constructor
  GFG()
  {
    root = null;
  }
 
  // This method mainly
  // calls insertRec()
  void insert(int key)
  {
    root = insertRec(root, key);
  }
 
  /* A recursive function to
    insert a new key in BST */
  Node insertRec(Node root, int key)
  {
 
    /* If the tree is empty,
        return a new node */
    if (root == null)
    {
      root = new Node(key);
      return root;
    }
 
    /* Otherwise, recur
        down the tree */
    if (key < root.key)
      root.left = insertRec(root.left, key);
    else if (key > root.key)
      root.right = insertRec(root.right, key);
 
    /* return the root */
    return root;
  }
 
  // A function to do
  // inorder traversal of BST
  void inorderRec(Node root)
  {
    if (root != null)
    {
      inorderRec(root.left);
      Console.Write(root.key + " ");
      inorderRec(root.right);
    }
  }
  void treeins(int []arr)
  {
    for(int i = 0; i < arr.Length; i++)
    {
      insert(arr[i]);
    }
 
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    GFG tree = new GFG();
    int []arr = {5, 4, 7, 2, 11};
    tree.treeins(arr);
    tree.inorderRec(tree.root);
  }
}
 
// This code is contributed by Rajput-Ji


输出:

2 4 5 7 11

平均案例时间复杂度: O(n log n)将一个项目平均添加到二叉搜索树中平均需要O(log n)时间。因此,添加n个项目将花费O(n log n)时间
最坏情况下的时间复杂度: O(n 2 )。可以通过使用自平衡二进制搜索树(如Red Black Tree,AVL Tree)来改善Tree Sort的最坏情况下的时间复杂度。在最坏的情况下,使用自平衡二叉树Tree Sort将花费O(n log n)的时间对数组进行排序。
辅助空间: O(n)