📌  相关文章
📜  使用BST的预遍历的元素的数量小于根的数量

📅  最后修改于: 2021-05-24 23:28:44             🧑  作者: Mango

预先遍历BST。任务是找到少于根的元素数量。

例子:

Input: preorder[] = {3, 2, 1, 0, 5, 4, 6}
Output: 3

Input: preorder[] = {5, 4, 3, 2, 1}
Output: 4

对于二叉搜索树,预遍历的形式为:

简单方法:

  1. 遍历给定的预定。
  2. 检查当前元素是否大于根。
  3. 如果是,则返回indexOfCurrentElement – 1作为否。小于根的元素将是除根以外所有出现在当前元素之前的元素。
C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to find the first index of the element
// that is greater than the root
int findLargestIndex(int arr[], int n)
{
    int i, root = arr[0];
  
    // Traverse the given preorder
    for(i = 0; i < n-1; i++)
    {
        // Check if the number is greater than root
        // If yes then return that index-1
        if(arr[i] > root)
           return i-1;
    }
  
}
  
// Driver Code
int main()
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = sizeof(preorder) / sizeof(preorder[0]);
  
     cout << findLargestIndex(preorder, n);
  
    return 0;
}


Java
// Java implementation of
// above approach
  
class GFG
{
// Function to find the first 
// index of the element that 
// is greater than the root
static int findLargestIndex(int arr[], 
                            int n)
{
    int i, root = arr[0];
  
    // Traverse the given preorder
    for(i = 0; i < n - 1; i++)
    {
        // Check if the number is
        // greater than root
        // If yes then return
        // that index-1
        if(arr[i] > root)
        return i-1;
    }
    return 0;
}
  
// Driver Code
public static void main(String ags[])
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = preorder.length;
  
    System.out.println(findLargestIndex(preorder, n));
}
}
  
// This code is contributed 
// by Subhadeep Gupta


Python3
# Python3 implementation of above approach
  
# Function to find the first index of 
# the element that is greater than the root
def findLargestIndex(arr, n):
  
    i, root = arr[0], arr[0];
  
    # Traverse the given preorder
    for i in range(0, n - 1):
          
        # Check if the number is greater than 
        # root. If yes then return that index-1
        if(arr[i] > root):
            return i - 1;
  
# Driver Code
preorder= [3, 2, 1, 0, 5, 4, 6];
n = len(preorder)
  
print(findLargestIndex(preorder, n));
  
# This code is contributed 
# by Akanksha Rai


C#
// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to find the first 
// index of the element that
// is greater than the root
static int findLargestIndex(int []arr, 
                            int n)
{
    int i, root = arr[0];
  
    // Traverse the given preorder
    for(i = 0; i < n - 1; i++)
    {
        // Check if the number is 
        // greater than root. If yes 
        // then return that index-1
        if(arr[i] > root)
        return i - 1;
    }
    return 0;
}
  
// Driver Code
static public void Main()
{
    int []preorder = {3, 2, 1, 0, 5, 4, 6};
    int n = preorder.Length;
  
    Console.WriteLine(findLargestIndex(preorder, n));
}
}
  
// This code is contributed 
// by Subhadeep Gupta


PHP
 $root) 
        return $i - 1; 
    } 
  
} 
  
// Driver Code 
$preorder = array(3, 2, 1, 0, 5, 4, 6); 
$n = count($preorder);
echo findLargestIndex($preorder, $n); 
  
// This code is contributed 
// by 29AjayKumar
?>


C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the smaller elements
int findLargestIndex(int arr[], int n)
{
    int root = arr[0], lb = 0, ub = n-1;
    while(lb < ub)
    {
  
        int mid = (lb + ub)/2;
  
        // Check if the element at mid
        // is greater than root.
        if(arr[mid] > root)
            ub = mid - 1;
        else
        {
          // if the element at mid is lesser 
          //  than root and element at mid+1 
          // is greater
           if(arr[mid + 1] > root)
              return mid;
            else lb = mid + 1; 
        }
     }
     return lb;
}
  
