java - java 中的树 (bst,maxHeap)

标签 java c# data-structures tree binary-tree

此信息与树的类型有关。换句话说,你应该认识到这些信息金字塔的顶峰(Max Heap),或二叉搜索树或两者的组合或它们都没有。 输入格式如下: (94,RR) (17,L) (36,RL) (51,R) (20,) (76,RRR) ()
输入格式树中给定对的每个节点。第一个值表示节点的数量,第二个值表示到达该节点必须导航的根路径。 Node 没有办法代表树的根。有了这些信息,您应该能够输入“识别树”。 例如树如下:

(94,RR) (17,L) (36,RL) (51,R) (20,) (76,RRR) ()

tree for this string

这棵树不是最大金字塔(最大堆),不是二叉搜索树,也不是两者的组合。

Entrance :
At the entrance you get a string that information is relevant to a tree. Each tree
With the statement (s) end. Entry Exit program ends.
Output:
You are one of the following phrases in your printed output.

1.BST: If the input tree is a binary search tree.
2.MaxHeap: If the tree is the maximum pyramid entrance.  
3.BST MaxHeap: If the combined input of binary search tree and the pyramid is the maximum.
4.Nothing: If there is none of the above.

**Sample Input:**
(94,RR) (17,L) (36,RL) (51,R) (20,) (76,RRR) ()
(94,) (59,LL) (61,RR) (53,LR) (79,L) (77,R) (15,RL) ()
(72,) (44,LR) (15,L) (2,LL) ()
Exit
**Sample Output:**
Nothing
MaxHeap
BST

现在我无法解决这个问题。请帮忙。 谢谢。

最佳答案

让我们用数组表示您的数据。我们可以通过使用公式 2*i 作为节点 i 的左子节点和 2*i+1 来从该数组构建二叉树对于节点i 的右子节点。我还假设您已经准备好自己的基本 BST。

1.如何确定给定的树是否是 BST: 按表示树位置的 String 的大小对输入进行排序。您的数据可以存储在名为 Pair 的类中,该类存储表示值的整数和表示相对于根的位置的字符串。然后我们就可以对其进行排序。以下是实现该类和存储该类的数组的方法:

class Pair
{
    int val;
    String pos;
    Pair(int val, String pos)
    {
        this.val=val;
        this.pos=pos;
    }
}


现在在主方法或任何地方,您可以开始构建树数组

int n;
//Take n which is number of nodes here
Pair[] input=new Pair[n];
for(int i=0;i<n;i++)
{
    input[i]=new Pair(*input value*, *input Position*);
}

Arrays.sort(input, new Comparator<Book>() //Sorts by the distance from start node
{
    @Override
    public int compare(Pair p1, Pair p2)
    {
        return p1.pos.length()-p2.pos.length();
    }
});

现在,只需在 BST 中创建一个名为 Insert(Node curr, int value, String where, intillWhere)' 的递归方法。我再次假设您的 BST 有一个 Node` 类,用于存储数据以及左右子引用。

class Node //The node for the BSt should look something like this 
{
    Node left, right;
    int data;
    Node(Node left, Node right, int data)
    {
        this.left=left;
        this.right=right;
        this.data=data;
    }
}

<br>

//Method for inserting into the BST
void Insert(Node curr, int value, String where, int tillWhere)
{
    if(curr==null)
        curr=new Node(value);
    else
    {
        if(where.charAt(tillWhere)=='L')
            Insert(curr.left, value, where, tillWhere+1);
        else
            Insert(curr.right, value, where, tillWhere+1);
    }
}


现在,只需执行 BST 的中序遍历,并将结果存储在数组 Inorder 中。之后,如果数据按排序顺序排列,则它将是 BST。

ArrayList<Integer> inOrder=new ArrayList<Integer>();

//Method
void Inorder(Node curr)
{
    if(curr!=null)
    {
        if(curr.left!=null)
            Inorder(curr.left);
        inOrder.add(curr.data); //Appending to list
        if(curr.right!=null)
            Inorder(curr.right);
    }
}

//Now after method call:
boolean isBST(ArrayList<Integer> inOrder)
{
    for(int i=1;i<=inOrder.size();i++)
        if(inOrder.get(i)<inOrder.get(i-1)) //Not possible in BST
            return false; 
    return true
}
  • 如何确定给定的树是否是堆:只需满足父级 >= 其子级的属性即可。我们可以为此编写一个简单的递归解决方案,如下所示:

    boolean isHeap(int[] arr, int i, int n)// array storing the tree, initial postion , size
    {
       if (i > (n - 2)/2) //Root
           return true;
    
       // If an internal node and is greater than its children, and
       // same is recursively true for the children
       if (arr[i] >= arr[2*i + 1]  &&  arr[i] >= arr[2*i + 2] &&
           isHeap(arr, 2*i + 1, n) && isHeap(arr, 2*i + 2, n))
           return true;
    
       return false;
    }
    



    你可以根据我上面给你的数据找出你问题中的所有4个条件。我希望这有帮助!

  • 关于java - java 中的树 (bst,maxHeap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34243762/

    相关文章:

    algorithm - 0/1背包与动态规划

    java - 如何收集必须像字符串一样格式化的数字?

    java - 多个 url 模式的部署问题?

    java - 提高合并两个 ArrayList 的性能

    c# - Html 邮件中的字体

    c# - 在bot框架v4中,如何实现带有评论框和提交按钮的评分卡

    java - 无法查明 Java 中 'Null Pointer Exception' 的原因

    c++ - 首选哪种数据结构而不是操作多个 vector

    Java日历如何验证日期

    c# - 获取一个页面只能有一个服务器端表单标签错误