CLRS 9.1-1 :

证明:在最坏情况下,利用n + [lgn] - 2此比较,即可找到n个元素中的第2小元素。(提示:同时找最小元素)

算法思想:

1.将数组中的元素分组,每组两个元素,然后比较每组中的两个元素得到最小值,重新得到包含原来一半元素的数组,继续重复上述过程,那么最后一个元素必然为最小值。如图所示,数组为{2, 1, 4, 3, 5}

2.上述过程形成的是一个二叉树,其中叶子节点都为数组元素,非叶子节点刚好4个,这是二叉树的性质。

3.然后我们来找第二小元素,第二小元素必然跟着1,首先赋值为5,然后再赋值为3, 然后赋值为2,即为所求。

PS:本章讨论的元素都互异,不存在相同值(见原书)

#include <iostream>
using namespace std;
class Node
{
public:
  Node* left;
  Node* right;
  int data;
  Node();
  Node(int d);
};
class BinaryTree
{
public:
  Node* root;
  //创建树
  void create_tree(Node** node, int len);
  //求最小值
  int min(int a, int b);
  //寻找第二小值
  int search_second_small();
  BinaryTree();
};
int main()
{
  int arr[10] = {89, 123, 7, 9, 2, 5, 25, 8, 43, 23};

//叶子节点
  Node** node =new Node*[10];
  for(int i =0; i <10; i++)
    node[i] =new Node(arr[i]);
  BinaryTree* bi_tree =new BinaryTree();
  bi_tree->create_tree(node, 10);
  cout<<bi_tree->root->data<<endl;
  cout<<bi_tree->search_second_small()<<endl;
  return 0;
}
Node::Node()
{
  left = right = NULL;
}
Node::Node(int d)
{
  data = d;
  left = right = NULL;
}
void BinaryTree::create_tree(Node** node, int len)
{
  //len == 2时,就剩下两个元素进行比较了,得到最后一个元素为root节点,即最小值节点
  if(len ==2)
  {
    root->left = node[0];
    root->right = node[1];
    root->data = min(node[0]->data, node[1]->data);
  }
  else
  {
    int new_len = (len%2) ? (len/2+1) : len/2;
    Node** new_node =new Node*[new_len];
    //new_node元素个数为奇数
    if(len%2)
    {
      for(int i =0; i < new_len -1; i++)
      {
        //构建父亲节点
        new_node[i] =new Node(min(node[2*i]->data, node[2*i+1]->data));
        new_node[i]->left = node[2*i];
        new_node[i]->right = node[2*i+1];
      }
      new_node[new_len -1] = node[len -1];
    }
    //new_node元素个数为偶数
    else
    {
      for(int i =0; i < new_len; i++)
      {
        //构建父亲节点
        new_node[i] =new Node(min(node[2*i]->data, node[2*i+1]->data));
        new_node[i]->left = node[2*i];
        new_node[i]->right = node[2*i+1];
      }
    }
    create_tree(new_node, new_len);
    delete[] new_node;
  }
}
int BinaryTree::min(int a, int b)
{
  return a < b ? a : b;
}
int BinaryTree::search_second_small()
{
  int second =1000000;
  Node* p = root;
  while(p->left != NULL && p->right != NULL)
  {
    if(p->data == p->left->data && second > p->right->data)
    {
      second = p->right->data;
      p = p->left;
    }
    else if(p->data == p->right->data && second > p->left->data)
    {
      second = p->left->data;
      p = p->right;
    }
    else
      return second;
  }
  return second;
}
BinaryTree::BinaryTree()
{
  root = new Node();
}

