📜  在给定的完全二叉树中查找值 K,其索引值从 1 到 N

📅  最后修改于: 2022-05-13 01:56:07.510000             🧑  作者: Mango

在给定的完全二叉树中查找值 K,其索引值从 1 到 N

给定一个完整的二叉树,其索引值从 1 到 N 和一个键K 。任务是检查树中是否存在键。如果密钥存在则打印“true”,否则打印“false”。

例子:

朴素方法:最简单的方法是简单地遍历整个树并检查树中是否存在键。

时间复杂度: O(N)
辅助空间: O(1)

高效方法:该方法基于树是完全二叉树的思想,节点从顶部开始从 1 到 N 进行索引。因此,如果所有密钥都存在,我们可以追踪从根到密钥的路径。以下是步骤:

  1. 对于给定节点i的完整二叉树,其子节点将是2*i2*i + 1 。因此,给定节点i ,其父节点将是i/2
  2. 迭代找出key的父节点,直到找到树的根节点,并将steps存储在数组steps[]中,左为-1,右为1(左表示当前节点是其左子节点parent 和 right 表示当前节点是其父节点的右子节点)。
  3. 现在根据存储在数组steps[]中的移动横穿树。如果整个数组遍历成功,则表示该键存在于树中,因此打印“True”,否则打印“False”。

下面是上述方法的实现:

Java
1
       /   \  
     2      3  
    /  \   / \
   4    5 6   7
 /  \   /
8    9 10


C#
1
       /   \  
     2      3  
    /  \   / \
   4    5 6   7
 /  \   /
8    9 10


输出:
// Java program for the above approach
import java.util.*;
import java.io.*;
 
// Class containing left
// and right child of current node
// and key value
class Node {
    int data;
    Node left, right;
 
    public Node(int item)
    {
        data = item;
        left = null;
        right = null;
    }
}
 
class Solution {
 
    // Function to store the
    // list of the directions
    public static ArrayList
    findSteps(Node root, int data)
    {
        ArrayList steps
            = new ArrayList();
 
        // While the root is not found
        while (data != 1) {
            // If it is the right
            // child of its parent
            if (data / 2.0 > data / 2) {
 
                // Add right step (1)
                steps.add(1);
            }
            else {
 
                // Add left step (-1)
                steps.add(-1);
            }
 
            int parent = data / 2;
 
            // Repeat the same
            // for its parent
            data = parent;
        }
 
        // Reverse the steps list
        Collections.reverse(steps);
 
        // Return the steps
        return steps;
    }
 
    // Function to find
    // if the key is present or not
    public static boolean
    findKey(Node root, int data)
    {
        // Get the steps to be followed
        ArrayList steps
            = findSteps(root, data);
 
        // Taking one step at a time
        for (Integer cur_step : steps) {
 
            // Go left
            if (cur_step == -1) {
 
                // If left child does
                // not exist return false
                if (root.left == null)
                    return false;
 
                root = root.left;
            }
 
            // Go right
            else {
 
                // If right child does
                // not exist return false
                if (root.right == null)
                    return false;
 
                root = root.right;
            }
        }
 
        // If the entire array of steps
        // has been successfully
        // traversed
        return true;
    }
 
    public static void main(String[] args)
    {
        /* Consider the following complete binary tree
                 1
                / \ 
               2   3 
              / \ / \
             4  5 6  7
            / \ /
           8  9 10 
        */
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);
        root.left.left.left = new Node(8);
        root.left.left.right = new Node(9);
        root.left.right.left = new Node(10);
 
        // Function Call
        System.out.println(findKey(root, 7));
    }
}

时间复杂度: O(logN)
辅助空间: O(1)