zoukankan      html  css  js  c++  java
  • UESTC1559 Data Structure Problem [最大堆,最小堆,二叉搜索树]

    Data Structure Problem

    Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 214 Tried: 795

    Submit

    Status

    Best Solution

    Back

    Description

     

    Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data structure problem.
    A Heap data structure is a binary tree with the following properties:
    1. It is a complete binary tree; that is, each level of the tree is completely filled, except possibly the bottom level. At this level, it is filled from left to right.
    2. It satisfies the heap-order property: The key stored in each node is greater than or equal to the keys stored in its children.
    So such a heap is sometimes called a max-heap. (Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.)
    A binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
    1. The left subtree of a node contains only nodes with keys less than (greater than) the node's key.
    2. The right subtree of a node contains only nodes with keys greater than (less than) the node's key.
    3. Both the left and right subtrees must also be binary search trees.
    Given a complete binary tree with N keys, your task is to determine the type of it.
    Note that either a max-heap or a min-heap is acceptable, and it is also acceptable for both increasing ordered BST and decreasing ordered BST.

     

    Input

     


    The first line of the input is T (no more than 100), which stands for the number of test cases you need to solve.
    For each test case, the first line contains an integer N (1 <= N <= 1000), indicating the number of keys in the binary tree. On the second line, a permutation of 1 to N is given. The key stored in root node is given by the first integer, and the 2ith and 2i+1th integers are keys in the left child and right child of the ith integer respectively.

     

    Output

     


    For every test case, you should output "Case #k: " first, where k indicates the case number and counts from 1. Then output the type of the binary tree:
    “Neither” --- It is neither a Heap nor a BST.
    “Both” --- It is both a Heap and a BST.
    “Heap” --- It is only a Heap.
    “BST” --- It is only a BST.

     

    Sample Input

     

    4
    1
    1
    3
    1 2 3
    3
    2 1 3
    4
    2 1 3 4

     

    Sample Output

     

    Case #1: Both
    Case #2: Heap
    Case #3: BST
    Case #4: Neither

     

    Source

     

    Sichuan State Programming Contest 2011

    数据结构:判断最大堆,最小堆,二叉搜索树


    二叉搜索树:

    二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树

    堆:

    堆是一种特殊的树形数据结构,每个结点都有一个值。通常我们所说的堆的数据结构,是指二叉堆。堆的特点是根结点的值最小(或最大),且根结点的两个子树也是一个堆
    堆分为大根堆,小根堆,大根堆就是树的根结点大于叶子结点.

    完全二叉树:

    完全二叉树(Complete Binary Tree) 

      若设二叉树的深度为h,除第 h 层外,其它各

    层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。 

      完全二叉树是由满二叉树而引出来的。对于深度为K的,有N个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。 

      若一棵二叉树至多只有最下面的两层上的结点的度数可以小于2,并且最下层上的结点都集中在该层最左边的若干位置上,则此二叉树成为完全二叉树。

  • 相关阅读:
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 47 全排列 II(二)
    Java实现 LeetCode 47 全排列 II(二)
  • 原文地址:https://www.cnblogs.com/XBWer/p/2682714.html
Copyright © 2011-2022 走看看