算法导论9.1-1习题解答(二叉树)相关推荐

  1. 算法导论12.2节习题解答

    CLRS 12.2-1 C错,240及240之后的节点应该都为911左子树上的节点,那么所有节点必然小于或等于911,但点912明显违反了. CLRS 12.2-2 SEARCH-MINIMUM(x) ...

  2. 北师范《计算机导论》在线作业,计算机导论教学指导与习题解答简介,目录书摘...

    编辑推荐: 根据教育部"高等学校计算机科学与技术专业规范"组织编写 与美国ACM和IEEE CS ComputingCurricula新进展同步 国家精品教材配套用书 ●提供了教材 ...

  3. 算法导论-3.递归部分习题选

    这一部分主要算法导论中递归式.堆排序和快速排序章节里选择的对我而言较有价值的题目. 练习4.1-1 证明 $T(n)=T(\lceil n/2\rceil)+1$ 的解为 $O(\lg n)$ . 解 ...

  4. 算法导论第二章部分习题自我解答

    练习: 2.3-2:去掉合并排序中的哨兵值 #include <iostream> using namespace std; void Merge(int* A, int p, int q ...

  5. 算法导论第三版习题4.5

    4.5-1 a f(n)=1,nlogba=nlog42=n12f(n)=1,n^{log_b a} = n^{log_4 2} = n^{\frac{1}{2}} 当0<ϵ≤12时,f(n)= ...

  6. 算法导论第七章习题答案(第三版) Introduction to Algorithm

    Exercises 7.1-1.略. 7.1-2. 返回的q值等于r,当数组中所有元素都相同时,可以将等于主元的值轮流放到两个集合中. 7.1-3 for循环正好是Θ(n)的复杂度,所以PARTITI ...

  7. 算法导论第10章习题

    10.1-1 略 10.1-2 把这个数组的头和尾各当成一个stack就好了,如果一个stack在push的过程中发现已经有值了就说明放满了. 10.1-3 略 10.1-4 ENQUE,DEQUE中 ...

  8. 算法导论第三版习题6.4

    6.4-1 (a) 首先调用BUILD-MAX-HEAP(A)得到最大堆A1={25,13,20,8,7,13,2,5,4}A_1=\{25,13,20,8,7,13,2,5,4\} (b) 然后将A ...

  9. 数据结构与算法 Python语言实现 课后习题解答Chapter 2

    2.7 Exercises Reinforcement R-2.1 Give three examples of life-critical software applications. https: ...

  10. 算法导论第三版习题4.3

    4.3-1 假设对∀m≤n0,∃c≥0,\forall m\le n_0,\exists c\ge0,使得: T(m)≤cm2 \begin{align} T(m) \le cm^2 \end{ali ...

最新文章

  1. nodejs获取ASP.Net WebAPI(IIS Windows验证)
  2. 一流科技CEO袁进辉:人工智能产业化困局和机遇
  3. aop在项目中的实际运用_【消防验收】防火玻璃在实际运用中的四大关键问题
  4. 大小端问题傻傻分不清?
  5. vue生命周期详解、钩子函数的调用(简单易懂)
  6. python使用HDF文件格式,保存多个类型的数据到一个文件
  7. javaweb网上订餐系统源码_【有源码】毕设网上订餐系统
  8. 原创内容将成网络视频的一支奇军
  9. 【报告分享】2020中国企业直播应用场景趋势研究报告.pdf(附下载链接)
  10. linux shell中获取mongodb最大连接数、内存使用情况等
  11. 自动化运维工具puppet(四)
  12. access查询设计sol视图_Access删除索引
  13. c++string类寻找子串
  14. fedora15影音播放解码器安装
  15. js之dialogArguments
  16. 读研整活笔记1:调研编译器solang
  17. UE4 虚幻引擎,光照系统。BP_Sky_Sphere 白天黑夜效果
  18. Android基础整合项目之节日群发助手(一)
  19. Android读取服务器图片
  20. 文件服务器异地容灾,服务器异地容灾

热门文章

  1. 在API中发现的好东东
  2. bzoj 1488: [HNOI2009]图的同构
  3. python_55_局部和全局变量
  4. 2017.5.20欢(bei)乐(ju)赛解题报告
  5. java 多线程 28 : 多线程组件之 Semaphore 信号量
  6. MSP430 PIN 操作寄存器
  7. MFC中 使用Tab Control 控件在对话框中添加属性页
  8. USB Device Desctiptor 相关
  9. 1.maven下仅shiro框架对shiro的测试使用
  10. 字符串函数 (strfun)