// Driver Code
int main()
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = sizeof(preorder) / sizeof(preorder[0]);
  
     cout << findLargestIndex(preorder, n);
  
    return 0;
}


Java
// Java implementation 
// of above approach
import java.util.*;
  
class GFG
{
  
// Function to count the
// smaller elements
static int findLargestIndex(int arr[], 
                            int n)
{
    int root = arr[0],
        lb = 0, ub = n - 1;
    while(lb < ub)
    {
  
        int mid = (lb + ub) / 2;
  
        // Check if the element at 
        // mid is greater than root.
        if(arr[mid] > root)
            ub = mid - 1;
        else
        {
              
            // if the element at mid is 
            // lesser than root and 
            // element at mid+1 is greater
            if(arr[mid + 1] > root)
                return mid;
            else lb = mid + 1; 
        }
    }
    return lb;
}
  
// Driver Code
public static void main(String args[])
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = preorder.length;
  
    System.out.println(
           findLargestIndex(preorder, n));
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of above approach
  
# Function to count the smaller elements
def findLargestIndex(arr, n):
    root = arr[0];
    lb = 0;
    ub = n - 1;
    while(lb < ub):
  
        mid = (lb + ub) // 2;
          
        # Check if the element at mid
        # is greater than root.
        if(arr[mid] > root):
            ub = mid - 1;
        else:
              
            # if the element at mid is lesser 
            # than root and element at mid+1 
            # is greater
            if(arr[mid + 1] > root):
                return mid;
            else:
                lb = mid + 1; 
    return lb;
  
# Driver Code
preorder = [3, 2, 1, 0, 5, 4, 6];
n = len(preorder);
  
print(findLargestIndex(preorder, n));
  
# This code is contributed by mits


C#
// C# implementation 
// of above approach 
using System;
  
class GFG 
{ 
  
// Function to count the 
// smaller elements 
static int findLargestIndex(int []arr, 
                            int n) 
{ 
    int root = arr[0], 
        lb = 0, ub = n - 1; 
    while(lb < ub) 
    { 
  
        int mid = (lb + ub) / 2; 
  
        // Check if the element at 
        // mid is greater than root. 
        if(arr[mid] > root) 
            ub = mid - 1; 
        else
        { 
              
            // if the element at mid is 
            // lesser than root and 
            // element at mid+1 is greater 
            if(arr[mid + 1] > root) 
                return mid; 
            else lb = mid + 1; 
        } 
    } 
    return lb; 
} 
  
// Driver Code 
public static void Main(String []args) 
{ 
    int []preorder = {3, 2, 1, 0, 5, 4, 6}; 
    int n = preorder.Length; 
  
    Console.WriteLine( 
        findLargestIndex(preorder, n)); 
} 
} 
  
// This code contributed by Rajput-Ji


PHP
 $root)
            $ub = $mid - 1;
        else
        {
            // if the element at mid is lesser 
            // than root and element at mid+1 
            // is greater
            if($arr[$mid + 1] > $root)
                return $mid;
            else
                $lb = $mid + 1; 
        }
    }
    return $lb;
}
  
// Driver Code
$preorder = array(3, 2, 1, 0, 5, 4, 6);
$n = count($preorder);
  
echo findLargestIndex($preorder, $n);
  
// This code is contributed by mits
?>


输出:
3

时间复杂度: O(n)

高效的方法(使用二进制搜索) :这里的想法是利用二进制搜索的扩展形式。步骤如下:

  1. 快到中间检查中间的元素是否大于根。如果是,那么我们递归到数组的左半部分。
  2. 否则,如果mid的元素小于root,而mid + 1的元素大于root,则返回mid作为我们的答案。
  3. 否则,我们在数组的右半部分递归以重复上述步骤。

以下是上述想法的实现。

C++

// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the smaller elements
int findLargestIndex(int arr[], int n)
{
    int root = arr[0], lb = 0, ub = n-1;
    while(lb < ub)
    {
  
        int mid = (lb + ub)/2;
  
        // Check if the element at mid
        // is greater than root.
        if(arr[mid] > root)
            ub = mid - 1;
        else
        {
          // if the element at mid is lesser 
          //  than root and element at mid+1 
          // is greater
           if(arr[mid + 1] > root)
              return mid;
            else lb = mid + 1; 
        }
     }
     return lb;
}
  
// Driver Code
int main()
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = sizeof(preorder) / sizeof(preorder[0]);
  
     cout << findLargestIndex(preorder, n);
  
    return 0;
}

Java

// Java implementation 
// of above approach
import java.util.*;
  
class GFG
{
  
// Function to count the
// smaller elements
static int findLargestIndex(int arr[], 
                            int n)
{
    int root = arr[0],
        lb = 0, ub = n - 1;
    while(lb < ub)
    {
  
        int mid = (lb + ub) / 2;
  
        // Check if the element at 
        // mid is greater than root.
        if(arr[mid] > root)
            ub = mid - 1;
        else
        {
              
            // if the element at mid is 
            // lesser than root and 
            // element at mid+1 is greater
            if(arr[mid + 1] > root)
                return mid;
            else lb = mid + 1; 
        }
    }
    return lb;
}
  
// Driver Code
public static void main(String args[])
{
    int preorder[] = {3, 2, 1, 0, 5, 4, 6};
    int n = preorder.length;
  
    System.out.println(
           findLargestIndex(preorder, n));
}
}
  
// This code is contributed by Arnab Kundu

Python3

# Python3 implementation of above approach
  
# Function to count the smaller elements
def findLargestIndex(arr, n):
    root = arr[0];
    lb = 0;
    ub = n - 1;
    while(lb < ub):
  
        mid = (lb + ub) // 2;
          
        # Check if the element at mid
        # is greater than root.
        if(arr[mid] > root):
            ub = mid - 1;
        else:
              
            # if the element at mid is lesser 
            # than root and element at mid+1 
            # is greater
            if(arr[mid + 1] > root):
                return mid;
            else:
                lb = mid + 1; 
    return lb;
  
# Driver Code
preorder = [3, 2, 1, 0, 5, 4, 6];
n = len(preorder);
  
print(findLargestIndex(preorder, n));
  
# This code is contributed by mits

C#

// C# implementation 
// of above approach 
using System;
  
class GFG 
{ 
  
// Function to count the 
// smaller elements 
static int findLargestIndex(int []arr, 
                            int n) 
{ 
    int root = arr[0], 
        lb = 0, ub = n - 1; 
    while(lb < ub) 
    { 
  
        int mid = (lb + ub) / 2; 
  
        // Check if the element at 
        // mid is greater than root. 
        if(arr[mid] > root) 
            ub = mid - 1; 
        else
        { 
              
            // if the element at mid is 
            // lesser than root and 
            // element at mid+1 is greater 
            if(arr[mid + 1] > root) 
                return mid; 
            else lb = mid + 1; 
        } 
    } 
    return lb; 
} 
  
// Driver Code 
public static void Main(String []args) 
{ 
    int []preorder = {3, 2, 1, 0, 5, 4, 6}; 
    int n = preorder.Length; 
  
    Console.WriteLine( 
        findLargestIndex(preorder, n)); 
} 
} 
  
// This code contributed by Rajput-Ji

的PHP

 $root)
            $ub = $mid - 1;
        else
        {
            // if the element at mid is lesser 
            // than root and element at mid+1 
            // is greater
            if($arr[$mid + 1] > $root)
                return $mid;
            else
                $lb = $mid + 1; 
        }
    }
    return $lb;
}
  
// Driver Code
$preorder = array(3, 2, 1, 0, 5, 4, 6);
$n = count($preorder);
  
echo findLargestIndex($preorder, $n);
  
// This code is contributed by mits
?>
输出:
3

时间复杂度: O(logn)