【问题标题】:tree (bst,maxHeap) in javajava中的树(bst,maxHeap)
【发布时间】:2015-12-12 19:05:27
【问题描述】:

此信息与树的类型有关。换句话说,你应该认识到这个信息金字塔的峰值(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

这棵树不是最大金字塔(max Heap),不是二叉搜索树,也不是它们的组合。

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

现在我无法为这个问题实现树。请帮助。 谢谢。

【问题讨论】:

  • 你的问题是什么?我找不到问题,只是任务。如果是作业,SO 中没有人为你做作业,对不起。
  • 为什么没人?我不介意这样做。让我们150美元/小时。交易,@moha6421?
  • 好吧,没人免费 :)
  • 请原谅我无法解决这个问题的实现树。请帮助。

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


【解决方案1】:

让我们在一个数组中表示您的数据。我们可以通过使用公式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;
    }
}


现在在 main 方法或任何地方,你可以开始构建你的树数组

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, int tillWhere)'. I am assuming again, that your BST has aNode 的递归方法,用于存储数据以及左右子引用。

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 的In-order traversal 并将结果存储在数组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
}
  1. 如何判断给定的树是否是堆: 只需满足父级>=子级的属性即可。我们可以像这样编写一个简单的递归解决方案:

    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 个条件。我希望这会有所帮助!

【讨论】:

  • 请随时指出任何错误。我写的有点匆忙
猜你喜欢
  • 2021-01-31
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多