📜  数据结构和算法 |设置 10

📅  最后修改于: 2021-09-27 22:53:38             🧑  作者: Mango

以下问题已在 GATE CS 2007 考试中提出。

1.二叉树的高度是任何根到叶路径中的最大边数。高度为 h 的二叉树的最大节点数为:
(A) 2^h -1
(B) 2^(h-1) – 1
(C) 2^(h+1) -1
(D) 2*(h+1)

答案 (C)
完整树的最大节点数将在那里。
高度为 h = 1 + 2 + 2^2 + 2*3 + … 的完整树中的节点数。 2^h = 2^(h+1) – 1

2:三个未标记节点最多可以形成的二叉树数为:
(一) 1
(乙) 5
(三) 4
(四) 3

答案 (B)

O
          /     \
        O        O
           (i)

            O             
          /                             
       O                 
     /                      
   O                         
        (ii)

         O
       / 
     O
        \
          O
       (iii)

  O                      
     \                        
       O                     
          \                  
           O                                                         
    (iv)

       O
          \
            O
          /
       O               
    (v)

请注意,节点未标记。如果节点被标记,我们会得到更多数量的树。

3. 以下哪种排序算法的最坏情况复杂度最低?
(一)归并排序
(B) 冒泡排序
(C) 快速排序
(D) 选择排序

答案 (A)

上述排序算法的最坏情况复杂度如下:
归并排序——nLogn
冒泡排序 — n^2
快速排序 – n^2
选择排序——n^2

4. 使用堆栈计算以下带有单个数字操作数的后缀表达式:

8 2 3 ^ / 2 3 * + 5 1 * - 

请注意,^ 是幂运算符。评估第一个 * 后堆栈的顶部两个元素是:

(一) 6, 1
(二) 5、7
(C) 3、2
(四) 1, 5

答案 (A)
评估任何后缀表达式的算法相当简单:

1. While there are input tokens left
    o Read the next token from input.
    o If the token is a value
       + Push it onto the stack.
    o Otherwise, the token is an operator 
      (operator here includes both operators, and functions).
       * It is known a priori that the operator takes n arguments.
       * If there are fewer than n values on the stack
        (Error) The user has not input sufficient values in the expression.
       * Else, Pop the top n values from the stack.
       * Evaluate the operator, with the values as arguments.
       * Push the returned results, if any, back onto the stack.
2. If there is only one value in the stack
    o That value is the result of the calculation.
3. If there are more values in the stack
    o (Error)  The user input has too many values.

算法来源:http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_postfix_algorithm

让我们为给定的表达式运行上述算法。
前三个令牌是值,因此它们只是被推送。压入8、2、3后,堆栈如下

8, 2, 3  

读取 ^ 时,弹出前两个并计算 power(2^3)

8, 8 

读取 / 时,弹出前两个并执行除法(8/8)

1 

接下来的两个令牌是值,因此它们被简单地推送。压入2和3后,堆栈如下

1, 2, 3

当 * 出现时,弹出前两个并执行乘法。

1, 6


5、二叉树的中序遍历和前序遍历分别是dbeafcg和abdecfg。二叉树的后序遍历为:

(一) debfgca
(B) edbgfca
(C) edbfgca
(D) defgbca

答案 (A)

Below is the given tree.
                              a 
                           /    \
                        /          \
                      b             c
                   /   \          /   \
                 /       \      /       \
               d         e    f          g

请参阅 GATE Corner 了解所有往年论文/解决方案/解释、教学大纲、重要日期、笔记等。